blob: 304c8c6445a5848b63decd14f3625bee3d861511 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200124#ifdef FEAT_TEXT_PROP
125static void update_popups(void);
126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200128static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100129static void win_draw_end(win_T *wp, int c1, int c2, int draw_margin, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100131static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
132static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
133static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200135static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200141# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void start_search_hl(void);
143static void end_search_hl(void);
144static void init_search_hl(win_T *wp);
145static void prepare_search_hl(win_T *wp, linenr_T lnum);
146static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
147static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200152static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200154static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void win_rest_invalid(win_T *wp);
156static void msg_pos_mode(void);
157static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200158static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200160#ifdef FEAT_MENU
161static void redraw_win_toolbar(win_T *wp);
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/* Ugly global: overrule attribute used by screen_char() */
171static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200173#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
174/* Can limit syntax highlight time to 'redrawtime'. */
175# define SYN_TIME_LIMIT 1
176#endif
177
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200178#ifdef FEAT_RIGHTLEFT
179# define HAS_RIGHTLEFT(x) x
180#else
181# define HAS_RIGHTLEFT(x) FALSE
182#endif
183
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200184// flags for screen_line()
185#define SLF_RIGHTLEFT 1
186#define SLF_POPUP 2
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100191 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200204 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100243 // This may be needed when switching tabs.
244 if (must_redraw < type)
245 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246}
247
248/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000249 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 */
251 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100252redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
254 redraw_buf_later(curbuf, type);
255}
256
257 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100258redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259{
260 win_T *wp;
261
262 FOR_ALL_WINDOWS(wp)
263 {
264 if (wp->w_buffer == buf)
265 redraw_win_later(wp, type);
266 }
267}
268
Bram Moolenaar113e1072019-01-20 15:30:40 +0100269#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200270 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100271redraw_buf_line_later(buf_T *buf, linenr_T lnum)
272{
273 win_T *wp;
274
275 FOR_ALL_WINDOWS(wp)
276 if (wp->w_buffer == buf && lnum >= wp->w_topline
277 && lnum < wp->w_botline)
278 redrawWinline(wp, lnum);
279}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100280#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100281
Bram Moolenaar113e1072019-01-20 15:30:40 +0100282#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100283 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200284redraw_buf_and_status_later(buf_T *buf, int type)
285{
286 win_T *wp;
287
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200288#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200289 if (wild_menu_showing != 0)
290 /* Don't redraw while the command line completion is displayed, it
291 * would disappear. */
292 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200293#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200294 FOR_ALL_WINDOWS(wp)
295 {
296 if (wp->w_buffer == buf)
297 {
298 redraw_win_later(wp, type);
299 wp->w_redr_status = TRUE;
300 }
301 }
302}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100303#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200304
Bram Moolenaar113e1072019-01-20 15:30:40 +0100305#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200307 * Redraw as soon as possible. When the command line is not scrolled redraw
308 * right away and restore what was on the command line.
309 * Return a code indicating what happened.
310 */
311 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100312redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313{
314 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 int r;
317 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200318 schar_T *screenline; /* copy from ScreenLines[] */
319 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200321 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200323 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200324
325 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200326 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200327 return ret;
328
329 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 rows = screen_Rows - cmdline_row;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200331 screenline = LALLOC_MULT(schar_T, rows * cols);
332 screenattr = LALLOC_MULT(sattr_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200333 if (screenline == NULL || screenattr == NULL)
334 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (enc_utf8)
336 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200337 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200338 if (screenlineUC == NULL)
339 ret = 2;
340 for (i = 0; i < p_mco; ++i)
341 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200342 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenlineC[i] == NULL)
344 ret = 2;
345 }
346 }
347 if (enc_dbcs == DBCS_JPNU)
348 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200349 screenline2 = LALLOC_MULT(schar_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 if (screenline2 == NULL)
351 ret = 2;
352 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353
354 if (ret != 2)
355 {
356 /* Save the text displayed in the command line area. */
357 for (r = 0; r < rows; ++r)
358 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 (size_t)cols * sizeof(schar_T));
362 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 if (enc_utf8)
366 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenlineC[i] + r * cols,
372 ScreenLinesC[i] + LineOffset[cmdline_row + r],
373 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374 }
375 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200376 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200379 }
380
381 update_screen(0);
382 ret = 3;
383
384 if (must_redraw == 0)
385 {
386 int off = (int)(current_ScreenLine - ScreenLines);
387
388 /* Restore the text displayed in the command line area. */
389 for (r = 0; r < rows; ++r)
390 {
391 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 screenline + r * cols,
393 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenattr + r * cols,
396 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 if (enc_utf8)
398 {
399 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenlineUC + r * cols,
401 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 for (i = 0; i < p_mco; ++i)
403 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200404 screenlineC[i] + r * cols,
405 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 }
407 if (enc_dbcs == DBCS_JPNU)
408 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenline2 + r * cols,
410 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200411 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200412 }
413 ret = 4;
414 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200415 }
416
417 vim_free(screenline);
418 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200419 if (enc_utf8)
420 {
421 vim_free(screenlineUC);
422 for (i = 0; i < p_mco; ++i)
423 vim_free(screenlineC[i]);
424 }
425 if (enc_dbcs == DBCS_JPNU)
426 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200427
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200428 /* Show the intro message when appropriate. */
429 maybe_intro_message();
430
431 setcursor();
432
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433 return ret;
434}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100435#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200436
437/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100438 * Invoked after an asynchronous callback is called.
439 * If an echo command was used the cursor needs to be put back where
440 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200441 * If "call_update_screen" is FALSE don't call update_screen() when at the
442 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100443 */
444 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200445redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100446{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200447 ++redrawing_for_callback;
448
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100449 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200450 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100451 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200452 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 // Don't redraw when in prompt_for_number().
454 if (cmdline_row > 0)
455 {
456 // Redrawing only works when the screen didn't scroll. Don't clear
457 // wildmenu entries.
458 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200459#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200460 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200461#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 && call_update_screen)
463 update_screen(0);
464
465 // Redraw in the same position, so that the user can continue
466 // editing the command.
467 redrawcmdline_ex(FALSE);
468 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200469 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200470 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100471 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200473 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100474 setcursor();
475 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100478 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200479 // Don't update the cursor when it is blinking and off to avoid
480 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100481 out_flush_cursor(FALSE, FALSE);
482 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100483#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100484 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200485
486 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100487}
488
489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Changed something in the current window, at buffer line "lnum", that
491 * requires that line and possibly other lines to be redrawn.
492 * Used when entering/leaving Insert mode with the cursor on a folded line.
493 * Used to remove the "$" from a change command.
494 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
495 * may become invalid and the whole window will have to be redrawn.
496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100498redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200499 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100500 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200502 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
503 wp->w_redraw_top = lnum;
504 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
505 wp->w_redraw_bot = lnum;
506 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507}
508
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200509/*
510 * To be called when "updating_screen" was set before and now the postponed
511 * side effects may take place.
512 */
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200513 void
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200514after_updating_screen(int may_resize_shell UNUSED)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200515{
516 updating_screen = FALSE;
517#ifdef FEAT_GUI
518 if (may_resize_shell)
519 gui_may_resize_shell();
520#endif
521#ifdef FEAT_TERMINAL
522 term_check_channel_closed_recently();
523#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200524
525#ifdef HAVE_DROP_FILE
526 // If handle_drop() was called while updating_screen was TRUE need to
527 // handle the drop now.
528 handle_any_postponed_drop();
529#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200530}
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200533 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 */
535 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100536update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 redraw_curbuf_later(type);
539 update_screen(type);
540}
541
542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 * Based on the current value of curwin->w_topline, transfer a screenfull
544 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200545 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200547 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200548update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200550 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 win_T *wp;
552 static int did_intro = FALSE;
553#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
554 int did_one;
555#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200556#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200557 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200558 int gui_cursor_col = 0;
559 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200560#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200561 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000563 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200565 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200567 if (type == VALID_NO_UPDATE)
568 {
569 no_update = TRUE;
570 type = 0;
571 }
572
Bram Moolenaara3347722019-05-11 21:14:24 +0200573#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200574 {
575 buf_T *buf;
576
577 // Before updating the screen, notify any listeners of changed text.
578 FOR_ALL_BUFFERS(buf)
579 invoke_listeners(buf);
580 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200581#endif
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (must_redraw)
584 {
585 if (type < must_redraw) /* use maximal type */
586 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000587
588 /* must_redraw is reset here, so that when we run into some weird
589 * reason to redraw while busy redrawing (e.g., asynchronous
590 * scrolling), or update_topline() in win_update() will cause a
591 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 must_redraw = 0;
593 }
594
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200595 /* May need to update w_lines[]. */
596 if (curwin->w_lines_valid == 0 && type < NOT_VALID
597#ifdef FEAT_TERMINAL
598 && !term_do_update_window(curwin)
599#endif
600 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 type = NOT_VALID;
602
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000603 /* Postpone the redrawing when it's not needed and when being called
604 * recursively. */
605 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {
607 redraw_later(type); /* remember type for next time */
608 must_redraw = type;
609 if (type > INVERTED_ALL)
610 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200611 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200613#ifdef FEAT_TEXT_PROP
614 // TODO: avoid redrawing everything when there is a popup window.
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200615 if (popup_any_visible())
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200616 type = NOT_VALID;
617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
619 updating_screen = TRUE;
620#ifdef FEAT_SYN_HL
621 ++display_tick; /* let syntax code know we're in a next round of
622 * display updating */
623#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200624 if (no_update)
625 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627 /*
628 * if the screen was scrolled up when displaying a message, scroll it down
629 */
630 if (msg_scrolled)
631 {
632 clear_cmdline = TRUE;
633 if (msg_scrolled > Rows - 5) /* clearing is faster */
634 type = CLEAR;
635 else if (type != CLEAR)
636 {
637 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200638 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
639 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 type = CLEAR;
641 FOR_ALL_WINDOWS(wp)
642 {
643 if (W_WINROW(wp) < msg_scrolled)
644 {
645 if (W_WINROW(wp) + wp->w_height > msg_scrolled
646 && wp->w_redr_type < REDRAW_TOP
647 && wp->w_lines_valid > 0
648 && wp->w_topline == wp->w_lines[0].wl_lnum)
649 {
650 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
651 wp->w_redr_type = REDRAW_TOP;
652 }
653 else
654 {
655 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200656 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
657 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
660 }
661 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200662 if (!no_update)
663 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000664 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
666 msg_scrolled = 0;
667 need_wait_return = FALSE;
668 }
669
670 /* reset cmdline_row now (may have been changed temporarily) */
671 compute_cmdrow();
672
673 /* Check for changed highlighting */
674 if (need_highlight_changed)
675 highlight_changed();
676
677 if (type == CLEAR) /* first clear screen */
678 {
679 screenclear(); /* will reset clear_cmdline */
680 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200681 /* must_redraw may be set indirectly, avoid another redraw later */
682 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 }
684
685 if (clear_cmdline) /* going to clear cmdline (done below) */
686 check_for_delay(FALSE);
687
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000688#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200689 /* Force redraw when width of 'number' or 'relativenumber' column
690 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000691 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200692 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
693 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000694 curwin->w_redr_type = NOT_VALID;
695#endif
696
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 /*
698 * Only start redrawing if there is really something to do.
699 */
700 if (type == INVERTED)
701 update_curswant();
702 if (curwin->w_redr_type < type
703 && !((type == VALID
704 && curwin->w_lines[0].wl_valid
705#ifdef FEAT_DIFF
706 && curwin->w_topfill == curwin->w_old_topfill
707 && curwin->w_botfill == curwin->w_old_botfill
708#endif
709 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000711 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
713 && curwin->w_old_visual_mode == VIsual_mode
714 && (curwin->w_valid & VALID_VIRTCOL)
715 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 ))
717 curwin->w_redr_type = type;
718
Bram Moolenaar5a305422006-04-28 22:38:25 +0000719 /* Redraw the tab pages line if needed. */
720 if (redraw_tabline || type >= NOT_VALID)
721 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000722
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#ifdef FEAT_SYN_HL
724 /*
725 * Correct stored syntax highlighting info for changes in each displayed
726 * buffer. Each buffer must only be done once.
727 */
728 FOR_ALL_WINDOWS(wp)
729 {
730 if (wp->w_buffer->b_mod_set)
731 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 win_T *wwp;
733
734 /* Check if we already did this buffer. */
735 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
736 if (wwp->w_buffer == wp->w_buffer)
737 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200738 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 syn_stack_apply_changes(wp->w_buffer);
740 }
741 }
742#endif
743
744 /*
745 * Go from top to bottom through the windows, redrawing the ones that need
746 * it.
747 */
748#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
749 did_one = FALSE;
750#endif
751#ifdef FEAT_SEARCH_EXTRA
752 search_hl.rm.regprog = NULL;
753#endif
754 FOR_ALL_WINDOWS(wp)
755 {
756 if (wp->w_redr_type != 0)
757 {
758 cursor_off();
759#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
760 if (!did_one)
761 {
762 did_one = TRUE;
763# ifdef FEAT_SEARCH_EXTRA
764 start_search_hl();
765# endif
766# ifdef FEAT_CLIPBOARD
767 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200768 if (clip_star.available && clip_isautosel_star())
769 clip_update_selection(&clip_star);
770 if (clip_plus.available && clip_isautosel_plus())
771 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772# endif
773#ifdef FEAT_GUI
774 /* Remove the cursor before starting to do anything, because
775 * scrolling may make it difficult to redraw the text under
776 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200777 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200778 {
779 gui_cursor_col = gui.cursor_col;
780 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200782 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784#endif
785 }
786#endif
787 win_update(wp);
788 }
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 /* redraw status line after the window to minimize cursor movement */
791 if (wp->w_redr_status)
792 {
793 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200794 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797#if defined(FEAT_SEARCH_EXTRA)
798 end_search_hl();
799#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100800#ifdef FEAT_INS_EXPAND
801 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200802 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 /* Reset b_mod_set flags. Going through all windows is probably faster
806 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200807 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200810 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812 /* Clear or redraw the command line. Done last, because scrolling may
813 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200814 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 showmode();
816
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200817 if (no_update)
818 --no_win_do_lines_ins;
819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200821 if (!did_intro)
822 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 did_intro = TRUE;
824
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200825#ifdef FEAT_TEXT_PROP
826 // Display popup windows on top of the others.
827 update_popups();
828#endif
829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#ifdef FEAT_GUI
831 /* Redraw the cursor and update the scrollbars when all screen updating is
832 * done. */
833 if (gui.in_use)
834 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200835 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200836 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100837 mch_disable_flush();
838 out_flush(); /* required before updating the cursor */
839 mch_enable_flush();
840
Bram Moolenaar144445d2016-07-08 21:41:54 +0200841 /* Put the GUI position where the cursor was, gui_update_cursor()
842 * uses that. */
843 gui.col = gui_cursor_col;
844 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200845 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100847 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200848 screen_cur_col = gui.col;
849 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200850 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100851 else
852 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 gui_update_scrollbars(FALSE);
854 }
855#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200856 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857}
858
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100859#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100860/*
861 * Prepare for updating one or more windows.
862 * Caller must check for "updating_screen" already set to avoid recursiveness.
863 */
864 static void
865update_prepare(void)
866{
867 cursor_off();
868 updating_screen = TRUE;
869#ifdef FEAT_GUI
870 /* Remove the cursor before starting to do anything, because scrolling may
871 * make it difficult to redraw the text under it. */
872 if (gui.in_use)
873 gui_undraw_cursor();
874#endif
875#ifdef FEAT_SEARCH_EXTRA
876 start_search_hl();
877#endif
878}
879
880/*
881 * Finish updating one or more windows.
882 */
883 static void
884update_finish(void)
885{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200886 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100887 showmode();
888
889# ifdef FEAT_SEARCH_EXTRA
890 end_search_hl();
891# endif
892
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200893 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100894
895# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100896 /* Redraw the cursor and update the scrollbars when all screen updating is
897 * done. */
898 if (gui.in_use)
899 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100900 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100901 gui_update_scrollbars(FALSE);
902 }
903# endif
904}
905#endif
906
Bram Moolenaar860cae12010-06-05 23:22:07 +0200907#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200908/*
909 * Return TRUE if the cursor line in window "wp" may be concealed, according
910 * to the 'concealcursor' option.
911 */
912 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100913conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200914{
915 int c;
916
917 if (*wp->w_p_cocu == NUL)
918 return FALSE;
919 if (get_real_state() & VISUAL)
920 c = 'v';
921 else if (State & INSERT)
922 c = 'i';
923 else if (State & NORMAL)
924 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200925 else if (State & CMDLINE)
926 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200927 else
928 return FALSE;
929 return vim_strchr(wp->w_p_cocu, c) != NULL;
930}
931
932/*
933 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
934 */
935 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200936conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200937{
938 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
939 {
940 need_cursor_line_redraw = TRUE;
941 /* Need to recompute cursor column, e.g., when starting Visual mode
942 * without concealing. */
943 curs_columns(TRUE);
944 }
945}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946#endif
947
Bram Moolenaar113e1072019-01-20 15:30:40 +0100948#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100950update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951{
952 win_T *wp;
953 int doit = FALSE;
954
955# ifdef FEAT_FOLDING
956 win_foldinfo.fi_level = 0;
957# endif
958
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100959 // update/delete a specific sign
960 redraw_buf_line_later(buf, lnum);
961
962 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if (wp->w_redr_type != 0)
965 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100967 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200968 * happening (recursive call), messages on the screen or still starting up.
969 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100970 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200971 || State == ASKMORE || State == HITRETURN
972 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100973#ifdef FEAT_GUI
974 || gui.starting
975#endif
976 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 return;
978
979 /* update all windows that need updating */
980 update_prepare();
981
Bram Moolenaar29323592016-07-24 22:04:11 +0200982 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
984 if (wp->w_redr_type != 0)
985 win_update(wp);
986 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200987 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 update_finish();
991}
992#endif
993
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200994#ifdef FEAT_TEXT_PROP
995 static void
996update_popups(void)
997{
998 win_T *wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200999
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001000 // Find the window with the lowest zindex that hasn't been updated yet,
1001 // so that the window with a higher zindex is drawn later, thus goes on
1002 // top.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001003 // TODO: don't redraw every popup every time.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001004 popup_reset_handled();
1005 while ((wp = find_next_popup(TRUE)) != NULL)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001006 {
Bram Moolenaar17146962019-05-30 00:12:11 +02001007 // Recompute the position if the text changed.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001008 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1009 popup_adjust_position(wp);
Bram Moolenaar17146962019-05-30 00:12:11 +02001010
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001011 win_update(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001012 }
1013}
1014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001016/*
1017 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
1018 * window then get the "Pmenu" highlight attribute.
1019 */
1020 static int
1021get_wcr_attr(win_T *wp)
1022{
1023 int wcr_attr = 0;
1024
1025 if (*wp->w_p_wcr != NUL)
1026 wcr_attr = syn_name2attr(wp->w_p_wcr);
1027#ifdef FEAT_TEXT_PROP
1028 if (bt_popup(wp->w_buffer) && wcr_attr == 0)
1029 wcr_attr = HL_ATTR(HLF_PNI);
1030#endif
1031 return wcr_attr;
1032}
1033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034#if defined(FEAT_GUI) || defined(PROTO)
1035/*
1036 * Update a single window, its status line and maybe the command line msg.
1037 * Used for the GUI scrollbar.
1038 */
1039 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001040updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001042 /* return if already busy updating */
1043 if (updating_screen)
1044 return;
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 update_prepare();
1047
1048#ifdef FEAT_CLIPBOARD
1049 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001050 if (clip_star.available && clip_isautosel_star())
1051 clip_update_selection(&clip_star);
1052 if (clip_plus.available && clip_isautosel_plus())
1053 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001057
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001058 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001059 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001060 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 if (wp->w_redr_status
1063# ifdef FEAT_CMDL_INFO
1064 || p_ru
1065# endif
1066# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001067 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068# endif
1069 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001070 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
1072 update_finish();
1073}
1074#endif
1075
1076/*
1077 * Update a single window.
1078 *
1079 * This may cause the windows below it also to be redrawn (when clearing the
1080 * screen or scrolling lines).
1081 *
1082 * How the window is redrawn depends on wp->w_redr_type. Each type also
1083 * implies the one below it.
1084 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001085 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1087 * INVERTED redraw the changed part of the Visual area
1088 * INVERTED_ALL redraw the whole Visual area
1089 * VALID 1. scroll up/down to adjust for a changed w_topline
1090 * 2. update lines at the top when scrolled down
1091 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001092 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 * b_mod_top and b_mod_bot.
1094 * - if wp->w_redraw_top non-zero, redraw lines between
1095 * wp->w_redraw_top and wp->w_redr_bot.
1096 * - continue redrawing when syntax status is invalid.
1097 * 4. if scrolled up, update lines at the bottom.
1098 * This results in three areas that may need updating:
1099 * top: from first row to top_end (when scrolled down)
1100 * mid: from mid_start to mid_end (update inversion or changed text)
1101 * bot: from bot_start to last row (when scrolled up)
1102 */
1103 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001104win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105{
1106 buf_T *buf = wp->w_buffer;
1107 int type;
1108 int top_end = 0; /* Below last row of the top area that needs
1109 updating. 0 when no top area updating. */
1110 int mid_start = 999;/* first row of the mid area that needs
1111 updating. 999 when no mid area updating. */
1112 int mid_end = 0; /* Below last row of the mid area that needs
1113 updating. 0 when no mid area updating. */
1114 int bot_start = 999;/* first row of the bot area that needs
1115 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 int scrolled_down = FALSE; /* TRUE when scrolled down when
1117 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001119 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 int top_to_mod = FALSE; /* redraw above mod_top */
1121#endif
1122
1123 int row; /* current window row to display */
1124 linenr_T lnum; /* current buffer lnum to display */
1125 int idx; /* current index in w_lines[] */
1126 int srow; /* starting row of the current line */
1127
1128 int eof = FALSE; /* if TRUE, we hit the end of the file */
1129 int didline = FALSE; /* if TRUE, we finished the last line */
1130 int i;
1131 long j;
1132 static int recursive = FALSE; /* being called recursively */
1133 int old_botline = wp->w_botline;
1134#ifdef FEAT_FOLDING
1135 long fold_count;
1136#endif
1137#ifdef FEAT_SYN_HL
1138 /* remember what happened to the previous line, to know if
1139 * check_visual_highlight() can be used */
1140#define DID_NONE 1 /* didn't update a line */
1141#define DID_LINE 2 /* updated a normal line */
1142#define DID_FOLD 3 /* updated a folded line */
1143 int did_update = DID_NONE;
1144 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1145#endif
1146 linenr_T mod_top = 0;
1147 linenr_T mod_bot = 0;
1148#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1149 int save_got_int;
1150#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001151#ifdef SYN_TIME_LIMIT
1152 proftime_T syntax_tm;
1153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155 type = wp->w_redr_type;
1156
1157 if (type == NOT_VALID)
1158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 wp->w_lines_valid = 0;
1161 }
1162
1163 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001164 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {
1166 wp->w_redr_type = 0;
1167 return;
1168 }
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 /* Window is zero-width: Only need to draw the separator. */
1171 if (wp->w_width == 0)
1172 {
1173 /* draw the vertical separator right of this window */
1174 draw_vsep_win(wp, 0);
1175 wp->w_redr_type = 0;
1176 return;
1177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001179#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001180 // If this window contains a terminal, redraw works completely differently.
1181 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001182 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001183 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001184# ifdef FEAT_MENU
1185 /* Draw the window toolbar, if there is one. */
1186 if (winbar_height(wp) > 0)
1187 redraw_win_toolbar(wp);
1188# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001189 wp->w_redr_type = 0;
1190 return;
1191 }
1192#endif
1193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001195 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196#endif
1197
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001198#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001199 /* Force redraw when width of 'number' or 'relativenumber' column
1200 * changes. */
1201 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001202 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001203 {
1204 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001205 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001206 }
1207 else
1208#endif
1209
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1211 {
1212 /*
1213 * When there are both inserted/deleted lines and specific lines to be
1214 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1215 * everything (only happens when redrawing is off for while).
1216 */
1217 type = NOT_VALID;
1218 }
1219 else
1220 {
1221 /*
1222 * Set mod_top to the first line that needs displaying because of
1223 * changes. Set mod_bot to the first line after the changes.
1224 */
1225 mod_top = wp->w_redraw_top;
1226 if (wp->w_redraw_bot != 0)
1227 mod_bot = wp->w_redraw_bot + 1;
1228 else
1229 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if (buf->b_mod_set)
1231 {
1232 if (mod_top == 0 || mod_top > buf->b_mod_top)
1233 {
1234 mod_top = buf->b_mod_top;
1235#ifdef FEAT_SYN_HL
1236 /* Need to redraw lines above the change that may be included
1237 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001238 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001240 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 if (mod_top < 1)
1242 mod_top = 1;
1243 }
1244#endif
1245 }
1246 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1247 mod_bot = buf->b_mod_bot;
1248
1249#ifdef FEAT_SEARCH_EXTRA
1250 /* When 'hlsearch' is on and using a multi-line search pattern, a
1251 * change in one line may make the Search highlighting in a
1252 * previous line invalid. Simple solution: redraw all visible
1253 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001254 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001256 if (search_hl.rm.regprog != NULL
1257 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001259 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001260 {
1261 cur = wp->w_match_head;
1262 while (cur != NULL)
1263 {
1264 if (cur->match.regprog != NULL
1265 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001266 {
1267 top_to_mod = TRUE;
1268 break;
1269 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001270 cur = cur->next;
1271 }
1272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273#endif
1274 }
1275#ifdef FEAT_FOLDING
1276 if (mod_top != 0 && hasAnyFolding(wp))
1277 {
1278 linenr_T lnumt, lnumb;
1279
1280 /*
1281 * A change in a line can cause lines above it to become folded or
1282 * unfolded. Find the top most buffer line that may be affected.
1283 * If the line was previously folded and displayed, get the first
1284 * line of that fold. If the line is folded now, get the first
1285 * folded line. Use the minimum of these two.
1286 */
1287
1288 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1289 * the line below it. If there is no valid entry, use w_topline.
1290 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1291 * to this line. If there is no valid entry, use MAXLNUM. */
1292 lnumt = wp->w_topline;
1293 lnumb = MAXLNUM;
1294 for (i = 0; i < wp->w_lines_valid; ++i)
1295 if (wp->w_lines[i].wl_valid)
1296 {
1297 if (wp->w_lines[i].wl_lastlnum < mod_top)
1298 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1299 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1300 {
1301 lnumb = wp->w_lines[i].wl_lnum;
1302 /* When there is a fold column it might need updating
1303 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001304 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 ++lnumb;
1306 }
1307 }
1308
1309 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1310 if (mod_top > lnumt)
1311 mod_top = lnumt;
1312
1313 /* Now do the same for the bottom line (one above mod_bot). */
1314 --mod_bot;
1315 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1316 ++mod_bot;
1317 if (mod_bot < lnumb)
1318 mod_bot = lnumb;
1319 }
1320#endif
1321
1322 /* When a change starts above w_topline and the end is below
1323 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001324 * If the end of the change is above w_topline: do like no change was
1325 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 if (mod_top != 0 && mod_top < wp->w_topline)
1327 {
1328 if (mod_bot > wp->w_topline)
1329 mod_top = wp->w_topline;
1330#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001331 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 top_end = 1;
1333#endif
1334 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001335
1336 /* When line numbers are displayed need to redraw all lines below
1337 * inserted/deleted lines. */
1338 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1339 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001341 wp->w_redraw_top = 0; // reset for next time
1342 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343
1344 /*
1345 * When only displaying the lines at the top, set top_end. Used when
1346 * window has scrolled down for msg_scrolled.
1347 */
1348 if (type == REDRAW_TOP)
1349 {
1350 j = 0;
1351 for (i = 0; i < wp->w_lines_valid; ++i)
1352 {
1353 j += wp->w_lines[i].wl_size;
1354 if (j >= wp->w_upd_rows)
1355 {
1356 top_end = j;
1357 break;
1358 }
1359 }
1360 if (top_end == 0)
1361 /* not found (cannot happen?): redraw everything */
1362 type = NOT_VALID;
1363 else
1364 /* top area defined, the rest is VALID */
1365 type = VALID;
1366 }
1367
Bram Moolenaar367329b2007-08-30 11:53:22 +00001368 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001369 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1370 * non-zero and thus not FALSE) will indicate that screenclear() was not
1371 * called. */
1372 if (screen_cleared)
1373 screen_cleared = MAYBE;
1374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 /*
1376 * If there are no changes on the screen that require a complete redraw,
1377 * handle three cases:
1378 * 1: we are off the top of the screen by a few lines: scroll down
1379 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1380 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1381 * w_lines[] that needs updating.
1382 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001383 if ((type == VALID || type == SOME_VALID
1384 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385#ifdef FEAT_DIFF
1386 && !wp->w_botfill && !wp->w_old_botfill
1387#endif
1388 )
1389 {
1390 if (mod_top != 0 && wp->w_topline == mod_top)
1391 {
1392 /*
1393 * w_topline is the first changed line, the scrolling will be done
1394 * further down.
1395 */
1396 }
1397 else if (wp->w_lines[0].wl_valid
1398 && (wp->w_topline < wp->w_lines[0].wl_lnum
1399#ifdef FEAT_DIFF
1400 || (wp->w_topline == wp->w_lines[0].wl_lnum
1401 && wp->w_topfill > wp->w_old_topfill)
1402#endif
1403 ))
1404 {
1405 /*
1406 * New topline is above old topline: May scroll down.
1407 */
1408#ifdef FEAT_FOLDING
1409 if (hasAnyFolding(wp))
1410 {
1411 linenr_T ln;
1412
1413 /* count the number of lines we are off, counting a sequence
1414 * of folded lines as one */
1415 j = 0;
1416 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1417 {
1418 ++j;
1419 if (j >= wp->w_height - 2)
1420 break;
1421 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1422 }
1423 }
1424 else
1425#endif
1426 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1427 if (j < wp->w_height - 2) /* not too far off */
1428 {
1429 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1430#ifdef FEAT_DIFF
1431 /* insert extra lines for previously invisible filler lines */
1432 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1433 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1434 - wp->w_old_topfill;
1435#endif
1436 if (i < wp->w_height - 2) /* less than a screen off */
1437 {
1438 /*
1439 * Try to insert the correct number of lines.
1440 * If not the last window, delete the lines at the bottom.
1441 * win_ins_lines may fail when the terminal can't do it.
1442 */
1443 if (i > 0)
1444 check_for_delay(FALSE);
1445 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1446 {
1447 if (wp->w_lines_valid != 0)
1448 {
1449 /* Need to update rows that are new, stop at the
1450 * first one that scrolled down. */
1451 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453
1454 /* Move the entries that were scrolled, disable
1455 * the entries for the lines to be redrawn. */
1456 if ((wp->w_lines_valid += j) > wp->w_height)
1457 wp->w_lines_valid = wp->w_height;
1458 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1459 wp->w_lines[idx] = wp->w_lines[idx - j];
1460 while (idx >= 0)
1461 wp->w_lines[idx--].wl_valid = FALSE;
1462 }
1463 }
1464 else
1465 mid_start = 0; /* redraw all lines */
1466 }
1467 else
1468 mid_start = 0; /* redraw all lines */
1469 }
1470 else
1471 mid_start = 0; /* redraw all lines */
1472 }
1473 else
1474 {
1475 /*
1476 * New topline is at or below old topline: May scroll up.
1477 * When topline didn't change, find first entry in w_lines[] that
1478 * needs updating.
1479 */
1480
1481 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1482 j = -1;
1483 row = 0;
1484 for (i = 0; i < wp->w_lines_valid; i++)
1485 {
1486 if (wp->w_lines[i].wl_valid
1487 && wp->w_lines[i].wl_lnum == wp->w_topline)
1488 {
1489 j = i;
1490 break;
1491 }
1492 row += wp->w_lines[i].wl_size;
1493 }
1494 if (j == -1)
1495 {
1496 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1497 * lines */
1498 mid_start = 0;
1499 }
1500 else
1501 {
1502 /*
1503 * Try to delete the correct number of lines.
1504 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1505 */
1506#ifdef FEAT_DIFF
1507 /* If the topline didn't change, delete old filler lines,
1508 * otherwise delete filler lines of the new topline... */
1509 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1510 row += wp->w_old_topfill;
1511 else
1512 row += diff_check_fill(wp, wp->w_topline);
1513 /* ... but don't delete new filler lines. */
1514 row -= wp->w_topfill;
1515#endif
1516 if (row > 0)
1517 {
1518 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001519 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1520 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 bot_start = wp->w_height - row;
1522 else
1523 mid_start = 0; /* redraw all lines */
1524 }
1525 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1526 {
1527 /*
1528 * Skip the lines (below the deleted lines) that are still
1529 * valid and don't need redrawing. Copy their info
1530 * upwards, to compensate for the deleted lines. Set
1531 * bot_start to the first row that needs redrawing.
1532 */
1533 bot_start = 0;
1534 idx = 0;
1535 for (;;)
1536 {
1537 wp->w_lines[idx] = wp->w_lines[j];
1538 /* stop at line that didn't fit, unless it is still
1539 * valid (no lines deleted) */
1540 if (row > 0 && bot_start + row
1541 + (int)wp->w_lines[j].wl_size > wp->w_height)
1542 {
1543 wp->w_lines_valid = idx + 1;
1544 break;
1545 }
1546 bot_start += wp->w_lines[idx++].wl_size;
1547
1548 /* stop at the last valid entry in w_lines[].wl_size */
1549 if (++j >= wp->w_lines_valid)
1550 {
1551 wp->w_lines_valid = idx;
1552 break;
1553 }
1554 }
1555#ifdef FEAT_DIFF
1556 /* Correct the first entry for filler lines at the top
1557 * when it won't get updated below. */
1558 if (wp->w_p_diff && bot_start > 0)
1559 wp->w_lines[0].wl_size =
1560 plines_win_nofill(wp, wp->w_topline, TRUE)
1561 + wp->w_topfill;
1562#endif
1563 }
1564 }
1565 }
1566
1567 /* When starting redraw in the first line, redraw all lines. When
1568 * there is only one window it's probably faster to clear the screen
1569 * first. */
1570 if (mid_start == 0)
1571 {
1572 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001573 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001574 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001575 /* Clear the screen when it was not done by win_del_lines() or
1576 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1577 * then. */
1578 if (screen_cleared != TRUE)
1579 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001580 /* The screen was cleared, redraw the tab pages line. */
1581 if (redraw_tabline)
1582 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001585
1586 /* When win_del_lines() or win_ins_lines() caused the screen to be
1587 * cleared (only happens for the first window) or when screenclear()
1588 * was called directly above, "must_redraw" will have been set to
1589 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1590 if (screen_cleared == TRUE)
1591 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
1593 else
1594 {
1595 /* Not VALID or INVERTED: redraw all lines. */
1596 mid_start = 0;
1597 mid_end = wp->w_height;
1598 }
1599
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001600 if (type == SOME_VALID)
1601 {
1602 /* SOME_VALID: redraw all lines. */
1603 mid_start = 0;
1604 mid_end = wp->w_height;
1605 type = NOT_VALID;
1606 }
1607
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 /* check if we are updating or removing the inverted part */
1609 if ((VIsual_active && buf == curwin->w_buffer)
1610 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1611 {
1612 linenr_T from, to;
1613
1614 if (VIsual_active)
1615 {
1616 if (VIsual_active
1617 && (VIsual_mode != wp->w_old_visual_mode
1618 || type == INVERTED_ALL))
1619 {
1620 /*
1621 * If the type of Visual selection changed, redraw the whole
1622 * selection. Also when the ownership of the X selection is
1623 * gained or lost.
1624 */
1625 if (curwin->w_cursor.lnum < VIsual.lnum)
1626 {
1627 from = curwin->w_cursor.lnum;
1628 to = VIsual.lnum;
1629 }
1630 else
1631 {
1632 from = VIsual.lnum;
1633 to = curwin->w_cursor.lnum;
1634 }
1635 /* redraw more when the cursor moved as well */
1636 if (wp->w_old_cursor_lnum < from)
1637 from = wp->w_old_cursor_lnum;
1638 if (wp->w_old_cursor_lnum > to)
1639 to = wp->w_old_cursor_lnum;
1640 if (wp->w_old_visual_lnum < from)
1641 from = wp->w_old_visual_lnum;
1642 if (wp->w_old_visual_lnum > to)
1643 to = wp->w_old_visual_lnum;
1644 }
1645 else
1646 {
1647 /*
1648 * Find the line numbers that need to be updated: The lines
1649 * between the old cursor position and the current cursor
1650 * position. Also check if the Visual position changed.
1651 */
1652 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1653 {
1654 from = curwin->w_cursor.lnum;
1655 to = wp->w_old_cursor_lnum;
1656 }
1657 else
1658 {
1659 from = wp->w_old_cursor_lnum;
1660 to = curwin->w_cursor.lnum;
1661 if (from == 0) /* Visual mode just started */
1662 from = to;
1663 }
1664
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001665 if (VIsual.lnum != wp->w_old_visual_lnum
1666 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
1668 if (wp->w_old_visual_lnum < from
1669 && wp->w_old_visual_lnum != 0)
1670 from = wp->w_old_visual_lnum;
1671 if (wp->w_old_visual_lnum > to)
1672 to = wp->w_old_visual_lnum;
1673 if (VIsual.lnum < from)
1674 from = VIsual.lnum;
1675 if (VIsual.lnum > to)
1676 to = VIsual.lnum;
1677 }
1678 }
1679
1680 /*
1681 * If in block mode and changed column or curwin->w_curswant:
1682 * update all lines.
1683 * First compute the actual start and end column.
1684 */
1685 if (VIsual_mode == Ctrl_V)
1686 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001687 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001688#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001689 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaar404406a2014-10-09 13:24:43 +02001691 if (curwin->w_p_lbr)
1692 ve_flags = VE_ALL;
1693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001695#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001696 ve_flags = save_ve_flags;
1697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 ++toc;
1699 if (curwin->w_curswant == MAXCOL)
1700 toc = MAXCOL;
1701
1702 if (fromc != wp->w_old_cursor_fcol
1703 || toc != wp->w_old_cursor_lcol)
1704 {
1705 if (from > VIsual.lnum)
1706 from = VIsual.lnum;
1707 if (to < VIsual.lnum)
1708 to = VIsual.lnum;
1709 }
1710 wp->w_old_cursor_fcol = fromc;
1711 wp->w_old_cursor_lcol = toc;
1712 }
1713 }
1714 else
1715 {
1716 /* Use the line numbers of the old Visual area. */
1717 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1718 {
1719 from = wp->w_old_cursor_lnum;
1720 to = wp->w_old_visual_lnum;
1721 }
1722 else
1723 {
1724 from = wp->w_old_visual_lnum;
1725 to = wp->w_old_cursor_lnum;
1726 }
1727 }
1728
1729 /*
1730 * There is no need to update lines above the top of the window.
1731 */
1732 if (from < wp->w_topline)
1733 from = wp->w_topline;
1734
1735 /*
1736 * If we know the value of w_botline, use it to restrict the update to
1737 * the lines that are visible in the window.
1738 */
1739 if (wp->w_valid & VALID_BOTLINE)
1740 {
1741 if (from >= wp->w_botline)
1742 from = wp->w_botline - 1;
1743 if (to >= wp->w_botline)
1744 to = wp->w_botline - 1;
1745 }
1746
1747 /*
1748 * Find the minimal part to be updated.
1749 * Watch out for scrolling that made entries in w_lines[] invalid.
1750 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1751 * top_end; need to redraw from top_end to the "to" line.
1752 * A middle mouse click with a Visual selection may change the text
1753 * above the Visual area and reset wl_valid, do count these for
1754 * mid_end (in srow).
1755 */
1756 if (mid_start > 0)
1757 {
1758 lnum = wp->w_topline;
1759 idx = 0;
1760 srow = 0;
1761 if (scrolled_down)
1762 mid_start = top_end;
1763 else
1764 mid_start = 0;
1765 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1766 {
1767 if (wp->w_lines[idx].wl_valid)
1768 mid_start += wp->w_lines[idx].wl_size;
1769 else if (!scrolled_down)
1770 srow += wp->w_lines[idx].wl_size;
1771 ++idx;
1772# ifdef FEAT_FOLDING
1773 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1774 lnum = wp->w_lines[idx].wl_lnum;
1775 else
1776# endif
1777 ++lnum;
1778 }
1779 srow += mid_start;
1780 mid_end = wp->w_height;
1781 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1782 {
1783 if (wp->w_lines[idx].wl_valid
1784 && wp->w_lines[idx].wl_lnum >= to + 1)
1785 {
1786 /* Only update until first row of this line */
1787 mid_end = srow;
1788 break;
1789 }
1790 srow += wp->w_lines[idx].wl_size;
1791 }
1792 }
1793 }
1794
1795 if (VIsual_active && buf == curwin->w_buffer)
1796 {
1797 wp->w_old_visual_mode = VIsual_mode;
1798 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1799 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001800 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 wp->w_old_curswant = curwin->w_curswant;
1802 }
1803 else
1804 {
1805 wp->w_old_visual_mode = 0;
1806 wp->w_old_cursor_lnum = 0;
1807 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001808 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1812 /* reset got_int, otherwise regexp won't work */
1813 save_got_int = got_int;
1814 got_int = 0;
1815#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001816#ifdef SYN_TIME_LIMIT
1817 /* Set the time limit to 'redrawtime'. */
1818 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001819 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821#ifdef FEAT_FOLDING
1822 win_foldinfo.fi_level = 0;
1823#endif
1824
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001825#ifdef FEAT_MENU
1826 /*
1827 * Draw the window toolbar, if there is one.
1828 * TODO: only when needed.
1829 */
1830 if (winbar_height(wp) > 0)
1831 redraw_win_toolbar(wp);
1832#endif
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 /*
1835 * Update all the window rows.
1836 */
1837 idx = 0; /* first entry in w_lines[].wl_size */
1838 row = 0;
1839 srow = 0;
1840 lnum = wp->w_topline; /* first line shown in window */
1841 for (;;)
1842 {
1843 /* stop updating when reached the end of the window (check for _past_
1844 * the end of the window is at the end of the loop) */
1845 if (row == wp->w_height)
1846 {
1847 didline = TRUE;
1848 break;
1849 }
1850
1851 /* stop updating when hit the end of the file */
1852 if (lnum > buf->b_ml.ml_line_count)
1853 {
1854 eof = TRUE;
1855 break;
1856 }
1857
1858 /* Remember the starting row of the line that is going to be dealt
1859 * with. It is used further down when the line doesn't fit. */
1860 srow = row;
1861
1862 /*
1863 * Update a line when it is in an area that needs updating, when it
1864 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001865 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 * win_del_lines(), check if the current line includes it.
1867 * When syntax folding is being used, the saved syntax states will
1868 * already have been updated, we can't see where the syntax state is
1869 * the same again, just update until the end of the window.
1870 */
1871 if (row < top_end
1872 || (row >= mid_start && row < mid_end)
1873#ifdef FEAT_SEARCH_EXTRA
1874 || top_to_mod
1875#endif
1876 || idx >= wp->w_lines_valid
1877 || (row + wp->w_lines[idx].wl_size > bot_start)
1878 || (mod_top != 0
1879 && (lnum == mod_top
1880 || (lnum >= mod_top
1881 && (lnum < mod_bot
1882#ifdef FEAT_SYN_HL
1883 || did_update == DID_FOLD
1884 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001885 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 && (
1887# ifdef FEAT_FOLDING
1888 (foldmethodIsSyntax(wp)
1889 && hasAnyFolding(wp)) ||
1890# endif
1891 syntax_check_changed(lnum)))
1892#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001893#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001894 /* match in fixed position might need redraw
1895 * if lines were inserted or deleted */
1896 || (wp->w_match_head != NULL
1897 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 )))))
1900 {
1901#ifdef FEAT_SEARCH_EXTRA
1902 if (lnum == mod_top)
1903 top_to_mod = FALSE;
1904#endif
1905
1906 /*
1907 * When at start of changed lines: May scroll following lines
1908 * up or down to minimize redrawing.
1909 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001910 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 */
1912 if (lnum == mod_top
1913 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001914 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
1916 int old_rows = 0;
1917 int new_rows = 0;
1918 int xtra_rows;
1919 linenr_T l;
1920
1921 /* Count the old number of window rows, using w_lines[], which
1922 * should still contain the sizes for the lines as they are
1923 * currently displayed. */
1924 for (i = idx; i < wp->w_lines_valid; ++i)
1925 {
1926 /* Only valid lines have a meaningful wl_lnum. Invalid
1927 * lines are part of the changed area. */
1928 if (wp->w_lines[i].wl_valid
1929 && wp->w_lines[i].wl_lnum == mod_bot)
1930 break;
1931 old_rows += wp->w_lines[i].wl_size;
1932#ifdef FEAT_FOLDING
1933 if (wp->w_lines[i].wl_valid
1934 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1935 {
1936 /* Must have found the last valid entry above mod_bot.
1937 * Add following invalid entries. */
1938 ++i;
1939 while (i < wp->w_lines_valid
1940 && !wp->w_lines[i].wl_valid)
1941 old_rows += wp->w_lines[i++].wl_size;
1942 break;
1943 }
1944#endif
1945 }
1946
1947 if (i >= wp->w_lines_valid)
1948 {
1949 /* We can't find a valid line below the changed lines,
1950 * need to redraw until the end of the window.
1951 * Inserting/deleting lines has no use. */
1952 bot_start = 0;
1953 }
1954 else
1955 {
1956 /* Able to count old number of rows: Count new window
1957 * rows, and may insert/delete lines */
1958 j = idx;
1959 for (l = lnum; l < mod_bot; ++l)
1960 {
1961#ifdef FEAT_FOLDING
1962 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1963 ++new_rows;
1964 else
1965#endif
1966#ifdef FEAT_DIFF
1967 if (l == wp->w_topline)
1968 new_rows += plines_win_nofill(wp, l, TRUE)
1969 + wp->w_topfill;
1970 else
1971#endif
1972 new_rows += plines_win(wp, l, TRUE);
1973 ++j;
1974 if (new_rows > wp->w_height - row - 2)
1975 {
1976 /* it's getting too much, must redraw the rest */
1977 new_rows = 9999;
1978 break;
1979 }
1980 }
1981 xtra_rows = new_rows - old_rows;
1982 if (xtra_rows < 0)
1983 {
1984 /* May scroll text up. If there is not enough
1985 * remaining text or scrolling fails, must redraw the
1986 * rest. If scrolling works, must redraw the text
1987 * below the scrolled text. */
1988 if (row - xtra_rows >= wp->w_height - 2)
1989 mod_bot = MAXLNUM;
1990 else
1991 {
1992 check_for_delay(FALSE);
1993 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001994 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 mod_bot = MAXLNUM;
1996 else
1997 bot_start = wp->w_height + xtra_rows;
1998 }
1999 }
2000 else if (xtra_rows > 0)
2001 {
2002 /* May scroll text down. If there is not enough
2003 * remaining text of scrolling fails, must redraw the
2004 * rest. */
2005 if (row + xtra_rows >= wp->w_height - 2)
2006 mod_bot = MAXLNUM;
2007 else
2008 {
2009 check_for_delay(FALSE);
2010 if (win_ins_lines(wp, row + old_rows,
2011 xtra_rows, FALSE, FALSE) == FAIL)
2012 mod_bot = MAXLNUM;
2013 else if (top_end > row + old_rows)
2014 /* Scrolled the part at the top that requires
2015 * updating down. */
2016 top_end += xtra_rows;
2017 }
2018 }
2019
2020 /* When not updating the rest, may need to move w_lines[]
2021 * entries. */
2022 if (mod_bot != MAXLNUM && i != j)
2023 {
2024 if (j < i)
2025 {
2026 int x = row + new_rows;
2027
2028 /* move entries in w_lines[] upwards */
2029 for (;;)
2030 {
2031 /* stop at last valid entry in w_lines[] */
2032 if (i >= wp->w_lines_valid)
2033 {
2034 wp->w_lines_valid = j;
2035 break;
2036 }
2037 wp->w_lines[j] = wp->w_lines[i];
2038 /* stop at a line that won't fit */
2039 if (x + (int)wp->w_lines[j].wl_size
2040 > wp->w_height)
2041 {
2042 wp->w_lines_valid = j + 1;
2043 break;
2044 }
2045 x += wp->w_lines[j++].wl_size;
2046 ++i;
2047 }
2048 if (bot_start > x)
2049 bot_start = x;
2050 }
2051 else /* j > i */
2052 {
2053 /* move entries in w_lines[] downwards */
2054 j -= i;
2055 wp->w_lines_valid += j;
2056 if (wp->w_lines_valid > wp->w_height)
2057 wp->w_lines_valid = wp->w_height;
2058 for (i = wp->w_lines_valid; i - j >= idx; --i)
2059 wp->w_lines[i] = wp->w_lines[i - j];
2060
2061 /* The w_lines[] entries for inserted lines are
2062 * now invalid, but wl_size may be used above.
2063 * Reset to zero. */
2064 while (i >= idx)
2065 {
2066 wp->w_lines[i].wl_size = 0;
2067 wp->w_lines[i--].wl_valid = FALSE;
2068 }
2069 }
2070 }
2071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
2073
2074#ifdef FEAT_FOLDING
2075 /*
2076 * When lines are folded, display one line for all of them.
2077 * Otherwise, display normally (can be several display lines when
2078 * 'wrap' is on).
2079 */
2080 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2081 if (fold_count != 0)
2082 {
2083 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2084 ++row;
2085 --fold_count;
2086 wp->w_lines[idx].wl_folded = TRUE;
2087 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2088# ifdef FEAT_SYN_HL
2089 did_update = DID_FOLD;
2090# endif
2091 }
2092 else
2093#endif
2094 if (idx < wp->w_lines_valid
2095 && wp->w_lines[idx].wl_valid
2096 && wp->w_lines[idx].wl_lnum == lnum
2097 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002098 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar17146962019-05-30 00:12:11 +02002099#ifdef FEAT_TEXT_PROP
2100 && !bt_popup(wp->w_buffer)
2101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 && srow + wp->w_lines[idx].wl_size > wp->w_height
2103#ifdef FEAT_DIFF
2104 && diff_check_fill(wp, lnum) == 0
2105#endif
2106 )
2107 {
2108 /* This line is not going to fit. Don't draw anything here,
2109 * will draw "@ " lines below. */
2110 row = wp->w_height + 1;
2111 }
2112 else
2113 {
2114#ifdef FEAT_SEARCH_EXTRA
2115 prepare_search_hl(wp, lnum);
2116#endif
2117#ifdef FEAT_SYN_HL
2118 /* Let the syntax stuff know we skipped a few lines. */
2119 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002120 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 syntax_end_parsing(syntax_last_parsed + 1);
2122#endif
2123
2124 /*
2125 * Display one line.
2126 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002127 row = win_line(wp, lnum, srow, wp->w_height,
2128 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129
2130#ifdef FEAT_FOLDING
2131 wp->w_lines[idx].wl_folded = FALSE;
2132 wp->w_lines[idx].wl_lastlnum = lnum;
2133#endif
2134#ifdef FEAT_SYN_HL
2135 did_update = DID_LINE;
2136 syntax_last_parsed = lnum;
2137#endif
2138 }
2139
2140 wp->w_lines[idx].wl_lnum = lnum;
2141 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002142
2143 /* Past end of the window or end of the screen. Note that after
2144 * resizing wp->w_height may be end up too big. That's a problem
2145 * elsewhere, but prevent a crash here. */
2146 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
2148 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002149 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2151 ++idx;
2152 break;
2153 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002154 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 wp->w_lines[idx].wl_size = row - srow;
2156 ++idx;
2157#ifdef FEAT_FOLDING
2158 lnum += fold_count + 1;
2159#else
2160 ++lnum;
2161#endif
2162 }
2163 else
2164 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002165 if (wp->w_p_rnu)
2166 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002167#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002168 // 'relativenumber' set: The text doesn't need to be drawn, but
2169 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002170 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2171 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002172 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002173 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002174#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002175 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002176 }
2177
2178 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 row += wp->w_lines[idx++].wl_size;
2180 if (row > wp->w_height) /* past end of screen */
2181 break;
2182#ifdef FEAT_FOLDING
2183 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2184#else
2185 ++lnum;
2186#endif
2187#ifdef FEAT_SYN_HL
2188 did_update = DID_NONE;
2189#endif
2190 }
2191
2192 if (lnum > buf->b_ml.ml_line_count)
2193 {
2194 eof = TRUE;
2195 break;
2196 }
2197 }
2198 /*
2199 * End of loop over all window lines.
2200 */
2201
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002202#ifdef FEAT_VTP
2203 /* Rewrite the character at the end of the screen line. */
2204 if (use_vtp())
2205 {
2206 int i;
2207
2208 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002209 if (enc_utf8)
2210 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2211 LineOffset[i] + screen_Columns) > 1)
2212 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2213 else
2214 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2215 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002216 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2217 }
2218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 if (idx > wp->w_lines_valid)
2221 wp->w_lines_valid = idx;
2222
2223#ifdef FEAT_SYN_HL
2224 /*
2225 * Let the syntax stuff know we stop parsing here.
2226 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002227 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 syntax_end_parsing(syntax_last_parsed + 1);
2229#endif
2230
2231 /*
2232 * If we didn't hit the end of the file, and we didn't finish the last
2233 * line we were working on, then the line didn't fit.
2234 */
2235 wp->w_empty_rows = 0;
2236#ifdef FEAT_DIFF
2237 wp->w_filler_rows = 0;
2238#endif
2239 if (!eof && !didline)
2240 {
2241 if (lnum == wp->w_topline)
2242 {
2243 /*
2244 * Single line that does not fit!
2245 * Don't overwrite it, it can be edited.
2246 */
2247 wp->w_botline = lnum + 1;
2248 }
2249#ifdef FEAT_DIFF
2250 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2251 {
2252 /* Window ends in filler lines. */
2253 wp->w_botline = lnum;
2254 wp->w_filler_rows = wp->w_height - srow;
2255 }
2256#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002257#ifdef FEAT_TEXT_PROP
2258 else if (bt_popup(wp->w_buffer))
2259 {
2260 // popup line that doesn't fit is left as-is
2261 wp->w_botline = lnum;
2262 }
2263#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002264 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2265 {
2266 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2267
2268 /*
2269 * Last line isn't finished: Display "@@@" in the last screen line.
2270 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002271 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002272 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002273 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002274 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002275 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002276 set_empty_rows(wp, srow);
2277 wp->w_botline = lnum;
2278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2280 {
2281 /*
2282 * Last line isn't finished: Display "@@@" at the end.
2283 */
2284 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2285 W_WINROW(wp) + wp->w_height,
2286 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002287 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 set_empty_rows(wp, srow);
2289 wp->w_botline = lnum;
2290 }
2291 else
2292 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002293 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 wp->w_botline = lnum;
2295 }
2296 }
2297 else
2298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (eof) /* we hit the end of the file */
2301 {
2302 wp->w_botline = buf->b_ml.ml_line_count + 1;
2303#ifdef FEAT_DIFF
2304 j = diff_check_fill(wp, wp->w_botline);
2305 if (j > 0 && !wp->w_botfill)
2306 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002307 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (char2cells(fill_diff) > 1)
2309 i = '-';
2310 else
2311 i = fill_diff;
2312 if (row + j > wp->w_height)
2313 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002314 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 row += j;
2316 }
2317#endif
2318 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002319 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 wp->w_botline = lnum;
2321
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002322 // Make sure the rest of the screen is blank
2323 // put '~'s on rows that aren't part of the file.
Bram Moolenaar17146962019-05-30 00:12:11 +02002324 win_draw_end(wp,
2325#ifdef FEAT_TEXT_PROP
2326 bt_popup(wp->w_buffer) ? ' ' :
2327#endif
2328 '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
2330
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002331#ifdef SYN_TIME_LIMIT
2332 syn_set_timeout(NULL);
2333#endif
2334
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 /* Reset the type of redrawing required, the window has been updated. */
2336 wp->w_redr_type = 0;
2337#ifdef FEAT_DIFF
2338 wp->w_old_topfill = wp->w_topfill;
2339 wp->w_old_botfill = wp->w_botfill;
2340#endif
2341
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002342 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 /*
2345 * There is a trick with w_botline. If we invalidate it on each
2346 * change that might modify it, this will cause a lot of expensive
2347 * calls to plines() in update_topline() each time. Therefore the
2348 * value of w_botline is often approximated, and this value is used to
2349 * compute the value of w_topline. If the value of w_botline was
2350 * wrong, check that the value of w_topline is correct (cursor is on
2351 * the visible part of the text). If it's not, we need to redraw
2352 * again. Mostly this just means scrolling up a few lines, so it
2353 * doesn't look too bad. Only do this for the current window (where
2354 * changes are relevant).
2355 */
2356 wp->w_valid |= VALID_BOTLINE;
2357 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2358 {
2359 recursive = TRUE;
2360 curwin->w_valid &= ~VALID_TOPLINE;
2361 update_topline(); /* may invalidate w_botline again */
2362 if (must_redraw != 0)
2363 {
2364 /* Don't update for changes in buffer again. */
2365 i = curbuf->b_mod_set;
2366 curbuf->b_mod_set = FALSE;
2367 win_update(curwin);
2368 must_redraw = 0;
2369 curbuf->b_mod_set = i;
2370 }
2371 recursive = FALSE;
2372 }
2373 }
2374
2375#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2376 /* restore got_int, unless CTRL-C was hit while redrawing */
2377 if (!got_int)
2378 got_int = save_got_int;
2379#endif
2380}
2381
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002383 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2384 * Return the new offset.
2385 */
2386 static int
2387screen_fill_end(
2388 win_T *wp,
2389 int c1,
2390 int c2,
2391 int off,
2392 int width,
2393 int row,
2394 int endrow,
2395 int attr)
2396{
2397 int nn = off + width;
2398
2399 if (nn > wp->w_width)
2400 nn = wp->w_width;
2401#ifdef FEAT_RIGHTLEFT
2402 if (wp->w_p_rl)
2403 {
2404 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2405 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2406 c1, c2, attr);
2407 }
2408 else
2409#endif
2410 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2411 wp->w_wincol + off, (int)wp->w_wincol + nn,
2412 c1, c2, attr);
2413 return nn;
2414}
2415
2416/*
2417 * Clear lines near the end the window and mark the unused lines with "c1".
2418 * use "c2" as the filler character.
2419 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 */
2421 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002422win_draw_end(
2423 win_T *wp,
2424 int c1,
2425 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002426 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002427 int row,
2428 int endrow,
2429 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002432 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002433 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002434
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002435 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002436
2437 if (draw_margin)
2438 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002439#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002440 int fdc = compute_foldcolumn(wp, 0);
2441
2442 if (fdc > 0)
2443 // draw the fold column
2444 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002445 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002446#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002447#ifdef FEAT_SIGNS
2448 if (signcolumn_on(wp))
2449 // draw the sign column
2450 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002451 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002452#endif
2453 if ((wp->w_p_nu || wp->w_p_rnu)
2454 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2455 // draw the number column
2456 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002457 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460#ifdef FEAT_RIGHTLEFT
2461 if (wp->w_p_rl)
2462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002464 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002465 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002467 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002468 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
2470 else
2471#endif
2472 {
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 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002475 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002477
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 set_empty_rows(wp, row);
2479}
2480
Bram Moolenaar1a384422010-07-14 19:53:30 +02002481#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002482/*
2483 * Advance **color_cols and return TRUE when there are columns to draw.
2484 */
2485 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002486advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002487{
2488 while (**color_cols >= 0 && vcol > **color_cols)
2489 ++*color_cols;
2490 return (**color_cols >= 0);
2491}
2492#endif
2493
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002494#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2495/*
2496 * Copy "text" to ScreenLines using "attr".
2497 * Returns the next screen column.
2498 */
2499 static int
2500text_to_screenline(win_T *wp, char_u *text, int col)
2501{
2502 int off = (int)(current_ScreenLine - ScreenLines);
2503
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002504 if (has_mbyte)
2505 {
2506 int cells;
2507 int u8c, u8cc[MAX_MCO];
2508 int i;
2509 int idx;
2510 int c_len;
2511 char_u *p;
2512# ifdef FEAT_ARABIC
2513 int prev_c = 0; /* previous Arabic character */
2514 int prev_c1 = 0; /* first composing char for prev_c */
2515# endif
2516
2517# ifdef FEAT_RIGHTLEFT
2518 if (wp->w_p_rl)
2519 idx = off;
2520 else
2521# endif
2522 idx = off + col;
2523
2524 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2525 for (p = text; *p != NUL; )
2526 {
2527 cells = (*mb_ptr2cells)(p);
2528 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002529 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002530# ifdef FEAT_RIGHTLEFT
2531 - (wp->w_p_rl ? col : 0)
2532# endif
2533 )
2534 break;
2535 ScreenLines[idx] = *p;
2536 if (enc_utf8)
2537 {
2538 u8c = utfc_ptr2char(p, u8cc);
2539 if (*p < 0x80 && u8cc[0] == 0)
2540 {
2541 ScreenLinesUC[idx] = 0;
2542#ifdef FEAT_ARABIC
2543 prev_c = u8c;
2544#endif
2545 }
2546 else
2547 {
2548#ifdef FEAT_ARABIC
2549 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2550 {
2551 /* Do Arabic shaping. */
2552 int pc, pc1, nc;
2553 int pcc[MAX_MCO];
2554 int firstbyte = *p;
2555
2556 /* The idea of what is the previous and next
2557 * character depends on 'rightleft'. */
2558 if (wp->w_p_rl)
2559 {
2560 pc = prev_c;
2561 pc1 = prev_c1;
2562 nc = utf_ptr2char(p + c_len);
2563 prev_c1 = u8cc[0];
2564 }
2565 else
2566 {
2567 pc = utfc_ptr2char(p + c_len, pcc);
2568 nc = prev_c;
2569 pc1 = pcc[0];
2570 }
2571 prev_c = u8c;
2572
2573 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2574 pc, pc1, nc);
2575 ScreenLines[idx] = firstbyte;
2576 }
2577 else
2578 prev_c = u8c;
2579#endif
2580 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002581 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002582 for (i = 0; i < Screen_mco; ++i)
2583 {
2584 ScreenLinesC[i][idx] = u8cc[i];
2585 if (u8cc[i] == 0)
2586 break;
2587 }
2588 }
2589 if (cells > 1)
2590 ScreenLines[idx + 1] = 0;
2591 }
2592 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2593 /* double-byte single width character */
2594 ScreenLines2[idx] = p[1];
2595 else if (cells > 1)
2596 /* double-width character */
2597 ScreenLines[idx + 1] = p[1];
2598 col += cells;
2599 idx += cells;
2600 p += c_len;
2601 }
2602 }
2603 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002604 {
2605 int len = (int)STRLEN(text);
2606
Bram Moolenaar02631462017-09-22 15:20:32 +02002607 if (len > wp->w_width - col)
2608 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002609 if (len > 0)
2610 {
2611#ifdef FEAT_RIGHTLEFT
2612 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002613 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002614 else
2615#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002616 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002617 col += len;
2618 }
2619 }
2620 return col;
2621}
2622#endif
2623
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624#ifdef FEAT_FOLDING
2625/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002626 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2627 * space is available for window "wp", minus "col".
2628 */
2629 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002630compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002631{
2632 int fdc = wp->w_p_fdc;
2633 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002634 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002635
2636 if (fdc > wwidth - (col + wmw))
2637 fdc = wwidth - (col + wmw);
2638 return fdc;
2639}
2640
2641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 * Display one folded line.
2643 */
2644 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002645fold_line(
2646 win_T *wp,
2647 long fold_count,
2648 foldinfo_T *foldinfo,
2649 linenr_T lnum,
2650 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002652 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 pos_T *top, *bot;
2654 linenr_T lnume = lnum + fold_count - 1;
2655 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002656 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 int col;
2659 int txtcol;
2660 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002661 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
2663 /* Build the fold line:
2664 * 1. Add the cmdwin_type for the command-line window
2665 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002666 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 * 4. Compose the text
2668 * 5. Add the text
2669 * 6. set highlighting for the Visual area an other text
2670 */
2671 col = 0;
2672
2673 /*
2674 * 1. Add the cmdwin_type for the command-line window
2675 * Ignores 'rightleft', this window is never right-left.
2676 */
2677#ifdef FEAT_CMDWIN
2678 if (cmdwin_type != 0 && wp == curwin)
2679 {
2680 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002681 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (enc_utf8)
2683 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 ++col;
2685 }
2686#endif
2687
2688 /*
2689 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002690 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002692 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if (fdc > 0)
2694 {
2695 fill_foldcolumn(buf, wp, TRUE, lnum);
2696#ifdef FEAT_RIGHTLEFT
2697 if (wp->w_p_rl)
2698 {
2699 int i;
2700
Bram Moolenaar02631462017-09-22 15:20:32 +02002701 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002702 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 /* reverse the fold column */
2704 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002705 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 }
2707 else
2708#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002709 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 col += fdc;
2711 }
2712
2713#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002714# define RL_MEMSET(p, v, l) \
2715 do { \
2716 if (wp->w_p_rl) \
2717 for (ri = 0; ri < l; ++ri) \
2718 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2719 else \
2720 for (ri = 0; ri < l; ++ri) \
2721 ScreenAttrs[off + (p) + ri] = v; \
2722 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002724# define RL_MEMSET(p, v, l) \
2725 do { \
2726 for (ri = 0; ri < l; ++ri) \
2727 ScreenAttrs[off + (p) + ri] = v; \
2728 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729#endif
2730
Bram Moolenaar64486672010-05-16 15:46:46 +02002731 /* Set all attributes of the 'number' or 'relativenumber' column and the
2732 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002733 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
2735#ifdef FEAT_SIGNS
2736 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002737 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002739 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 if (len > 0)
2741 {
2742 if (len > 2)
2743 len = 2;
2744# ifdef FEAT_RIGHTLEFT
2745 if (wp->w_p_rl)
2746 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002747 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002748 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 else
2750# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002751 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 col += len;
2753 }
2754 }
2755#endif
2756
2757 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002758 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002760 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002762 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if (len > 0)
2764 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002765 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002766 long num;
2767 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002768
2769 if (len > w + 1)
2770 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002771
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002772 if (wp->w_p_nu && !wp->w_p_rnu)
2773 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002774 num = (long)lnum;
2775 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002776 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002777 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002778 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002779 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002780 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002781 /* 'number' + 'relativenumber': cursor line shows absolute
2782 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002783 num = lnum;
2784 fmt = "%-*ld ";
2785 }
2786 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002787
Bram Moolenaar700e7342013-01-30 12:31:36 +01002788 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789#ifdef FEAT_RIGHTLEFT
2790 if (wp->w_p_rl)
2791 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002792 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002793 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 else
2795#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002796 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 col += len;
2798 }
2799 }
2800
2801 /*
2802 * 4. Compose the folded-line string with 'foldtext', if set.
2803 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002804 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805
2806 txtcol = col; /* remember where text starts */
2807
2808 /*
2809 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2810 * Right-left text is put in columns 0 - number-col, normal text is put
2811 * in columns number-col - window-width.
2812 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002813 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
2815 /* Fill the rest of the line with the fold filler */
2816#ifdef FEAT_RIGHTLEFT
2817 if (wp->w_p_rl)
2818 col -= txtcol;
2819#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002820 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821#ifdef FEAT_RIGHTLEFT
2822 - (wp->w_p_rl ? txtcol : 0)
2823#endif
2824 )
2825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 if (enc_utf8)
2827 {
2828 if (fill_fold >= 0x80)
2829 {
2830 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002831 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002832 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 }
2834 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002837 ScreenLines[off + col] = fill_fold;
2838 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002839 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002841 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002842 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 }
2844
2845 if (text != buf)
2846 vim_free(text);
2847
2848 /*
2849 * 6. set highlighting for the Visual area an other text.
2850 * If all folded lines are in the Visual area, highlight the line.
2851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2853 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002854 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 {
2856 /* Visual is after curwin->w_cursor */
2857 top = &curwin->w_cursor;
2858 bot = &VIsual;
2859 }
2860 else
2861 {
2862 /* Visual is before curwin->w_cursor */
2863 top = &VIsual;
2864 bot = &curwin->w_cursor;
2865 }
2866 if (lnum >= top->lnum
2867 && lnume <= bot->lnum
2868 && (VIsual_mode != 'v'
2869 || ((lnum > top->lnum
2870 || (lnum == top->lnum
2871 && top->col == 0))
2872 && (lnume < bot->lnum
2873 || (lnume == bot->lnum
2874 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002875 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 {
2877 if (VIsual_mode == Ctrl_V)
2878 {
2879 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002880 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002882 if (wp->w_old_cursor_lcol != MAXCOL
2883 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002884 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 len = wp->w_old_cursor_lcol;
2886 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002887 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002888 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002889 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
2891 }
2892 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002895 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
2898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002900#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002901 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2902 if (wp->w_p_cc_cols)
2903 {
2904 int i = 0;
2905 int j = wp->w_p_cc_cols[i];
2906 int old_txtcol = txtcol;
2907
2908 while (j > -1)
2909 {
2910 txtcol += j;
2911 if (wp->w_p_wrap)
2912 txtcol -= wp->w_skipcol;
2913 else
2914 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002915 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002916 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002917 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002918 txtcol = old_txtcol;
2919 j = wp->w_p_cc_cols[++i];
2920 }
2921 }
2922
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002923 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002924 if (wp->w_p_cuc)
2925 {
2926 txtcol += wp->w_virtcol;
2927 if (wp->w_p_wrap)
2928 txtcol -= wp->w_skipcol;
2929 else
2930 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002931 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002932 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002933 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002934 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936
Bram Moolenaar02631462017-09-22 15:20:32 +02002937 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002938 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939
2940 /*
2941 * Update w_cline_height and w_cline_folded if the cursor line was
2942 * updated (saves a call to plines() later).
2943 */
2944 if (wp == curwin
2945 && lnum <= curwin->w_cursor.lnum
2946 && lnume >= curwin->w_cursor.lnum)
2947 {
2948 curwin->w_cline_row = row;
2949 curwin->w_cline_height = 1;
2950 curwin->w_cline_folded = TRUE;
2951 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2952 }
2953}
2954
2955/*
2956 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2957 */
2958 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002959copy_text_attr(
2960 int off,
2961 char_u *buf,
2962 int len,
2963 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002965 int i;
2966
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 if (enc_utf8)
2969 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002970 for (i = 0; i < len; ++i)
2971 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972}
2973
2974/*
2975 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002976 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 */
2978 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002979fill_foldcolumn(
2980 char_u *p,
2981 win_T *wp,
2982 int closed, /* TRUE of FALSE */
2983 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 int i = 0;
2986 int level;
2987 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002988 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002989 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
2991 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002992 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
2994 level = win_foldinfo.fi_level;
2995 if (level > 0)
2996 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002997 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002998 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 /* If the column is too narrow, we start at the lowest level that
3001 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003002 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 if (first_level < 1)
3004 first_level = 1;
3005
Bram Moolenaar1c934292015-01-27 16:39:29 +01003006 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 {
3008 if (win_foldinfo.fi_lnum == lnum
3009 && first_level + i >= win_foldinfo.fi_low_level)
3010 p[i] = '-';
3011 else if (first_level == 1)
3012 p[i] = '|';
3013 else if (first_level + i <= 9)
3014 p[i] = '0' + first_level + i;
3015 else
3016 p[i] = '>';
3017 if (first_level + i == level)
3018 break;
3019 }
3020 }
3021 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003022 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023}
3024#endif /* FEAT_FOLDING */
3025
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003026#ifdef FEAT_TEXT_PROP
3027static textprop_T *current_text_props = NULL;
3028static buf_T *current_buf = NULL;
3029
3030 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003031text_prop_compare(const void *s1, const void *s2)
3032{
3033 int idx1, idx2;
3034 proptype_T *pt1, *pt2;
3035 colnr_T col1, col2;
3036
3037 idx1 = *(int *)s1;
3038 idx2 = *(int *)s2;
3039 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3040 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3041 if (pt1 == pt2)
3042 return 0;
3043 if (pt1 == NULL)
3044 return -1;
3045 if (pt2 == NULL)
3046 return 1;
3047 if (pt1->pt_priority != pt2->pt_priority)
3048 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3049 col1 = current_text_props[idx1].tp_col;
3050 col2 = current_text_props[idx2].tp_col;
3051 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3052}
3053#endif
3054
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055/*
3056 * Display line "lnum" of window 'wp' on the screen.
3057 * Start at row "startrow", stop when "endrow" is reached.
3058 * wp->w_virtcol needs to be valid.
3059 *
3060 * Return the number of last row the line occupies.
3061 */
3062 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003063win_line(
3064 win_T *wp,
3065 linenr_T lnum,
3066 int startrow,
3067 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003068 int nochange UNUSED, // not updating for changed text
3069 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003071 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3073 int c = 0; /* init for GCC */
3074 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003075#ifdef FEAT_LINEBREAK
3076 long vcol_sbr = -1; /* virtual column after showbreak */
3077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 long vcol_prev = -1; /* "vcol" of previous character */
3079 char_u *line; /* current line */
3080 char_u *ptr; /* current position in "line" */
3081 int row; /* row in the window, excl w_winrow */
3082 int screen_row; /* row on the screen, incl w_winrow */
3083
3084 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3085 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003086 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003087 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003089 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 int extra_attr = 0; /* attributes when n_extra != 0 */
3091 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3092 displaying lcs_eol at end-of-line */
3093 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3094 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3095
3096 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3097 int saved_n_extra = 0;
3098 char_u *saved_p_extra = NULL;
3099 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003100 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 int saved_char_attr = 0;
3102
3103 int n_attr = 0; /* chars with special attr */
3104 int saved_attr2 = 0; /* char_attr saved for n_attr */
3105 int n_attr3 = 0; /* chars with overruling special attr */
3106 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3107
3108 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3109
3110 int fromcol, tocol; /* start/end of inverting */
3111 int fromcol_prev = -2; /* start of inverting after cursor */
3112 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003114 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 pos_T pos;
3116 long v;
3117
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003118 int char_attr = 0; // attributes for next character
3119 int attr_pri = FALSE; // char_attr has priority
3120 int area_highlighting = FALSE; // Visual or incsearch highlighting
3121 // in this line
3122 int vi_attr = 0; // attributes for Visual and incsearch
3123 // highlighting
3124 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003125 int win_attr = 0; // background for whole window, except
3126 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003127 int area_attr = 0; // attributes desired by highlighting
3128 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003130 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 int syntax_attr = 0; /* attributes desired by syntax */
3132 int has_syntax = FALSE; /* this buffer has syntax highl. */
3133 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003134 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003135 int draw_color_col = FALSE; /* highlight colorcolumn */
3136 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003137#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003138#ifdef FEAT_TEXT_PROP
3139 int text_prop_count;
3140 int text_prop_next = 0; // next text property to use
3141 textprop_T *text_props = NULL;
3142 int *text_prop_idxs = NULL;
3143 int text_props_active = 0;
3144 proptype_T *text_prop_type = NULL;
3145 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003146 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003147#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003148#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003149 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003150# define SPWORDLEN 150
3151 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003152 int nextlinecol = 0; /* column where nextline[] starts */
3153 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003154 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003155 int spell_attr = 0; /* attributes desired by spelling */
3156 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003157 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3158 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003159 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003160 static int cap_col = -1; /* column to check for Cap word */
3161 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003162 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003164 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 int multi_attr = 0; /* attributes desired by multibyte */
3166 int mb_l = 1; /* multi-byte byte length */
3167 int mb_c = 0; /* decoded multi-byte character */
3168 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003169 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170#ifdef FEAT_DIFF
3171 int filler_lines; /* nr of filler lines to be drawn */
3172 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003173 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 int change_start = MAXCOL; /* first col of changed area */
3175 int change_end = -1; /* last col of changed area */
3176#endif
3177 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3178#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003179 int need_showbreak = FALSE; /* overlong line, skipping first x
3180 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003182#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003183 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003185 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186#endif
3187#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003188 matchitem_T *cur; /* points to the match list */
3189 match_T *shl; /* points to search_hl or a match */
3190 int shl_flag; /* flag to indicate whether search_hl
3191 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003192 int pos_inprogress; /* marks that position match search is
3193 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003194 int prevcol_hl_flag; /* flag to indicate whether prevcol
3195 equals startcol of search_hl or one
3196 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197#endif
3198#ifdef FEAT_ARABIC
3199 int prev_c = 0; /* previous Arabic character */
3200 int prev_c1 = 0; /* first composing char for prev_c */
3201#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003202#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003203 int did_line_attr = 0;
3204#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003205#ifdef FEAT_TERMINAL
3206 int get_term_attr = FALSE;
3207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
3209 /* draw_state: items that are drawn in sequence: */
3210#define WL_START 0 /* nothing done yet */
3211#ifdef FEAT_CMDWIN
3212# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3213#else
3214# define WL_CMDLINE WL_START
3215#endif
3216#ifdef FEAT_FOLDING
3217# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3218#else
3219# define WL_FOLD WL_CMDLINE
3220#endif
3221#ifdef FEAT_SIGNS
3222# define WL_SIGN WL_FOLD + 1 /* column for signs */
3223#else
3224# define WL_SIGN WL_FOLD /* column for signs */
3225#endif
3226#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003227#ifdef FEAT_LINEBREAK
3228# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003230# define WL_BRI WL_NR
3231#endif
3232#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3233# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3234#else
3235# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236#endif
3237#define WL_LINE WL_SBR + 1 /* text in the line */
3238 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003239#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 int feedback_col = 0;
3241 int feedback_old_attr = -1;
3242#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003243 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
Bram Moolenaar860cae12010-06-05 23:22:07 +02003245#ifdef FEAT_CONCEAL
3246 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003247 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003248 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003249 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003250 int is_concealing = FALSE;
3251 int boguscols = 0; /* nonexistent columns added to force
3252 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003253 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003254 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003255 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003256 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003257# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003258# define FIX_FOR_BOGUSCOLS \
3259 { \
3260 n_extra += vcol_off; \
3261 vcol -= vcol_off; \
3262 vcol_off = 0; \
3263 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003264 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003265 boguscols = 0; \
3266 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003267#else
3268# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
3271 if (startrow > endrow) /* past the end already! */
3272 return startrow;
3273
3274 row = startrow;
3275 screen_row = row + W_WINROW(wp);
3276
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003277 if (!number_only)
3278 {
3279 /*
3280 * To speed up the loop below, set extra_check when there is linebreak,
3281 * trailing white space and/or syntax processing to be done.
3282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003284 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285#endif
3286#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003287 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003288# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003289 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003290# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003291 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003293 /* Prepare for syntax highlighting in this line. When there is an
3294 * error, stop syntax highlighting. */
3295 save_did_emsg = did_emsg;
3296 did_emsg = FALSE;
3297 syntax_start(wp, lnum);
3298 if (did_emsg)
3299 wp->w_s->b_syn_error = TRUE;
3300 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003301 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003302 did_emsg = save_did_emsg;
3303#ifdef SYN_TIME_LIMIT
3304 if (!wp->w_s->b_syn_slow)
3305#endif
3306 {
3307 has_syntax = TRUE;
3308 extra_check = TRUE;
3309 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003312
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003313 /* Check for columns to display for 'colorcolumn'. */
3314 color_cols = wp->w_p_cc_cols;
3315 if (color_cols != NULL)
3316 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003317#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003318
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003319#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003320 if (term_show_buffer(wp->w_buffer))
3321 {
3322 extra_check = TRUE;
3323 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003324 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003325 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003326#endif
3327
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003328#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003329 if (wp->w_p_spell
3330 && *wp->w_s->b_p_spl != NUL
3331 && wp->w_s->b_langp.ga_len > 0
3332 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003333 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003334 /* Prepare for spell checking. */
3335 has_spell = TRUE;
3336 extra_check = TRUE;
3337
Bram Moolenaar7701f302018-10-02 21:20:32 +02003338 /* Get the start of the next line, so that words that wrap to the
3339 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003340 * Trick: skip a few chars for C/shell/Vim comments */
3341 nextline[SPWORDLEN] = NUL;
3342 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3343 {
3344 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3345 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3346 }
3347
Bram Moolenaar7701f302018-10-02 21:20:32 +02003348 /* When a word wrapped from the previous line the start of the
3349 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003350 if (lnum == checked_lnum)
3351 cur_checked_col = checked_col;
3352 checked_lnum = 0;
3353
3354 /* When there was a sentence end in the previous line may require a
3355 * word starting with capital in this line. In line 1 always check
3356 * the first word. */
3357 if (lnum != capcol_lnum)
3358 cap_col = -1;
3359 if (lnum == 1)
3360 cap_col = 0;
3361 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363#endif
3364
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003365 /*
3366 * handle visual active in this window
3367 */
3368 fromcol = -10;
3369 tocol = MAXCOL;
3370 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003372 /* Visual is after curwin->w_cursor */
3373 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003375 top = &curwin->w_cursor;
3376 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003378 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003380 top = &VIsual;
3381 bot = &curwin->w_cursor;
3382 }
3383 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3384 if (VIsual_mode == Ctrl_V) /* block mode */
3385 {
3386 if (lnum_in_visual_area)
3387 {
3388 fromcol = wp->w_old_cursor_fcol;
3389 tocol = wp->w_old_cursor_lcol;
3390 }
3391 }
3392 else /* non-block mode */
3393 {
3394 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003396 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003398 if (VIsual_mode == 'V') /* linewise */
3399 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 else
3401 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003402 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3403 if (gchar_pos(top) == NUL)
3404 tocol = fromcol + 1;
3405 }
3406 }
3407 if (VIsual_mode != 'V' && lnum == bot->lnum)
3408 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003409 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003410 {
3411 fromcol = -10;
3412 tocol = MAXCOL;
3413 }
3414 else if (bot->col == MAXCOL)
3415 tocol = MAXCOL;
3416 else
3417 {
3418 pos = *bot;
3419 if (*p_sel == 'e')
3420 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3421 else
3422 {
3423 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3424 ++tocol;
3425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
3427 }
3428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003430 /* Check if the character under the cursor should not be inverted */
3431 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003432#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003433 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003434#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003435 )
3436 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003438 /* if inverting in this line set area_highlighting */
3439 if (fromcol >= 0)
3440 {
3441 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003442 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003444 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003445 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003446 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003447 && clip_isautosel_plus()))
3448 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003453 /*
3454 * handle 'incsearch' and ":s///c" highlighting
3455 */
3456 else if (highlight_match
3457 && wp == curwin
3458 && lnum >= curwin->w_cursor.lnum
3459 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003461 if (lnum == curwin->w_cursor.lnum)
3462 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003463 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003464 else
3465 fromcol = 0;
3466 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3467 {
3468 pos.lnum = lnum;
3469 pos.col = search_match_endcol;
3470 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3471 }
3472 else
3473 tocol = MAXCOL;
3474 /* do at least one character; happens when past end of line */
3475 if (fromcol == tocol)
3476 tocol = fromcol + 1;
3477 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003478 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481
3482#ifdef FEAT_DIFF
3483 filler_lines = diff_check(wp, lnum);
3484 if (filler_lines < 0)
3485 {
3486 if (filler_lines == -1)
3487 {
3488 if (diff_find_change(wp, lnum, &change_start, &change_end))
3489 diff_hlf = HLF_ADD; /* added line */
3490 else if (change_start == 0)
3491 diff_hlf = HLF_TXD; /* changed text */
3492 else
3493 diff_hlf = HLF_CHD; /* changed line */
3494 }
3495 else
3496 diff_hlf = HLF_ADD; /* added line */
3497 filler_lines = 0;
3498 area_highlighting = TRUE;
3499 }
3500 if (lnum == wp->w_topline)
3501 filler_lines = wp->w_topfill;
3502 filler_todo = filler_lines;
3503#endif
3504
3505#ifdef LINE_ATTR
3506# ifdef FEAT_SIGNS
3507 /* If this line has a sign with line highlighting set line_attr. */
3508 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3509 if (v != 0)
3510 line_attr = sign_get_attr((int)v, TRUE);
3511# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003512# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003514 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003515 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516# endif
3517 if (line_attr != 0)
3518 area_highlighting = TRUE;
3519#endif
3520
3521 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3522 ptr = line;
3523
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003524#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003525 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003526 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003527 /* For checking first word with a capital skip white space. */
3528 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003529 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003530
Bram Moolenaar30abd282005-06-22 22:35:10 +00003531 /* To be able to spell-check over line boundaries copy the end of the
3532 * current line into nextline[]. Above the start of the next line was
3533 * copied to nextline[SPWORDLEN]. */
3534 if (nextline[SPWORDLEN] == NUL)
3535 {
3536 /* No next line or it is empty. */
3537 nextlinecol = MAXCOL;
3538 nextline_idx = 0;
3539 }
3540 else
3541 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003542 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003543 if (v < SPWORDLEN)
3544 {
3545 /* Short line, use it completely and append the start of the
3546 * next line. */
3547 nextlinecol = 0;
3548 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003549 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003550 nextline_idx = v + 1;
3551 }
3552 else
3553 {
3554 /* Long line, use only the last SPWORDLEN bytes. */
3555 nextlinecol = v - SPWORDLEN;
3556 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3557 nextline_idx = SPWORDLEN + 1;
3558 }
3559 }
3560 }
3561#endif
3562
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003563 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003565 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003566 extra_check = TRUE;
3567 /* find start of trailing whitespace */
3568 if (lcs_trail)
3569 {
3570 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003571 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003572 --trailcol;
3573 trailcol += (colnr_T) (ptr - line);
3574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
3576
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003577 wcr_attr = get_wcr_attr(wp);
3578 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003579 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003580 win_attr = wcr_attr;
3581 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003582 }
3583#ifdef FEAT_TEXT_PROP
3584 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003585 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003586#endif
3587
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 /*
3589 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3590 * first character to be displayed.
3591 */
3592 if (wp->w_p_wrap)
3593 v = wp->w_skipcol;
3594 else
3595 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003596 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 while (vcol < v && *ptr != NUL)
3601 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003602 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003605 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
3607
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003608 /* When:
3609 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003610 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003611 * - 'virtualedit' is set, or
3612 * - the visual mode is active,
3613 * the end of the line may be before the start of the displayed part.
3614 */
3615 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003616#ifdef FEAT_SYN_HL
3617 wp->w_p_cuc || draw_color_col ||
3618#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003619 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003620 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623 /* Handle a character that's not completely on the screen: Put ptr at
3624 * that character but skip the first few screen characters. */
3625 if (vcol > v)
3626 {
3627 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003629 /* If the character fits on the screen, don't need to skip it.
3630 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003631 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003632 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
3634
3635 /*
3636 * Adjust for when the inverted text is before the screen,
3637 * and when the start of the inverted text is before the screen.
3638 */
3639 if (tocol <= vcol)
3640 fromcol = 0;
3641 else if (fromcol >= 0 && fromcol < vcol)
3642 fromcol = vcol;
3643
3644#ifdef FEAT_LINEBREAK
3645 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3646 if (wp->w_p_wrap)
3647 need_showbreak = TRUE;
3648#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003649#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003650 /* When spell checking a word we need to figure out the start of the
3651 * word and if it's badly spelled or not. */
3652 if (has_spell)
3653 {
3654 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003655 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003656 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003657
3658 pos = wp->w_cursor;
3659 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003660 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003661 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003662
3663 /* spell_move_to() may call ml_get() and make "line" invalid */
3664 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3665 ptr = line + linecol;
3666
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003667 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003668 {
3669 /* no bad word found at line start, don't check until end of a
3670 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003671 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003672 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003673 }
3674 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003675 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003676 /* bad word found, use attributes until end of word */
3677 word_end = wp->w_cursor.col + len + 1;
3678
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003679 /* Turn index into actual attributes. */
3680 if (spell_hlf != HLF_COUNT)
3681 spell_attr = highlight_attr[spell_hlf];
3682 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003683 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003684
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003685# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003686 /* Need to restart syntax highlighting for this line. */
3687 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003688 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003689# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003690 }
3691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
3693
3694 /*
3695 * Correct highlighting for cursor that can't be disabled.
3696 * Avoids having to check this for each character.
3697 */
3698 if (fromcol >= 0)
3699 {
3700 if (noinvcur)
3701 {
3702 if ((colnr_T)fromcol == wp->w_virtcol)
3703 {
3704 /* highlighting starts at cursor, let it start just after the
3705 * cursor */
3706 fromcol_prev = fromcol;
3707 fromcol = -1;
3708 }
3709 else if ((colnr_T)fromcol < wp->w_virtcol)
3710 /* restart highlighting after the cursor */
3711 fromcol_prev = wp->w_virtcol;
3712 }
3713 if (fromcol >= tocol)
3714 fromcol = -1;
3715 }
3716
3717#ifdef FEAT_SEARCH_EXTRA
3718 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003719 * Handle highlighting the last used search pattern and matches.
3720 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003721 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003723 cur = wp->w_match_head;
3724 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003725 while ((cur != NULL || shl_flag == FALSE) && !number_only
3726 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003728 if (shl_flag == FALSE)
3729 {
3730 shl = &search_hl;
3731 shl_flag = TRUE;
3732 }
3733 else
3734 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003735 shl->startcol = MAXCOL;
3736 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003738 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003739 v = (long)(ptr - line);
3740 if (cur != NULL)
3741 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003742 next_search_hl(wp, shl, lnum, (colnr_T)v,
3743 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003744
3745 /* Need to get the line again, a multi-line regexp may have made it
3746 * invalid. */
3747 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3748 ptr = line + v;
3749
3750 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003752 if (shl->lnum == lnum)
3753 shl->startcol = shl->rm.startpos[0].col;
3754 else
3755 shl->startcol = 0;
3756 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3757 - shl->rm.startpos[0].lnum)
3758 shl->endcol = shl->rm.endpos[0].col;
3759 else
3760 shl->endcol = MAXCOL;
3761 /* Highlight one character for an empty match. */
3762 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003764 if (has_mbyte && line[shl->endcol] != NUL)
3765 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3766 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003767 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003769 if ((long)shl->startcol < v) /* match at leftcol */
3770 {
3771 shl->attr_cur = shl->attr;
3772 search_attr = shl->attr;
3773 }
3774 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003776 if (shl != &search_hl && cur != NULL)
3777 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779#endif
3780
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003781#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003782 // Cursor line highlighting for 'cursorline' in the current window.
3783 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003784 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003785 // Do not show the cursor line when Visual mode is active, because it's
3786 // not clear what is selected then. Do update w_last_cursorline.
3787 if (!(wp == curwin && VIsual_active))
3788 {
3789 line_attr = HL_ATTR(HLF_CUL);
3790 area_highlighting = TRUE;
3791 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003792 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003793 }
3794#endif
3795
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003796#ifdef FEAT_TEXT_PROP
3797 {
3798 char_u *prop_start;
3799
3800 text_prop_count = get_text_props(wp->w_buffer, lnum,
3801 &prop_start, FALSE);
3802 if (text_prop_count > 0)
3803 {
3804 // Make a copy of the properties, so that they are properly
3805 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003806 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003807 if (text_props != NULL)
3808 mch_memmove(text_props, prop_start,
3809 text_prop_count * sizeof(textprop_T));
3810
3811 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003812 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003813 area_highlighting = TRUE;
3814 extra_check = TRUE;
3815 }
3816 }
3817#endif
3818
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003819 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822#ifdef FEAT_RIGHTLEFT
3823 if (wp->w_p_rl)
3824 {
3825 /* Rightleft window: process the text in the normal direction, but put
3826 * it in current_ScreenLine[] from right to left. Start at the
3827 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003828 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003830 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832#endif
3833
3834 /*
3835 * Repeat for the whole displayed line.
3836 */
3837 for (;;)
3838 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003839#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003840 int has_match_conc = 0; // match wants to conceal
3841 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 /* Skip this quickly when working on the text. */
3844 if (draw_state != WL_LINE)
3845 {
3846#ifdef FEAT_CMDWIN
3847 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3848 {
3849 draw_state = WL_CMDLINE;
3850 if (cmdwin_type != 0 && wp == curwin)
3851 {
3852 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003854 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003855 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003856 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
3858 }
3859#endif
3860
3861#ifdef FEAT_FOLDING
3862 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3863 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003864 int fdc = compute_foldcolumn(wp, 0);
3865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003867 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003869 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003870 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003871 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003872 p_extra_free = alloc(12 + 1);
3873
3874 if (p_extra_free != NULL)
3875 {
3876 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3877 n_extra = fdc;
3878 p_extra_free[n_extra] = NUL;
3879 p_extra = p_extra_free;
3880 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003881 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003882 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
3885 }
3886#endif
3887
3888#ifdef FEAT_SIGNS
3889 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3890 {
3891 draw_state = WL_SIGN;
3892 /* Show the sign column when there are any signs in this
3893 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003894 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003896 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003898 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899# endif
3900
3901 /* Draw two cells with the sign value or blank. */
3902 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003903 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003904 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 n_extra = 2;
3906
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003907 if (row == startrow
3908#ifdef FEAT_DIFF
3909 + filler_lines && filler_todo <= 0
3910#endif
3911 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
3913 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3914 SIGN_TEXT);
3915# ifdef FEAT_SIGN_ICONS
3916 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3917 SIGN_ICON);
3918 if (gui.in_use && icon_sign != 0)
3919 {
3920 /* Use the image in this position. */
3921 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003922 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923# ifdef FEAT_NETBEANS_INTG
3924 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003927 c_final = NUL;
3928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929# endif
3930 char_attr = icon_sign;
3931 }
3932 else
3933# endif
3934 if (text_sign != 0)
3935 {
3936 p_extra = sign_get_text(text_sign);
3937 if (p_extra != NULL)
3938 {
3939 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003940 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003941 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943 char_attr = sign_get_attr(text_sign, FALSE);
3944 }
3945 }
3946 }
3947 }
3948#endif
3949
3950 if (draw_state == WL_NR - 1 && n_extra == 0)
3951 {
3952 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003953 /* Display the absolute or relative line number. After the
3954 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3955 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 && (row == startrow
3957#ifdef FEAT_DIFF
3958 + filler_lines
3959#endif
3960 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3961 {
3962 /* Draw the line number (empty space after wrapping). */
3963 if (row == startrow
3964#ifdef FEAT_DIFF
3965 + filler_lines
3966#endif
3967 )
3968 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003969 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003970 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003971
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003972 if (wp->w_p_nu && !wp->w_p_rnu)
3973 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003974 num = (long)lnum;
3975 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003976 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003977 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003978 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003979 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003980 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003981 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003982 num = lnum;
3983 fmt = "%-*ld ";
3984 }
3985 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003986
Bram Moolenaar700e7342013-01-30 12:31:36 +01003987 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003988 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 if (wp->w_skipcol > 0)
3990 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3991 *p_extra = '-';
3992#ifdef FEAT_RIGHTLEFT
3993 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003994 {
3995 char_u *p1, *p2;
3996 int t;
3997
3998 // like rl_mirror(), but keep the space at the end
3999 p2 = skiptowhite(extra) - 1;
4000 for (p1 = extra; p1 < p2; ++p1, --p2)
4001 {
4002 t = *p1;
4003 *p1 = *p2;
4004 *p2 = t;
4005 }
4006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007#endif
4008 p_extra = extra;
4009 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004010 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 }
4012 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004013 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004015 c_final = NUL;
4016 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004017 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004018 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004019#ifdef FEAT_SYN_HL
4020 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004021 * the current line differently.
4022 * TODO: Can we use CursorLine instead of CursorLineNr
4023 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004024 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004025 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004026 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029 }
4030
Bram Moolenaar597a4222014-06-25 14:39:50 +02004031#ifdef FEAT_LINEBREAK
4032 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4033 && n_extra == 0 && *p_sbr != NUL)
4034 /* draw indent after showbreak value */
4035 draw_state = WL_BRI;
4036 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4037 /* After the showbreak, draw the breakindent */
4038 draw_state = WL_BRI - 1;
4039
4040 /* draw 'breakindent': indent wrapped text accordingly */
4041 if (draw_state == WL_BRI - 1 && n_extra == 0)
4042 {
4043 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004044 /* if need_showbreak is set, breakindent also applies */
4045 if (wp->w_p_bri && n_extra == 0
4046 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004047# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004048 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004049# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004050 )
4051 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004052 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004053# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004054 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004055 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004056 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004057# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004058 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4059 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004060 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004061# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004062 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004063# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004064 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004065 c_extra = ' ';
4066 n_extra = get_breakindent_win(wp,
4067 ml_get_buf(wp->w_buffer, lnum, FALSE));
4068 /* Correct end of highlighted area for 'breakindent',
4069 * required when 'linebreak' is also set. */
4070 if (tocol == vcol)
4071 tocol += n_extra;
4072 }
4073 }
4074#endif
4075
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4077 if (draw_state == WL_SBR - 1 && n_extra == 0)
4078 {
4079 draw_state = WL_SBR;
4080# ifdef FEAT_DIFF
4081 if (filler_todo > 0)
4082 {
4083 /* Draw "deleted" diff line(s). */
4084 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004087 c_final = NUL;
4088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004092 c_final = NUL;
4093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094# ifdef FEAT_RIGHTLEFT
4095 if (wp->w_p_rl)
4096 n_extra = col + 1;
4097 else
4098# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004099 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004100 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102# endif
4103# ifdef FEAT_LINEBREAK
4104 if (*p_sbr != NUL && need_showbreak)
4105 {
4106 /* Draw 'showbreak' at the start of each broken line. */
4107 p_extra = p_sbr;
4108 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004109 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004111 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004113 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 /* Correct end of highlighted area for 'showbreak',
4115 * required when 'linebreak' is also set. */
4116 if (tocol == vcol)
4117 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004118#ifdef FEAT_SYN_HL
4119 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004120 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004121 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004122 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125# endif
4126 }
4127#endif
4128
4129 if (draw_state == WL_LINE - 1 && n_extra == 0)
4130 {
4131 draw_state = WL_LINE;
4132 if (saved_n_extra)
4133 {
4134 /* Continue item from end of wrapped line. */
4135 n_extra = saved_n_extra;
4136 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004137 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 p_extra = saved_p_extra;
4139 char_attr = saved_char_attr;
4140 }
4141 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004142 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144 }
4145
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004146 // When still displaying '$' of change command, stop at cursor.
4147 // When only displaying the (relative) line number and that's done,
4148 // stop here.
4149 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004150 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#ifdef FEAT_DIFF
4152 && filler_todo <= 0
4153#endif
4154 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004155 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004157 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004158 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004159 /* Pretend we have finished updating the window. Except when
4160 * 'cursorcolumn' is set. */
4161#ifdef FEAT_SYN_HL
4162 if (wp->w_p_cuc)
4163 row = wp->w_cline_row + wp->w_cline_height;
4164 else
4165#endif
4166 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 break;
4168 }
4169
Bram Moolenaar637532b2019-01-03 21:44:40 +01004170 if (draw_state == WL_LINE && (area_highlighting
4171#ifdef FEAT_SPELL
4172 || has_spell
4173#endif
4174 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 {
4176 /* handle Visual or match highlighting in this line */
4177 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4179 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004181 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004183 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 else if (area_attr != 0
4185 && (vcol == tocol
4186 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
4189#ifdef FEAT_SEARCH_EXTRA
4190 if (!n_extra)
4191 {
4192 /*
4193 * Check for start/end of search pattern match.
4194 * After end, check for start/end of next match.
4195 * When another match, have to check for start again.
4196 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004197 * Do this for 'search_hl' and the match list (ordered by
4198 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004200 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004201 cur = wp->w_match_head;
4202 shl_flag = FALSE;
4203 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004205 if (shl_flag == FALSE
4206 && ((cur != NULL
4207 && cur->priority > SEARCH_HL_PRIORITY)
4208 || cur == NULL))
4209 {
4210 shl = &search_hl;
4211 shl_flag = TRUE;
4212 }
4213 else
4214 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004215 if (cur != NULL)
4216 cur->pos.cur = 0;
4217 pos_inprogress = TRUE;
4218 while (shl->rm.regprog != NULL
4219 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004221 if (shl->startcol != MAXCOL
4222 && v >= (long)shl->startcol
4223 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004225 int tmp_col = v + MB_PTR2LEN(ptr);
4226
4227 if (shl->endcol < tmp_col)
4228 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004230#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004231 // Match with the "Conceal" group results in hiding
4232 // the match.
4233 if (cur != NULL
4234 && shl != &search_hl
4235 && syn_name2id((char_u *)"Conceal")
4236 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004237 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004238 has_match_conc =
4239 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004240 match_conc = cur->conceal_char;
4241 }
4242 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004243 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004246 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
4248 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004249 next_search_hl(wp, shl, lnum, (colnr_T)v,
4250 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004251 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004252 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
4254 /* Need to get the line again, a multi-line regexp
4255 * may have made it invalid. */
4256 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4257 ptr = line + v;
4258
4259 if (shl->lnum == lnum)
4260 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004261 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004263 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004265 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004267 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
4269 /* highlight empty match, try again after
4270 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004272 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004273 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004275 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277
4278 /* Loop to check if the match starts at the
4279 * current position */
4280 continue;
4281 }
4282 }
4283 break;
4284 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004285 if (shl != &search_hl && cur != NULL)
4286 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004288
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004289 /* Use attributes from match with highest priority among
4290 * 'search_hl' and the match list. */
4291 search_attr = search_hl.attr_cur;
4292 cur = wp->w_match_head;
4293 shl_flag = FALSE;
4294 while (cur != NULL || shl_flag == FALSE)
4295 {
4296 if (shl_flag == FALSE
4297 && ((cur != NULL
4298 && cur->priority > SEARCH_HL_PRIORITY)
4299 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004300 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004301 shl = &search_hl;
4302 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004303 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004304 else
4305 shl = &cur->hl;
4306 if (shl->attr_cur != 0)
4307 search_attr = shl->attr_cur;
4308 if (shl != &search_hl && cur != NULL)
4309 cur = cur->next;
4310 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004311 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004312 if (*ptr == NUL && (did_line_attr >= 1
4313 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004314 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
4316#endif
4317
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004319 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004321 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4322 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004324 if (diff_hlf == HLF_TXD && ptr - line > change_end
4325 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004327 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004328 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004329 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 }
4331#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004332
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004333#ifdef FEAT_TEXT_PROP
4334 if (text_props != NULL)
4335 {
4336 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004337 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004338
4339 // Check if any active property ends.
4340 for (pi = 0; pi < text_props_active; ++pi)
4341 {
4342 int tpi = text_prop_idxs[pi];
4343
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004344 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004345 + text_props[tpi].tp_len)
4346 {
4347 if (pi + 1 < text_props_active)
4348 mch_memmove(text_prop_idxs + pi,
4349 text_prop_idxs + pi + 1,
4350 sizeof(int)
4351 * (text_props_active - (pi + 1)));
4352 --text_props_active;
4353 --pi;
4354 }
4355 }
4356
4357 // Add any text property that starts in this column.
4358 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004359 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004360 text_prop_idxs[text_props_active++] = text_prop_next++;
4361
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004362 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004363 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004364 if (text_props_active > 0)
4365 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004366 // Sort the properties on priority and/or starting last.
4367 // Then combine the attributes, highest priority last.
4368 current_text_props = text_props;
4369 current_buf = wp->w_buffer;
4370 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4371 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004372
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004373 for (pi = 0; pi < text_props_active; ++pi)
4374 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004375 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004376 proptype_T *pt = text_prop_type_by_id(
4377 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004378
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004379 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004380 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004381 int pt_attr = syn_id2attr(pt->pt_hl_id);
4382
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004383 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004384 text_prop_attr =
4385 hl_combine_attr(text_prop_attr, pt_attr);
4386 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004387 }
4388 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004389 }
4390 }
4391#endif
4392
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004393 /* Decide which of the highlight attributes to use. */
4394 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004395#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004396 if (area_attr != 0)
4397 char_attr = hl_combine_attr(line_attr, area_attr);
4398 else if (search_attr != 0)
4399 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004400# ifdef FEAT_TEXT_PROP
4401 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004402 {
4403 char_attr = hl_combine_attr(
4404 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4405 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004406# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004407 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004408 || vcol < fromcol || vcol_prev < fromcol_prev
4409 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004410 // Use line_attr when not in the Visual or 'incsearch' area
4411 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004412 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004413#else
4414 if (area_attr != 0)
4415 char_attr = area_attr;
4416 else if (search_attr != 0)
4417 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004418#endif
4419 else
4420 {
4421 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004422#ifdef FEAT_TEXT_PROP
4423 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004424 {
4425 if (text_prop_combine)
4426 char_attr = hl_combine_attr(
4427 syntax_attr, text_prop_attr);
4428 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004429 char_attr = hl_combine_attr(
4430 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004431 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004432 else
4433#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004434#ifdef FEAT_SYN_HL
4435 if (has_syntax)
4436 char_attr = syntax_attr;
4437 else
4438#endif
4439 char_attr = 0;
4440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004442 if (char_attr == 0)
4443 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
4445 /*
4446 * Get the next character to put on the screen.
4447 */
4448 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004449 * The "p_extra" points to the extra stuff that is inserted to
4450 * represent special characters (non-printable stuff) and other
4451 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004452 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004453 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4454 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4456 */
4457 if (n_extra > 0)
4458 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004459 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004461 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004463 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
4465 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004466 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004467 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469 else
4470 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472 else
4473 {
4474 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 if (has_mbyte)
4476 {
4477 mb_c = c;
4478 if (enc_utf8)
4479 {
4480 /* If the UTF-8 character is more than one byte:
4481 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004482 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 mb_utf8 = FALSE;
4484 if (mb_l > n_extra)
4485 mb_l = 1;
4486 else if (mb_l > 1)
4487 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004488 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004490 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492 }
4493 else
4494 {
4495 /* if this is a DBCS character, put it in "mb_c" */
4496 mb_l = MB_BYTE2LEN(c);
4497 if (mb_l >= n_extra)
4498 mb_l = 1;
4499 else if (mb_l > 1)
4500 mb_c = (c << 8) + p_extra[1];
4501 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004502 if (mb_l == 0) /* at the NUL at end-of-line */
4503 mb_l = 1;
4504
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 /* If a double-width char doesn't fit display a '>' in the
4506 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004507 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508# ifdef FEAT_RIGHTLEFT
4509 wp->w_p_rl ? (col <= 0) :
4510# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004511 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 && (*mb_char2cells)(mb_c) == 2)
4513 {
4514 c = '>';
4515 mb_c = c;
4516 mb_l = 1;
4517 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004518 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 /* put the pointer back to output the double-width
4520 * character at the start of the next line. */
4521 ++n_extra;
4522 --p_extra;
4523 }
4524 else
4525 {
4526 n_extra -= mb_l - 1;
4527 p_extra += mb_l - 1;
4528 }
4529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 ++p_extra;
4531 }
4532 --n_extra;
4533 }
4534 else
4535 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004536#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004537 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004538#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004539
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004540 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004541 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 /*
4543 * Get a character from the line itself.
4544 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004545 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004546#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004547 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (has_mbyte)
4550 {
4551 mb_c = c;
4552 if (enc_utf8)
4553 {
4554 /* If the UTF-8 character is more than one byte: Decode it
4555 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004556 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 mb_utf8 = FALSE;
4558 if (mb_l > 1)
4559 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004560 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 /* Overlong encoded ASCII or ASCII with composing char
4562 * is displayed normally, except a NUL. */
4563 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004564 {
4565 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004566#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004567 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004568#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004571
4572 /* At start of the line we can have a composing char.
4573 * Draw it as a space with a composing char. */
4574 if (utf_iscomposing(mb_c))
4575 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004576 int i;
4577
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004578 for (i = Screen_mco - 1; i > 0; --i)
4579 u8cc[i] = u8cc[i - 1];
4580 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004581 mb_c = ' ';
4582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
4584
4585 if ((mb_l == 1 && c >= 0x80)
4586 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004587 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 {
4589 /*
4590 * Illegal UTF-8 byte: display as <xx>.
4591 * Non-BMP character : display as ? or fullwidth ?.
4592 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004593 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004594# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004595 if (wp->w_p_rl) /* reverse */
4596 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 p_extra = extra;
4599 c = *p_extra;
4600 mb_c = mb_ptr2char_adv(&p_extra);
4601 mb_utf8 = (c >= 0x80);
4602 n_extra = (int)STRLEN(p_extra);
4603 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004604 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (area_attr == 0 && search_attr == 0)
4606 {
4607 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004608 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 saved_attr2 = char_attr; /* save current attr */
4610 }
4611 }
4612 else if (mb_l == 0) /* at the NUL at end-of-line */
4613 mb_l = 1;
4614#ifdef FEAT_ARABIC
4615 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4616 {
4617 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004618 int pc, pc1, nc;
4619 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620
4621 /* The idea of what is the previous and next
4622 * character depends on 'rightleft'. */
4623 if (wp->w_p_rl)
4624 {
4625 pc = prev_c;
4626 pc1 = prev_c1;
4627 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004628 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 }
4630 else
4631 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004632 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004634 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636 prev_c = mb_c;
4637
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004638 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640 else
4641 prev_c = mb_c;
4642#endif
4643 }
4644 else /* enc_dbcs */
4645 {
4646 mb_l = MB_BYTE2LEN(c);
4647 if (mb_l == 0) /* at the NUL at end-of-line */
4648 mb_l = 1;
4649 else if (mb_l > 1)
4650 {
4651 /* We assume a second byte below 32 is illegal.
4652 * Hopefully this is OK for all double-byte encodings!
4653 */
4654 if (ptr[1] >= 32)
4655 mb_c = (c << 8) + ptr[1];
4656 else
4657 {
4658 if (ptr[1] == NUL)
4659 {
4660 /* head byte at end of line */
4661 mb_l = 1;
4662 transchar_nonprint(extra, c);
4663 }
4664 else
4665 {
4666 /* illegal tail byte */
4667 mb_l = 2;
4668 STRCPY(extra, "XX");
4669 }
4670 p_extra = extra;
4671 n_extra = (int)STRLEN(extra) - 1;
4672 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004673 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 c = *p_extra++;
4675 if (area_attr == 0 && search_attr == 0)
4676 {
4677 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004678 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 saved_attr2 = char_attr; /* save current attr */
4680 }
4681 mb_c = c;
4682 }
4683 }
4684 }
4685 /* If a double-width char doesn't fit display a '>' in the
4686 * last column; the character is displayed at the start of the
4687 * next line. */
4688 if ((
4689# ifdef FEAT_RIGHTLEFT
4690 wp->w_p_rl ? (col <= 0) :
4691# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004692 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 && (*mb_char2cells)(mb_c) == 2)
4694 {
4695 c = '>';
4696 mb_c = c;
4697 mb_utf8 = FALSE;
4698 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004699 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004700 // Put pointer back so that the character will be
4701 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004703#ifdef FEAT_CONCEAL
4704 did_decrement_ptr = TRUE;
4705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
4707 else if (*ptr != NUL)
4708 ptr += mb_l - 1;
4709
4710 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004711 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004712 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004713 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004716 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004717 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 c = ' ';
4719 if (area_attr == 0 && search_attr == 0)
4720 {
4721 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004722 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 saved_attr2 = char_attr; /* save current attr */
4724 }
4725 mb_c = c;
4726 mb_utf8 = FALSE;
4727 mb_l = 1;
4728 }
4729
4730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 ++ptr;
4732
4733 if (extra_check)
4734 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004735#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004736 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004737#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004738
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004739#ifdef FEAT_TERMINAL
4740 if (get_term_attr)
4741 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004742 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004743
4744 if (!attr_pri)
4745 char_attr = syntax_attr;
4746 else
4747 char_attr = hl_combine_attr(syntax_attr, char_attr);
4748 }
4749#endif
4750
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004751#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004752 // Get syntax attribute, unless still at the start of the line
4753 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004754 v = (long)(ptr - line);
4755 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
4757 /* Get the syntax attribute for the character. If there
4758 * is an error, disable syntax highlighting. */
4759 save_did_emsg = did_emsg;
4760 did_emsg = FALSE;
4761
Bram Moolenaar217ad922005-03-20 22:37:15 +00004762 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004763# ifdef FEAT_SPELL
4764 has_spell ? &can_spell :
4765# endif
4766 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767
4768 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004769 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004770 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004771 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004772 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 else
4775 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004776
4777 // combine syntax attribute with 'wincolor'
4778 if (win_attr != 0)
4779 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4780
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004781#ifdef SYN_TIME_LIMIT
4782 if (wp->w_s->b_syn_slow)
4783 has_syntax = FALSE;
4784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
4786 /* Need to get the line again, a multi-line regexp may
4787 * have made it invalid. */
4788 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4789 ptr = line + v;
4790
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004791# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004792 // Text properties overrule syntax highlighting or combine.
4793 if (text_prop_attr == 0 || text_prop_combine)
4794# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004795 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004796 int comb_attr = syntax_attr;
4797# ifdef FEAT_TEXT_PROP
4798 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4799# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004800 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004801 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004802 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004803 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004804 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004805# ifdef FEAT_CONCEAL
4806 /* no concealing past the end of the line, it interferes
4807 * with line highlighting */
4808 if (c == NUL)
4809 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004810 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004811 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004814#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004815
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004816#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004817 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004818 * Only do this when there is no syntax highlighting, the
4819 * @Spell cluster is not used or the current syntax item
4820 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004821 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004822 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004823 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004824 if (c != 0 && (
4825# ifdef FEAT_SYN_HL
4826 !has_syntax ||
4827# endif
4828 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004829 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004830 char_u *prev_ptr, *p;
4831 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004832 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004833 if (has_mbyte)
4834 {
4835 prev_ptr = ptr - mb_l;
4836 v -= mb_l - 1;
4837 }
4838 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004839 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004840
4841 /* Use nextline[] if possible, it has the start of the
4842 * next line concatenated. */
4843 if ((prev_ptr - line) - nextlinecol >= 0)
4844 p = nextline + (prev_ptr - line) - nextlinecol;
4845 else
4846 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004847 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004848 len = spell_check(wp, p, &spell_hlf, &cap_col,
4849 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004850 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004851
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004852 /* In Insert mode only highlight a word that
4853 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004854 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004855 && (State & INSERT) != 0
4856 && wp->w_cursor.lnum == lnum
4857 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004858 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004859 && wp->w_cursor.col < (colnr_T)word_end)
4860 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004861 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004862 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004863 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004864
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004865 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004866 && (p - nextline) + len > nextline_idx)
4867 {
4868 /* Remember that the good word continues at the
4869 * start of the next line. */
4870 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004871 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004872 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004873
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004874 /* Turn index into actual attributes. */
4875 if (spell_hlf != HLF_COUNT)
4876 spell_attr = highlight_attr[spell_hlf];
4877
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004878 if (cap_col > 0)
4879 {
4880 if (p != prev_ptr
4881 && (p - nextline) + cap_col >= nextline_idx)
4882 {
4883 /* Remember that the word in the next line
4884 * must start with a capital. */
4885 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004886 cap_col = (int)((p - nextline) + cap_col
4887 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004888 }
4889 else
4890 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004891 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004892 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004893 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004894 }
4895 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004896 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004897 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004898 char_attr = hl_combine_attr(char_attr, spell_attr);
4899 else
4900 char_attr = hl_combine_attr(spell_attr, char_attr);
4901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902#endif
4903#ifdef FEAT_LINEBREAK
4904 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004905 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004907 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004908 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004910 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004911 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004912
Bram Moolenaar597a4222014-06-25 14:39:50 +02004913 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004914 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004915 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004916 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004917# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004918 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004919 wp->w_buffer->b_p_vts_array) - 1;
4920# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004921 n_extra = (int)wp->w_buffer->b_p_ts
4922 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004923# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004924
Bram Moolenaar4df70292015-03-21 14:20:16 +01004925 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004926 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004927 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004928 {
4929#ifdef FEAT_CONCEAL
4930 if (c == TAB)
4931 /* See "Tab alignment" below. */
4932 FIX_FOR_BOGUSCOLS;
4933#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004934 if (!wp->w_p_list)
4935 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938#endif
4939
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004940 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4941 // But not when the character is followed by a composing
4942 // character (use mb_l to check that).
4943 if (wp->w_p_list
4944 && ((((c == 160 && mb_l == 1)
4945 || (mb_utf8
4946 && ((mb_c == 160 && mb_l == 2)
4947 || (mb_c == 0x202f && mb_l == 3))))
4948 && lcs_nbsp)
4949 || (c == ' '
4950 && mb_l == 1
4951 && lcs_space
4952 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004953 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004954 c = (c == ' ') ? lcs_space : lcs_nbsp;
4955 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004956 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004957 n_attr = 1;
4958 extra_attr = HL_ATTR(HLF_8);
4959 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004960 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004961 mb_c = c;
4962 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004963 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004964 mb_utf8 = TRUE;
4965 u8cc[0] = 0;
4966 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004967 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004968 else
4969 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004970 }
4971
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4973 {
4974 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004975 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
4977 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004978 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 saved_attr2 = char_attr; /* save current attr */
4980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004982 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
4984 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004985 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004986 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988 else
4989 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991 }
4992
4993 /*
4994 * Handling of non-printable characters.
4995 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004996 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
4998 /*
4999 * when getting a character from the file, we may have to
5000 * turn it into something else on the way to putting it
5001 * into "ScreenLines".
5002 */
5003 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5004 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005005 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005006 long vcol_adjusted = vcol; /* removed showbreak length */
5007#ifdef FEAT_LINEBREAK
5008 /* only adjust the tab_len, when at the first column
5009 * after the showbreak value was drawn */
5010 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5011 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005014#ifdef FEAT_VARTABS
5015 tab_len = tabstop_padding(vcol_adjusted,
5016 wp->w_buffer->b_p_ts,
5017 wp->w_buffer->b_p_vts_array) - 1;
5018#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005019 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005020 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5021#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005022
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005023#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005024 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005025#endif
5026 /* tab amount depends on current column */
5027 n_extra = tab_len;
5028#ifdef FEAT_LINEBREAK
5029 else
5030 {
5031 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005032 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005033 int i;
5034 int saved_nextra = n_extra;
5035
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005036#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005037 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005038 /* there are characters to conceal */
5039 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005040 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5041 */
5042 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5043 && n_extra > tab_len)
5044 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005045#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005046
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005047 /* if n_extra > 0, it gives the number of chars, to
5048 * use for a tab, else we need to calculate the width
5049 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005050 len = (tab_len * mb_char2len(lcs_tab2));
5051 if (n_extra > 0)
5052 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005053 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005054 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005055 vim_memset(p, ' ', len);
5056 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005057 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005058 p_extra_free = p;
5059 for (i = 0; i < tab_len; i++)
5060 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005061 if (*p == NUL)
5062 {
5063 tab_len = i;
5064 break;
5065 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005066 mb_char2bytes(lcs_tab2, p);
5067 p += mb_char2len(lcs_tab2);
5068 n_extra += mb_char2len(lcs_tab2)
5069 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005070 }
5071 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005072#ifdef FEAT_CONCEAL
5073 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5074 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005075 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005076 n_extra -= vcol_off;
5077#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005078 }
5079#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005080#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005081 {
5082 int vc_saved = vcol_off;
5083
5084 /* Tab alignment should be identical regardless of
5085 * 'conceallevel' value. So tab compensates of all
5086 * previous concealed characters, and thus resets
5087 * vcol_off and boguscols accumulated so far in the
5088 * line. Note that the tab can be longer than
5089 * 'tabstop' when there are concealed characters. */
5090 FIX_FOR_BOGUSCOLS;
5091
5092 /* Make sure, the highlighting for the tab char will be
5093 * correctly set further below (effectively reverts the
5094 * FIX_FOR_BOGSUCOLS macro */
5095 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005096 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005097 tab_len += vc_saved;
5098 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if (wp->w_p_list)
5102 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005103 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005104#ifdef FEAT_LINEBREAK
5105 if (wp->w_p_lbr)
5106 c_extra = NUL; /* using p_extra from above */
5107 else
5108#endif
5109 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005110 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005111 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005112 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005115 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
5117 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005118 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005119 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 }
5122 else
5123 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005124 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 c_extra = ' ';
5126 c = ' ';
5127 }
5128 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005129 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005130 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005131 || ((fromcol >= 0 || fromcol_prev >= 0)
5132 && tocol > vcol
5133 && VIsual_mode != Ctrl_V
5134 && (
5135# ifdef FEAT_RIGHTLEFT
5136 wp->w_p_rl ? (col >= 0) :
5137# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005138 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005139 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005140 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005141 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005142 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005144 /* Display a '$' after the line or highlight an extra
5145 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5147 /* For a diff line the highlighting continues after the
5148 * "$". */
5149 if (
5150# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005151 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152# ifdef LINE_ATTR
5153 &&
5154# endif
5155# endif
5156# ifdef LINE_ATTR
5157 line_attr == 0
5158# endif
5159 )
5160#endif
5161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 /* In virtualedit, visual selections may extend
5163 * beyond end of line. */
5164 if (area_highlighting && virtual_active()
5165 && tocol != MAXCOL && vcol < tocol)
5166 n_extra = 0;
5167 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
5169 p_extra = at_end_str;
5170 n_extra = 1;
5171 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005172 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
5174 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005175 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005176 c = lcs_eol;
5177 else
5178 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 lcs_eol_one = -1;
5180 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005181 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005183 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 n_attr = 1;
5185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005187 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
5189 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005190 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005191 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193 else
5194 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
5196 else if (c != NUL)
5197 {
5198 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005199 if (n_extra == 0)
5200 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#ifdef FEAT_RIGHTLEFT
5202 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5203 rl_mirror(p_extra); /* reverse "<12>" */
5204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005206 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005207#ifdef FEAT_LINEBREAK
5208 if (wp->w_p_lbr)
5209 {
5210 char_u *p;
5211
5212 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005213 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005214 vim_memset(p, ' ', n_extra);
5215 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5216 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005217 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005218 p_extra_free = p_extra = p;
5219 }
5220 else
5221#endif
5222 {
5223 n_extra = byte2cells(c) - 1;
5224 c = *p_extra++;
5225 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005226 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
5228 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005229 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 saved_attr2 = char_attr; /* save current attr */
5231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 else if (VIsual_active
5235 && (VIsual_mode == Ctrl_V
5236 || VIsual_mode == 'v')
5237 && virtual_active()
5238 && tocol != MAXCOL
5239 && vcol < tocol
5240 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005241#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005243#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005244 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 {
5246 c = ' ';
5247 --ptr; /* put it back at the NUL */
5248 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005249#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 else if ((
5251# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005252 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005254# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005255 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 ) && (
5259# ifdef FEAT_RIGHTLEFT
5260 wp->w_p_rl ? (col >= 0) :
5261# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005262 (col
5263# ifdef FEAT_CONCEAL
5264 - boguscols
5265# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005266 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 {
5268 /* Highlight until the right side of the window */
5269 c = ' ';
5270 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005271
5272 /* Remember we do the char for line highlighting. */
5273 ++did_line_attr;
5274
5275 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005276 if (line_attr != 0 && char_attr == search_attr
5277 && (did_line_attr > 1
5278 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005279 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280# ifdef FEAT_DIFF
5281 if (diff_hlf == HLF_TXD)
5282 {
5283 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005284 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005285 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005286 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005287 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5288 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005289 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005293# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005294 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005295 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005296 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005297 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5298 char_attr = hl_combine_attr(char_attr,
5299 HL_ATTR(HLF_CUL));
5300 }
5301# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 }
5303#endif
5304 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005305
5306#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005307 if ( wp->w_p_cole > 0
5308 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005309 conceal_cursor_line(wp))
5310 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005311 && !(lnum_in_visual_area
5312 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005313 {
5314 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005315 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005316 && (syn_get_sub_char() != NUL || match_conc
5317 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005318 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005319 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005320 /* First time at this concealed item: display one
5321 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005322 if (match_conc)
5323 c = match_conc;
5324 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005325 c = syn_get_sub_char();
5326 else if (lcs_conceal != NUL)
5327 c = lcs_conceal;
5328 else
5329 c = ' ';
5330
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005331 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005332
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005333 if (n_extra > 0)
5334 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005335 vcol += n_extra;
5336 if (wp->w_p_wrap && n_extra > 0)
5337 {
5338# ifdef FEAT_RIGHTLEFT
5339 if (wp->w_p_rl)
5340 {
5341 col -= n_extra;
5342 boguscols -= n_extra;
5343 }
5344 else
5345# endif
5346 {
5347 boguscols += n_extra;
5348 col += n_extra;
5349 }
5350 }
5351 n_extra = 0;
5352 n_attr = 0;
5353 }
5354 else if (n_skip == 0)
5355 {
5356 is_concealing = TRUE;
5357 n_skip = 1;
5358 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005359 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005360 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005361 {
5362 mb_utf8 = TRUE;
5363 u8cc[0] = 0;
5364 c = 0xc0;
5365 }
5366 else
5367 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005368 }
5369 else
5370 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005371 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005372 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005373 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005374
5375 if (n_skip > 0 && did_decrement_ptr)
5376 // not showing the '>', put pointer back to avoid getting stuck
5377 ++ptr;
5378
5379#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 }
5381
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005382#ifdef FEAT_CONCEAL
5383 /* In the cursor line and we may be concealing characters: correct
5384 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005385 if (!did_wcol && draw_state == WL_LINE
5386 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005387 && conceal_cursor_line(wp)
5388 && (int)wp->w_virtcol <= vcol + n_skip)
5389 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005390# ifdef FEAT_RIGHTLEFT
5391 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005392 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005393 else
5394# endif
5395 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005396 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005397 did_wcol = TRUE;
5398 }
5399#endif
5400
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 /* Don't override visual selection highlighting. */
5402 if (n_attr > 0
5403 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005404 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 char_attr = extra_attr;
5406
Bram Moolenaar81695252004-12-29 20:58:21 +00005407#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 /* XIM don't send preedit_start and preedit_end, but they send
5409 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5410 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005411 if (p_imst == IM_ON_THE_SPOT
5412 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005413 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 && (State & INSERT)
5415 && !p_imdisable
5416 && im_is_preediting()
5417 && draw_state == WL_LINE)
5418 {
5419 colnr_T tcol;
5420
5421 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005422 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 else
5424 tcol = preedit_end_col;
5425 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5426 {
5427 if (feedback_old_attr < 0)
5428 {
5429 feedback_col = 0;
5430 feedback_old_attr = char_attr;
5431 }
5432 char_attr = im_get_feedback_attr(feedback_col);
5433 if (char_attr < 0)
5434 char_attr = feedback_old_attr;
5435 feedback_col++;
5436 }
5437 else if (feedback_old_attr >= 0)
5438 {
5439 char_attr = feedback_old_attr;
5440 feedback_old_attr = -1;
5441 feedback_col = 0;
5442 }
5443 }
5444#endif
5445 /*
5446 * Handle the case where we are in column 0 but not on the first
5447 * character of the line and the user wants us to show us a
5448 * special character (via 'listchars' option "precedes:<char>".
5449 */
5450 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005451 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5453#ifdef FEAT_DIFF
5454 && filler_todo <= 0
5455#endif
5456 && draw_state > WL_NR
5457 && c != NUL)
5458 {
5459 c = lcs_prec;
5460 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005461 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5462 {
5463 /* Double-width character being overwritten by the "precedes"
5464 * character, need to fill up half the character. */
5465 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005466 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005467 n_extra = 1;
5468 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005469 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005472 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 {
5474 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005475 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005476 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 }
5478 else
5479 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005480 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
5482 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005483 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 n_attr3 = 1;
5485 }
5486 }
5487
5488 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005489 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005491 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005492#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005493 || did_line_attr == 1
5494#endif
5495 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005497#ifdef FEAT_SEARCH_EXTRA
5498 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005499
5500 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005501 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005502 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005503#endif
5504
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005505 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 * highlight match at end of line. If it's beyond the last
5507 * char on the screen, just overwrite that one (tricky!) Not
5508 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005509#ifdef FEAT_SEARCH_EXTRA
5510 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005511 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005512 prevcol_hl_flag = TRUE;
5513 else
5514 {
5515 cur = wp->w_match_head;
5516 while (cur != NULL)
5517 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005518 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005519 {
5520 prevcol_hl_flag = TRUE;
5521 break;
5522 }
5523 cur = cur->next;
5524 }
5525 }
5526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005528 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005529 && (VIsual_mode != Ctrl_V
5530 || lnum == VIsual.lnum
5531 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005532 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533#ifdef FEAT_SEARCH_EXTRA
5534 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005535 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005536# ifdef FEAT_SYN_HL
5537 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5538 && !(wp == curwin && VIsual_active))
5539# endif
5540# ifdef FEAT_DIFF
5541 && diff_hlf == (hlf_T)0
5542# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005543# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005544 && did_line_attr <= 1
5545# endif
5546 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547#endif
5548 ))
5549 {
5550 int n = 0;
5551
5552#ifdef FEAT_RIGHTLEFT
5553 if (wp->w_p_rl)
5554 {
5555 if (col < 0)
5556 n = 1;
5557 }
5558 else
5559#endif
5560 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005561 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 n = -1;
5563 }
5564 if (n != 0)
5565 {
5566 /* At the window boundary, highlight the last character
5567 * instead (better than nothing). */
5568 off += n;
5569 col += n;
5570 }
5571 else
5572 {
5573 /* Add a blank character to highlight. */
5574 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 if (enc_utf8)
5576 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
5578#ifdef FEAT_SEARCH_EXTRA
5579 if (area_attr == 0)
5580 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005581 /* Use attributes from match with highest priority among
5582 * 'search_hl' and the match list. */
5583 char_attr = search_hl.attr;
5584 cur = wp->w_match_head;
5585 shl_flag = FALSE;
5586 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005587 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005588 if (shl_flag == FALSE
5589 && ((cur != NULL
5590 && cur->priority > SEARCH_HL_PRIORITY)
5591 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005592 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005593 shl = &search_hl;
5594 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005595 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005596 else
5597 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005598 if ((ptr - line) - 1 == (long)shl->startcol
5599 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005600 char_attr = shl->attr;
5601 if (shl != &search_hl && cur != NULL)
5602 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 }
5605#endif
5606 ScreenAttrs[off] = char_attr;
5607#ifdef FEAT_RIGHTLEFT
5608 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005609 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005611 --off;
5612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 else
5614#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005617 ++off;
5618 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005619 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005620#ifdef FEAT_SYN_HL
5621 eol_hl_off = 1;
5622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625
Bram Moolenaar91170f82006-05-05 21:15:17 +00005626 /*
5627 * At end of the text line.
5628 */
5629 if (c == NUL)
5630 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005631#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005632 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005633 if (wp->w_p_wrap)
5634 v = wp->w_skipcol;
5635 else
5636 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005637
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005638 /* check if line ends before left margin */
5639 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005640 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005641#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005642 // Get rid of the boguscols now, we want to draw until the right
5643 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005644 col -= boguscols;
5645 boguscols = 0;
5646#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005647
5648 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005649 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005650
5651 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005652 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5653 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005654 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005655 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005656 || draw_color_col
5657 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005658# ifdef FEAT_RIGHTLEFT
5659 && !wp->w_p_rl
5660# endif
5661 )
5662 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005663 int rightmost_vcol = 0;
5664 int i;
5665
5666 if (wp->w_p_cuc)
5667 rightmost_vcol = wp->w_virtcol;
5668 if (draw_color_col)
5669 /* determine rightmost colorcolumn to possibly draw */
5670 for (i = 0; color_cols[i] >= 0; ++i)
5671 if (rightmost_vcol < color_cols[i])
5672 rightmost_vcol = color_cols[i];
5673
Bram Moolenaar02631462017-09-22 15:20:32 +02005674 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005675 {
5676 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005677 if (enc_utf8)
5678 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005679 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005680 if (draw_color_col)
5681 draw_color_col = advance_color_col(VCOL_HLC,
5682 &color_cols);
5683
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005684 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005685 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005686 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005687 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005688 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005689 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005690
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005691 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005692 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005693
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005694 ++vcol;
5695 }
5696 }
5697#endif
5698
Bram Moolenaar53f81742017-09-22 14:35:51 +02005699 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005700 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 row++;
5702
5703 /*
5704 * Update w_cline_height and w_cline_folded if the cursor line was
5705 * updated (saves a call to plines() later).
5706 */
5707 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5708 {
5709 curwin->w_cline_row = startrow;
5710 curwin->w_cline_height = row - startrow;
5711#ifdef FEAT_FOLDING
5712 curwin->w_cline_folded = FALSE;
5713#endif
5714 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5715 }
5716
5717 break;
5718 }
5719
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005720 // Show "extends" character from 'listchars' if beyond the line end and
5721 // 'list' is set.
5722 if (lcs_ext != NUL
5723 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 && !wp->w_p_wrap
5725#ifdef FEAT_DIFF
5726 && filler_todo <= 0
5727#endif
5728 && (
5729#ifdef FEAT_RIGHTLEFT
5730 wp->w_p_rl ? col == 0 :
5731#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005732 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005734 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5736 {
5737 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005738 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005740 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
5742 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005743 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005744 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
5746 else
5747 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005750#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005751 /* advance to the next 'colorcolumn' */
5752 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005753 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005754
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005755 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005756 * highlight the cursor position itself.
5757 * Also highlight the 'colorcolumn' if it is different than
5758 * 'cursorcolumn' */
5759 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005760 if (draw_state == WL_LINE && !lnum_in_visual_area
5761 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005762 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005763 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005764 && lnum != wp->w_cursor.lnum)
5765 {
5766 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005767 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005768 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005769 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005770 {
5771 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005772 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005773 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005774 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005775#endif
5776
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 /*
5778 * Store character to be displayed.
5779 * Skip characters that are left of the screen for 'nowrap'.
5780 */
5781 vcol_prev = vcol;
5782 if (draw_state < WL_LINE || n_skip <= 0)
5783 {
5784 /*
5785 * Store the character.
5786 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005787#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5789 {
5790 /* A double-wide character is: put first halve in left cell. */
5791 --off;
5792 --col;
5793 }
5794#endif
5795 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005797 {
5798 if ((mb_c & 0xff00) == 0x8e00)
5799 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 else if (enc_utf8)
5803 {
5804 if (mb_utf8)
5805 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005806 int i;
5807
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005809 if ((c & 0xff) == 0)
5810 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005811 for (i = 0; i < Screen_mco; ++i)
5812 {
5813 ScreenLinesC[i][off] = u8cc[i];
5814 if (u8cc[i] == 0)
5815 break;
5816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
5818 else
5819 ScreenLinesUC[off] = 0;
5820 }
5821 if (multi_attr)
5822 {
5823 ScreenAttrs[off] = multi_attr;
5824 multi_attr = 0;
5825 }
5826 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 ScreenAttrs[off] = char_attr;
5828
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5830 {
5831 /* Need to fill two screen columns. */
5832 ++off;
5833 ++col;
5834 if (enc_utf8)
5835 /* UTF-8: Put a 0 in the second screen char. */
5836 ScreenLines[off] = 0;
5837 else
5838 /* DBCS: Put second byte in the second screen char. */
5839 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005840 if (draw_state > WL_NR
5841#ifdef FEAT_DIFF
5842 && filler_todo <= 0
5843#endif
5844 )
5845 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 /* When "tocol" is halfway a character, set it to the end of
5847 * the character, otherwise highlighting won't stop. */
5848 if (tocol == vcol)
5849 ++tocol;
5850#ifdef FEAT_RIGHTLEFT
5851 if (wp->w_p_rl)
5852 {
5853 /* now it's time to backup one cell */
5854 --off;
5855 --col;
5856 }
5857#endif
5858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859#ifdef FEAT_RIGHTLEFT
5860 if (wp->w_p_rl)
5861 {
5862 --off;
5863 --col;
5864 }
5865 else
5866#endif
5867 {
5868 ++off;
5869 ++col;
5870 }
5871 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005872#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005873 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005874 {
5875 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005876 ++vcol_off;
5877 if (n_extra > 0)
5878 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005879 if (wp->w_p_wrap)
5880 {
5881 /*
5882 * Special voodoo required if 'wrap' is on.
5883 *
5884 * Advance the column indicator to force the line
5885 * drawing to wrap early. This will make the line
5886 * take up the same screen space when parts are concealed,
5887 * so that cursor line computations aren't messed up.
5888 *
5889 * To avoid the fictitious advance of 'col' causing
5890 * trailing junk to be written out of the screen line
5891 * we are building, 'boguscols' keeps track of the number
5892 * of bad columns we have advanced.
5893 */
5894 if (n_extra > 0)
5895 {
5896 vcol += n_extra;
5897# ifdef FEAT_RIGHTLEFT
5898 if (wp->w_p_rl)
5899 {
5900 col -= n_extra;
5901 boguscols -= n_extra;
5902 }
5903 else
5904# endif
5905 {
5906 col += n_extra;
5907 boguscols += n_extra;
5908 }
5909 n_extra = 0;
5910 n_attr = 0;
5911 }
5912
5913
Bram Moolenaar860cae12010-06-05 23:22:07 +02005914 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5915 {
5916 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005917# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005918 if (wp->w_p_rl)
5919 {
5920 --boguscols;
5921 --col;
5922 }
5923 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005924# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005925 {
5926 ++boguscols;
5927 ++col;
5928 }
5929 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005930
5931# ifdef FEAT_RIGHTLEFT
5932 if (wp->w_p_rl)
5933 {
5934 --boguscols;
5935 --col;
5936 }
5937 else
5938# endif
5939 {
5940 ++boguscols;
5941 ++col;
5942 }
5943 }
5944 else
5945 {
5946 if (n_extra > 0)
5947 {
5948 vcol += n_extra;
5949 n_extra = 0;
5950 n_attr = 0;
5951 }
5952 }
5953
5954 }
5955#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 else
5957 --n_skip;
5958
Bram Moolenaar64486672010-05-16 15:46:46 +02005959 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5960 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005961 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962#ifdef FEAT_DIFF
5963 && filler_todo <= 0
5964#endif
5965 )
5966 ++vcol;
5967
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005968#ifdef FEAT_SYN_HL
5969 if (vcol_save_attr >= 0)
5970 char_attr = vcol_save_attr;
5971#endif
5972
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 /* restore attributes after "predeces" in 'listchars' */
5974 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5975 char_attr = saved_attr3;
5976
5977 /* restore attributes after last 'listchars' or 'number' char */
5978 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5979 char_attr = saved_attr2;
5980
5981 /*
5982 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005983 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 */
5985 if ((
5986#ifdef FEAT_RIGHTLEFT
5987 wp->w_p_rl ? (col < 0) :
5988#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005989 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 && (*ptr != NUL
5991#ifdef FEAT_DIFF
5992 || filler_todo > 0
5993#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005994 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5996 )
5997 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005998#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005999 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006000 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006001 boguscols = 0;
6002#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006003 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006004 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 ++row;
6007 ++screen_row;
6008
6009 /* When not wrapping and finished diff lines, or when displayed
6010 * '$' and highlighting until last column, break here. */
6011 if ((!wp->w_p_wrap
6012#ifdef FEAT_DIFF
6013 && filler_todo <= 0
6014#endif
6015 ) || lcs_eol_one == -1)
6016 break;
6017
6018 /* When the window is too narrow draw all "@" lines. */
6019 if (draw_state != WL_LINE
6020#ifdef FEAT_DIFF
6021 && filler_todo <= 0
6022#endif
6023 )
6024 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006025 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 row = endrow;
6028 }
6029
6030 /* When line got too long for screen break here. */
6031 if (row == endrow)
6032 {
6033 ++row;
6034 break;
6035 }
6036
6037 if (screen_cur_row == screen_row - 1
6038#ifdef FEAT_DIFF
6039 && filler_todo <= 0
6040#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006041 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 {
6043 /* Remember that the line wraps, used for modeless copy. */
6044 LineWraps[screen_row - 1] = TRUE;
6045
6046 /*
6047 * Special trick to make copy/paste of wrapped lines work with
6048 * xterm/screen: write an extra character beyond the end of
6049 * the line. This will work with all terminal types
6050 * (regardless of the xn,am settings).
6051 * Only do this on a fast tty.
6052 * Only do this if the cursor is on the current line
6053 * (something has been written in it).
6054 * Don't do this for the GUI.
6055 * Don't do this for double-width characters.
6056 * Don't do this for a window not at the right screen border.
6057 */
6058 if (p_tf
6059#ifdef FEAT_GUI
6060 && !gui.in_use
6061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006063 && ((*mb_off2cells)(LineOffset[screen_row],
6064 LineOffset[screen_row] + screen_Columns)
6065 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006067 + (int)Columns - 2,
6068 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006069 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 {
6071 /* First make sure we are at the end of the screen line,
6072 * then output the same character again to let the
6073 * terminal know about the wrap. If the terminal doesn't
6074 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006075 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 screen_char(LineOffset[screen_row - 1]
6077 + (unsigned)Columns - 1,
6078 screen_row - 1, (int)(Columns - 1));
6079
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 /* When there is a multi-byte character, just output a
6081 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006082 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6083 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 out_char(' ');
6085 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 out_char(ScreenLines[LineOffset[screen_row - 1]
6087 + (Columns - 1)]);
6088 /* force a redraw of the first char on the next line */
6089 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6090 screen_start(); /* don't know where cursor is now */
6091 }
6092 }
6093
6094 col = 0;
6095 off = (unsigned)(current_ScreenLine - ScreenLines);
6096#ifdef FEAT_RIGHTLEFT
6097 if (wp->w_p_rl)
6098 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006099 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 off += col;
6101 }
6102#endif
6103
6104 /* reset the drawing state for the start of a wrapped line */
6105 draw_state = WL_START;
6106 saved_n_extra = n_extra;
6107 saved_p_extra = p_extra;
6108 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006109 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 saved_char_attr = char_attr;
6111 n_extra = 0;
6112 lcs_prec_todo = lcs_prec;
6113#ifdef FEAT_LINEBREAK
6114# ifdef FEAT_DIFF
6115 if (filler_todo <= 0)
6116# endif
6117 need_showbreak = TRUE;
6118#endif
6119#ifdef FEAT_DIFF
6120 --filler_todo;
6121 /* When the filler lines are actually below the last line of the
6122 * file, don't draw the line itself, break here. */
6123 if (filler_todo == 0 && wp->w_botfill)
6124 break;
6125#endif
6126 }
6127
6128 } /* for every character in the line */
6129
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006130#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006131 /* After an empty line check first word for capital. */
6132 if (*skipwhite(line) == NUL)
6133 {
6134 capcol_lnum = lnum + 1;
6135 cap_col = 0;
6136 }
6137#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006138#ifdef FEAT_TEXT_PROP
6139 vim_free(text_props);
6140 vim_free(text_prop_idxs);
6141#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006142
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006143 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 return row;
6145}
6146
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006147/*
6148 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006149 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006150 */
6151 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006152comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006153{
6154 int i;
6155
6156 for (i = 0; i < Screen_mco; ++i)
6157 {
6158 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6159 return TRUE;
6160 if (ScreenLinesC[i][off_from] == 0)
6161 break;
6162 }
6163 return FALSE;
6164}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006165
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166/*
6167 * Check whether the given character needs redrawing:
6168 * - the (first byte of the) character is different
6169 * - the attributes are different
6170 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006171 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 */
6173 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006174char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175{
6176 if (cols > 0
6177 && ((ScreenLines[off_from] != ScreenLines[off_to]
6178 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 || (enc_dbcs != 0
6180 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6181 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6182 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6183 : (cols > 1 && ScreenLines[off_from + 1]
6184 != ScreenLines[off_to + 1])))
6185 || (enc_utf8
6186 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6187 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006188 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006189 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6190 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006191 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 return TRUE;
6193 return FALSE;
6194}
6195
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006196#if defined(FEAT_TERMINAL) || defined(PROTO)
6197/*
6198 * Return the index in ScreenLines[] for the current screen line.
6199 */
6200 int
6201screen_get_current_line_off()
6202{
6203 return (int)(current_ScreenLine - ScreenLines);
6204}
6205#endif
6206
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207/*
6208 * Move one "cooked" screen line to the screen, but only the characters that
6209 * have actually changed. Handle insert/delete character.
6210 * "coloff" gives the first column on the screen for this line.
6211 * "endcol" gives the columns where valid characters are.
6212 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6213 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006214 * "flags" can have bits:
6215 * SLF_POPUP popup window
6216 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6218 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6219 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006220 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006221screen_line(
6222 int row,
6223 int coloff,
6224 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006225 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006226 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227{
6228 unsigned off_from;
6229 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006230 unsigned max_off_from;
6231 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 int force = FALSE; /* force update rest of the line */
6235 int redraw_this /* bool: does character need redraw? */
6236#ifdef FEAT_GUI
6237 = TRUE /* For GUI when while-loop empty */
6238#endif
6239 ;
6240 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 int clear_next = FALSE;
6242 int char_cells; /* 1: normal char */
6243 /* 2: occupies two display cells */
6244# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006246 /* Check for illegal row and col, just in case. */
6247 if (row >= Rows)
6248 row = Rows - 1;
6249 if (endcol > Columns)
6250 endcol = Columns;
6251
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252# ifdef FEAT_CLIPBOARD
6253 clip_may_clear_selection(row, row);
6254# endif
6255
6256 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6257 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006258 max_off_from = off_from + screen_Columns;
6259 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
6261#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006262 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 {
6264 /* Clear rest first, because it's left of the text. */
6265 if (clear_width > 0)
6266 {
6267 while (col <= endcol && ScreenLines[off_to] == ' '
6268 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006269 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
6271 ++off_to;
6272 ++col;
6273 }
6274 if (col <= endcol)
6275 screen_fill(row, row + 1, col + coloff,
6276 endcol + coloff + 1, ' ', ' ', 0);
6277 }
6278 col = endcol + 1;
6279 off_to = LineOffset[row] + col + coloff;
6280 off_from += col;
6281 endcol = (clear_width > 0 ? clear_width : -clear_width);
6282 }
6283#endif /* FEAT_RIGHTLEFT */
6284
6285 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6286
6287 while (col < endcol)
6288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006290 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 else
6292 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293
6294 redraw_this = redraw_next;
6295 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6296 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6297
6298#ifdef FEAT_GUI
6299 /* If the next character was bold, then redraw the current character to
6300 * remove any pixels that might have spilt over into us. This only
6301 * happens in the GUI.
6302 */
6303 if (redraw_next && gui.in_use)
6304 {
6305 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006306 if (hl > HL_ALL)
6307 hl = syn_attr2attr(hl);
6308 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 redraw_this = TRUE;
6310 }
6311#endif
6312
6313 if (redraw_this)
6314 {
6315 /*
6316 * Special handling when 'xs' termcap flag set (hpterm):
6317 * Attributes for characters are stored at the position where the
6318 * cursor is when writing the highlighting code. The
6319 * start-highlighting code must be written with the cursor on the
6320 * first highlighted character. The stop-highlighting code must
6321 * be written with the cursor just after the last highlighted
6322 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006323 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 * to clear the rest of the line, and force redrawing it
6325 * completely.
6326 */
6327 if ( p_wiv
6328 && !force
6329#ifdef FEAT_GUI
6330 && !gui.in_use
6331#endif
6332 && ScreenAttrs[off_to] != 0
6333 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6334 {
6335 /*
6336 * Need to remove highlighting attributes here.
6337 */
6338 windgoto(row, col + coloff);
6339 out_str(T_CE); /* clear rest of this screen line */
6340 screen_start(); /* don't know where cursor is now */
6341 force = TRUE; /* force redraw of rest of the line */
6342 redraw_next = TRUE; /* or else next char would miss out */
6343
6344 /*
6345 * If the previous character was highlighted, need to stop
6346 * highlighting at this character.
6347 */
6348 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6349 {
6350 screen_attr = ScreenAttrs[off_to - 1];
6351 term_windgoto(row, col + coloff);
6352 screen_stop_highlight();
6353 }
6354 else
6355 screen_attr = 0; /* highlighting has stopped */
6356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 if (enc_dbcs != 0)
6358 {
6359 /* Check if overwriting a double-byte with a single-byte or
6360 * the other way around requires another character to be
6361 * redrawn. For UTF-8 this isn't needed, because comparing
6362 * ScreenLinesUC[] is sufficient. */
6363 if (char_cells == 1
6364 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006365 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 {
6367 /* Writing a single-cell character over a double-cell
6368 * character: need to redraw the next cell. */
6369 ScreenLines[off_to + 1] = 0;
6370 redraw_next = TRUE;
6371 }
6372 else if (char_cells == 2
6373 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006374 && (*mb_off2cells)(off_to, max_off_to) == 1
6375 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 {
6377 /* Writing the second half of a double-cell character over
6378 * a double-cell character: need to redraw the second
6379 * cell. */
6380 ScreenLines[off_to + 2] = 0;
6381 redraw_next = TRUE;
6382 }
6383
6384 if (enc_dbcs == DBCS_JPNU)
6385 ScreenLines2[off_to] = ScreenLines2[off_from];
6386 }
6387 /* When writing a single-width character over a double-width
6388 * character and at the end of the redrawn text, need to clear out
6389 * the right halve of the old character.
6390 * Also required when writing the right halve of a double-width
6391 * char over the left halve of an existing one. */
6392 if (has_mbyte && col + char_cells == endcol
6393 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006394 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006396 && (*mb_off2cells)(off_to, max_off_to) == 1
6397 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399
6400 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 if (enc_utf8)
6402 {
6403 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6404 if (ScreenLinesUC[off_from] != 0)
6405 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006406 int i;
6407
6408 for (i = 0; i < Screen_mco; ++i)
6409 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 }
6411 }
6412 if (char_cells == 2)
6413 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
6415#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006416 /* The bold trick makes a single column of pixels appear in the
6417 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 * character should be redrawn too. This happens for our own GUI
6419 * and for some xterms. */
6420 if (
6421# ifdef FEAT_GUI
6422 gui.in_use
6423# endif
6424# if defined(FEAT_GUI) && defined(UNIX)
6425 ||
6426# endif
6427# ifdef UNIX
6428 term_is_xterm
6429# endif
6430 )
6431 {
6432 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006433 if (hl > HL_ALL)
6434 hl = syn_attr2attr(hl);
6435 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 redraw_next = TRUE;
6437 }
6438#endif
6439 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006440
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006441 /* For simplicity set the attributes of second half of a
6442 * double-wide character equal to the first half. */
6443 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006445
6446 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 screen_char(off_to, row, col + coloff);
6450 }
6451 else if ( p_wiv
6452#ifdef FEAT_GUI
6453 && !gui.in_use
6454#endif
6455 && col + coloff > 0)
6456 {
6457 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6458 {
6459 /*
6460 * Don't output stop-highlight when moving the cursor, it will
6461 * stop the highlighting when it should continue.
6462 */
6463 screen_attr = 0;
6464 }
6465 else if (screen_attr != 0)
6466 screen_stop_highlight();
6467 }
6468
6469 off_to += CHAR_CELLS;
6470 off_from += CHAR_CELLS;
6471 col += CHAR_CELLS;
6472 }
6473
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 if (clear_next)
6475 {
6476 /* Clear the second half of a double-wide character of which the left
6477 * half was overwritten with a single-wide character. */
6478 ScreenLines[off_to] = ' ';
6479 if (enc_utf8)
6480 ScreenLinesUC[off_to] = 0;
6481 screen_char(off_to, row, col + coloff);
6482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483
6484 if (clear_width > 0
6485#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006486 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487#endif
6488 )
6489 {
6490#ifdef FEAT_GUI
6491 int startCol = col;
6492#endif
6493
6494 /* blank out the rest of the line */
6495 while (col < clear_width && ScreenLines[off_to] == ' '
6496 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006497 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 {
6499 ++off_to;
6500 ++col;
6501 }
6502 if (col < clear_width)
6503 {
6504#ifdef FEAT_GUI
6505 /*
6506 * In the GUI, clearing the rest of the line may leave pixels
6507 * behind if the first character cleared was bold. Some bold
6508 * fonts spill over the left. In this case we redraw the previous
6509 * character too. If we didn't skip any blanks above, then we
6510 * only redraw if the character wasn't already redrawn anyway.
6511 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006512 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 {
6514 hl = ScreenAttrs[off_to];
6515 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006516 {
6517 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006518
Bram Moolenaar9c697322006-10-09 20:11:17 +00006519 if (enc_utf8)
6520 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6521 * that its width is 2. */
6522 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6523 else if (enc_dbcs != 0)
6524 {
6525 /* find previous character by counting from first
6526 * column and get its width. */
6527 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006528 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006529
6530 while (off < off_to)
6531 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006532 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006533 off += prev_cells;
6534 }
6535 }
6536
6537 if (enc_dbcs != 0 && prev_cells > 1)
6538 screen_char_2(off_to - prev_cells, row,
6539 col + coloff - prev_cells);
6540 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006541 screen_char(off_to - prev_cells, row,
6542 col + coloff - prev_cells);
6543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 }
6545#endif
6546 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6547 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 off_to += clear_width - col;
6549 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 }
6551 }
6552
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006553 if (clear_width > 0
6554#ifdef FEAT_TEXT_PROP
6555 && !(flags & SLF_POPUP) // no separator for popup window
6556#endif
6557 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006559 // For a window that has a right neighbor, draw the separator char
6560 // right of the window contents.
6561 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 {
6563 int c;
6564
6565 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006566 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006567 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6568 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 || ScreenAttrs[off_to] != hl)
6570 {
6571 ScreenLines[off_to] = c;
6572 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 if (enc_utf8)
6574 {
6575 if (c >= 0x80)
6576 {
6577 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006578 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 }
6580 else
6581 ScreenLinesUC[off_to] = 0;
6582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 screen_char(off_to, row, col + coloff);
6584 }
6585 }
6586 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 LineWraps[row] = FALSE;
6588 }
6589}
6590
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006591#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006593 * Mirror text "str" for right-left displaying.
6594 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006596 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006597rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598{
6599 char_u *p1, *p2;
6600 int t;
6601
6602 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6603 {
6604 t = *p1;
6605 *p1 = *p2;
6606 *p2 = t;
6607 }
6608}
6609#endif
6610
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611/*
6612 * mark all status lines for redraw; used after first :cd
6613 */
6614 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006615status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617 win_T *wp;
6618
Bram Moolenaar29323592016-07-24 22:04:11 +02006619 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 if (wp->w_status_height)
6621 {
6622 wp->w_redr_status = TRUE;
6623 redraw_later(VALID);
6624 }
6625}
6626
6627/*
6628 * mark all status lines of the current buffer for redraw
6629 */
6630 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006631status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632{
6633 win_T *wp;
6634
Bram Moolenaar29323592016-07-24 22:04:11 +02006635 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6637 {
6638 wp->w_redr_status = TRUE;
6639 redraw_later(VALID);
6640 }
6641}
6642
6643/*
6644 * Redraw all status lines that need to be redrawn.
6645 */
6646 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006647redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 win_T *wp;
6650
Bram Moolenaar29323592016-07-24 22:04:11 +02006651 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006653 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006654 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006655 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657
Bram Moolenaar4033c552017-09-16 20:54:51 +02006658#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659/*
6660 * Redraw all status lines at the bottom of frame "frp".
6661 */
6662 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006663win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
6665 if (frp->fr_layout == FR_LEAF)
6666 frp->fr_win->w_redr_status = TRUE;
6667 else if (frp->fr_layout == FR_ROW)
6668 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006669 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 win_redraw_last_status(frp);
6671 }
6672 else /* frp->fr_layout == FR_COL */
6673 {
6674 frp = frp->fr_child;
6675 while (frp->fr_next != NULL)
6676 frp = frp->fr_next;
6677 win_redraw_last_status(frp);
6678 }
6679}
6680#endif
6681
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682/*
6683 * Draw the verticap separator right of window "wp" starting with line "row".
6684 */
6685 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006686draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
6688 int hl;
6689 int c;
6690
6691 if (wp->w_vsep_width)
6692 {
6693 /* draw the vertical separator right of this window */
6694 c = fillchar_vsep(&hl);
6695 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6696 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6697 c, ' ', hl);
6698 }
6699}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
6701#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006702static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
6704/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006705 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 */
6707 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006708status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
6710 int len = 0;
6711
6712#ifdef FEAT_MENU
6713 int emenu = (xp->xp_context == EXPAND_MENUS
6714 || xp->xp_context == EXPAND_MENUNAMES);
6715
6716 /* Check for menu separators - replace with '|'. */
6717 if (emenu && menu_is_separator(s))
6718 return 1;
6719#endif
6720
6721 while (*s != NUL)
6722 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006723 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006724 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006725 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 }
6727
6728 return len;
6729}
6730
6731/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006732 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006733 * These are backslashes used for escaping. Do show backslashes in help tags.
6734 */
6735 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006736skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006737{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006738 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006739#ifdef FEAT_MENU
6740 || ((xp->xp_context == EXPAND_MENUS
6741 || xp->xp_context == EXPAND_MENUNAMES)
6742 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6743#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006744 )
6745 {
6746#ifndef BACKSLASH_IN_FILENAME
6747 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6748 return 2;
6749#endif
6750 return 1;
6751 }
6752 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006753}
6754
6755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 * Show wildchar matches in the status line.
6757 * Show at least the "match" item.
6758 * We start at item 'first_match' in the list and show all matches that fit.
6759 *
6760 * If inversion is possible we use it. Else '=' characters are used.
6761 */
6762 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006763win_redr_status_matches(
6764 expand_T *xp,
6765 int num_matches,
6766 char_u **matches, /* list of matches */
6767 int match,
6768 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769{
6770#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6771 int row;
6772 char_u *buf;
6773 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006774 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 int fillchar;
6776 int attr;
6777 int i;
6778 int highlight = TRUE;
6779 char_u *selstart = NULL;
6780 int selstart_col = 0;
6781 char_u *selend = NULL;
6782 static int first_match = 0;
6783 int add_left = FALSE;
6784 char_u *s;
6785#ifdef FEAT_MENU
6786 int emenu;
6787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789
6790 if (matches == NULL) /* interrupted completion? */
6791 return;
6792
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006793 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006794 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006795 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006796 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (buf == NULL)
6798 return;
6799
6800 if (match == -1) /* don't show match but original text */
6801 {
6802 match = 0;
6803 highlight = FALSE;
6804 }
6805 /* count 1 for the ending ">" */
6806 clen = status_match_len(xp, L_MATCH(match)) + 3;
6807 if (match == 0)
6808 first_match = 0;
6809 else if (match < first_match)
6810 {
6811 /* jumping left, as far as we can go */
6812 first_match = match;
6813 add_left = TRUE;
6814 }
6815 else
6816 {
6817 /* check if match fits on the screen */
6818 for (i = first_match; i < match; ++i)
6819 clen += status_match_len(xp, L_MATCH(i)) + 2;
6820 if (first_match > 0)
6821 clen += 2;
6822 /* jumping right, put match at the left */
6823 if ((long)clen > Columns)
6824 {
6825 first_match = match;
6826 /* if showing the last match, we can add some on the left */
6827 clen = 2;
6828 for (i = match; i < num_matches; ++i)
6829 {
6830 clen += status_match_len(xp, L_MATCH(i)) + 2;
6831 if ((long)clen >= Columns)
6832 break;
6833 }
6834 if (i == num_matches)
6835 add_left = TRUE;
6836 }
6837 }
6838 if (add_left)
6839 while (first_match > 0)
6840 {
6841 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6842 if ((long)clen >= Columns)
6843 break;
6844 --first_match;
6845 }
6846
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006847 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848
6849 if (first_match == 0)
6850 {
6851 *buf = NUL;
6852 len = 0;
6853 }
6854 else
6855 {
6856 STRCPY(buf, "< ");
6857 len = 2;
6858 }
6859 clen = len;
6860
6861 i = first_match;
6862 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6863 {
6864 if (i == match)
6865 {
6866 selstart = buf + len;
6867 selstart_col = clen;
6868 }
6869
6870 s = L_MATCH(i);
6871 /* Check for menu separators - replace with '|' */
6872#ifdef FEAT_MENU
6873 emenu = (xp->xp_context == EXPAND_MENUS
6874 || xp->xp_context == EXPAND_MENUNAMES);
6875 if (emenu && menu_is_separator(s))
6876 {
6877 STRCPY(buf + len, transchar('|'));
6878 l = (int)STRLEN(buf + len);
6879 len += l;
6880 clen += l;
6881 }
6882 else
6883#endif
6884 for ( ; *s != NUL; ++s)
6885 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006886 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006888 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
6890 STRNCPY(buf + len, s, l);
6891 s += l - 1;
6892 len += l;
6893 }
6894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 {
6896 STRCPY(buf + len, transchar_byte(*s));
6897 len += (int)STRLEN(buf + len);
6898 }
6899 }
6900 if (i == match)
6901 selend = buf + len;
6902
6903 *(buf + len++) = ' ';
6904 *(buf + len++) = ' ';
6905 clen += 2;
6906 if (++i == num_matches)
6907 break;
6908 }
6909
6910 if (i != num_matches)
6911 {
6912 *(buf + len++) = '>';
6913 ++clen;
6914 }
6915
6916 buf[len] = NUL;
6917
6918 row = cmdline_row - 1;
6919 if (row >= 0)
6920 {
6921 if (wild_menu_showing == 0)
6922 {
6923 if (msg_scrolled > 0)
6924 {
6925 /* Put the wildmenu just above the command line. If there is
6926 * no room, scroll the screen one line up. */
6927 if (cmdline_row == Rows - 1)
6928 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006929 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 ++msg_scrolled;
6931 }
6932 else
6933 {
6934 ++cmdline_row;
6935 ++row;
6936 }
6937 wild_menu_showing = WM_SCROLLED;
6938 }
6939 else
6940 {
6941 /* Create status line if needed by setting 'laststatus' to 2.
6942 * Set 'winminheight' to zero to avoid that the window is
6943 * resized. */
6944 if (lastwin->w_status_height == 0)
6945 {
6946 save_p_ls = p_ls;
6947 save_p_wmh = p_wmh;
6948 p_ls = 2;
6949 p_wmh = 0;
6950 last_status(FALSE);
6951 }
6952 wild_menu_showing = WM_SHOWN;
6953 }
6954 }
6955
6956 screen_puts(buf, row, 0, attr);
6957 if (selstart != NULL && highlight)
6958 {
6959 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006960 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 }
6962
6963 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6964 }
6965
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 vim_free(buf);
6968}
6969#endif
6970
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971/*
6972 * Redraw the status line of window wp.
6973 *
6974 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006975 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6976 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006978 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006979win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980{
6981 int row;
6982 char_u *p;
6983 int len;
6984 int fillchar;
6985 int attr;
6986 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006987 static int busy = FALSE;
6988
6989 /* It's possible to get here recursively when 'statusline' (indirectly)
6990 * invokes ":redrawstatus". Simply ignore the call then. */
6991 if (busy)
6992 return;
6993 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994
6995 wp->w_redr_status = FALSE;
6996 if (wp->w_status_height == 0)
6997 {
6998 /* no status line, can only be last window */
6999 redraw_cmdline = TRUE;
7000 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007001 else if (!redrawing()
7002#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007003 // don't update status line when popup menu is visible and may be
7004 // drawn over it, unless it will be redrawn later
7005 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007006#endif
7007 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {
7009 /* Don't redraw right now, do it later. */
7010 wp->w_redr_status = TRUE;
7011 }
7012#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007013 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 {
7015 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007016 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 }
7018#endif
7019 else
7020 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007021 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007023 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 p = NameBuff;
7025 len = (int)STRLEN(p);
7026
Bram Moolenaard85f2712017-07-28 21:51:57 +02007027 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028#ifdef FEAT_QUICKFIX
7029 || wp->w_p_pvw
7030#endif
7031 || bufIsChanged(wp->w_buffer)
7032 || wp->w_buffer->b_p_ro)
7033 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007034 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007036 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 len += (int)STRLEN(p + len);
7038 }
7039#ifdef FEAT_QUICKFIX
7040 if (wp->w_p_pvw)
7041 {
7042 STRCPY(p + len, _("[Preview]"));
7043 len += (int)STRLEN(p + len);
7044 }
7045#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007046 if (bufIsChanged(wp->w_buffer)
7047#ifdef FEAT_TERMINAL
7048 && !bt_terminal(wp->w_buffer)
7049#endif
7050 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {
7052 STRCPY(p + len, "[+]");
7053 len += 3;
7054 }
7055 if (wp->w_buffer->b_p_ro)
7056 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007057 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007058 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 }
7060
Bram Moolenaar02631462017-09-22 15:20:32 +02007061 this_ru_col = ru_col - (Columns - wp->w_width);
7062 if (this_ru_col < (wp->w_width + 1) / 2)
7063 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 if (this_ru_col <= 1)
7065 {
7066 p = (char_u *)"<"; /* No room for file name! */
7067 len = 1;
7068 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007069 else if (has_mbyte)
7070 {
7071 int clen = 0, i;
7072
7073 /* Count total number of display cells. */
7074 clen = mb_string2cells(p, -1);
7075
7076 /* Find first character that will fit.
7077 * Going from start to end is much faster for DBCS. */
7078 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7079 i += (*mb_ptr2len)(p + i))
7080 clen -= (*mb_ptr2cells)(p + i);
7081 len = clen;
7082 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007084 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007086 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 }
7088
Bram Moolenaara12a1612019-01-24 16:39:02 +01007089 }
7090 else if (len > this_ru_col - 1)
7091 {
7092 p += len - (this_ru_col - 1);
7093 *p = '<';
7094 len = this_ru_col - 1;
7095 }
7096
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007098 screen_puts(p, row, wp->w_wincol, attr);
7099 screen_fill(row, row + 1, len + wp->w_wincol,
7100 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007102 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7104 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007105 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
7107#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007108 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109#endif
7110 }
7111
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 /*
7113 * May need to draw the character below the vertical separator.
7114 */
7115 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7116 {
7117 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007118 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 else
7120 fillchar = fillchar_vsep(&attr);
7121 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7122 attr);
7123 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007124 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125}
7126
Bram Moolenaar238a5642006-02-21 22:12:05 +00007127#ifdef FEAT_STL_OPT
7128/*
7129 * Redraw the status line according to 'statusline' and take care of any
7130 * errors encountered.
7131 */
7132 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007133redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007134{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007135 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007136 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007137
7138 /* When called recursively return. This can happen when the statusline
7139 * contains an expression that triggers a redraw. */
7140 if (entered)
7141 return;
7142 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007143
Bram Moolenaara742e082016-04-05 21:10:38 +02007144 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007145 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007146 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007147 {
7148 /* When there is an error disable the statusline, otherwise the
7149 * display is messed up with errors and a redraw triggers the problem
7150 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007151 set_string_option_direct((char_u *)"statusline", -1,
7152 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007153 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007154 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007155 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007156 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007157}
7158#endif
7159
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160/*
7161 * Return TRUE if the status line of window "wp" is connected to the status
7162 * line of the window right of it. If not, then it's a vertical separator.
7163 * Only call if (wp->w_vsep_width != 0).
7164 */
7165 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007166stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167{
7168 frame_T *fr;
7169
7170 fr = wp->w_frame;
7171 while (fr->fr_parent != NULL)
7172 {
7173 if (fr->fr_parent->fr_layout == FR_COL)
7174 {
7175 if (fr->fr_next != NULL)
7176 break;
7177 }
7178 else
7179 {
7180 if (fr->fr_next != NULL)
7181 return TRUE;
7182 }
7183 fr = fr->fr_parent;
7184 }
7185 return FALSE;
7186}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189/*
7190 * Get the value to show for the language mappings, active 'keymap'.
7191 */
7192 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007193get_keymap_str(
7194 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007195 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007196 char_u *buf, /* buffer for the result */
7197 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
7199 char_u *p;
7200
7201 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7202 return FALSE;
7203
7204 {
7205#ifdef FEAT_EVAL
7206 buf_T *old_curbuf = curbuf;
7207 win_T *old_curwin = curwin;
7208 char_u *s;
7209
7210 curbuf = wp->w_buffer;
7211 curwin = wp;
7212 STRCPY(buf, "b:keymap_name"); /* must be writable */
7213 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007214 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 --emsg_skip;
7216 curbuf = old_curbuf;
7217 curwin = old_curwin;
7218 if (p == NULL || *p == NUL)
7219#endif
7220 {
7221#ifdef FEAT_KEYMAP
7222 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7223 p = wp->w_buffer->b_p_keymap;
7224 else
7225#endif
7226 p = (char_u *)"lang";
7227 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007228 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 buf[0] = NUL;
7230#ifdef FEAT_EVAL
7231 vim_free(s);
7232#endif
7233 }
7234 return buf[0] != NUL;
7235}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
7237#if defined(FEAT_STL_OPT) || defined(PROTO)
7238/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007239 * Redraw the status line or ruler of window "wp".
7240 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 */
7242 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007243win_redr_custom(
7244 win_T *wp,
7245 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007247 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 int attr;
7249 int curattr;
7250 int row;
7251 int col = 0;
7252 int maxwidth;
7253 int width;
7254 int n;
7255 int len;
7256 int fillchar;
7257 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007258 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007260 struct stl_hlrec hltab[STL_MAX_ITEM];
7261 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007262 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007263 win_T *ewp;
7264 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265
Bram Moolenaar1d633412013-12-11 15:52:01 +01007266 /* There is a tiny chance that this gets called recursively: When
7267 * redrawing a status line triggers redrawing the ruler or tabline.
7268 * Avoid trouble by not allowing recursion. */
7269 if (entered)
7270 return;
7271 entered = TRUE;
7272
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007274 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007276 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007277 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007278 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007279 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007280 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007281 maxwidth = Columns;
7282# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007283 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007286 else
7287 {
7288 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007289 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007290 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007291
7292 if (draw_ruler)
7293 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007294 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007295 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007296 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007297 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007298 if (*++stl == '-')
7299 stl++;
7300 if (atoi((char *)stl))
7301 while (VIM_ISDIGIT(*stl))
7302 stl++;
7303 if (*stl++ != '(')
7304 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007305 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007306 col = ru_col - (Columns - wp->w_width);
7307 if (col < (wp->w_width + 1) / 2)
7308 col = (wp->w_width + 1) / 2;
7309 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007310 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007311 {
7312 row = Rows - 1;
7313 --maxwidth; /* writing in last column may cause scrolling */
7314 fillchar = ' ';
7315 attr = 0;
7316 }
7317
7318# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007319 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007320# endif
7321 }
7322 else
7323 {
7324 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007325 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007326 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007327 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007328# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007329 use_sandbox = was_set_insecurely((char_u *)"statusline",
7330 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007331# endif
7332 }
7333
Bram Moolenaar53f81742017-09-22 14:35:51 +02007334 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007335 }
7336
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007338 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339
Bram Moolenaar61452852011-02-01 18:01:11 +01007340 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7341 * the cursor away and back. */
7342 ewp = wp == NULL ? curwin : wp;
7343 p_crb_save = ewp->w_p_crb;
7344 ewp->w_p_crb = FALSE;
7345
Bram Moolenaar362f3562009-11-03 16:20:34 +00007346 /* Make a copy, because the statusline may include a function call that
7347 * might change the option value and free the memory. */
7348 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007349 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007350 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007351 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007352 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007353 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007355 /* Make all characters printable. */
7356 p = transstr(buf);
7357 if (p != NULL)
7358 {
7359 vim_strncpy(buf, p, sizeof(buf) - 1);
7360 vim_free(p);
7361 }
7362
7363 /* fill up with "fillchar" */
7364 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007365 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 ++width;
7369 }
7370 buf[len] = NUL;
7371
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007372 /*
7373 * Draw each snippet with the specified highlighting.
7374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 curattr = attr;
7376 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007377 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007379 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 screen_puts_len(p, len, row, col, curattr);
7381 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007382 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007384 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007386 else if (hltab[n].userhl < 0)
7387 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007388#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007389 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7390 && wp->w_status_height != 0)
7391 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007392 else if (wp != NULL && bt_terminal(wp->w_buffer)
7393 && wp->w_status_height != 0)
7394 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007395#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007396 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007397 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007399 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 }
7401 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007402
7403 if (wp == NULL)
7404 {
7405 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7406 col = 0;
7407 len = 0;
7408 p = buf;
7409 fillchar = 0;
7410 for (n = 0; tabtab[n].start != NULL; n++)
7411 {
7412 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7413 while (col < len)
7414 TabPageIdxs[col++] = fillchar;
7415 p = tabtab[n].start;
7416 fillchar = tabtab[n].userhl;
7417 }
7418 while (col < Columns)
7419 TabPageIdxs[col++] = fillchar;
7420 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007421
7422theend:
7423 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424}
7425
7426#endif /* FEAT_STL_OPT */
7427
7428/*
7429 * Output a single character directly to the screen and update ScreenLines.
7430 */
7431 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007432screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 char_u buf[MB_MAXBYTES + 1];
7435
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007436 if (has_mbyte)
7437 buf[(*mb_char2bytes)(c, buf)] = NUL;
7438 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007439 {
7440 buf[0] = c;
7441 buf[1] = NUL;
7442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 screen_puts(buf, row, col, attr);
7444}
7445
7446/*
7447 * Get a single character directly from ScreenLines into "bytes[]".
7448 * Also return its attribute in *attrp;
7449 */
7450 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007451screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
7453 unsigned off;
7454
7455 /* safety check */
7456 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7457 {
7458 off = LineOffset[row] + col;
7459 *attrp = ScreenAttrs[off];
7460 bytes[0] = ScreenLines[off];
7461 bytes[1] = NUL;
7462
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 if (enc_utf8 && ScreenLinesUC[off] != 0)
7464 bytes[utfc_char2bytes(off, bytes)] = NUL;
7465 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7466 {
7467 bytes[0] = ScreenLines[off];
7468 bytes[1] = ScreenLines2[off];
7469 bytes[2] = NUL;
7470 }
7471 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7472 {
7473 bytes[1] = ScreenLines[off + 1];
7474 bytes[2] = NUL;
7475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 }
7477}
7478
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007479/*
7480 * Return TRUE if composing characters for screen posn "off" differs from
7481 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007482 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007483 */
7484 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007485screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007486{
7487 int i;
7488
7489 for (i = 0; i < Screen_mco; ++i)
7490 {
7491 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7492 return TRUE;
7493 if (u8cc[i] == 0)
7494 break;
7495 }
7496 return FALSE;
7497}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007498
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499/*
7500 * Put string '*text' on the screen at position 'row' and 'col', with
7501 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7502 * Note: only outputs within one row, message is truncated at screen boundary!
7503 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7504 */
7505 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007506screen_puts(
7507 char_u *text,
7508 int row,
7509 int col,
7510 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511{
7512 screen_puts_len(text, -1, row, col, attr);
7513}
7514
7515/*
7516 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7517 * a NUL.
7518 */
7519 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007520screen_puts_len(
7521 char_u *text,
7522 int textlen,
7523 int row,
7524 int col,
7525 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526{
7527 unsigned off;
7528 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007529 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007531 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 int mbyte_blen = 1;
7533 int mbyte_cells = 1;
7534 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007535 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007537#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007539 int pc, nc, nc1;
7540 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007542 int force_redraw_this;
7543 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007544 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545
7546 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7547 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007548 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549
Bram Moolenaarc236c162008-07-13 17:41:49 +00007550 /* When drawing over the right halve of a double-wide char clear out the
7551 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007552 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007553#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007554 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007555#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007556 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007557 {
7558 ScreenLines[off - 1] = ' ';
7559 ScreenAttrs[off - 1] = 0;
7560 if (enc_utf8)
7561 {
7562 ScreenLinesUC[off - 1] = 0;
7563 ScreenLinesC[0][off - 1] = 0;
7564 }
7565 /* redraw the previous cell, make it empty */
7566 screen_char(off - 1, row, col - 1);
7567 /* force the cell at "col" to be redrawn */
7568 force_redraw_next = TRUE;
7569 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007570
Bram Moolenaar367329b2007-08-30 11:53:22 +00007571 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007572 while (col < screen_Columns
7573 && (len < 0 || (int)(ptr - text) < len)
7574 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 {
7576 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 /* check if this is the first byte of a multibyte */
7578 if (has_mbyte)
7579 {
7580 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007581 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007583 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7585 mbyte_cells = 1;
7586 else if (enc_dbcs != 0)
7587 mbyte_cells = mbyte_blen;
7588 else /* enc_utf8 */
7589 {
7590 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007591 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 (int)((text + len) - ptr));
7593 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007594 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007596#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7598 {
7599 /* Do Arabic shaping. */
7600 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7601 {
7602 /* Past end of string to be displayed. */
7603 nc = NUL;
7604 nc1 = NUL;
7605 }
7606 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007607 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007608 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7609 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007610 nc1 = pcc[0];
7611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 pc = prev_c;
7613 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007614 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 }
7616 else
7617 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007618#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007619 if (col + mbyte_cells > screen_Columns)
7620 {
7621 /* Only 1 cell left, but character requires 2 cells:
7622 * display a '>' in the last column to avoid wrapping. */
7623 c = '>';
7624 mbyte_cells = 1;
7625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 }
7627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007629 force_redraw_this = force_redraw_next;
7630 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007631
7632 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 || (mbyte_cells == 2
7634 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7635 || (enc_dbcs == DBCS_JPNU
7636 && c == 0x8e
7637 && ScreenLines2[off] != ptr[1])
7638 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007639 && (ScreenLinesUC[off] !=
7640 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7641 || (ScreenLinesUC[off] != 0
7642 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007644 || exmode_active;
7645
Bram Moolenaara12a1612019-01-24 16:39:02 +01007646 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 {
7648#if defined(FEAT_GUI) || defined(UNIX)
7649 /* The bold trick makes a single row of pixels appear in the next
7650 * character. When a bold character is removed, the next
7651 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007652 * and for some xterms. */
7653 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654# ifdef FEAT_GUI
7655 gui.in_use
7656# endif
7657# if defined(FEAT_GUI) && defined(UNIX)
7658 ||
7659# endif
7660# ifdef UNIX
7661 term_is_xterm
7662# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007663 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007665 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007667 if (n > HL_ALL)
7668 n = syn_attr2attr(n);
7669 if (n & HL_BOLD)
7670 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 }
7672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 /* When at the end of the text and overwriting a two-cell
7674 * character with a one-cell character, need to clear the next
7675 * cell. Also when overwriting the left halve of a two-cell char
7676 * with the right halve of a two-cell char. Do this only once
7677 * (mb_off2cells() may return 2 on the right halve). */
7678 if (clear_next_cell)
7679 clear_next_cell = FALSE;
7680 else if (has_mbyte
7681 && (len < 0 ? ptr[mbyte_blen] == NUL
7682 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007683 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007685 && (*mb_off2cells)(off, max_off) == 1
7686 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 clear_next_cell = TRUE;
7688
7689 /* Make sure we never leave a second byte of a double-byte behind,
7690 * it confuses mb_off2cells(). */
7691 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007692 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007694 && (*mb_off2cells)(off, max_off) == 1
7695 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 ScreenLines[off] = c;
7698 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 if (enc_utf8)
7700 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007701 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 ScreenLinesUC[off] = 0;
7703 else
7704 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007705 int i;
7706
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007708 for (i = 0; i < Screen_mco; ++i)
7709 {
7710 ScreenLinesC[i][off] = u8cc[i];
7711 if (u8cc[i] == 0)
7712 break;
7713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
7715 if (mbyte_cells == 2)
7716 {
7717 ScreenLines[off + 1] = 0;
7718 ScreenAttrs[off + 1] = attr;
7719 }
7720 screen_char(off, row, col);
7721 }
7722 else if (mbyte_cells == 2)
7723 {
7724 ScreenLines[off + 1] = ptr[1];
7725 ScreenAttrs[off + 1] = attr;
7726 screen_char_2(off, row, col);
7727 }
7728 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7729 {
7730 ScreenLines2[off] = ptr[1];
7731 screen_char(off, row, col);
7732 }
7733 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 screen_char(off, row, col);
7735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 if (has_mbyte)
7737 {
7738 off += mbyte_cells;
7739 col += mbyte_cells;
7740 ptr += mbyte_blen;
7741 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007742 {
7743 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007745 len = -1;
7746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 }
7748 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {
7750 ++off;
7751 ++col;
7752 ++ptr;
7753 }
7754 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007755
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007756 /* If we detected the next character needs to be redrawn, but the text
7757 * doesn't extend up to there, update the character here. */
7758 if (force_redraw_next && col < screen_Columns)
7759 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007760 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7761 screen_char_2(off, row, col);
7762 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007763 screen_char(off, row, col);
7764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765}
7766
7767#ifdef FEAT_SEARCH_EXTRA
7768/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007769 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 */
7771 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007772start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
7774 if (p_hls && !no_hlsearch)
7775 {
7776 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007777 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007778# ifdef FEAT_RELTIME
7779 /* Set the time limit to 'redrawtime'. */
7780 profile_setlimit(p_rdt, &search_hl.tm);
7781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
7783}
7784
7785/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007786 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 */
7788 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007789end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790{
7791 if (search_hl.rm.regprog != NULL)
7792 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007793 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 search_hl.rm.regprog = NULL;
7795 }
7796}
7797
7798/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007799 * Init for calling prepare_search_hl().
7800 */
7801 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007802init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007803{
7804 matchitem_T *cur;
7805
7806 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7807 * match */
7808 cur = wp->w_match_head;
7809 while (cur != NULL)
7810 {
7811 cur->hl.rm = cur->match;
7812 if (cur->hlg_id == 0)
7813 cur->hl.attr = 0;
7814 else
7815 cur->hl.attr = syn_id2attr(cur->hlg_id);
7816 cur->hl.buf = wp->w_buffer;
7817 cur->hl.lnum = 0;
7818 cur->hl.first_lnum = 0;
7819# ifdef FEAT_RELTIME
7820 /* Set the time limit to 'redrawtime'. */
7821 profile_setlimit(p_rdt, &(cur->hl.tm));
7822# endif
7823 cur = cur->next;
7824 }
7825 search_hl.buf = wp->w_buffer;
7826 search_hl.lnum = 0;
7827 search_hl.first_lnum = 0;
7828 /* time limit is set at the toplevel, for all windows */
7829}
7830
7831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 * Advance to the match in window "wp" line "lnum" or past it.
7833 */
7834 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007835prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007837 matchitem_T *cur; /* points to the match list */
7838 match_T *shl; /* points to search_hl or a match */
7839 int shl_flag; /* flag to indicate whether search_hl
7840 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007841 int pos_inprogress; /* marks that position match search is
7842 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 int n;
7844
7845 /*
7846 * When using a multi-line pattern, start searching at the top
7847 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007848 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007850 cur = wp->w_match_head;
7851 shl_flag = FALSE;
7852 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007854 if (shl_flag == FALSE)
7855 {
7856 shl = &search_hl;
7857 shl_flag = TRUE;
7858 }
7859 else
7860 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 if (shl->rm.regprog != NULL
7862 && shl->lnum == 0
7863 && re_multiline(shl->rm.regprog))
7864 {
7865 if (shl->first_lnum == 0)
7866 {
7867# ifdef FEAT_FOLDING
7868 for (shl->first_lnum = lnum;
7869 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7870 if (hasFoldingWin(wp, shl->first_lnum - 1,
7871 NULL, NULL, TRUE, NULL))
7872 break;
7873# else
7874 shl->first_lnum = wp->w_topline;
7875# endif
7876 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007877 if (cur != NULL)
7878 cur->pos.cur = 0;
7879 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007881 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7882 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007884 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7885 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007886 pos_inprogress = cur == NULL || cur->pos.cur == 0
7887 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 if (shl->lnum != 0)
7889 {
7890 shl->first_lnum = shl->lnum
7891 + shl->rm.endpos[0].lnum
7892 - shl->rm.startpos[0].lnum;
7893 n = shl->rm.endpos[0].col;
7894 }
7895 else
7896 {
7897 ++shl->first_lnum;
7898 n = 0;
7899 }
7900 }
7901 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007902 if (shl != &search_hl && cur != NULL)
7903 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 }
7905}
7906
7907/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007908 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 * Uses shl->buf.
7910 * Sets shl->lnum and shl->rm contents.
7911 * Note: Assumes a previous match is always before "lnum", unless
7912 * shl->lnum is zero.
7913 * Careful: Any pointers for buffer lines will become invalid.
7914 */
7915 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007916next_search_hl(
7917 win_T *win,
7918 match_T *shl, /* points to search_hl or a match */
7919 linenr_T lnum,
7920 colnr_T mincol, /* minimal column for a match */
7921 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922{
7923 linenr_T l;
7924 colnr_T matchcol;
7925 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007926 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007928 // for :{range}s/pat only highlight inside the range
7929 if (lnum < search_first_line || lnum > search_last_line)
7930 {
7931 shl->lnum = 0;
7932 return;
7933 }
7934
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 if (shl->lnum != 0)
7936 {
7937 /* Check for three situations:
7938 * 1. If the "lnum" is below a previous match, start a new search.
7939 * 2. If the previous match includes "mincol", use it.
7940 * 3. Continue after the previous match.
7941 */
7942 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7943 if (lnum > l)
7944 shl->lnum = 0;
7945 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7946 return;
7947 }
7948
7949 /*
7950 * Repeat searching for a match until one is found that includes "mincol"
7951 * or none is found in this line.
7952 */
7953 called_emsg = FALSE;
7954 for (;;)
7955 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007956#ifdef FEAT_RELTIME
7957 /* Stop searching after passing the time limit. */
7958 if (profile_passed_limit(&(shl->tm)))
7959 {
7960 shl->lnum = 0; /* no match found in time */
7961 break;
7962 }
7963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 /* Three situations:
7965 * 1. No useful previous match: search from start of line.
7966 * 2. Not Vi compatible or empty match: continue at next character.
7967 * Break the loop if this is beyond the end of the line.
7968 * 3. Vi compatible searching: continue at end of previous match.
7969 */
7970 if (shl->lnum == 0)
7971 matchcol = 0;
7972 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7973 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007974 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007976 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007977
7978 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007979 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007980 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007982 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 shl->lnum = 0;
7984 break;
7985 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007986 if (has_mbyte)
7987 matchcol += mb_ptr2len(ml);
7988 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007989 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 }
7991 else
7992 matchcol = shl->rm.endpos[0].col;
7993
7994 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007995 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007997 /* Remember whether shl->rm is using a copy of the regprog in
7998 * cur->match. */
7999 int regprog_is_copy = (shl != &search_hl && cur != NULL
8000 && shl == &cur->hl
8001 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008002 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008003
Bram Moolenaarb3414592014-06-17 17:48:32 +02008004 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8005 matchcol,
8006#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008007 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008008#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008009 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008010#endif
8011 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008012 /* Copy the regprog, in case it got freed and recompiled. */
8013 if (regprog_is_copy)
8014 cur->match.regprog = cur->hl.rm.regprog;
8015
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008016 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008017 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008018 /* Error while handling regexp: stop using this regexp. */
8019 if (shl == &search_hl)
8020 {
8021 /* don't free regprog in the match list, it's a copy */
8022 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008023 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008024 }
8025 shl->rm.regprog = NULL;
8026 shl->lnum = 0;
8027 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8028 message */
8029 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008030 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008031 }
8032 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008033 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008034 else
8035 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 if (nmatched == 0)
8037 {
8038 shl->lnum = 0; /* no match found */
8039 break;
8040 }
8041 if (shl->rm.startpos[0].lnum > 0
8042 || shl->rm.startpos[0].col >= mincol
8043 || nmatched > 1
8044 || shl->rm.endpos[0].col > mincol)
8045 {
8046 shl->lnum += shl->rm.startpos[0].lnum;
8047 break; /* useful match found */
8048 }
8049 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008050
8051 // Restore called_emsg for assert_fails().
8052 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054
Bram Moolenaar85077472016-10-16 14:35:48 +02008055/*
8056 * If there is a match fill "shl" and return one.
8057 * Return zero otherwise.
8058 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008059 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008060next_search_hl_pos(
8061 match_T *shl, /* points to a match */
8062 linenr_T lnum,
8063 posmatch_T *posmatch, /* match positions */
8064 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008065{
8066 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008067 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008068
Bram Moolenaarb3414592014-06-17 17:48:32 +02008069 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8070 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008071 llpos_T *pos = &posmatch->pos[i];
8072
8073 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008074 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008075 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008076 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008077 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008078 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008079 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008080 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008081 /* if this match comes before the one at "found" then swap
8082 * them */
8083 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008084 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008085 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008086
Bram Moolenaar85077472016-10-16 14:35:48 +02008087 *pos = posmatch->pos[found];
8088 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008089 }
8090 }
8091 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008092 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008093 }
8094 }
8095 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008096 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008097 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008098 colnr_T start = posmatch->pos[found].col == 0
8099 ? 0 : posmatch->pos[found].col - 1;
8100 colnr_T end = posmatch->pos[found].col == 0
8101 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008102
Bram Moolenaar85077472016-10-16 14:35:48 +02008103 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008104 shl->rm.startpos[0].lnum = 0;
8105 shl->rm.startpos[0].col = start;
8106 shl->rm.endpos[0].lnum = 0;
8107 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008108 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008109 posmatch->cur = found + 1;
8110 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008111 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008112 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008113}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008114#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008115
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008117screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118{
8119 attrentry_T *aep = NULL;
8120
8121 screen_attr = attr;
8122 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008123#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 && termcap_active
8125#endif
8126 )
8127 {
8128#ifdef FEAT_GUI
8129 if (gui.in_use)
8130 {
8131 char buf[20];
8132
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008133 /* The GUI handles this internally. */
8134 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 OUT_STR(buf);
8136 }
8137 else
8138#endif
8139 {
8140 if (attr > HL_ALL) /* special HL attr. */
8141 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008142 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 aep = syn_cterm_attr2entry(attr);
8144 else
8145 aep = syn_term_attr2entry(attr);
8146 if (aep == NULL) /* did ":syntax clear" */
8147 attr = 0;
8148 else
8149 attr = aep->ae_attr;
8150 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008151 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008153 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008154#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008155 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8156 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8157 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008158#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008159 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008160 /* If the Normal FG color has BOLD attribute and the new HL
8161 * has a FG color defined, clear BOLD. */
8162 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008163 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008165 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008166 out_str(T_UCS);
8167 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008168 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8169 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008171 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008173 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008175 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008176 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177
8178 /*
8179 * Output the color or start string after bold etc., in case the
8180 * bold etc. override the color setting.
8181 */
8182 if (aep != NULL)
8183 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008184#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008185 /* When 'termguicolors' is set but fg or bg is unset,
8186 * fall back to the cterm colors. This helps for SpellBad,
8187 * where the GUI uses a red undercurl. */
8188 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008190 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008191 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008192 }
8193 else
8194#endif
8195 if (t_colors > 1)
8196 {
8197 if (aep->ae_u.cterm.fg_color)
8198 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8199 }
8200#ifdef FEAT_TERMGUICOLORS
8201 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8202 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008203 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008204 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 }
8206 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008207#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008208 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008210 if (aep->ae_u.cterm.bg_color)
8211 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8212 }
8213
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008214 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008215 {
8216 if (aep->ae_u.term.start != NULL)
8217 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 }
8219 }
8220 }
8221 }
8222}
8223
8224 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008225screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226{
8227 int do_ME = FALSE; /* output T_ME code */
8228
8229 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008230#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 && termcap_active
8232#endif
8233 )
8234 {
8235#ifdef FEAT_GUI
8236 if (gui.in_use)
8237 {
8238 char buf[20];
8239
8240 /* use internal GUI code */
8241 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8242 OUT_STR(buf);
8243 }
8244 else
8245#endif
8246 {
8247 if (screen_attr > HL_ALL) /* special HL attr. */
8248 {
8249 attrentry_T *aep;
8250
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008251 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {
8253 /*
8254 * Assume that t_me restores the original colors!
8255 */
8256 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008257 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008258#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008259 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8260 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8261 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008262#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008263 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008264#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008265 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8266 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8267 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008268#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008269 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 do_ME = TRUE;
8271 }
8272 else
8273 {
8274 aep = syn_term_attr2entry(screen_attr);
8275 if (aep != NULL && aep->ae_u.term.stop != NULL)
8276 {
8277 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8278 do_ME = TRUE;
8279 else
8280 out_str(aep->ae_u.term.stop);
8281 }
8282 }
8283 if (aep == NULL) /* did ":syntax clear" */
8284 screen_attr = 0;
8285 else
8286 screen_attr = aep->ae_attr;
8287 }
8288
8289 /*
8290 * Often all ending-codes are equal to T_ME. Avoid outputting the
8291 * same sequence several times.
8292 */
8293 if (screen_attr & HL_STANDOUT)
8294 {
8295 if (STRCMP(T_SE, T_ME) == 0)
8296 do_ME = TRUE;
8297 else
8298 out_str(T_SE);
8299 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008300 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008301 {
8302 if (STRCMP(T_UCE, T_ME) == 0)
8303 do_ME = TRUE;
8304 else
8305 out_str(T_UCE);
8306 }
8307 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008308 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {
8310 if (STRCMP(T_UE, T_ME) == 0)
8311 do_ME = TRUE;
8312 else
8313 out_str(T_UE);
8314 }
8315 if (screen_attr & HL_ITALIC)
8316 {
8317 if (STRCMP(T_CZR, T_ME) == 0)
8318 do_ME = TRUE;
8319 else
8320 out_str(T_CZR);
8321 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008322 if (screen_attr & HL_STRIKETHROUGH)
8323 {
8324 if (STRCMP(T_STE, T_ME) == 0)
8325 do_ME = TRUE;
8326 else
8327 out_str(T_STE);
8328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8330 out_str(T_ME);
8331
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008332#ifdef FEAT_TERMGUICOLORS
8333 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008335 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008336 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008337 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008338 term_bg_rgb_color(cterm_normal_bg_gui_color);
8339 }
8340 else
8341#endif
8342 {
8343 if (t_colors > 1)
8344 {
8345 /* set Normal cterm colors */
8346 if (cterm_normal_fg_color != 0)
8347 term_fg_color(cterm_normal_fg_color - 1);
8348 if (cterm_normal_bg_color != 0)
8349 term_bg_color(cterm_normal_bg_color - 1);
8350 if (cterm_normal_fg_bold)
8351 out_str(T_MD);
8352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 }
8354 }
8355 }
8356 screen_attr = 0;
8357}
8358
8359/*
8360 * Reset the colors for a cterm. Used when leaving Vim.
8361 * The machine specific code may override this again.
8362 */
8363 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008364reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008366 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 {
8368 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008369#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008370 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8371 || cterm_normal_bg_gui_color != INVALCOLOR)
8372 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008373#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {
8377 out_str(T_OP);
8378 screen_attr = -1;
8379 }
8380 if (cterm_normal_fg_bold)
8381 {
8382 out_str(T_ME);
8383 screen_attr = -1;
8384 }
8385 }
8386}
8387
8388/*
8389 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8390 * using the attributes from ScreenAttrs["off"].
8391 */
8392 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008393screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394{
8395 int attr;
8396
8397 /* Check for illegal values, just in case (could happen just after
8398 * resizing). */
8399 if (row >= screen_Rows || col >= screen_Columns)
8400 return;
8401
Bram Moolenaarae654382019-01-17 21:09:05 +01008402#ifdef FEAT_INS_EXPAND
8403 if (pum_under_menu(row, col))
8404 return;
8405#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008406 /* Outputting a character in the last cell on the screen may scroll the
8407 * screen up. Only do it when the "xn" termcap property is set, otherwise
8408 * mark the character invalid (update it when scrolled up). */
8409 if (*T_XN == NUL
8410 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411#ifdef FEAT_RIGHTLEFT
8412 /* account for first command-line character in rightleft mode */
8413 && !cmdmsg_rl
8414#endif
8415 )
8416 {
8417 ScreenAttrs[off] = (sattr_T)-1;
8418 return;
8419 }
8420
8421 /*
8422 * Stop highlighting first, so it's easier to move the cursor.
8423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 if (screen_char_attr != 0)
8425 attr = screen_char_attr;
8426 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 attr = ScreenAttrs[off];
8428 if (screen_attr != attr)
8429 screen_stop_highlight();
8430
8431 windgoto(row, col);
8432
8433 if (screen_attr != attr)
8434 screen_start_highlight(attr);
8435
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 if (enc_utf8 && ScreenLinesUC[off] != 0)
8437 {
8438 char_u buf[MB_MAXBYTES + 1];
8439
Bram Moolenaarcb070082016-04-02 22:14:51 +02008440 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008441 {
8442 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008443#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008444 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008445#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008446 )
8447 {
8448 /* Clear the two screen cells. If the character is actually
8449 * single width it won't change the second cell. */
8450 out_str((char_u *)" ");
8451 term_windgoto(row, col);
8452 }
8453 /* not sure where the cursor is after drawing the ambiguous width
8454 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008455 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008456 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008457 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008459
8460 /* Convert the UTF-8 character to bytes and write it. */
8461 buf[utfc_char2bytes(off, buf)] = NUL;
8462 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 }
8464 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 /* double-byte character in single-width cell */
8469 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8470 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 }
8472
8473 screen_cur_col++;
8474}
8475
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476/*
8477 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8478 * on the screen at position 'row' and 'col'.
8479 * The attributes of the first byte is used for all. This is required to
8480 * output the two bytes of a double-byte character with nothing in between.
8481 */
8482 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008483screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 /* Check for illegal values (could be wrong when screen was resized). */
8486 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8487 return;
8488
8489 /* Outputting the last character on the screen may scrollup the screen.
8490 * Don't to it! Mark the character invalid (update it when scrolled up) */
8491 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8492 {
8493 ScreenAttrs[off] = (sattr_T)-1;
8494 return;
8495 }
8496
8497 /* Output the first byte normally (positions the cursor), then write the
8498 * second byte directly. */
8499 screen_char(off, row, col);
8500 out_char(ScreenLines[off + 1]);
8501 ++screen_cur_col;
8502}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504/*
8505 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8506 * This uses the contents of ScreenLines[] and doesn't change it.
8507 */
8508 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008509screen_draw_rectangle(
8510 int row,
8511 int col,
8512 int height,
8513 int width,
8514 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515{
8516 int r, c;
8517 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008518 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008520 /* Can't use ScreenLines unless initialized */
8521 if (ScreenLines == NULL)
8522 return;
8523
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 if (invert)
8525 screen_char_attr = HL_INVERSE;
8526 for (r = row; r < row + height; ++r)
8527 {
8528 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008529 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 for (c = col; c < col + width; ++c)
8531 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008532 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 {
8534 screen_char_2(off + c, r, c);
8535 ++c;
8536 }
8537 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 {
8539 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008540 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 }
8543 }
8544 }
8545 screen_char_attr = 0;
8546}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548/*
8549 * Redraw the characters for a vertically split window.
8550 */
8551 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008552redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553{
8554 int col;
8555 int width;
8556
8557# ifdef FEAT_CLIPBOARD
8558 clip_may_clear_selection(row, end - 1);
8559# endif
8560
8561 if (wp == NULL)
8562 {
8563 col = 0;
8564 width = Columns;
8565 }
8566 else
8567 {
8568 col = wp->w_wincol;
8569 width = wp->w_width;
8570 }
8571 screen_draw_rectangle(row, col, end - row, width, FALSE);
8572}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008574 static void
8575space_to_screenline(int off, int attr)
8576{
8577 ScreenLines[off] = ' ';
8578 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008579 if (enc_utf8)
8580 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008581}
8582
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583/*
8584 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8585 * with character 'c1' in first column followed by 'c2' in the other columns.
8586 * Use attributes 'attr'.
8587 */
8588 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008589screen_fill(
8590 int start_row,
8591 int end_row,
8592 int start_col,
8593 int end_col,
8594 int c1,
8595 int c2,
8596 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
8598 int row;
8599 int col;
8600 int off;
8601 int end_off;
8602 int did_delete;
8603 int c;
8604 int norm_term;
8605#if defined(FEAT_GUI) || defined(UNIX)
8606 int force_next = FALSE;
8607#endif
8608
8609 if (end_row > screen_Rows) /* safety check */
8610 end_row = screen_Rows;
8611 if (end_col > screen_Columns) /* safety check */
8612 end_col = screen_Columns;
8613 if (ScreenLines == NULL
8614 || start_row >= end_row
8615 || start_col >= end_col) /* nothing to do */
8616 return;
8617
8618 /* it's a "normal" terminal when not in a GUI or cterm */
8619 norm_term = (
8620#ifdef FEAT_GUI
8621 !gui.in_use &&
8622#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008623 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 for (row = start_row; row < end_row; ++row)
8625 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008626 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008627#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008628 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008629#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008630 )
8631 {
8632 /* When drawing over the right halve of a double-wide char clear
8633 * out the left halve. When drawing over the left halve of a
8634 * double wide-char clear out the right halve. Only needed in a
8635 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008636 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008637 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008638 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008639 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 /*
8642 * Try to use delete-line termcap code, when no attributes or in a
8643 * "normal" terminal, where a bold/italic space is just a
8644 * space.
8645 */
8646 did_delete = FALSE;
8647 if (c2 == ' '
8648 && end_col == Columns
8649 && can_clear(T_CE)
8650 && (attr == 0
8651 || (norm_term
8652 && attr <= HL_ALL
8653 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8654 {
8655 /*
8656 * check if we really need to clear something
8657 */
8658 col = start_col;
8659 if (c1 != ' ') /* don't clear first char */
8660 ++col;
8661
8662 off = LineOffset[row] + col;
8663 end_off = LineOffset[row] + end_col;
8664
8665 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 if (enc_utf8)
8667 while (off < end_off && ScreenLines[off] == ' '
8668 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8669 ++off;
8670 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 while (off < end_off && ScreenLines[off] == ' '
8672 && ScreenAttrs[off] == 0)
8673 ++off;
8674 if (off < end_off) /* something to be cleared */
8675 {
8676 col = off - LineOffset[row];
8677 screen_stop_highlight();
8678 term_windgoto(row, col);/* clear rest of this screen line */
8679 out_str(T_CE);
8680 screen_start(); /* don't know where cursor is now */
8681 col = end_col - col;
8682 while (col--) /* clear chars in ScreenLines */
8683 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008684 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 ++off;
8686 }
8687 }
8688 did_delete = TRUE; /* the chars are cleared now */
8689 }
8690
8691 off = LineOffset[row] + start_col;
8692 c = c1;
8693 for (col = start_col; col < end_col; ++col)
8694 {
8695 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008696 || (enc_utf8 && (int)ScreenLinesUC[off]
8697 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 || ScreenAttrs[off] != attr
8699#if defined(FEAT_GUI) || defined(UNIX)
8700 || force_next
8701#endif
8702 )
8703 {
8704#if defined(FEAT_GUI) || defined(UNIX)
8705 /* The bold trick may make a single row of pixels appear in
8706 * the next character. When a bold character is removed, the
8707 * next character should be redrawn too. This happens for our
8708 * own GUI and for some xterms. */
8709 if (
8710# ifdef FEAT_GUI
8711 gui.in_use
8712# endif
8713# if defined(FEAT_GUI) && defined(UNIX)
8714 ||
8715# endif
8716# ifdef UNIX
8717 term_is_xterm
8718# endif
8719 )
8720 {
8721 if (ScreenLines[off] != ' '
8722 && (ScreenAttrs[off] > HL_ALL
8723 || ScreenAttrs[off] & HL_BOLD))
8724 force_next = TRUE;
8725 else
8726 force_next = FALSE;
8727 }
8728#endif
8729 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 if (enc_utf8)
8731 {
8732 if (c >= 0x80)
8733 {
8734 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008735 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 }
8737 else
8738 ScreenLinesUC[off] = 0;
8739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 ScreenAttrs[off] = attr;
8741 if (!did_delete || c != ' ')
8742 screen_char(off, row, col);
8743 }
8744 ++off;
8745 if (col == start_col)
8746 {
8747 if (did_delete)
8748 break;
8749 c = c2;
8750 }
8751 }
8752 if (end_col == Columns)
8753 LineWraps[row] = FALSE;
8754 if (row == Rows - 1) /* overwritten the command line */
8755 {
8756 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008757 if (start_col == 0 && end_col == Columns
8758 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008760 if (start_col == 0)
8761 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 }
8763 }
8764}
8765
8766/*
8767 * Check if there should be a delay. Used before clearing or redrawing the
8768 * screen or the command line.
8769 */
8770 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008771check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772{
8773 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8774 && !did_wait_return
8775 && emsg_silent == 0)
8776 {
8777 out_flush();
8778 ui_delay(1000L, TRUE);
8779 emsg_on_display = FALSE;
8780 if (check_msg_scroll)
8781 msg_scroll = FALSE;
8782 }
8783}
8784
8785/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008786 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8787 */
8788 static void
8789clear_TabPageIdxs(void)
8790{
8791 int scol;
8792
8793 for (scol = 0; scol < Columns; ++scol)
8794 TabPageIdxs[scol] = 0;
8795}
8796
8797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008799 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 * Returns TRUE if there is a valid screen to write to.
8801 * Returns FALSE when starting up and screen not initialized yet.
8802 */
8803 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008804screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008806 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 return (ScreenLines != NULL);
8808}
8809
8810/*
8811 * Resize the shell to Rows and Columns.
8812 * Allocate ScreenLines[] and associated items.
8813 *
8814 * There may be some time between setting Rows and Columns and (re)allocating
8815 * ScreenLines[]. This happens when starting up and when (manually) changing
8816 * the shell size. Always use screen_Rows and screen_Columns to access items
8817 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8818 * final size of the shell is needed.
8819 */
8820 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008821screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822{
8823 int new_row, old_row;
8824#ifdef FEAT_GUI
8825 int old_Rows;
8826#endif
8827 win_T *wp;
8828 int outofmem = FALSE;
8829 int len;
8830 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008832 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008834 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 sattr_T *new_ScreenAttrs;
8836 unsigned *new_LineOffset;
8837 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008838 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008839 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008841 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008842 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008844retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 /*
8846 * Allocation of the screen buffers is done only when the size changes and
8847 * when Rows and Columns have been set and we have started doing full
8848 * screen stuff.
8849 */
8850 if ((ScreenLines != NULL
8851 && Rows == screen_Rows
8852 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 && enc_utf8 == (ScreenLinesUC != NULL)
8854 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008855 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 || Rows == 0
8857 || Columns == 0
8858 || (!full_screen && ScreenLines == NULL))
8859 return;
8860
8861 /*
8862 * It's possible that we produce an out-of-memory message below, which
8863 * will cause this function to be called again. To break the loop, just
8864 * return here.
8865 */
8866 if (entered)
8867 return;
8868 entered = TRUE;
8869
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008870 /*
8871 * Note that the window sizes are updated before reallocating the arrays,
8872 * thus we must not redraw here!
8873 */
8874 ++RedrawingDisabled;
8875
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 win_new_shellsize(); /* fit the windows in the new sized shell */
8877
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 comp_col(); /* recompute columns for shown command and ruler */
8879
8880 /*
8881 * We're changing the size of the screen.
8882 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8883 * - Move lines from the old arrays into the new arrays, clear extra
8884 * lines (unless the screen is going to be cleared).
8885 * - Free the old arrays.
8886 *
8887 * If anything fails, make ScreenLines NULL, so we don't do anything!
8888 * Continuing with the old ScreenLines may result in a crash, because the
8889 * size is wrong.
8890 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008891 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008893 if (aucmd_win != NULL)
8894 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008895#ifdef FEAT_TEXT_PROP
8896 // global popup windows
8897 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8898 win_free_lsize(wp);
8899 // tab-local popup windows
8900 FOR_ALL_TABPAGES(tp)
8901 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8902 win_free_lsize(wp);
8903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008905 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
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 Moolenaarc799fe22019-05-28 23:08:19 +02008909 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008910 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008911 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8912 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 }
8914 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008915 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8916 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8917 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8918 new_LineWraps = LALLOC_MULT(char_u, Rows);
8919 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008921 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 {
8923 if (win_alloc_lines(wp) == FAIL)
8924 {
8925 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008926 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 }
8928 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008929 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8930 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008931 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008932#ifdef FEAT_TEXT_PROP
8933 // global popup windows
8934 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8935 if (win_alloc_lines(wp) == FAIL)
8936 {
8937 outofmem = TRUE;
8938 goto give_up;
8939 }
8940 // tab-local popup windows
8941 FOR_ALL_TABPAGES(tp)
8942 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8943 if (win_alloc_lines(wp) == FAIL)
8944 {
8945 outofmem = TRUE;
8946 goto give_up;
8947 }
8948#endif
8949
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008950give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008952 for (i = 0; i < p_mco; ++i)
8953 if (new_ScreenLinesC[i] == NULL)
8954 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008956 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 || new_ScreenAttrs == NULL
8959 || new_LineOffset == NULL
8960 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008961 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 || outofmem)
8963 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008964 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008965 {
8966 /* guess the size */
8967 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8968
8969 /* Remember we did this to avoid getting outofmem messages over
8970 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008971 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008972 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008973 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008974 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008975 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008976 VIM_CLEAR(new_ScreenLinesC[i]);
8977 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008978 VIM_CLEAR(new_ScreenAttrs);
8979 VIM_CLEAR(new_LineOffset);
8980 VIM_CLEAR(new_LineWraps);
8981 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 }
8983 else
8984 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008985 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008986
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 for (new_row = 0; new_row < Rows; ++new_row)
8988 {
8989 new_LineOffset[new_row] = new_row * Columns;
8990 new_LineWraps[new_row] = FALSE;
8991
8992 /*
8993 * If the screen is not going to be cleared, copy as much as
8994 * possible from the old screen to the new one and clear the rest
8995 * (used when resizing the window at the "--more--" prompt or when
8996 * executing an external command, for the GUI).
8997 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008998 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 {
9000 (void)vim_memset(new_ScreenLines + new_row * Columns,
9001 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 if (enc_utf8)
9003 {
9004 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9005 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009006 for (i = 0; i < p_mco; ++i)
9007 (void)vim_memset(new_ScreenLinesC[i]
9008 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 0, (size_t)Columns * sizeof(u8char_T));
9010 }
9011 if (enc_dbcs == DBCS_JPNU)
9012 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9013 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9015 0, (size_t)Columns * sizeof(sattr_T));
9016 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009017 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 {
9019 if (screen_Columns < Columns)
9020 len = screen_Columns;
9021 else
9022 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009023 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009024 * may be invalid now. Also when p_mco changes. */
9025 if (!(enc_utf8 && ScreenLinesUC == NULL)
9026 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009027 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9028 ScreenLines + LineOffset[old_row],
9029 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009030 if (enc_utf8 && ScreenLinesUC != NULL
9031 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 {
9033 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9034 ScreenLinesUC + LineOffset[old_row],
9035 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009036 for (i = 0; i < p_mco; ++i)
9037 mch_memmove(new_ScreenLinesC[i]
9038 + new_LineOffset[new_row],
9039 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 (size_t)len * sizeof(u8char_T));
9041 }
9042 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9043 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9044 ScreenLines2 + LineOffset[old_row],
9045 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9047 ScreenAttrs + LineOffset[old_row],
9048 (size_t)len * sizeof(sattr_T));
9049 }
9050 }
9051 }
9052 /* Use the last line of the screen for the current line. */
9053 current_ScreenLine = new_ScreenLines + Rows * Columns;
9054 }
9055
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009056 free_screenlines();
9057
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009060 for (i = 0; i < p_mco; ++i)
9061 ScreenLinesC[i] = new_ScreenLinesC[i];
9062 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 ScreenAttrs = new_ScreenAttrs;
9065 LineOffset = new_LineOffset;
9066 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009067 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068
9069 /* It's important that screen_Rows and screen_Columns reflect the actual
9070 * size of ScreenLines[]. Set them before calling anything. */
9071#ifdef FEAT_GUI
9072 old_Rows = screen_Rows;
9073#endif
9074 screen_Rows = Rows;
9075 screen_Columns = Columns;
9076
9077 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009078 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080#ifdef FEAT_GUI
9081 else if (gui.in_use
9082 && !gui.starting
9083 && ScreenLines != NULL
9084 && old_Rows != Rows)
9085 {
9086 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9087 /*
9088 * Adjust the position of the cursor, for when executing an external
9089 * command.
9090 */
9091 if (msg_row >= Rows) /* Rows got smaller */
9092 msg_row = Rows - 1; /* put cursor at last row */
9093 else if (Rows > old_Rows) /* Rows got bigger */
9094 msg_row += Rows - old_Rows; /* put cursor in same place */
9095 if (msg_col >= Columns) /* Columns got smaller */
9096 msg_col = Columns - 1; /* put cursor at last column */
9097 }
9098#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009099 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009102 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009103
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009104 /*
9105 * Do not apply autocommands more than 3 times to avoid an endless loop
9106 * in case applying autocommands always changes Rows or Columns.
9107 */
9108 if (starting == 0 && ++retry_count <= 3)
9109 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009110 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009111 /* In rare cases, autocommands may have altered Rows or Columns,
9112 * jump back to check if we need to allocate the screen again. */
9113 goto retry;
9114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115}
9116
9117 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009118free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009119{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009120 int i;
9121
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009122 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009123 for (i = 0; i < Screen_mco; ++i)
9124 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009125 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009126 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009127 vim_free(ScreenAttrs);
9128 vim_free(LineOffset);
9129 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009130 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009131}
9132
9133 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009134screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135{
9136 check_for_delay(FALSE);
9137 screenalloc(FALSE); /* allocate screen buffers if size changed */
9138 screenclear2(); /* clear the screen */
9139}
9140
9141 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009142screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143{
9144 int i;
9145
9146 if (starting == NO_SCREEN || ScreenLines == NULL
9147#ifdef FEAT_GUI
9148 || (gui.in_use && gui.starting)
9149#endif
9150 )
9151 return;
9152
9153#ifdef FEAT_GUI
9154 if (!gui.in_use)
9155#endif
9156 screen_attr = -1; /* force setting the Normal colors */
9157 screen_stop_highlight(); /* don't want highlighting here */
9158
9159#ifdef FEAT_CLIPBOARD
9160 /* disable selection without redrawing it */
9161 clip_scroll_selection(9999);
9162#endif
9163
9164 /* blank out ScreenLines */
9165 for (i = 0; i < Rows; ++i)
9166 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009167 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 LineWraps[i] = FALSE;
9169 }
9170
9171 if (can_clear(T_CL))
9172 {
9173 out_str(T_CL); /* clear the display */
9174 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009175 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 }
9177 else
9178 {
9179 /* can't clear the screen, mark all chars with invalid attributes */
9180 for (i = 0; i < Rows; ++i)
9181 lineinvalid(LineOffset[i], (int)Columns);
9182 clear_cmdline = TRUE;
9183 }
9184
9185 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9186
9187 win_rest_invalid(firstwin);
9188 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009189 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 if (must_redraw == CLEAR) /* no need to clear again */
9191 must_redraw = NOT_VALID;
9192 compute_cmdrow();
9193 msg_row = cmdline_row; /* put cursor on last line for messages */
9194 msg_col = 0;
9195 screen_start(); /* don't know where cursor is now */
9196 msg_scrolled = 0; /* can't scroll back */
9197 msg_didany = FALSE;
9198 msg_didout = FALSE;
9199}
9200
9201/*
9202 * Clear one line in ScreenLines.
9203 */
9204 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009205lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206{
9207 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 if (enc_utf8)
9209 (void)vim_memset(ScreenLinesUC + off, 0,
9210 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009211 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212}
9213
9214/*
9215 * Mark one line in ScreenLines invalid by setting the attributes to an
9216 * invalid value.
9217 */
9218 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009219lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220{
9221 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9222}
9223
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224/*
9225 * Copy part of a Screenline for vertically split window "wp".
9226 */
9227 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009228linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229{
9230 unsigned off_to = LineOffset[to] + wp->w_wincol;
9231 unsigned off_from = LineOffset[from] + wp->w_wincol;
9232
9233 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9234 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 if (enc_utf8)
9236 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009237 int i;
9238
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9240 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009241 for (i = 0; i < p_mco; ++i)
9242 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9243 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 }
9245 if (enc_dbcs == DBCS_JPNU)
9246 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9247 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9249 wp->w_width * sizeof(sattr_T));
9250}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251
9252/*
9253 * Return TRUE if clearing with term string "p" would work.
9254 * It can't work when the string is empty or it won't set the right background.
9255 */
9256 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009257can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
9259 return (*p != NUL && (t_colors <= 1
9260#ifdef FEAT_GUI
9261 || gui.in_use
9262#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009263#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009264 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009265 || (!p_tgc && cterm_normal_bg_color == 0)
9266#else
9267 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009268#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009269 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270}
9271
9272/*
9273 * Reset cursor position. Use whenever cursor was moved because of outputting
9274 * something directly to the screen (shell commands) or a terminal control
9275 * code.
9276 */
9277 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009278screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279{
9280 screen_cur_row = screen_cur_col = 9999;
9281}
9282
9283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 * Move the cursor to position "row","col" in the screen.
9285 * This tries to find the most efficient way to move, minimizing the number of
9286 * characters sent to the terminal.
9287 */
9288 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009289windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009291 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 int i;
9293 int plan;
9294 int cost;
9295 int wouldbe_col;
9296 int noinvcurs;
9297 char_u *bs;
9298 int goto_cost;
9299 int attr;
9300
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009301#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9303
9304#define PLAN_LE 1
9305#define PLAN_CR 2
9306#define PLAN_NL 3
9307#define PLAN_WRITE 4
9308 /* Can't use ScreenLines unless initialized */
9309 if (ScreenLines == NULL)
9310 return;
9311
9312 if (col != screen_cur_col || row != screen_cur_row)
9313 {
9314 /* Check for valid position. */
9315 if (row < 0) /* window without text lines? */
9316 row = 0;
9317 if (row >= screen_Rows)
9318 row = screen_Rows - 1;
9319 if (col >= screen_Columns)
9320 col = screen_Columns - 1;
9321
9322 /* check if no cursor movement is allowed in highlight mode */
9323 if (screen_attr && *T_MS == NUL)
9324 noinvcurs = HIGHL_COST;
9325 else
9326 noinvcurs = 0;
9327 goto_cost = GOTO_COST + noinvcurs;
9328
9329 /*
9330 * Plan how to do the positioning:
9331 * 1. Use CR to move it to column 0, same row.
9332 * 2. Use T_LE to move it a few columns to the left.
9333 * 3. Use NL to move a few lines down, column 0.
9334 * 4. Move a few columns to the right with T_ND or by writing chars.
9335 *
9336 * Don't do this if the cursor went beyond the last column, the cursor
9337 * position is unknown then (some terminals wrap, some don't )
9338 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009339 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 * characters to move the cursor to the right.
9341 */
9342 if (row >= screen_cur_row && screen_cur_col < Columns)
9343 {
9344 /*
9345 * If the cursor is in the same row, bigger col, we can use CR
9346 * or T_LE.
9347 */
9348 bs = NULL; /* init for GCC */
9349 attr = screen_attr;
9350 if (row == screen_cur_row && col < screen_cur_col)
9351 {
9352 /* "le" is preferred over "bc", because "bc" is obsolete */
9353 if (*T_LE)
9354 bs = T_LE; /* "cursor left" */
9355 else
9356 bs = T_BC; /* "backspace character (old) */
9357 if (*bs)
9358 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9359 else
9360 cost = 999;
9361 if (col + 1 < cost) /* using CR is less characters */
9362 {
9363 plan = PLAN_CR;
9364 wouldbe_col = 0;
9365 cost = 1; /* CR is just one character */
9366 }
9367 else
9368 {
9369 plan = PLAN_LE;
9370 wouldbe_col = col;
9371 }
9372 if (noinvcurs) /* will stop highlighting */
9373 {
9374 cost += noinvcurs;
9375 attr = 0;
9376 }
9377 }
9378
9379 /*
9380 * If the cursor is above where we want to be, we can use CR LF.
9381 */
9382 else if (row > screen_cur_row)
9383 {
9384 plan = PLAN_NL;
9385 wouldbe_col = 0;
9386 cost = (row - screen_cur_row) * 2; /* CR LF */
9387 if (noinvcurs) /* will stop highlighting */
9388 {
9389 cost += noinvcurs;
9390 attr = 0;
9391 }
9392 }
9393
9394 /*
9395 * If the cursor is in the same row, smaller col, just use write.
9396 */
9397 else
9398 {
9399 plan = PLAN_WRITE;
9400 wouldbe_col = screen_cur_col;
9401 cost = 0;
9402 }
9403
9404 /*
9405 * Check if any characters that need to be written have the
9406 * correct attributes. Also avoid UTF-8 characters.
9407 */
9408 i = col - wouldbe_col;
9409 if (i > 0)
9410 cost += i;
9411 if (cost < goto_cost && i > 0)
9412 {
9413 /*
9414 * Check if the attributes are correct without additionally
9415 * stopping highlighting.
9416 */
9417 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9418 while (i && *p++ == attr)
9419 --i;
9420 if (i != 0)
9421 {
9422 /*
9423 * Try if it works when highlighting is stopped here.
9424 */
9425 if (*--p == 0)
9426 {
9427 cost += noinvcurs;
9428 while (i && *p++ == 0)
9429 --i;
9430 }
9431 if (i != 0)
9432 cost = 999; /* different attributes, don't do it */
9433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 if (enc_utf8)
9435 {
9436 /* Don't use an UTF-8 char for positioning, it's slow. */
9437 for (i = wouldbe_col; i < col; ++i)
9438 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9439 {
9440 cost = 999;
9441 break;
9442 }
9443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 }
9445
9446 /*
9447 * We can do it without term_windgoto()!
9448 */
9449 if (cost < goto_cost)
9450 {
9451 if (plan == PLAN_LE)
9452 {
9453 if (noinvcurs)
9454 screen_stop_highlight();
9455 while (screen_cur_col > col)
9456 {
9457 out_str(bs);
9458 --screen_cur_col;
9459 }
9460 }
9461 else if (plan == PLAN_CR)
9462 {
9463 if (noinvcurs)
9464 screen_stop_highlight();
9465 out_char('\r');
9466 screen_cur_col = 0;
9467 }
9468 else if (plan == PLAN_NL)
9469 {
9470 if (noinvcurs)
9471 screen_stop_highlight();
9472 while (screen_cur_row < row)
9473 {
9474 out_char('\n');
9475 ++screen_cur_row;
9476 }
9477 screen_cur_col = 0;
9478 }
9479
9480 i = col - screen_cur_col;
9481 if (i > 0)
9482 {
9483 /*
9484 * Use cursor-right if it's one character only. Avoids
9485 * removing a line of pixels from the last bold char, when
9486 * using the bold trick in the GUI.
9487 */
9488 if (T_ND[0] != NUL && T_ND[1] == NUL)
9489 {
9490 while (i-- > 0)
9491 out_char(*T_ND);
9492 }
9493 else
9494 {
9495 int off;
9496
9497 off = LineOffset[row] + screen_cur_col;
9498 while (i-- > 0)
9499 {
9500 if (ScreenAttrs[off] != screen_attr)
9501 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 if (enc_dbcs == DBCS_JPNU
9505 && ScreenLines[off] == 0x8e)
9506 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 ++off;
9508 }
9509 }
9510 }
9511 }
9512 }
9513 else
9514 cost = 999;
9515
9516 if (cost >= goto_cost)
9517 {
9518 if (noinvcurs)
9519 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009520 if (row == screen_cur_row && (col > screen_cur_col)
9521 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 term_cursor_right(col - screen_cur_col);
9523 else
9524 term_windgoto(row, col);
9525 }
9526 screen_cur_row = row;
9527 screen_cur_col = col;
9528 }
9529}
9530
9531/*
9532 * Set cursor to its position in the current window.
9533 */
9534 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009535setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009537 setcursor_mayforce(FALSE);
9538}
9539
9540/*
9541 * Set cursor to its position in the current window.
9542 * When "force" is TRUE also when not redrawing.
9543 */
9544 void
9545setcursor_mayforce(int force)
9546{
9547 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 {
9549 validate_cursor();
9550 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009551 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009553 /* With 'rightleft' set and the cursor on a double-wide
9554 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009555 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9556 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009557 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009558 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559#endif
9560 curwin->w_wcol));
9561 }
9562}
9563
9564
9565/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009566 * Insert 'line_count' lines at 'row' in window 'wp'.
9567 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9568 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 * scrolling.
9570 * Returns FAIL if the lines are not inserted, OK for success.
9571 */
9572 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009573win_ins_lines(
9574 win_T *wp,
9575 int row,
9576 int line_count,
9577 int invalid,
9578 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579{
9580 int did_delete;
9581 int nextrow;
9582 int lastrow;
9583 int retval;
9584
9585 if (invalid)
9586 wp->w_lines_valid = 0;
9587
9588 if (wp->w_height < 5)
9589 return FAIL;
9590
9591 if (line_count > wp->w_height - row)
9592 line_count = wp->w_height - row;
9593
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009594 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 if (retval != MAYBE)
9596 return retval;
9597
9598 /*
9599 * If there is a next window or a status line, we first try to delete the
9600 * lines at the bottom to avoid messing what is after the window.
9601 * If this fails and there are following windows, don't do anything to avoid
9602 * messing up those windows, better just redraw.
9603 */
9604 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 if (wp->w_next != NULL || wp->w_status_height)
9606 {
9607 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009608 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 did_delete = TRUE;
9610 else if (wp->w_next)
9611 return FAIL;
9612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 /*
9614 * if no lines deleted, blank the lines that will end up below the window
9615 */
9616 if (!did_delete)
9617 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009620 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 lastrow = nextrow + line_count;
9622 if (lastrow > Rows)
9623 lastrow = Rows;
9624 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009625 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 ' ', ' ', 0);
9627 }
9628
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009629 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 == FAIL)
9631 {
9632 /* deletion will have messed up other windows */
9633 if (did_delete)
9634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 win_rest_invalid(W_NEXT(wp));
9637 }
9638 return FAIL;
9639 }
9640
9641 return OK;
9642}
9643
9644/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009645 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9647 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9648 * scrolling
9649 * Return OK for success, FAIL if the lines are not deleted.
9650 */
9651 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009652win_del_lines(
9653 win_T *wp,
9654 int row,
9655 int line_count,
9656 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009657 int mayclear,
9658 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659{
9660 int retval;
9661
9662 if (invalid)
9663 wp->w_lines_valid = 0;
9664
9665 if (line_count > wp->w_height - row)
9666 line_count = wp->w_height - row;
9667
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009668 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 if (retval != MAYBE)
9670 return retval;
9671
9672 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009673 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 return FAIL;
9675
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 /*
9677 * If there are windows or status lines below, try to put them at the
9678 * correct place. If we can't do that, they have to be redrawn.
9679 */
9680 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9681 {
9682 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009683 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 {
9685 wp->w_redr_status = TRUE;
9686 win_rest_invalid(wp->w_next);
9687 }
9688 }
9689 /*
9690 * If this is the last window and there is no status line, redraw the
9691 * command line later.
9692 */
9693 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 redraw_cmdline = TRUE;
9695 return OK;
9696}
9697
9698/*
9699 * Common code for win_ins_lines() and win_del_lines().
9700 * Returns OK or FAIL when the work has been done.
9701 * Returns MAYBE when not finished yet.
9702 */
9703 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009704win_do_lines(
9705 win_T *wp,
9706 int row,
9707 int line_count,
9708 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009709 int del,
9710 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711{
9712 int retval;
9713
9714 if (!redrawing() || line_count <= 0)
9715 return FAIL;
9716
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009717 /* When inserting lines would result in loss of command output, just redraw
9718 * the lines. */
9719 if (no_win_do_lines_ins && !del)
9720 return FAIL;
9721
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009723 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009725 if (!no_win_do_lines_ins)
9726 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 return FAIL;
9728 }
9729
9730 /*
9731 * Delete all remaining lines
9732 */
9733 if (row + line_count >= wp->w_height)
9734 {
9735 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009736 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 ' ', ' ', 0);
9738 return OK;
9739 }
9740
9741 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009742 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009744 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009746 if (!no_win_do_lines_ins)
9747 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748
9749 /*
9750 * If the terminal can set a scroll region, use that.
9751 * Always do this in a vertically split window. This will redraw from
9752 * ScreenLines[] when t_CV isn't defined. That's faster than using
9753 * win_line().
9754 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009755 * a character in the lower right corner of the scroll region may cause a
9756 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009758 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 scroll_region_set(wp, row);
9762 if (del)
9763 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009764 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 else
9766 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009767 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 scroll_region_reset();
9770 return retval;
9771 }
9772
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9774 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775
9776 return MAYBE;
9777}
9778
9779/*
9780 * window 'wp' and everything after it is messed up, mark it for redraw
9781 */
9782 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009783win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 {
9787 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 wp->w_redr_status = TRUE;
9789 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 }
9791 redraw_cmdline = TRUE;
9792}
9793
9794/*
9795 * The rest of the routines in this file perform screen manipulations. The
9796 * given operation is performed physically on the screen. The corresponding
9797 * change is also made to the internal screen image. In this way, the editor
9798 * anticipates the effect of editing changes on the appearance of the screen.
9799 * That way, when we call screenupdate a complete redraw isn't usually
9800 * necessary. Another advantage is that we can keep adding code to anticipate
9801 * screen changes, and in the meantime, everything still works.
9802 */
9803
9804/*
9805 * types for inserting or deleting lines
9806 */
9807#define USE_T_CAL 1
9808#define USE_T_CDL 2
9809#define USE_T_AL 3
9810#define USE_T_CE 4
9811#define USE_T_DL 5
9812#define USE_T_SR 6
9813#define USE_NL 7
9814#define USE_T_CD 8
9815#define USE_REDRAW 9
9816
9817/*
9818 * insert lines on the screen and update ScreenLines[]
9819 * 'end' is the line after the scrolled part. Normally it is Rows.
9820 * When scrolling region used 'off' is the offset from the top for the region.
9821 * 'row' and 'end' are relative to the start of the region.
9822 *
9823 * return FAIL for failure, OK for success.
9824 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009825 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009826screen_ins_lines(
9827 int off,
9828 int row,
9829 int line_count,
9830 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009831 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009832 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833{
9834 int i;
9835 int j;
9836 unsigned temp;
9837 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009838 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 int type;
9840 int result_empty;
9841 int can_ce = can_clear(T_CE);
9842
9843 /*
9844 * FAIL if
9845 * - there is no valid screen
9846 * - the screen has to be redrawn completely
9847 * - the line count is less than one
9848 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009849 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009851 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9852#ifdef FEAT_CLIPBOARD
9853 || (clip_star.state != SELECT_CLEARED
9854 && redrawing_for_callback > 0)
9855#endif
9856 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 return FAIL;
9858
9859 /*
9860 * There are seven ways to insert lines:
9861 * 0. When in a vertically split window and t_CV isn't set, redraw the
9862 * characters from ScreenLines[].
9863 * 1. Use T_CD (clear to end of display) if it exists and the result of
9864 * the insert is just empty lines
9865 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9866 * present or line_count > 1. It looks better if we do all the inserts
9867 * at once.
9868 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9869 * insert is just empty lines and T_CE is not present or line_count >
9870 * 1.
9871 * 4. Use T_AL (insert line) if it exists.
9872 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9873 * just empty lines.
9874 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9875 * just empty lines.
9876 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9877 * the 'da' flag is not set or we have clear line capability.
9878 * 8. redraw the characters from ScreenLines[].
9879 *
9880 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9881 * the scrollbar for the window. It does have insert line, use that if it
9882 * exists.
9883 */
9884 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9886 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009887 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 type = USE_T_CD;
9889 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9890 type = USE_T_CAL;
9891 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9892 type = USE_T_CDL;
9893 else if (*T_AL != NUL)
9894 type = USE_T_AL;
9895 else if (can_ce && result_empty)
9896 type = USE_T_CE;
9897 else if (*T_DL != NUL && result_empty)
9898 type = USE_T_DL;
9899 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9900 type = USE_T_SR;
9901 else
9902 return FAIL;
9903
9904 /*
9905 * For clearing the lines screen_del_lines() is used. This will also take
9906 * care of t_db if necessary.
9907 */
9908 if (type == USE_T_CD || type == USE_T_CDL ||
9909 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009910 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911
9912 /*
9913 * If text is retained below the screen, first clear or delete as many
9914 * lines at the bottom of the window as are about to be inserted so that
9915 * the deleted lines won't later surface during a screen_del_lines.
9916 */
9917 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009918 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919
9920#ifdef FEAT_CLIPBOARD
9921 /* Remove a modeless selection when inserting lines halfway the screen
9922 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009923 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009924 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 else
9926 clip_scroll_selection(-line_count);
9927#endif
9928
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929#ifdef FEAT_GUI
9930 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9931 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009932 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933#endif
9934
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009935 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9936 cursor_col = wp->w_wincol;
9937
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 if (*T_CCS != NUL) /* cursor relative to region */
9939 cursor_row = row;
9940 else
9941 cursor_row = row + off;
9942
9943 /*
9944 * Shift LineOffset[] line_count down to reflect the inserted lines.
9945 * Clear the inserted lines in ScreenLines[].
9946 */
9947 row += off;
9948 end += off;
9949 for (i = 0; i < line_count; ++i)
9950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 if (wp != NULL && wp->w_width != Columns)
9952 {
9953 /* need to copy part of a line */
9954 j = end - 1 - i;
9955 while ((j -= line_count) >= row)
9956 linecopy(j + line_count, j, wp);
9957 j += line_count;
9958 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009959 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9960 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 else
9962 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9963 LineWraps[j] = FALSE;
9964 }
9965 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 {
9967 j = end - 1 - i;
9968 temp = LineOffset[j];
9969 while ((j -= line_count) >= row)
9970 {
9971 LineOffset[j + line_count] = LineOffset[j];
9972 LineWraps[j + line_count] = LineWraps[j];
9973 }
9974 LineOffset[j + line_count] = temp;
9975 LineWraps[j + line_count] = FALSE;
9976 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009977 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 else
9979 lineinvalid(temp, (int)Columns);
9980 }
9981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982
9983 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009984 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009985 if (clear_attr != 0)
9986 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 /* redraw the characters */
9989 if (type == USE_REDRAW)
9990 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009991 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 {
9993 term_append_lines(line_count);
9994 screen_start(); /* don't know where cursor is now */
9995 }
9996 else
9997 {
9998 for (i = 0; i < line_count; i++)
9999 {
10000 if (type == USE_T_AL)
10001 {
10002 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010003 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 out_str(T_AL);
10005 }
10006 else /* type == USE_T_SR */
10007 out_str(T_SR);
10008 screen_start(); /* don't know where cursor is now */
10009 }
10010 }
10011
10012 /*
10013 * With scroll-reverse and 'da' flag set we need to clear the lines that
10014 * have been scrolled down into the region.
10015 */
10016 if (type == USE_T_SR && *T_DA)
10017 {
10018 for (i = 0; i < line_count; ++i)
10019 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010020 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 out_str(T_CE);
10022 screen_start(); /* don't know where cursor is now */
10023 }
10024 }
10025
10026#ifdef FEAT_GUI
10027 gui_can_update_cursor();
10028 if (gui.in_use)
10029 out_flush(); /* always flush after a scroll */
10030#endif
10031 return OK;
10032}
10033
10034/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010035 * Delete lines on the screen and update ScreenLines[].
10036 * "end" is the line after the scrolled part. Normally it is Rows.
10037 * When scrolling region used "off" is the offset from the top for the region.
10038 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 *
10040 * Return OK for success, FAIL if the lines are not deleted.
10041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010043screen_del_lines(
10044 int off,
10045 int row,
10046 int line_count,
10047 int end,
10048 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010049 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010050 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051{
10052 int j;
10053 int i;
10054 unsigned temp;
10055 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010056 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 int cursor_end;
10058 int result_empty; /* result is empty until end of region */
10059 int can_delete; /* deleting line codes can be used */
10060 int type;
10061
10062 /*
10063 * FAIL if
10064 * - there is no valid screen
10065 * - the screen has to be redrawn completely
10066 * - the line count is less than one
10067 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010068 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010070 if (!screen_valid(TRUE) || line_count <= 0
10071 || (!force && line_count > p_ttyscroll)
10072#ifdef FEAT_CLIPBOARD
10073 || (clip_star.state != SELECT_CLEARED
10074 && redrawing_for_callback > 0)
10075#endif
10076 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 return FAIL;
10078
10079 /*
10080 * Check if the rest of the current region will become empty.
10081 */
10082 result_empty = row + line_count >= end;
10083
10084 /*
10085 * We can delete lines only when 'db' flag not set or when 'ce' option
10086 * available.
10087 */
10088 can_delete = (*T_DB == NUL || can_clear(T_CE));
10089
10090 /*
10091 * There are six ways to delete lines:
10092 * 0. When in a vertically split window and t_CV isn't set, redraw the
10093 * characters from ScreenLines[].
10094 * 1. Use T_CD if it exists and the result is empty.
10095 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10096 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10097 * none of the other ways work.
10098 * 4. Use T_CE (erase line) if the result is empty.
10099 * 5. Use T_DL (delete line) if it exists.
10100 * 6. redraw the characters from ScreenLines[].
10101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10103 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010104 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 type = USE_T_CD;
10106#if defined(__BEOS__) && defined(BEOS_DR8)
10107 /*
10108 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10109 * its internal termcap... this works okay for tests which test *T_DB !=
10110 * NUL. It has the disadvantage that the user cannot use any :set t_*
10111 * command to get T_DB (back) to empty_option, only :set term=... will do
10112 * the trick...
10113 * Anyway, this hack will hopefully go away with the next OS release.
10114 * (Olaf Seibert)
10115 */
10116 else if (row == 0 && T_DB == empty_option
10117 && (line_count == 1 || *T_CDL == NUL))
10118#else
10119 else if (row == 0 && (
10120#ifndef AMIGA
10121 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10122 * up, so use delete-line command */
10123 line_count == 1 ||
10124#endif
10125 *T_CDL == NUL))
10126#endif
10127 type = USE_NL;
10128 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10129 type = USE_T_CDL;
10130 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010131 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 type = USE_T_CE;
10133 else if (*T_DL != NUL && can_delete)
10134 type = USE_T_DL;
10135 else if (*T_CDL != NUL && can_delete)
10136 type = USE_T_CDL;
10137 else
10138 return FAIL;
10139
10140#ifdef FEAT_CLIPBOARD
10141 /* Remove a modeless selection when deleting lines halfway the screen or
10142 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010143 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010144 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 else
10146 clip_scroll_selection(line_count);
10147#endif
10148
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149#ifdef FEAT_GUI
10150 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10151 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010152 gui_dont_update_cursor(gui.cursor_row >= row + off
10153 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154#endif
10155
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010156 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10157 cursor_col = wp->w_wincol;
10158
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 if (*T_CCS != NUL) /* cursor relative to region */
10160 {
10161 cursor_row = row;
10162 cursor_end = end;
10163 }
10164 else
10165 {
10166 cursor_row = row + off;
10167 cursor_end = end + off;
10168 }
10169
10170 /*
10171 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10172 * Clear the inserted lines in ScreenLines[].
10173 */
10174 row += off;
10175 end += off;
10176 for (i = 0; i < line_count; ++i)
10177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 if (wp != NULL && wp->w_width != Columns)
10179 {
10180 /* need to copy part of a line */
10181 j = row + i;
10182 while ((j += line_count) <= end - 1)
10183 linecopy(j - line_count, j, wp);
10184 j -= line_count;
10185 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010186 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10187 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 else
10189 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10190 LineWraps[j] = FALSE;
10191 }
10192 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 {
10194 /* whole width, moving the line pointers is faster */
10195 j = row + i;
10196 temp = LineOffset[j];
10197 while ((j += line_count) <= end - 1)
10198 {
10199 LineOffset[j - line_count] = LineOffset[j];
10200 LineWraps[j - line_count] = LineWraps[j];
10201 }
10202 LineOffset[j - line_count] = temp;
10203 LineWraps[j - line_count] = FALSE;
10204 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010205 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 else
10207 lineinvalid(temp, (int)Columns);
10208 }
10209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010211 if (screen_attr != clear_attr)
10212 screen_stop_highlight();
10213 if (clear_attr != 0)
10214 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 /* redraw the characters */
10217 if (type == USE_REDRAW)
10218 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010219 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010221 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 out_str(T_CD);
10223 screen_start(); /* don't know where cursor is now */
10224 }
10225 else if (type == USE_T_CDL)
10226 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010227 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 term_delete_lines(line_count);
10229 screen_start(); /* don't know where cursor is now */
10230 }
10231 /*
10232 * Deleting lines at top of the screen or scroll region: Just scroll
10233 * the whole screen (scroll region) up by outputting newlines on the
10234 * last line.
10235 */
10236 else if (type == USE_NL)
10237 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010238 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 for (i = line_count; --i >= 0; )
10240 out_char('\n'); /* cursor will remain on same line */
10241 }
10242 else
10243 {
10244 for (i = line_count; --i >= 0; )
10245 {
10246 if (type == USE_T_DL)
10247 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010248 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 out_str(T_DL); /* delete a line */
10250 }
10251 else /* type == USE_T_CE */
10252 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010253 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 out_str(T_CE); /* erase a line */
10255 }
10256 screen_start(); /* don't know where cursor is now */
10257 }
10258 }
10259
10260 /*
10261 * If the 'db' flag is set, we need to clear the lines that have been
10262 * scrolled up at the bottom of the region.
10263 */
10264 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10265 {
10266 for (i = line_count; i > 0; --i)
10267 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010268 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 out_str(T_CE); /* erase a line */
10270 screen_start(); /* don't know where cursor is now */
10271 }
10272 }
10273
10274#ifdef FEAT_GUI
10275 gui_can_update_cursor();
10276 if (gui.in_use)
10277 out_flush(); /* always flush after a scroll */
10278#endif
10279
10280 return OK;
10281}
10282
10283/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010284 * Return TRUE when postponing displaying the mode message: when not redrawing
10285 * or inside a mapping.
10286 */
10287 int
10288skip_showmode()
10289{
10290 // Call char_avail() only when we are going to show something, because it
10291 // takes a bit of time. redrawing() may also call char_avail_avail().
10292 if (global_busy
10293 || msg_silent != 0
10294 || !redrawing()
10295 || (char_avail() && !KeyTyped))
10296 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010297 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010298 return TRUE;
10299 }
10300 return FALSE;
10301}
10302
10303/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010304 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 *
10306 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10307 * If clear_cmdline is FALSE there may be a message there that needs to be
10308 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010309 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 * Return the length of the message (0 if no message).
10311 */
10312 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010313showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314{
10315 int need_clear;
10316 int length = 0;
10317 int do_mode;
10318 int attr;
10319 int nwr_save;
10320#ifdef FEAT_INS_EXPAND
10321 int sub_attr;
10322#endif
10323
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010324 do_mode = ((p_smd && msg_silent == 0)
10325 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010326 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010327 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010328 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010330 if (skip_showmode())
10331 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332
10333 nwr_save = need_wait_return;
10334
10335 /* wait a bit before overwriting an important message */
10336 check_for_delay(FALSE);
10337
10338 /* if the cmdline is more than one line high, erase top lines */
10339 need_clear = clear_cmdline;
10340 if (clear_cmdline && cmdline_row < Rows - 1)
10341 msg_clr_cmdline(); /* will reset clear_cmdline */
10342
10343 /* Position on the last line in the window, column 0 */
10344 msg_pos_mode();
10345 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010346 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 if (do_mode)
10348 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010349 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010351 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010352# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010353 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010354# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010355 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010356# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010357 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010358# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010359 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010361 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362# endif
10363#endif
10364#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10365 if (gui.in_use)
10366 {
10367 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010368 {
10369 /* HANGUL */
10370 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010371 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010372 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010373 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 }
10376#endif
10377#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010378 /* CTRL-X in Insert mode */
10379 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 {
10381 /* These messages can get long, avoid a wrap in a narrow
10382 * window. Prefer showing edit_submode_extra. */
10383 length = (Rows - msg_row) * Columns - 3;
10384 if (edit_submode_extra != NULL)
10385 length -= vim_strsize(edit_submode_extra);
10386 if (length > 0)
10387 {
10388 if (edit_submode_pre != NULL)
10389 length -= vim_strsize(edit_submode_pre);
10390 if (length - vim_strsize(edit_submode) > 0)
10391 {
10392 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010393 msg_puts_attr((char *)edit_submode_pre, attr);
10394 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 }
10396 if (edit_submode_extra != NULL)
10397 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010398 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010400 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 else
10402 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010403 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 }
10405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 }
10407 else
10408#endif
10409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010411 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010412 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010413 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 else if (State & INSERT)
10415 {
10416#ifdef FEAT_RIGHTLEFT
10417 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010418 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010420 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010422 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010423 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010425 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010427 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428#ifdef FEAT_RIGHTLEFT
10429 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010430 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431#endif
10432#ifdef FEAT_KEYMAP
10433 if (State & LANGMAP)
10434 {
10435# ifdef FEAT_ARABIC
10436 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010437 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 else
10439# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010440 if (get_keymap_str(curwin, (char_u *)" (%s)",
10441 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010442 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 }
10444#endif
10445 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010446 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 if (VIsual_active)
10449 {
10450 char *p;
10451
10452 /* Don't concatenate separate words to avoid translation
10453 * problems. */
10454 switch ((VIsual_select ? 4 : 0)
10455 + (VIsual_mode == Ctrl_V) * 2
10456 + (VIsual_mode == 'V'))
10457 {
10458 case 0: p = N_(" VISUAL"); break;
10459 case 1: p = N_(" VISUAL LINE"); break;
10460 case 2: p = N_(" VISUAL BLOCK"); break;
10461 case 4: p = N_(" SELECT"); break;
10462 case 5: p = N_(" SELECT LINE"); break;
10463 default: p = N_(" SELECT BLOCK"); break;
10464 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010465 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010467 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010469
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 need_clear = TRUE;
10471 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010472 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473#ifdef FEAT_INS_EXPAND
10474 && edit_submode == NULL /* otherwise it gets too long */
10475#endif
10476 )
10477 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010478 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 need_clear = TRUE;
10480 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010481
10482 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010483 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 msg_clr_eos();
10485 msg_didout = FALSE; /* overwrite this message */
10486 length = msg_col;
10487 msg_col = 0;
10488 need_wait_return = nwr_save; /* never ask for hit-return for this */
10489 }
10490 else if (clear_cmdline && msg_silent == 0)
10491 /* Clear the whole command line. Will reset "clear_cmdline". */
10492 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010493 else if (redraw_mode)
10494 {
10495 msg_pos_mode();
10496 msg_clr_eos();
10497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498
10499#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 /* In Visual mode the size of the selected area must be redrawn. */
10501 if (VIsual_active)
10502 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503
10504 /* If the last window has no status line, the ruler is after the mode
10505 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010506 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010507 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508#endif
10509 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010510 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 clear_cmdline = FALSE;
10512
10513 return length;
10514}
10515
10516/*
10517 * Position for a mode message.
10518 */
10519 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010520msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521{
10522 msg_col = 0;
10523 msg_row = Rows - 1;
10524}
10525
10526/*
10527 * Delete mode message. Used when ESC is typed which is expected to end
10528 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010529 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 */
10531 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010532unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533{
10534 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010535 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 */
10537 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10538 redraw_cmdline = TRUE; /* delete mode later */
10539 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010540 clearmode();
10541}
10542
10543/*
10544 * Clear the mode message.
10545 */
10546 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010547clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010548{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010549 int save_msg_row = msg_row;
10550 int save_msg_col = msg_col;
10551
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010552 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010553 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010554 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010555 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010556
10557 msg_col = save_msg_col;
10558 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559}
10560
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010561 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010562recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010563{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010564 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010565 if (!shortmess(SHM_RECORDING))
10566 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010567 char s[4];
10568
10569 sprintf(s, " @%c", reg_recording);
10570 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010571 }
10572}
10573
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010574/*
10575 * Draw the tab pages line at the top of the Vim window.
10576 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010577 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010578draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010579{
10580 int tabcount = 0;
10581 tabpage_T *tp;
10582 int tabwidth;
10583 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010584 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010585 int attr;
10586 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010587 win_T *cwp;
10588 int wincount;
10589 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010590 int c;
10591 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010592 int attr_sel = HL_ATTR(HLF_TPS);
10593 int attr_nosel = HL_ATTR(HLF_TP);
10594 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010595 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010596 int room;
10597 int use_sep_chars = (t_colors < 8
10598#ifdef FEAT_GUI
10599 && !gui.in_use
10600#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010601#ifdef FEAT_TERMGUICOLORS
10602 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010603#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010604 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010605
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010606 if (ScreenLines == NULL)
10607 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010608 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010609
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010610#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010611 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010612 if (gui_use_tabline())
10613 {
10614 gui_update_tabline();
10615 return;
10616 }
10617#endif
10618
10619 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010620 return;
10621
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010622#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010623 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010624
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010625 /* Use the 'tabline' option if it's set. */
10626 if (*p_tal != NUL)
10627 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010628 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010629
10630 /* Check for an error. If there is one we would loop in redrawing the
10631 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010632 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010633 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010634 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010635 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010636 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010637 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010638 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010639 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010640#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010641 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010642 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010643 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010644
Bram Moolenaar238a5642006-02-21 22:12:05 +000010645 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10646 if (tabwidth < 6)
10647 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010648
Bram Moolenaar238a5642006-02-21 22:12:05 +000010649 attr = attr_nosel;
10650 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010651 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10652 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010653 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010654 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010655
Bram Moolenaar238a5642006-02-21 22:12:05 +000010656 if (tp->tp_topframe == topframe)
10657 attr = attr_sel;
10658 if (use_sep_chars && col > 0)
10659 screen_putchar('|', 0, col++, attr);
10660
10661 if (tp->tp_topframe != topframe)
10662 attr = attr_nosel;
10663
10664 screen_putchar(' ', 0, col++, attr);
10665
10666 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010667 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010668 cwp = curwin;
10669 wp = firstwin;
10670 }
10671 else
10672 {
10673 cwp = tp->tp_curwin;
10674 wp = tp->tp_firstwin;
10675 }
10676
10677 modified = FALSE;
10678 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10679 if (bufIsChanged(wp->w_buffer))
10680 modified = TRUE;
10681 if (modified || wincount > 1)
10682 {
10683 if (wincount > 1)
10684 {
10685 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010686 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010687 if (col + len >= Columns - 3)
10688 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010689 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010690#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010691 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010692#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010693 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010694#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010695 );
10696 col += len;
10697 }
10698 if (modified)
10699 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10700 screen_putchar(' ', 0, col++, attr);
10701 }
10702
10703 room = scol - col + tabwidth - 1;
10704 if (room > 0)
10705 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010706 /* Get buffer name in NameBuff[] */
10707 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010708 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010709 len = vim_strsize(NameBuff);
10710 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010711 if (has_mbyte)
10712 while (len > room)
10713 {
10714 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010715 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010716 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010717 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010718 {
10719 p += len - room;
10720 len = room;
10721 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010722 if (len > Columns - col - 1)
10723 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010724
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010725 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010726 col += len;
10727 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010728 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010729
10730 /* Store the tab page number in TabPageIdxs[], so that
10731 * jump_to_mouse() knows where each one is. */
10732 ++tabcount;
10733 while (scol < col)
10734 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010735 }
10736
Bram Moolenaar238a5642006-02-21 22:12:05 +000010737 if (use_sep_chars)
10738 c = '_';
10739 else
10740 c = ' ';
10741 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010742
10743 /* Put an "X" for closing the current tab if there are several. */
10744 if (first_tabpage->tp_next != NULL)
10745 {
10746 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10747 TabPageIdxs[Columns - 1] = -999;
10748 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010749 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010750
10751 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10752 * set. */
10753 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010754}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010755
10756/*
10757 * Get buffer name for "buf" into NameBuff[].
10758 * Takes care of special buffer names and translates special characters.
10759 */
10760 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010761get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010762{
10763 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010764 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010765 else
10766 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10767 trans_characters(NameBuff, MAXPATHL);
10768}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010769
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770/*
10771 * Get the character to use in a status line. Get its attributes in "*attr".
10772 */
10773 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010774fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775{
10776 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010777
10778#ifdef FEAT_TERMINAL
10779 if (bt_terminal(wp->w_buffer))
10780 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010781 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010782 {
10783 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010784 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010785 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010786 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010787 {
10788 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010789 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010790 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010791 }
10792 else
10793#endif
10794 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010796 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 fill = fill_stl;
10798 }
10799 else
10800 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010801 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 fill = fill_stlnc;
10803 }
10804 /* Use fill when there is highlighting, and highlighting of current
10805 * window differs, or the fillchars differ, or this is not the
10806 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010807 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010808 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 || (fill_stl != fill_stlnc)))
10810 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010811 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 return '^';
10813 return '=';
10814}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816/*
10817 * Get the character to use in a separator between vertically split windows.
10818 * Get its attributes in "*attr".
10819 */
10820 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010821fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010823 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 if (*attr == 0 && fill_vert == ' ')
10825 return '|';
10826 else
10827 return fill_vert;
10828}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829
10830/*
10831 * Return TRUE if redrawing should currently be done.
10832 */
10833 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010834redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010836#ifdef FEAT_EVAL
10837 if (disable_redraw_for_testing)
10838 return 0;
10839 else
10840#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010841 return ((!RedrawingDisabled
10842#ifdef FEAT_EVAL
10843 || ignore_redraw_flag_for_testing
10844#endif
10845 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846}
10847
10848/*
10849 * Return TRUE if printing messages should currently be done.
10850 */
10851 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010852messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853{
10854 return (!(p_lz && char_avail() && !KeyTyped));
10855}
10856
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010857#ifdef FEAT_MENU
10858/*
10859 * Draw the window toolbar.
10860 */
10861 static void
10862redraw_win_toolbar(win_T *wp)
10863{
10864 vimmenu_T *menu;
10865 int item_idx = 0;
10866 int item_count = 0;
10867 int col = 0;
10868 int next_col;
10869 int off = (int)(current_ScreenLine - ScreenLines);
10870 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10871 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10872
10873 vim_free(wp->w_winbar_items);
10874 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10875 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010876 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010877
10878 /* TODO: use fewer spaces if there is not enough room */
10879 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010880 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010881 {
10882 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010883 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010884 break;
10885 if (col > 1)
10886 {
10887 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010888 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010889 break;
10890 }
10891
10892 wp->w_winbar_items[item_idx].wb_startcol = col;
10893 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010894 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010895 break;
10896
10897 next_col = text_to_screenline(wp, menu->name, col);
10898 while (col < next_col)
10899 {
10900 ScreenAttrs[off + col] = button_attr;
10901 ++col;
10902 }
10903 wp->w_winbar_items[item_idx].wb_endcol = col;
10904 wp->w_winbar_items[item_idx].wb_menu = menu;
10905 ++item_idx;
10906
Bram Moolenaar02631462017-09-22 15:20:32 +020010907 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010908 break;
10909 space_to_screenline(off + col, button_attr);
10910 ++col;
10911 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010912 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010913 {
10914 space_to_screenline(off + col, fill_attr);
10915 ++col;
10916 }
10917 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10918
Bram Moolenaar02631462017-09-22 15:20:32 +020010919 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010920 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010921}
10922#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010923
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924/*
10925 * Show current status info in ruler and various other places
10926 * If always is FALSE, only show ruler if position has changed.
10927 */
10928 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010929showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930{
10931 if (!always && !redrawing())
10932 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010933#ifdef FEAT_INS_EXPAND
10934 if (pum_visible())
10935 {
10936 /* Don't redraw right now, do it later. */
10937 curwin->w_redr_status = TRUE;
10938 return;
10939 }
10940#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010941#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010942 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010943 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 else
10945#endif
10946#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010947 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948#endif
10949
10950#ifdef FEAT_TITLE
10951 if (need_maketitle
10952# ifdef FEAT_STL_OPT
10953 || (p_icon && (stl_syntax & STL_IN_ICON))
10954 || (p_title && (stl_syntax & STL_IN_TITLE))
10955# endif
10956 )
10957 maketitle();
10958#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010959 /* Redraw the tab pages line if needed. */
10960 if (redraw_tabline)
10961 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962}
10963
10964#ifdef FEAT_CMDL_INFO
10965 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010966win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010968#define RULER_BUF_LEN 70
10969 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 int row;
10971 int fillchar;
10972 int attr;
10973 int empty_line = FALSE;
10974 colnr_T virtcol;
10975 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010976 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 int this_ru_col;
10979 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010980 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981
10982 /* If 'ruler' off or redrawing disabled, don't do anything */
10983 if (!p_ru)
10984 return;
10985
10986 /*
10987 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10988 * after deleting lines, before cursor.lnum is corrected.
10989 */
10990 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10991 return;
10992
10993#ifdef FEAT_INS_EXPAND
10994 /* Don't draw the ruler while doing insert-completion, it might overwrite
10995 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 if (edit_submode != NULL)
10998 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010999 // Don't draw the ruler when the popup menu is visible, it may overlap.
11000 // Except when the popup menu will be redrawn anyway.
11001 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011002 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003#endif
11004
11005#ifdef FEAT_STL_OPT
11006 if (*p_ruf)
11007 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011008 int save_called_emsg = called_emsg;
11009
11010 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011012 if (called_emsg)
11013 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011014 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011015 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 return;
11017 }
11018#endif
11019
11020 /*
11021 * Check if not in Insert mode and the line is empty (will show "0-1").
11022 */
11023 if (!(State & INSERT)
11024 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11025 empty_line = TRUE;
11026
11027 /*
11028 * Only draw the ruler when something changed.
11029 */
11030 validate_virtcol_win(wp);
11031 if ( redraw_cmdline
11032 || always
11033 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11034 || wp->w_cursor.col != wp->w_ru_cursor.col
11035 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 || wp->w_topline != wp->w_ru_topline
11038 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11039#ifdef FEAT_DIFF
11040 || wp->w_topfill != wp->w_ru_topfill
11041#endif
11042 || empty_line != wp->w_ru_empty)
11043 {
11044 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 if (wp->w_status_height)
11046 {
11047 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011048 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011049 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011050 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 }
11052 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 {
11054 row = Rows - 1;
11055 fillchar = ' ';
11056 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 width = Columns;
11058 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 }
11060
11061 /* In list mode virtcol needs to be recomputed */
11062 virtcol = wp->w_virtcol;
11063 if (wp->w_p_list && lcs_tab1 == NUL)
11064 {
11065 wp->w_p_list = FALSE;
11066 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11067 wp->w_p_list = TRUE;
11068 }
11069
11070 /*
11071 * Some sprintfs return the length, some return a pointer.
11072 * To avoid portability problems we use strlen() here.
11073 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011074 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11076 ? 0L
11077 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011078 len = STRLEN(buffer);
11079 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11081 (int)virtcol + 1);
11082
11083 /*
11084 * Add a "50%" if there is room for it.
11085 * On the last line, don't print in the last column (scrolls the
11086 * screen up on some terminals).
11087 */
11088 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011089 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 this_ru_col = ru_col - (Columns - width);
11094 if (this_ru_col < 0)
11095 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 /* Never use more than half the window/screen width, leave the other
11097 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011098 if (this_ru_col < (width + 1) / 2)
11099 this_ru_col = (width + 1) / 2;
11100 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011102 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011103 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 if (has_mbyte)
11106 i += (*mb_char2bytes)(fillchar, buffer + i);
11107 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 buffer[i++] = fillchar;
11109 ++o;
11110 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011111 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 }
11113 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 if (has_mbyte)
11115 {
11116 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011117 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 {
11119 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011120 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 {
11122 buffer[i] = NUL;
11123 break;
11124 }
11125 }
11126 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011127 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011128 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129
Bram Moolenaar4033c552017-09-16 20:54:51 +020011130 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 i = redraw_cmdline;
11132 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011133 this_ru_col + off + (int)STRLEN(buffer),
11134 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 fillchar, fillchar, attr);
11136 /* don't redraw the cmdline because of showing the ruler */
11137 redraw_cmdline = i;
11138 wp->w_ru_cursor = wp->w_cursor;
11139 wp->w_ru_virtcol = wp->w_virtcol;
11140 wp->w_ru_empty = empty_line;
11141 wp->w_ru_topline = wp->w_topline;
11142 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11143#ifdef FEAT_DIFF
11144 wp->w_ru_topfill = wp->w_topfill;
11145#endif
11146 }
11147}
11148#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011149
11150#if defined(FEAT_LINEBREAK) || defined(PROTO)
11151/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011152 * Return the width of the 'number' and 'relativenumber' column.
11153 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011154 * Otherwise it depends on 'numberwidth' and the line count.
11155 */
11156 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011157number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011158{
11159 int n;
11160 linenr_T lnum;
11161
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011162 if (wp->w_p_rnu && !wp->w_p_nu)
11163 /* cursor line shows "0" */
11164 lnum = wp->w_height;
11165 else
11166 /* cursor line shows absolute line number */
11167 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011168
Bram Moolenaar6b314672015-03-20 15:42:10 +010011169 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011170 return wp->w_nrwidth_width;
11171 wp->w_nrwidth_line_count = lnum;
11172
11173 n = 0;
11174 do
11175 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011176 lnum /= 10;
11177 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011178 } while (lnum > 0);
11179
11180 /* 'numberwidth' gives the minimal width plus one */
11181 if (n < wp->w_p_nuw - 1)
11182 n = wp->w_p_nuw - 1;
11183
11184 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011185 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011186 return n;
11187}
11188#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011189
Bram Moolenaar113e1072019-01-20 15:30:40 +010011190#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011191/*
11192 * Return the current cursor column. This is the actual position on the
11193 * screen. First column is 0.
11194 */
11195 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011196screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011197{
11198 return screen_cur_col;
11199}
11200
11201/*
11202 * Return the current cursor row. This is the actual position on the screen.
11203 * First row is 0.
11204 */
11205 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011206screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011207{
11208 return screen_cur_row;
11209}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011210#endif