blob: be3bcfad5d1013610ce66b5366417478d3e3a1b6 [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
Bram Moolenaar988c4332019-06-02 14:12:11 +0200826 // Display popup windows on top of the windows.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200827 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
Bram Moolenaar988c4332019-06-02 14:12:11 +0200990#ifdef FEAT_TEXT_PROP
991 // Display popup windows on top of the others.
992 update_popups();
993#endif
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 update_finish();
996}
997#endif
998
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200999/*
1000 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
1001 * window then get the "Pmenu" highlight attribute.
1002 */
1003 static int
1004get_wcr_attr(win_T *wp)
1005{
1006 int wcr_attr = 0;
1007
1008 if (*wp->w_p_wcr != NUL)
1009 wcr_attr = syn_name2attr(wp->w_p_wcr);
1010#ifdef FEAT_TEXT_PROP
1011 if (bt_popup(wp->w_buffer) && wcr_attr == 0)
1012 wcr_attr = HL_ATTR(HLF_PNI);
1013#endif
1014 return wcr_attr;
1015}
1016
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001017#ifdef FEAT_TEXT_PROP
1018/*
1019 * Return a string of "len" spaces in IObuff.
1020 */
1021 static char_u *
1022get_spaces(int len)
1023{
1024 vim_memset(IObuff, ' ', (size_t)len);
1025 IObuff[len] = NUL;
1026 return IObuff;
1027}
1028
1029 static void
1030update_popups(void)
1031{
1032 win_T *wp;
1033 int top_off;
1034 int left_off;
1035 int total_width;
1036 int total_height;
1037 int popup_attr;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001038 int border_attr[4];
1039 int border_char[8] = {'-', '|', '-', '|', '+', '+', '+', '+', };
1040 char_u buf[MB_MAXBYTES];
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001041 int row;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001042 int i;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001043
1044 // Find the window with the lowest zindex that hasn't been updated yet,
1045 // so that the window with a higher zindex is drawn later, thus goes on
1046 // top.
1047 // TODO: don't redraw every popup every time.
1048 popup_reset_handled();
1049 while ((wp = find_next_popup(TRUE)) != NULL)
1050 {
1051 // Recompute the position if the text changed.
1052 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1053 popup_adjust_position(wp);
1054
1055 // adjust w_winrow and w_wincol for border and padding, since
1056 // win_update() doesn't handle them.
1057 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1058 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1059 wp->w_winrow += top_off;
1060 wp->w_wincol += left_off;
1061
1062 // Draw the popup text.
1063 win_update(wp);
1064
1065 wp->w_winrow -= top_off;
1066 wp->w_wincol -= left_off;
1067
1068 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1069 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
1070 total_height = wp->w_popup_border[0] + wp->w_popup_padding[0]
1071 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1072 popup_attr = get_wcr_attr(wp);
1073
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001074 if (enc_utf8)
1075 {
Bram Moolenaar790498b2019-06-01 22:15:29 +02001076 border_char[0] = border_char[2] = 0x2550;
1077 border_char[1] = border_char[3] = 0x2551;
1078 border_char[4] = 0x2554;
1079 border_char[5] = 0x2557;
1080 border_char[6] = 0x255d;
1081 border_char[7] = 0x255a;
1082 }
1083 for (i = 0; i < 8; ++i)
1084 if (wp->w_border_char[i] != 0)
1085 border_char[i] = wp->w_border_char[i];
1086
1087 for (i = 0; i < 4; ++i)
1088 {
1089 border_attr[i] = popup_attr;
1090 if (wp->w_border_highlight[i] != NULL)
1091 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001092 }
1093
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001094 if (wp->w_popup_border[0] > 0)
1095 {
1096 // top border
1097 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1098 wp->w_wincol,
1099 wp->w_wincol + total_width,
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001100 wp->w_popup_border[3] != 0
Bram Moolenaar790498b2019-06-01 22:15:29 +02001101 ? border_char[4] : border_char[0],
1102 border_char[0], border_attr[0]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001103 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001104 {
1105 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1106 screen_puts(buf, wp->w_winrow,
1107 wp->w_wincol + total_width - 1, border_attr[1]);
1108 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001109 }
1110
1111 if (wp->w_popup_padding[0] > 0)
1112 {
1113 // top padding
1114 row = wp->w_winrow + wp->w_popup_border[0];
1115 screen_fill(row, row + wp->w_popup_padding[0],
1116 wp->w_wincol + wp->w_popup_border[3],
1117 wp->w_wincol + total_width - wp->w_popup_border[1],
1118 ' ', ' ', popup_attr);
1119 }
1120
1121 for (row = wp->w_winrow + wp->w_popup_border[0];
1122 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1123 ++row)
1124 {
1125 // left border
1126 if (wp->w_popup_border[3] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001127 {
1128 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1129 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1130 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001131 // left padding
1132 if (wp->w_popup_padding[3] > 0)
1133 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1134 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1135 // right border
1136 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001137 {
1138 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1139 screen_puts(buf, row,
1140 wp->w_wincol + total_width - 1, border_attr[1]);
1141 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001142 // right padding
1143 if (wp->w_popup_padding[1] > 0)
1144 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1145 wp->w_wincol + wp->w_popup_border[3]
1146 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1147 }
1148
1149 if (wp->w_popup_padding[2] > 0)
1150 {
1151 // bottom padding
1152 row = wp->w_winrow + wp->w_popup_border[0]
1153 + wp->w_popup_padding[0] + wp->w_height;
1154 screen_fill(row, row + wp->w_popup_padding[2],
1155 wp->w_wincol + wp->w_popup_border[3],
1156 wp->w_wincol + total_width - wp->w_popup_border[1],
1157 ' ', ' ', popup_attr);
1158 }
1159
1160 if (wp->w_popup_border[2] > 0)
1161 {
1162 // bottom border
1163 row = wp->w_winrow + total_height - 1;
1164 screen_fill(row , row + 1,
1165 wp->w_wincol,
1166 wp->w_wincol + total_width,
Bram Moolenaar790498b2019-06-01 22:15:29 +02001167 wp->w_popup_border[3] != 0
1168 ? border_char[7] : border_char[2],
1169 border_char[2], border_attr[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001170 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001171 {
1172 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1173 screen_puts(buf, row,
1174 wp->w_wincol + total_width - 1, border_attr[2]);
1175 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001176 }
1177 }
1178}
1179#endif
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181#if defined(FEAT_GUI) || defined(PROTO)
1182/*
1183 * Update a single window, its status line and maybe the command line msg.
1184 * Used for the GUI scrollbar.
1185 */
1186 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001187updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001189 /* return if already busy updating */
1190 if (updating_screen)
1191 return;
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 update_prepare();
1194
1195#ifdef FEAT_CLIPBOARD
1196 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001197 if (clip_star.available && clip_isautosel_star())
1198 clip_update_selection(&clip_star);
1199 if (clip_plus.available && clip_isautosel_plus())
1200 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001204
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001205 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001206 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001207 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (wp->w_redr_status
1210# ifdef FEAT_CMDL_INFO
1211 || p_ru
1212# endif
1213# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001214 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215# endif
1216 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001217 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaar988c4332019-06-02 14:12:11 +02001219#ifdef FEAT_TEXT_PROP
1220 // Display popup windows on top of everything.
1221 update_popups();
1222#endif
1223
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 update_finish();
1225}
1226#endif
1227
1228/*
1229 * Update a single window.
1230 *
1231 * This may cause the windows below it also to be redrawn (when clearing the
1232 * screen or scrolling lines).
1233 *
1234 * How the window is redrawn depends on wp->w_redr_type. Each type also
1235 * implies the one below it.
1236 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001237 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1239 * INVERTED redraw the changed part of the Visual area
1240 * INVERTED_ALL redraw the whole Visual area
1241 * VALID 1. scroll up/down to adjust for a changed w_topline
1242 * 2. update lines at the top when scrolled down
1243 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001244 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 * b_mod_top and b_mod_bot.
1246 * - if wp->w_redraw_top non-zero, redraw lines between
1247 * wp->w_redraw_top and wp->w_redr_bot.
1248 * - continue redrawing when syntax status is invalid.
1249 * 4. if scrolled up, update lines at the bottom.
1250 * This results in three areas that may need updating:
1251 * top: from first row to top_end (when scrolled down)
1252 * mid: from mid_start to mid_end (update inversion or changed text)
1253 * bot: from bot_start to last row (when scrolled up)
1254 */
1255 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001256win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257{
1258 buf_T *buf = wp->w_buffer;
1259 int type;
1260 int top_end = 0; /* Below last row of the top area that needs
1261 updating. 0 when no top area updating. */
1262 int mid_start = 999;/* first row of the mid area that needs
1263 updating. 999 when no mid area updating. */
1264 int mid_end = 0; /* Below last row of the mid area that needs
1265 updating. 0 when no mid area updating. */
1266 int bot_start = 999;/* first row of the bot area that needs
1267 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 int scrolled_down = FALSE; /* TRUE when scrolled down when
1269 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001271 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 int top_to_mod = FALSE; /* redraw above mod_top */
1273#endif
1274
1275 int row; /* current window row to display */
1276 linenr_T lnum; /* current buffer lnum to display */
1277 int idx; /* current index in w_lines[] */
1278 int srow; /* starting row of the current line */
1279
1280 int eof = FALSE; /* if TRUE, we hit the end of the file */
1281 int didline = FALSE; /* if TRUE, we finished the last line */
1282 int i;
1283 long j;
1284 static int recursive = FALSE; /* being called recursively */
1285 int old_botline = wp->w_botline;
1286#ifdef FEAT_FOLDING
1287 long fold_count;
1288#endif
1289#ifdef FEAT_SYN_HL
1290 /* remember what happened to the previous line, to know if
1291 * check_visual_highlight() can be used */
1292#define DID_NONE 1 /* didn't update a line */
1293#define DID_LINE 2 /* updated a normal line */
1294#define DID_FOLD 3 /* updated a folded line */
1295 int did_update = DID_NONE;
1296 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1297#endif
1298 linenr_T mod_top = 0;
1299 linenr_T mod_bot = 0;
1300#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1301 int save_got_int;
1302#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001303#ifdef SYN_TIME_LIMIT
1304 proftime_T syntax_tm;
1305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306
1307 type = wp->w_redr_type;
1308
1309 if (type == NOT_VALID)
1310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 wp->w_lines_valid = 0;
1313 }
1314
1315 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001316 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 {
1318 wp->w_redr_type = 0;
1319 return;
1320 }
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 /* Window is zero-width: Only need to draw the separator. */
1323 if (wp->w_width == 0)
1324 {
1325 /* draw the vertical separator right of this window */
1326 draw_vsep_win(wp, 0);
1327 wp->w_redr_type = 0;
1328 return;
1329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001331#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001332 // If this window contains a terminal, redraw works completely differently.
1333 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001334 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001335 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001336# ifdef FEAT_MENU
1337 /* Draw the window toolbar, if there is one. */
1338 if (winbar_height(wp) > 0)
1339 redraw_win_toolbar(wp);
1340# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001341 wp->w_redr_type = 0;
1342 return;
1343 }
1344#endif
1345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001347 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348#endif
1349
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001350#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001351 /* Force redraw when width of 'number' or 'relativenumber' column
1352 * changes. */
1353 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001354 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001355 {
1356 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001357 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001358 }
1359 else
1360#endif
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1363 {
1364 /*
1365 * When there are both inserted/deleted lines and specific lines to be
1366 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1367 * everything (only happens when redrawing is off for while).
1368 */
1369 type = NOT_VALID;
1370 }
1371 else
1372 {
1373 /*
1374 * Set mod_top to the first line that needs displaying because of
1375 * changes. Set mod_bot to the first line after the changes.
1376 */
1377 mod_top = wp->w_redraw_top;
1378 if (wp->w_redraw_bot != 0)
1379 mod_bot = wp->w_redraw_bot + 1;
1380 else
1381 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 if (buf->b_mod_set)
1383 {
1384 if (mod_top == 0 || mod_top > buf->b_mod_top)
1385 {
1386 mod_top = buf->b_mod_top;
1387#ifdef FEAT_SYN_HL
1388 /* Need to redraw lines above the change that may be included
1389 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001390 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001392 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 if (mod_top < 1)
1394 mod_top = 1;
1395 }
1396#endif
1397 }
1398 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1399 mod_bot = buf->b_mod_bot;
1400
1401#ifdef FEAT_SEARCH_EXTRA
1402 /* When 'hlsearch' is on and using a multi-line search pattern, a
1403 * change in one line may make the Search highlighting in a
1404 * previous line invalid. Simple solution: redraw all visible
1405 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001406 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001408 if (search_hl.rm.regprog != NULL
1409 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001411 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001412 {
1413 cur = wp->w_match_head;
1414 while (cur != NULL)
1415 {
1416 if (cur->match.regprog != NULL
1417 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001418 {
1419 top_to_mod = TRUE;
1420 break;
1421 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001422 cur = cur->next;
1423 }
1424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425#endif
1426 }
1427#ifdef FEAT_FOLDING
1428 if (mod_top != 0 && hasAnyFolding(wp))
1429 {
1430 linenr_T lnumt, lnumb;
1431
1432 /*
1433 * A change in a line can cause lines above it to become folded or
1434 * unfolded. Find the top most buffer line that may be affected.
1435 * If the line was previously folded and displayed, get the first
1436 * line of that fold. If the line is folded now, get the first
1437 * folded line. Use the minimum of these two.
1438 */
1439
1440 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1441 * the line below it. If there is no valid entry, use w_topline.
1442 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1443 * to this line. If there is no valid entry, use MAXLNUM. */
1444 lnumt = wp->w_topline;
1445 lnumb = MAXLNUM;
1446 for (i = 0; i < wp->w_lines_valid; ++i)
1447 if (wp->w_lines[i].wl_valid)
1448 {
1449 if (wp->w_lines[i].wl_lastlnum < mod_top)
1450 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1451 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1452 {
1453 lnumb = wp->w_lines[i].wl_lnum;
1454 /* When there is a fold column it might need updating
1455 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001456 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 ++lnumb;
1458 }
1459 }
1460
1461 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1462 if (mod_top > lnumt)
1463 mod_top = lnumt;
1464
1465 /* Now do the same for the bottom line (one above mod_bot). */
1466 --mod_bot;
1467 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1468 ++mod_bot;
1469 if (mod_bot < lnumb)
1470 mod_bot = lnumb;
1471 }
1472#endif
1473
1474 /* When a change starts above w_topline and the end is below
1475 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001476 * If the end of the change is above w_topline: do like no change was
1477 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (mod_top != 0 && mod_top < wp->w_topline)
1479 {
1480 if (mod_bot > wp->w_topline)
1481 mod_top = wp->w_topline;
1482#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001483 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 top_end = 1;
1485#endif
1486 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001487
1488 /* When line numbers are displayed need to redraw all lines below
1489 * inserted/deleted lines. */
1490 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1491 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001493 wp->w_redraw_top = 0; // reset for next time
1494 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
1496 /*
1497 * When only displaying the lines at the top, set top_end. Used when
1498 * window has scrolled down for msg_scrolled.
1499 */
1500 if (type == REDRAW_TOP)
1501 {
1502 j = 0;
1503 for (i = 0; i < wp->w_lines_valid; ++i)
1504 {
1505 j += wp->w_lines[i].wl_size;
1506 if (j >= wp->w_upd_rows)
1507 {
1508 top_end = j;
1509 break;
1510 }
1511 }
1512 if (top_end == 0)
1513 /* not found (cannot happen?): redraw everything */
1514 type = NOT_VALID;
1515 else
1516 /* top area defined, the rest is VALID */
1517 type = VALID;
1518 }
1519
Bram Moolenaar367329b2007-08-30 11:53:22 +00001520 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001521 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1522 * non-zero and thus not FALSE) will indicate that screenclear() was not
1523 * called. */
1524 if (screen_cleared)
1525 screen_cleared = MAYBE;
1526
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 /*
1528 * If there are no changes on the screen that require a complete redraw,
1529 * handle three cases:
1530 * 1: we are off the top of the screen by a few lines: scroll down
1531 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1532 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1533 * w_lines[] that needs updating.
1534 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001535 if ((type == VALID || type == SOME_VALID
1536 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537#ifdef FEAT_DIFF
1538 && !wp->w_botfill && !wp->w_old_botfill
1539#endif
1540 )
1541 {
1542 if (mod_top != 0 && wp->w_topline == mod_top)
1543 {
1544 /*
1545 * w_topline is the first changed line, the scrolling will be done
1546 * further down.
1547 */
1548 }
1549 else if (wp->w_lines[0].wl_valid
1550 && (wp->w_topline < wp->w_lines[0].wl_lnum
1551#ifdef FEAT_DIFF
1552 || (wp->w_topline == wp->w_lines[0].wl_lnum
1553 && wp->w_topfill > wp->w_old_topfill)
1554#endif
1555 ))
1556 {
1557 /*
1558 * New topline is above old topline: May scroll down.
1559 */
1560#ifdef FEAT_FOLDING
1561 if (hasAnyFolding(wp))
1562 {
1563 linenr_T ln;
1564
1565 /* count the number of lines we are off, counting a sequence
1566 * of folded lines as one */
1567 j = 0;
1568 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1569 {
1570 ++j;
1571 if (j >= wp->w_height - 2)
1572 break;
1573 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1574 }
1575 }
1576 else
1577#endif
1578 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1579 if (j < wp->w_height - 2) /* not too far off */
1580 {
1581 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1582#ifdef FEAT_DIFF
1583 /* insert extra lines for previously invisible filler lines */
1584 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1585 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1586 - wp->w_old_topfill;
1587#endif
1588 if (i < wp->w_height - 2) /* less than a screen off */
1589 {
1590 /*
1591 * Try to insert the correct number of lines.
1592 * If not the last window, delete the lines at the bottom.
1593 * win_ins_lines may fail when the terminal can't do it.
1594 */
1595 if (i > 0)
1596 check_for_delay(FALSE);
1597 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1598 {
1599 if (wp->w_lines_valid != 0)
1600 {
1601 /* Need to update rows that are new, stop at the
1602 * first one that scrolled down. */
1603 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
1606 /* Move the entries that were scrolled, disable
1607 * the entries for the lines to be redrawn. */
1608 if ((wp->w_lines_valid += j) > wp->w_height)
1609 wp->w_lines_valid = wp->w_height;
1610 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1611 wp->w_lines[idx] = wp->w_lines[idx - j];
1612 while (idx >= 0)
1613 wp->w_lines[idx--].wl_valid = FALSE;
1614 }
1615 }
1616 else
1617 mid_start = 0; /* redraw all lines */
1618 }
1619 else
1620 mid_start = 0; /* redraw all lines */
1621 }
1622 else
1623 mid_start = 0; /* redraw all lines */
1624 }
1625 else
1626 {
1627 /*
1628 * New topline is at or below old topline: May scroll up.
1629 * When topline didn't change, find first entry in w_lines[] that
1630 * needs updating.
1631 */
1632
1633 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1634 j = -1;
1635 row = 0;
1636 for (i = 0; i < wp->w_lines_valid; i++)
1637 {
1638 if (wp->w_lines[i].wl_valid
1639 && wp->w_lines[i].wl_lnum == wp->w_topline)
1640 {
1641 j = i;
1642 break;
1643 }
1644 row += wp->w_lines[i].wl_size;
1645 }
1646 if (j == -1)
1647 {
1648 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1649 * lines */
1650 mid_start = 0;
1651 }
1652 else
1653 {
1654 /*
1655 * Try to delete the correct number of lines.
1656 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1657 */
1658#ifdef FEAT_DIFF
1659 /* If the topline didn't change, delete old filler lines,
1660 * otherwise delete filler lines of the new topline... */
1661 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1662 row += wp->w_old_topfill;
1663 else
1664 row += diff_check_fill(wp, wp->w_topline);
1665 /* ... but don't delete new filler lines. */
1666 row -= wp->w_topfill;
1667#endif
1668 if (row > 0)
1669 {
1670 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001671 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1672 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 bot_start = wp->w_height - row;
1674 else
1675 mid_start = 0; /* redraw all lines */
1676 }
1677 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1678 {
1679 /*
1680 * Skip the lines (below the deleted lines) that are still
1681 * valid and don't need redrawing. Copy their info
1682 * upwards, to compensate for the deleted lines. Set
1683 * bot_start to the first row that needs redrawing.
1684 */
1685 bot_start = 0;
1686 idx = 0;
1687 for (;;)
1688 {
1689 wp->w_lines[idx] = wp->w_lines[j];
1690 /* stop at line that didn't fit, unless it is still
1691 * valid (no lines deleted) */
1692 if (row > 0 && bot_start + row
1693 + (int)wp->w_lines[j].wl_size > wp->w_height)
1694 {
1695 wp->w_lines_valid = idx + 1;
1696 break;
1697 }
1698 bot_start += wp->w_lines[idx++].wl_size;
1699
1700 /* stop at the last valid entry in w_lines[].wl_size */
1701 if (++j >= wp->w_lines_valid)
1702 {
1703 wp->w_lines_valid = idx;
1704 break;
1705 }
1706 }
1707#ifdef FEAT_DIFF
1708 /* Correct the first entry for filler lines at the top
1709 * when it won't get updated below. */
1710 if (wp->w_p_diff && bot_start > 0)
1711 wp->w_lines[0].wl_size =
1712 plines_win_nofill(wp, wp->w_topline, TRUE)
1713 + wp->w_topfill;
1714#endif
1715 }
1716 }
1717 }
1718
1719 /* When starting redraw in the first line, redraw all lines. When
1720 * there is only one window it's probably faster to clear the screen
1721 * first. */
1722 if (mid_start == 0)
1723 {
1724 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001725 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001726 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001727 /* Clear the screen when it was not done by win_del_lines() or
1728 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1729 * then. */
1730 if (screen_cleared != TRUE)
1731 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001732 /* The screen was cleared, redraw the tab pages line. */
1733 if (redraw_tabline)
1734 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001737
1738 /* When win_del_lines() or win_ins_lines() caused the screen to be
1739 * cleared (only happens for the first window) or when screenclear()
1740 * was called directly above, "must_redraw" will have been set to
1741 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1742 if (screen_cleared == TRUE)
1743 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 }
1745 else
1746 {
1747 /* Not VALID or INVERTED: redraw all lines. */
1748 mid_start = 0;
1749 mid_end = wp->w_height;
1750 }
1751
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001752 if (type == SOME_VALID)
1753 {
1754 /* SOME_VALID: redraw all lines. */
1755 mid_start = 0;
1756 mid_end = wp->w_height;
1757 type = NOT_VALID;
1758 }
1759
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 /* check if we are updating or removing the inverted part */
1761 if ((VIsual_active && buf == curwin->w_buffer)
1762 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1763 {
1764 linenr_T from, to;
1765
1766 if (VIsual_active)
1767 {
1768 if (VIsual_active
1769 && (VIsual_mode != wp->w_old_visual_mode
1770 || type == INVERTED_ALL))
1771 {
1772 /*
1773 * If the type of Visual selection changed, redraw the whole
1774 * selection. Also when the ownership of the X selection is
1775 * gained or lost.
1776 */
1777 if (curwin->w_cursor.lnum < VIsual.lnum)
1778 {
1779 from = curwin->w_cursor.lnum;
1780 to = VIsual.lnum;
1781 }
1782 else
1783 {
1784 from = VIsual.lnum;
1785 to = curwin->w_cursor.lnum;
1786 }
1787 /* redraw more when the cursor moved as well */
1788 if (wp->w_old_cursor_lnum < from)
1789 from = wp->w_old_cursor_lnum;
1790 if (wp->w_old_cursor_lnum > to)
1791 to = wp->w_old_cursor_lnum;
1792 if (wp->w_old_visual_lnum < from)
1793 from = wp->w_old_visual_lnum;
1794 if (wp->w_old_visual_lnum > to)
1795 to = wp->w_old_visual_lnum;
1796 }
1797 else
1798 {
1799 /*
1800 * Find the line numbers that need to be updated: The lines
1801 * between the old cursor position and the current cursor
1802 * position. Also check if the Visual position changed.
1803 */
1804 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1805 {
1806 from = curwin->w_cursor.lnum;
1807 to = wp->w_old_cursor_lnum;
1808 }
1809 else
1810 {
1811 from = wp->w_old_cursor_lnum;
1812 to = curwin->w_cursor.lnum;
1813 if (from == 0) /* Visual mode just started */
1814 from = to;
1815 }
1816
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001817 if (VIsual.lnum != wp->w_old_visual_lnum
1818 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 {
1820 if (wp->w_old_visual_lnum < from
1821 && wp->w_old_visual_lnum != 0)
1822 from = wp->w_old_visual_lnum;
1823 if (wp->w_old_visual_lnum > to)
1824 to = wp->w_old_visual_lnum;
1825 if (VIsual.lnum < from)
1826 from = VIsual.lnum;
1827 if (VIsual.lnum > to)
1828 to = VIsual.lnum;
1829 }
1830 }
1831
1832 /*
1833 * If in block mode and changed column or curwin->w_curswant:
1834 * update all lines.
1835 * First compute the actual start and end column.
1836 */
1837 if (VIsual_mode == Ctrl_V)
1838 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001839 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001840#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001841 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaar404406a2014-10-09 13:24:43 +02001843 if (curwin->w_p_lbr)
1844 ve_flags = VE_ALL;
1845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001847#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001848 ve_flags = save_ve_flags;
1849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 ++toc;
1851 if (curwin->w_curswant == MAXCOL)
1852 toc = MAXCOL;
1853
1854 if (fromc != wp->w_old_cursor_fcol
1855 || toc != wp->w_old_cursor_lcol)
1856 {
1857 if (from > VIsual.lnum)
1858 from = VIsual.lnum;
1859 if (to < VIsual.lnum)
1860 to = VIsual.lnum;
1861 }
1862 wp->w_old_cursor_fcol = fromc;
1863 wp->w_old_cursor_lcol = toc;
1864 }
1865 }
1866 else
1867 {
1868 /* Use the line numbers of the old Visual area. */
1869 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1870 {
1871 from = wp->w_old_cursor_lnum;
1872 to = wp->w_old_visual_lnum;
1873 }
1874 else
1875 {
1876 from = wp->w_old_visual_lnum;
1877 to = wp->w_old_cursor_lnum;
1878 }
1879 }
1880
1881 /*
1882 * There is no need to update lines above the top of the window.
1883 */
1884 if (from < wp->w_topline)
1885 from = wp->w_topline;
1886
1887 /*
1888 * If we know the value of w_botline, use it to restrict the update to
1889 * the lines that are visible in the window.
1890 */
1891 if (wp->w_valid & VALID_BOTLINE)
1892 {
1893 if (from >= wp->w_botline)
1894 from = wp->w_botline - 1;
1895 if (to >= wp->w_botline)
1896 to = wp->w_botline - 1;
1897 }
1898
1899 /*
1900 * Find the minimal part to be updated.
1901 * Watch out for scrolling that made entries in w_lines[] invalid.
1902 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1903 * top_end; need to redraw from top_end to the "to" line.
1904 * A middle mouse click with a Visual selection may change the text
1905 * above the Visual area and reset wl_valid, do count these for
1906 * mid_end (in srow).
1907 */
1908 if (mid_start > 0)
1909 {
1910 lnum = wp->w_topline;
1911 idx = 0;
1912 srow = 0;
1913 if (scrolled_down)
1914 mid_start = top_end;
1915 else
1916 mid_start = 0;
1917 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1918 {
1919 if (wp->w_lines[idx].wl_valid)
1920 mid_start += wp->w_lines[idx].wl_size;
1921 else if (!scrolled_down)
1922 srow += wp->w_lines[idx].wl_size;
1923 ++idx;
1924# ifdef FEAT_FOLDING
1925 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1926 lnum = wp->w_lines[idx].wl_lnum;
1927 else
1928# endif
1929 ++lnum;
1930 }
1931 srow += mid_start;
1932 mid_end = wp->w_height;
1933 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1934 {
1935 if (wp->w_lines[idx].wl_valid
1936 && wp->w_lines[idx].wl_lnum >= to + 1)
1937 {
1938 /* Only update until first row of this line */
1939 mid_end = srow;
1940 break;
1941 }
1942 srow += wp->w_lines[idx].wl_size;
1943 }
1944 }
1945 }
1946
1947 if (VIsual_active && buf == curwin->w_buffer)
1948 {
1949 wp->w_old_visual_mode = VIsual_mode;
1950 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1951 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001952 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 wp->w_old_curswant = curwin->w_curswant;
1954 }
1955 else
1956 {
1957 wp->w_old_visual_mode = 0;
1958 wp->w_old_cursor_lnum = 0;
1959 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001960 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
1963#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1964 /* reset got_int, otherwise regexp won't work */
1965 save_got_int = got_int;
1966 got_int = 0;
1967#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001968#ifdef SYN_TIME_LIMIT
1969 /* Set the time limit to 'redrawtime'. */
1970 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001971 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973#ifdef FEAT_FOLDING
1974 win_foldinfo.fi_level = 0;
1975#endif
1976
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001977#ifdef FEAT_MENU
1978 /*
1979 * Draw the window toolbar, if there is one.
1980 * TODO: only when needed.
1981 */
1982 if (winbar_height(wp) > 0)
1983 redraw_win_toolbar(wp);
1984#endif
1985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 /*
1987 * Update all the window rows.
1988 */
1989 idx = 0; /* first entry in w_lines[].wl_size */
1990 row = 0;
1991 srow = 0;
1992 lnum = wp->w_topline; /* first line shown in window */
1993 for (;;)
1994 {
1995 /* stop updating when reached the end of the window (check for _past_
1996 * the end of the window is at the end of the loop) */
1997 if (row == wp->w_height)
1998 {
1999 didline = TRUE;
2000 break;
2001 }
2002
2003 /* stop updating when hit the end of the file */
2004 if (lnum > buf->b_ml.ml_line_count)
2005 {
2006 eof = TRUE;
2007 break;
2008 }
2009
2010 /* Remember the starting row of the line that is going to be dealt
2011 * with. It is used further down when the line doesn't fit. */
2012 srow = row;
2013
2014 /*
2015 * Update a line when it is in an area that needs updating, when it
2016 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002017 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 * win_del_lines(), check if the current line includes it.
2019 * When syntax folding is being used, the saved syntax states will
2020 * already have been updated, we can't see where the syntax state is
2021 * the same again, just update until the end of the window.
2022 */
2023 if (row < top_end
2024 || (row >= mid_start && row < mid_end)
2025#ifdef FEAT_SEARCH_EXTRA
2026 || top_to_mod
2027#endif
2028 || idx >= wp->w_lines_valid
2029 || (row + wp->w_lines[idx].wl_size > bot_start)
2030 || (mod_top != 0
2031 && (lnum == mod_top
2032 || (lnum >= mod_top
2033 && (lnum < mod_bot
2034#ifdef FEAT_SYN_HL
2035 || did_update == DID_FOLD
2036 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02002037 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 && (
2039# ifdef FEAT_FOLDING
2040 (foldmethodIsSyntax(wp)
2041 && hasAnyFolding(wp)) ||
2042# endif
2043 syntax_check_changed(lnum)))
2044#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002045#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02002046 /* match in fixed position might need redraw
2047 * if lines were inserted or deleted */
2048 || (wp->w_match_head != NULL
2049 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 )))))
2052 {
2053#ifdef FEAT_SEARCH_EXTRA
2054 if (lnum == mod_top)
2055 top_to_mod = FALSE;
2056#endif
2057
2058 /*
2059 * When at start of changed lines: May scroll following lines
2060 * up or down to minimize redrawing.
2061 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002062 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 */
2064 if (lnum == mod_top
2065 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002066 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 {
2068 int old_rows = 0;
2069 int new_rows = 0;
2070 int xtra_rows;
2071 linenr_T l;
2072
2073 /* Count the old number of window rows, using w_lines[], which
2074 * should still contain the sizes for the lines as they are
2075 * currently displayed. */
2076 for (i = idx; i < wp->w_lines_valid; ++i)
2077 {
2078 /* Only valid lines have a meaningful wl_lnum. Invalid
2079 * lines are part of the changed area. */
2080 if (wp->w_lines[i].wl_valid
2081 && wp->w_lines[i].wl_lnum == mod_bot)
2082 break;
2083 old_rows += wp->w_lines[i].wl_size;
2084#ifdef FEAT_FOLDING
2085 if (wp->w_lines[i].wl_valid
2086 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2087 {
2088 /* Must have found the last valid entry above mod_bot.
2089 * Add following invalid entries. */
2090 ++i;
2091 while (i < wp->w_lines_valid
2092 && !wp->w_lines[i].wl_valid)
2093 old_rows += wp->w_lines[i++].wl_size;
2094 break;
2095 }
2096#endif
2097 }
2098
2099 if (i >= wp->w_lines_valid)
2100 {
2101 /* We can't find a valid line below the changed lines,
2102 * need to redraw until the end of the window.
2103 * Inserting/deleting lines has no use. */
2104 bot_start = 0;
2105 }
2106 else
2107 {
2108 /* Able to count old number of rows: Count new window
2109 * rows, and may insert/delete lines */
2110 j = idx;
2111 for (l = lnum; l < mod_bot; ++l)
2112 {
2113#ifdef FEAT_FOLDING
2114 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2115 ++new_rows;
2116 else
2117#endif
2118#ifdef FEAT_DIFF
2119 if (l == wp->w_topline)
2120 new_rows += plines_win_nofill(wp, l, TRUE)
2121 + wp->w_topfill;
2122 else
2123#endif
2124 new_rows += plines_win(wp, l, TRUE);
2125 ++j;
2126 if (new_rows > wp->w_height - row - 2)
2127 {
2128 /* it's getting too much, must redraw the rest */
2129 new_rows = 9999;
2130 break;
2131 }
2132 }
2133 xtra_rows = new_rows - old_rows;
2134 if (xtra_rows < 0)
2135 {
2136 /* May scroll text up. If there is not enough
2137 * remaining text or scrolling fails, must redraw the
2138 * rest. If scrolling works, must redraw the text
2139 * below the scrolled text. */
2140 if (row - xtra_rows >= wp->w_height - 2)
2141 mod_bot = MAXLNUM;
2142 else
2143 {
2144 check_for_delay(FALSE);
2145 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002146 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 mod_bot = MAXLNUM;
2148 else
2149 bot_start = wp->w_height + xtra_rows;
2150 }
2151 }
2152 else if (xtra_rows > 0)
2153 {
2154 /* May scroll text down. If there is not enough
2155 * remaining text of scrolling fails, must redraw the
2156 * rest. */
2157 if (row + xtra_rows >= wp->w_height - 2)
2158 mod_bot = MAXLNUM;
2159 else
2160 {
2161 check_for_delay(FALSE);
2162 if (win_ins_lines(wp, row + old_rows,
2163 xtra_rows, FALSE, FALSE) == FAIL)
2164 mod_bot = MAXLNUM;
2165 else if (top_end > row + old_rows)
2166 /* Scrolled the part at the top that requires
2167 * updating down. */
2168 top_end += xtra_rows;
2169 }
2170 }
2171
2172 /* When not updating the rest, may need to move w_lines[]
2173 * entries. */
2174 if (mod_bot != MAXLNUM && i != j)
2175 {
2176 if (j < i)
2177 {
2178 int x = row + new_rows;
2179
2180 /* move entries in w_lines[] upwards */
2181 for (;;)
2182 {
2183 /* stop at last valid entry in w_lines[] */
2184 if (i >= wp->w_lines_valid)
2185 {
2186 wp->w_lines_valid = j;
2187 break;
2188 }
2189 wp->w_lines[j] = wp->w_lines[i];
2190 /* stop at a line that won't fit */
2191 if (x + (int)wp->w_lines[j].wl_size
2192 > wp->w_height)
2193 {
2194 wp->w_lines_valid = j + 1;
2195 break;
2196 }
2197 x += wp->w_lines[j++].wl_size;
2198 ++i;
2199 }
2200 if (bot_start > x)
2201 bot_start = x;
2202 }
2203 else /* j > i */
2204 {
2205 /* move entries in w_lines[] downwards */
2206 j -= i;
2207 wp->w_lines_valid += j;
2208 if (wp->w_lines_valid > wp->w_height)
2209 wp->w_lines_valid = wp->w_height;
2210 for (i = wp->w_lines_valid; i - j >= idx; --i)
2211 wp->w_lines[i] = wp->w_lines[i - j];
2212
2213 /* The w_lines[] entries for inserted lines are
2214 * now invalid, but wl_size may be used above.
2215 * Reset to zero. */
2216 while (i >= idx)
2217 {
2218 wp->w_lines[i].wl_size = 0;
2219 wp->w_lines[i--].wl_valid = FALSE;
2220 }
2221 }
2222 }
2223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225
2226#ifdef FEAT_FOLDING
2227 /*
2228 * When lines are folded, display one line for all of them.
2229 * Otherwise, display normally (can be several display lines when
2230 * 'wrap' is on).
2231 */
2232 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2233 if (fold_count != 0)
2234 {
2235 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2236 ++row;
2237 --fold_count;
2238 wp->w_lines[idx].wl_folded = TRUE;
2239 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2240# ifdef FEAT_SYN_HL
2241 did_update = DID_FOLD;
2242# endif
2243 }
2244 else
2245#endif
2246 if (idx < wp->w_lines_valid
2247 && wp->w_lines[idx].wl_valid
2248 && wp->w_lines[idx].wl_lnum == lnum
2249 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002250 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar17146962019-05-30 00:12:11 +02002251#ifdef FEAT_TEXT_PROP
2252 && !bt_popup(wp->w_buffer)
2253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 && srow + wp->w_lines[idx].wl_size > wp->w_height
2255#ifdef FEAT_DIFF
2256 && diff_check_fill(wp, lnum) == 0
2257#endif
2258 )
2259 {
2260 /* This line is not going to fit. Don't draw anything here,
2261 * will draw "@ " lines below. */
2262 row = wp->w_height + 1;
2263 }
2264 else
2265 {
2266#ifdef FEAT_SEARCH_EXTRA
2267 prepare_search_hl(wp, lnum);
2268#endif
2269#ifdef FEAT_SYN_HL
2270 /* Let the syntax stuff know we skipped a few lines. */
2271 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002272 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 syntax_end_parsing(syntax_last_parsed + 1);
2274#endif
2275
2276 /*
2277 * Display one line.
2278 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002279 row = win_line(wp, lnum, srow, wp->w_height,
2280 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281
2282#ifdef FEAT_FOLDING
2283 wp->w_lines[idx].wl_folded = FALSE;
2284 wp->w_lines[idx].wl_lastlnum = lnum;
2285#endif
2286#ifdef FEAT_SYN_HL
2287 did_update = DID_LINE;
2288 syntax_last_parsed = lnum;
2289#endif
2290 }
2291
2292 wp->w_lines[idx].wl_lnum = lnum;
2293 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002294
2295 /* Past end of the window or end of the screen. Note that after
2296 * resizing wp->w_height may be end up too big. That's a problem
2297 * elsewhere, but prevent a crash here. */
2298 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
2300 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002301 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2303 ++idx;
2304 break;
2305 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002306 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 wp->w_lines[idx].wl_size = row - srow;
2308 ++idx;
2309#ifdef FEAT_FOLDING
2310 lnum += fold_count + 1;
2311#else
2312 ++lnum;
2313#endif
2314 }
2315 else
2316 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002317 if (wp->w_p_rnu)
2318 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002319#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002320 // 'relativenumber' set: The text doesn't need to be drawn, but
2321 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002322 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2323 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002324 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002325 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002326#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002327 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002328 }
2329
2330 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 row += wp->w_lines[idx++].wl_size;
2332 if (row > wp->w_height) /* past end of screen */
2333 break;
2334#ifdef FEAT_FOLDING
2335 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2336#else
2337 ++lnum;
2338#endif
2339#ifdef FEAT_SYN_HL
2340 did_update = DID_NONE;
2341#endif
2342 }
2343
2344 if (lnum > buf->b_ml.ml_line_count)
2345 {
2346 eof = TRUE;
2347 break;
2348 }
2349 }
2350 /*
2351 * End of loop over all window lines.
2352 */
2353
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002354#ifdef FEAT_VTP
2355 /* Rewrite the character at the end of the screen line. */
2356 if (use_vtp())
2357 {
2358 int i;
2359
2360 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002361 if (enc_utf8)
2362 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2363 LineOffset[i] + screen_Columns) > 1)
2364 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2365 else
2366 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2367 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002368 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2369 }
2370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
2372 if (idx > wp->w_lines_valid)
2373 wp->w_lines_valid = idx;
2374
2375#ifdef FEAT_SYN_HL
2376 /*
2377 * Let the syntax stuff know we stop parsing here.
2378 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002379 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 syntax_end_parsing(syntax_last_parsed + 1);
2381#endif
2382
2383 /*
2384 * If we didn't hit the end of the file, and we didn't finish the last
2385 * line we were working on, then the line didn't fit.
2386 */
2387 wp->w_empty_rows = 0;
2388#ifdef FEAT_DIFF
2389 wp->w_filler_rows = 0;
2390#endif
2391 if (!eof && !didline)
2392 {
2393 if (lnum == wp->w_topline)
2394 {
2395 /*
2396 * Single line that does not fit!
2397 * Don't overwrite it, it can be edited.
2398 */
2399 wp->w_botline = lnum + 1;
2400 }
2401#ifdef FEAT_DIFF
2402 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2403 {
2404 /* Window ends in filler lines. */
2405 wp->w_botline = lnum;
2406 wp->w_filler_rows = wp->w_height - srow;
2407 }
2408#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002409#ifdef FEAT_TEXT_PROP
2410 else if (bt_popup(wp->w_buffer))
2411 {
2412 // popup line that doesn't fit is left as-is
2413 wp->w_botline = lnum;
2414 }
2415#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002416 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2417 {
2418 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2419
2420 /*
2421 * Last line isn't finished: Display "@@@" in the last screen line.
2422 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002423 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002424 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002425 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002426 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002427 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002428 set_empty_rows(wp, srow);
2429 wp->w_botline = lnum;
2430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2432 {
2433 /*
2434 * Last line isn't finished: Display "@@@" at the end.
2435 */
2436 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2437 W_WINROW(wp) + wp->w_height,
2438 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002439 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 set_empty_rows(wp, srow);
2441 wp->w_botline = lnum;
2442 }
2443 else
2444 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002445 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 wp->w_botline = lnum;
2447 }
2448 }
2449 else
2450 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 if (eof) /* we hit the end of the file */
2453 {
2454 wp->w_botline = buf->b_ml.ml_line_count + 1;
2455#ifdef FEAT_DIFF
2456 j = diff_check_fill(wp, wp->w_botline);
2457 if (j > 0 && !wp->w_botfill)
2458 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002459 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 if (char2cells(fill_diff) > 1)
2461 i = '-';
2462 else
2463 i = fill_diff;
2464 if (row + j > wp->w_height)
2465 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002466 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 row += j;
2468 }
2469#endif
2470 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002471 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 wp->w_botline = lnum;
2473
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002474 // Make sure the rest of the screen is blank
2475 // put '~'s on rows that aren't part of the file.
Bram Moolenaar17146962019-05-30 00:12:11 +02002476 win_draw_end(wp,
2477#ifdef FEAT_TEXT_PROP
2478 bt_popup(wp->w_buffer) ? ' ' :
2479#endif
2480 '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
2482
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002483#ifdef SYN_TIME_LIMIT
2484 syn_set_timeout(NULL);
2485#endif
2486
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 /* Reset the type of redrawing required, the window has been updated. */
2488 wp->w_redr_type = 0;
2489#ifdef FEAT_DIFF
2490 wp->w_old_topfill = wp->w_topfill;
2491 wp->w_old_botfill = wp->w_botfill;
2492#endif
2493
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002494 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
2496 /*
2497 * There is a trick with w_botline. If we invalidate it on each
2498 * change that might modify it, this will cause a lot of expensive
2499 * calls to plines() in update_topline() each time. Therefore the
2500 * value of w_botline is often approximated, and this value is used to
2501 * compute the value of w_topline. If the value of w_botline was
2502 * wrong, check that the value of w_topline is correct (cursor is on
2503 * the visible part of the text). If it's not, we need to redraw
2504 * again. Mostly this just means scrolling up a few lines, so it
2505 * doesn't look too bad. Only do this for the current window (where
2506 * changes are relevant).
2507 */
2508 wp->w_valid |= VALID_BOTLINE;
2509 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2510 {
2511 recursive = TRUE;
2512 curwin->w_valid &= ~VALID_TOPLINE;
2513 update_topline(); /* may invalidate w_botline again */
2514 if (must_redraw != 0)
2515 {
2516 /* Don't update for changes in buffer again. */
2517 i = curbuf->b_mod_set;
2518 curbuf->b_mod_set = FALSE;
2519 win_update(curwin);
2520 must_redraw = 0;
2521 curbuf->b_mod_set = i;
2522 }
2523 recursive = FALSE;
2524 }
2525 }
2526
2527#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2528 /* restore got_int, unless CTRL-C was hit while redrawing */
2529 if (!got_int)
2530 got_int = save_got_int;
2531#endif
2532}
2533
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002535 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2536 * Return the new offset.
2537 */
2538 static int
2539screen_fill_end(
2540 win_T *wp,
2541 int c1,
2542 int c2,
2543 int off,
2544 int width,
2545 int row,
2546 int endrow,
2547 int attr)
2548{
2549 int nn = off + width;
2550
2551 if (nn > wp->w_width)
2552 nn = wp->w_width;
2553#ifdef FEAT_RIGHTLEFT
2554 if (wp->w_p_rl)
2555 {
2556 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2557 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2558 c1, c2, attr);
2559 }
2560 else
2561#endif
2562 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2563 wp->w_wincol + off, (int)wp->w_wincol + nn,
2564 c1, c2, attr);
2565 return nn;
2566}
2567
2568/*
2569 * Clear lines near the end the window and mark the unused lines with "c1".
2570 * use "c2" as the filler character.
2571 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 */
2573 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002574win_draw_end(
2575 win_T *wp,
2576 int c1,
2577 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002578 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002579 int row,
2580 int endrow,
2581 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002584 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002585 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002586
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002587 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002588
2589 if (draw_margin)
2590 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002591#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002592 int fdc = compute_foldcolumn(wp, 0);
2593
2594 if (fdc > 0)
2595 // draw the fold column
2596 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002597 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002598#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002599#ifdef FEAT_SIGNS
2600 if (signcolumn_on(wp))
2601 // draw the sign column
2602 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002603 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002604#endif
2605 if ((wp->w_p_nu || wp->w_p_rnu)
2606 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2607 // draw the number column
2608 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002609 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
2612#ifdef FEAT_RIGHTLEFT
2613 if (wp->w_p_rl)
2614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002616 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002617 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002619 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002620 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 }
2622 else
2623#endif
2624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002626 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002627 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002629
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 set_empty_rows(wp, row);
2631}
2632
Bram Moolenaar1a384422010-07-14 19:53:30 +02002633#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002634/*
2635 * Advance **color_cols and return TRUE when there are columns to draw.
2636 */
2637 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002638advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002639{
2640 while (**color_cols >= 0 && vcol > **color_cols)
2641 ++*color_cols;
2642 return (**color_cols >= 0);
2643}
2644#endif
2645
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002646#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2647/*
2648 * Copy "text" to ScreenLines using "attr".
2649 * Returns the next screen column.
2650 */
2651 static int
2652text_to_screenline(win_T *wp, char_u *text, int col)
2653{
2654 int off = (int)(current_ScreenLine - ScreenLines);
2655
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002656 if (has_mbyte)
2657 {
2658 int cells;
2659 int u8c, u8cc[MAX_MCO];
2660 int i;
2661 int idx;
2662 int c_len;
2663 char_u *p;
2664# ifdef FEAT_ARABIC
2665 int prev_c = 0; /* previous Arabic character */
2666 int prev_c1 = 0; /* first composing char for prev_c */
2667# endif
2668
2669# ifdef FEAT_RIGHTLEFT
2670 if (wp->w_p_rl)
2671 idx = off;
2672 else
2673# endif
2674 idx = off + col;
2675
2676 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2677 for (p = text; *p != NUL; )
2678 {
2679 cells = (*mb_ptr2cells)(p);
2680 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002681 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002682# ifdef FEAT_RIGHTLEFT
2683 - (wp->w_p_rl ? col : 0)
2684# endif
2685 )
2686 break;
2687 ScreenLines[idx] = *p;
2688 if (enc_utf8)
2689 {
2690 u8c = utfc_ptr2char(p, u8cc);
2691 if (*p < 0x80 && u8cc[0] == 0)
2692 {
2693 ScreenLinesUC[idx] = 0;
2694#ifdef FEAT_ARABIC
2695 prev_c = u8c;
2696#endif
2697 }
2698 else
2699 {
2700#ifdef FEAT_ARABIC
2701 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2702 {
2703 /* Do Arabic shaping. */
2704 int pc, pc1, nc;
2705 int pcc[MAX_MCO];
2706 int firstbyte = *p;
2707
2708 /* The idea of what is the previous and next
2709 * character depends on 'rightleft'. */
2710 if (wp->w_p_rl)
2711 {
2712 pc = prev_c;
2713 pc1 = prev_c1;
2714 nc = utf_ptr2char(p + c_len);
2715 prev_c1 = u8cc[0];
2716 }
2717 else
2718 {
2719 pc = utfc_ptr2char(p + c_len, pcc);
2720 nc = prev_c;
2721 pc1 = pcc[0];
2722 }
2723 prev_c = u8c;
2724
2725 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2726 pc, pc1, nc);
2727 ScreenLines[idx] = firstbyte;
2728 }
2729 else
2730 prev_c = u8c;
2731#endif
2732 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002733 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002734 for (i = 0; i < Screen_mco; ++i)
2735 {
2736 ScreenLinesC[i][idx] = u8cc[i];
2737 if (u8cc[i] == 0)
2738 break;
2739 }
2740 }
2741 if (cells > 1)
2742 ScreenLines[idx + 1] = 0;
2743 }
2744 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2745 /* double-byte single width character */
2746 ScreenLines2[idx] = p[1];
2747 else if (cells > 1)
2748 /* double-width character */
2749 ScreenLines[idx + 1] = p[1];
2750 col += cells;
2751 idx += cells;
2752 p += c_len;
2753 }
2754 }
2755 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002756 {
2757 int len = (int)STRLEN(text);
2758
Bram Moolenaar02631462017-09-22 15:20:32 +02002759 if (len > wp->w_width - col)
2760 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002761 if (len > 0)
2762 {
2763#ifdef FEAT_RIGHTLEFT
2764 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002765 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002766 else
2767#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002768 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002769 col += len;
2770 }
2771 }
2772 return col;
2773}
2774#endif
2775
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776#ifdef FEAT_FOLDING
2777/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002778 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2779 * space is available for window "wp", minus "col".
2780 */
2781 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002782compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002783{
2784 int fdc = wp->w_p_fdc;
2785 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002786 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002787
2788 if (fdc > wwidth - (col + wmw))
2789 fdc = wwidth - (col + wmw);
2790 return fdc;
2791}
2792
2793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 * Display one folded line.
2795 */
2796 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002797fold_line(
2798 win_T *wp,
2799 long fold_count,
2800 foldinfo_T *foldinfo,
2801 linenr_T lnum,
2802 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002804 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 pos_T *top, *bot;
2806 linenr_T lnume = lnum + fold_count - 1;
2807 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002808 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 int col;
2811 int txtcol;
2812 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002813 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
2815 /* Build the fold line:
2816 * 1. Add the cmdwin_type for the command-line window
2817 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002818 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 * 4. Compose the text
2820 * 5. Add the text
2821 * 6. set highlighting for the Visual area an other text
2822 */
2823 col = 0;
2824
2825 /*
2826 * 1. Add the cmdwin_type for the command-line window
2827 * Ignores 'rightleft', this window is never right-left.
2828 */
2829#ifdef FEAT_CMDWIN
2830 if (cmdwin_type != 0 && wp == curwin)
2831 {
2832 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002833 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 if (enc_utf8)
2835 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 ++col;
2837 }
2838#endif
2839
2840 /*
2841 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002842 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002844 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 if (fdc > 0)
2846 {
2847 fill_foldcolumn(buf, wp, TRUE, lnum);
2848#ifdef FEAT_RIGHTLEFT
2849 if (wp->w_p_rl)
2850 {
2851 int i;
2852
Bram Moolenaar02631462017-09-22 15:20:32 +02002853 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002854 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 /* reverse the fold column */
2856 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002857 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
2859 else
2860#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002861 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 col += fdc;
2863 }
2864
2865#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002866# define RL_MEMSET(p, v, l) \
2867 do { \
2868 if (wp->w_p_rl) \
2869 for (ri = 0; ri < l; ++ri) \
2870 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2871 else \
2872 for (ri = 0; ri < l; ++ri) \
2873 ScreenAttrs[off + (p) + ri] = v; \
2874 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002876# define RL_MEMSET(p, v, l) \
2877 do { \
2878 for (ri = 0; ri < l; ++ri) \
2879 ScreenAttrs[off + (p) + ri] = v; \
2880 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881#endif
2882
Bram Moolenaar64486672010-05-16 15:46:46 +02002883 /* Set all attributes of the 'number' or 'relativenumber' column and the
2884 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002885 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886
2887#ifdef FEAT_SIGNS
2888 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002889 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002891 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 if (len > 0)
2893 {
2894 if (len > 2)
2895 len = 2;
2896# ifdef FEAT_RIGHTLEFT
2897 if (wp->w_p_rl)
2898 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002899 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002900 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 else
2902# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002903 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 col += len;
2905 }
2906 }
2907#endif
2908
2909 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002910 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002912 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002914 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 if (len > 0)
2916 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002917 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002918 long num;
2919 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002920
2921 if (len > w + 1)
2922 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002923
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002924 if (wp->w_p_nu && !wp->w_p_rnu)
2925 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002926 num = (long)lnum;
2927 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002928 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002929 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002930 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002931 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002932 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002933 /* 'number' + 'relativenumber': cursor line shows absolute
2934 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002935 num = lnum;
2936 fmt = "%-*ld ";
2937 }
2938 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002939
Bram Moolenaar700e7342013-01-30 12:31:36 +01002940 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941#ifdef FEAT_RIGHTLEFT
2942 if (wp->w_p_rl)
2943 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002944 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002945 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 else
2947#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002948 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 col += len;
2950 }
2951 }
2952
2953 /*
2954 * 4. Compose the folded-line string with 'foldtext', if set.
2955 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002956 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
2958 txtcol = col; /* remember where text starts */
2959
2960 /*
2961 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2962 * Right-left text is put in columns 0 - number-col, normal text is put
2963 * in columns number-col - window-width.
2964 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002965 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
2967 /* Fill the rest of the line with the fold filler */
2968#ifdef FEAT_RIGHTLEFT
2969 if (wp->w_p_rl)
2970 col -= txtcol;
2971#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002972 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973#ifdef FEAT_RIGHTLEFT
2974 - (wp->w_p_rl ? txtcol : 0)
2975#endif
2976 )
2977 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 if (enc_utf8)
2979 {
2980 if (fill_fold >= 0x80)
2981 {
2982 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002983 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002984 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 }
2986 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002987 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002989 ScreenLines[off + col] = fill_fold;
2990 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002991 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002993 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002994 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996
2997 if (text != buf)
2998 vim_free(text);
2999
3000 /*
3001 * 6. set highlighting for the Visual area an other text.
3002 * If all folded lines are in the Visual area, highlight the line.
3003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3005 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003006 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 {
3008 /* Visual is after curwin->w_cursor */
3009 top = &curwin->w_cursor;
3010 bot = &VIsual;
3011 }
3012 else
3013 {
3014 /* Visual is before curwin->w_cursor */
3015 top = &VIsual;
3016 bot = &curwin->w_cursor;
3017 }
3018 if (lnum >= top->lnum
3019 && lnume <= bot->lnum
3020 && (VIsual_mode != 'v'
3021 || ((lnum > top->lnum
3022 || (lnum == top->lnum
3023 && top->col == 0))
3024 && (lnume < bot->lnum
3025 || (lnume == bot->lnum
3026 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003027 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 {
3029 if (VIsual_mode == Ctrl_V)
3030 {
3031 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02003032 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02003034 if (wp->w_old_cursor_lcol != MAXCOL
3035 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02003036 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 len = wp->w_old_cursor_lcol;
3038 else
Bram Moolenaar02631462017-09-22 15:20:32 +02003039 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003040 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003041 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043 }
3044 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003045 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02003047 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
3050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003052#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003053 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
3054 if (wp->w_p_cc_cols)
3055 {
3056 int i = 0;
3057 int j = wp->w_p_cc_cols[i];
3058 int old_txtcol = txtcol;
3059
3060 while (j > -1)
3061 {
3062 txtcol += j;
3063 if (wp->w_p_wrap)
3064 txtcol -= wp->w_skipcol;
3065 else
3066 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003067 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003068 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003069 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003070 txtcol = old_txtcol;
3071 j = wp->w_p_cc_cols[++i];
3072 }
3073 }
3074
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003075 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00003076 if (wp->w_p_cuc)
3077 {
3078 txtcol += wp->w_virtcol;
3079 if (wp->w_p_wrap)
3080 txtcol -= wp->w_skipcol;
3081 else
3082 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003083 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00003084 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003085 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00003086 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
Bram Moolenaar02631462017-09-22 15:20:32 +02003089 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003090 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
3092 /*
3093 * Update w_cline_height and w_cline_folded if the cursor line was
3094 * updated (saves a call to plines() later).
3095 */
3096 if (wp == curwin
3097 && lnum <= curwin->w_cursor.lnum
3098 && lnume >= curwin->w_cursor.lnum)
3099 {
3100 curwin->w_cline_row = row;
3101 curwin->w_cline_height = 1;
3102 curwin->w_cline_folded = TRUE;
3103 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
3104 }
3105}
3106
3107/*
3108 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
3109 */
3110 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003111copy_text_attr(
3112 int off,
3113 char_u *buf,
3114 int len,
3115 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003117 int i;
3118
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 if (enc_utf8)
3121 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003122 for (i = 0; i < len; ++i)
3123 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124}
3125
3126/*
3127 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003128 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 */
3130 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003131fill_foldcolumn(
3132 char_u *p,
3133 win_T *wp,
3134 int closed, /* TRUE of FALSE */
3135 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136{
3137 int i = 0;
3138 int level;
3139 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003140 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003141 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
3143 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003144 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
3146 level = win_foldinfo.fi_level;
3147 if (level > 0)
3148 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003149 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003150 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003151
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 /* If the column is too narrow, we start at the lowest level that
3153 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003154 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 if (first_level < 1)
3156 first_level = 1;
3157
Bram Moolenaar1c934292015-01-27 16:39:29 +01003158 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 {
3160 if (win_foldinfo.fi_lnum == lnum
3161 && first_level + i >= win_foldinfo.fi_low_level)
3162 p[i] = '-';
3163 else if (first_level == 1)
3164 p[i] = '|';
3165 else if (first_level + i <= 9)
3166 p[i] = '0' + first_level + i;
3167 else
3168 p[i] = '>';
3169 if (first_level + i == level)
3170 break;
3171 }
3172 }
3173 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003174 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175}
3176#endif /* FEAT_FOLDING */
3177
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003178#ifdef FEAT_TEXT_PROP
3179static textprop_T *current_text_props = NULL;
3180static buf_T *current_buf = NULL;
3181
3182 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003183text_prop_compare(const void *s1, const void *s2)
3184{
3185 int idx1, idx2;
3186 proptype_T *pt1, *pt2;
3187 colnr_T col1, col2;
3188
3189 idx1 = *(int *)s1;
3190 idx2 = *(int *)s2;
3191 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3192 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3193 if (pt1 == pt2)
3194 return 0;
3195 if (pt1 == NULL)
3196 return -1;
3197 if (pt2 == NULL)
3198 return 1;
3199 if (pt1->pt_priority != pt2->pt_priority)
3200 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3201 col1 = current_text_props[idx1].tp_col;
3202 col2 = current_text_props[idx2].tp_col;
3203 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3204}
3205#endif
3206
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207/*
3208 * Display line "lnum" of window 'wp' on the screen.
3209 * Start at row "startrow", stop when "endrow" is reached.
3210 * wp->w_virtcol needs to be valid.
3211 *
3212 * Return the number of last row the line occupies.
3213 */
3214 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003215win_line(
3216 win_T *wp,
3217 linenr_T lnum,
3218 int startrow,
3219 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003220 int nochange UNUSED, // not updating for changed text
3221 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003223 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3225 int c = 0; /* init for GCC */
3226 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003227#ifdef FEAT_LINEBREAK
3228 long vcol_sbr = -1; /* virtual column after showbreak */
3229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 long vcol_prev = -1; /* "vcol" of previous character */
3231 char_u *line; /* current line */
3232 char_u *ptr; /* current position in "line" */
3233 int row; /* row in the window, excl w_winrow */
3234 int screen_row; /* row on the screen, incl w_winrow */
3235
3236 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3237 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003238 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003239 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003241 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 int extra_attr = 0; /* attributes when n_extra != 0 */
3243 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3244 displaying lcs_eol at end-of-line */
3245 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3246 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3247
3248 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3249 int saved_n_extra = 0;
3250 char_u *saved_p_extra = NULL;
3251 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003252 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 int saved_char_attr = 0;
3254
3255 int n_attr = 0; /* chars with special attr */
3256 int saved_attr2 = 0; /* char_attr saved for n_attr */
3257 int n_attr3 = 0; /* chars with overruling special attr */
3258 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3259
3260 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3261
3262 int fromcol, tocol; /* start/end of inverting */
3263 int fromcol_prev = -2; /* start of inverting after cursor */
3264 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003266 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 pos_T pos;
3268 long v;
3269
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003270 int char_attr = 0; // attributes for next character
3271 int attr_pri = FALSE; // char_attr has priority
3272 int area_highlighting = FALSE; // Visual or incsearch highlighting
3273 // in this line
3274 int vi_attr = 0; // attributes for Visual and incsearch
3275 // highlighting
3276 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003277 int win_attr = 0; // background for whole window, except
3278 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003279 int area_attr = 0; // attributes desired by highlighting
3280 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003282 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 int syntax_attr = 0; /* attributes desired by syntax */
3284 int has_syntax = FALSE; /* this buffer has syntax highl. */
3285 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003286 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003287 int draw_color_col = FALSE; /* highlight colorcolumn */
3288 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003289#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003290#ifdef FEAT_TEXT_PROP
3291 int text_prop_count;
3292 int text_prop_next = 0; // next text property to use
3293 textprop_T *text_props = NULL;
3294 int *text_prop_idxs = NULL;
3295 int text_props_active = 0;
3296 proptype_T *text_prop_type = NULL;
3297 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003298 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003299#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003300#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003301 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003302# define SPWORDLEN 150
3303 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003304 int nextlinecol = 0; /* column where nextline[] starts */
3305 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003306 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003307 int spell_attr = 0; /* attributes desired by spelling */
3308 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003309 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3310 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003311 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003312 static int cap_col = -1; /* column to check for Cap word */
3313 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003314 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003316 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 int multi_attr = 0; /* attributes desired by multibyte */
3318 int mb_l = 1; /* multi-byte byte length */
3319 int mb_c = 0; /* decoded multi-byte character */
3320 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003321 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#ifdef FEAT_DIFF
3323 int filler_lines; /* nr of filler lines to be drawn */
3324 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003325 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 int change_start = MAXCOL; /* first col of changed area */
3327 int change_end = -1; /* last col of changed area */
3328#endif
3329 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3330#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003331 int need_showbreak = FALSE; /* overlong line, skipping first x
3332 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003334#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003335 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003337 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338#endif
3339#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003340 matchitem_T *cur; /* points to the match list */
3341 match_T *shl; /* points to search_hl or a match */
3342 int shl_flag; /* flag to indicate whether search_hl
3343 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003344 int pos_inprogress; /* marks that position match search is
3345 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003346 int prevcol_hl_flag; /* flag to indicate whether prevcol
3347 equals startcol of search_hl or one
3348 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349#endif
3350#ifdef FEAT_ARABIC
3351 int prev_c = 0; /* previous Arabic character */
3352 int prev_c1 = 0; /* first composing char for prev_c */
3353#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003354#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003355 int did_line_attr = 0;
3356#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003357#ifdef FEAT_TERMINAL
3358 int get_term_attr = FALSE;
3359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
3361 /* draw_state: items that are drawn in sequence: */
3362#define WL_START 0 /* nothing done yet */
3363#ifdef FEAT_CMDWIN
3364# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3365#else
3366# define WL_CMDLINE WL_START
3367#endif
3368#ifdef FEAT_FOLDING
3369# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3370#else
3371# define WL_FOLD WL_CMDLINE
3372#endif
3373#ifdef FEAT_SIGNS
3374# define WL_SIGN WL_FOLD + 1 /* column for signs */
3375#else
3376# define WL_SIGN WL_FOLD /* column for signs */
3377#endif
3378#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003379#ifdef FEAT_LINEBREAK
3380# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003382# define WL_BRI WL_NR
3383#endif
3384#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3385# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3386#else
3387# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388#endif
3389#define WL_LINE WL_SBR + 1 /* text in the line */
3390 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003391#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 int feedback_col = 0;
3393 int feedback_old_attr = -1;
3394#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003395 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396
Bram Moolenaar860cae12010-06-05 23:22:07 +02003397#ifdef FEAT_CONCEAL
3398 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003399 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003400 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003401 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003402 int is_concealing = FALSE;
3403 int boguscols = 0; /* nonexistent columns added to force
3404 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003405 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003406 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003407 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003408 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003409# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003410# define FIX_FOR_BOGUSCOLS \
3411 { \
3412 n_extra += vcol_off; \
3413 vcol -= vcol_off; \
3414 vcol_off = 0; \
3415 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003416 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003417 boguscols = 0; \
3418 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003419#else
3420# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423 if (startrow > endrow) /* past the end already! */
3424 return startrow;
3425
3426 row = startrow;
3427 screen_row = row + W_WINROW(wp);
3428
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003429 if (!number_only)
3430 {
3431 /*
3432 * To speed up the loop below, set extra_check when there is linebreak,
3433 * trailing white space and/or syntax processing to be done.
3434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003436 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437#endif
3438#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003439 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003440# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003441 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003442# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003443 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003445 /* Prepare for syntax highlighting in this line. When there is an
3446 * error, stop syntax highlighting. */
3447 save_did_emsg = did_emsg;
3448 did_emsg = FALSE;
3449 syntax_start(wp, lnum);
3450 if (did_emsg)
3451 wp->w_s->b_syn_error = TRUE;
3452 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003453 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003454 did_emsg = save_did_emsg;
3455#ifdef SYN_TIME_LIMIT
3456 if (!wp->w_s->b_syn_slow)
3457#endif
3458 {
3459 has_syntax = TRUE;
3460 extra_check = TRUE;
3461 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003464
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003465 /* Check for columns to display for 'colorcolumn'. */
3466 color_cols = wp->w_p_cc_cols;
3467 if (color_cols != NULL)
3468 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003469#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003470
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003471#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003472 if (term_show_buffer(wp->w_buffer))
3473 {
3474 extra_check = TRUE;
3475 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003476 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003477 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003478#endif
3479
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003480#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003481 if (wp->w_p_spell
3482 && *wp->w_s->b_p_spl != NUL
3483 && wp->w_s->b_langp.ga_len > 0
3484 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003485 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003486 /* Prepare for spell checking. */
3487 has_spell = TRUE;
3488 extra_check = TRUE;
3489
Bram Moolenaar7701f302018-10-02 21:20:32 +02003490 /* Get the start of the next line, so that words that wrap to the
3491 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003492 * Trick: skip a few chars for C/shell/Vim comments */
3493 nextline[SPWORDLEN] = NUL;
3494 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3495 {
3496 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3497 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3498 }
3499
Bram Moolenaar7701f302018-10-02 21:20:32 +02003500 /* When a word wrapped from the previous line the start of the
3501 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003502 if (lnum == checked_lnum)
3503 cur_checked_col = checked_col;
3504 checked_lnum = 0;
3505
3506 /* When there was a sentence end in the previous line may require a
3507 * word starting with capital in this line. In line 1 always check
3508 * the first word. */
3509 if (lnum != capcol_lnum)
3510 cap_col = -1;
3511 if (lnum == 1)
3512 cap_col = 0;
3513 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515#endif
3516
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003517 /*
3518 * handle visual active in this window
3519 */
3520 fromcol = -10;
3521 tocol = MAXCOL;
3522 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003524 /* Visual is after curwin->w_cursor */
3525 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003527 top = &curwin->w_cursor;
3528 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003530 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003532 top = &VIsual;
3533 bot = &curwin->w_cursor;
3534 }
3535 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3536 if (VIsual_mode == Ctrl_V) /* block mode */
3537 {
3538 if (lnum_in_visual_area)
3539 {
3540 fromcol = wp->w_old_cursor_fcol;
3541 tocol = wp->w_old_cursor_lcol;
3542 }
3543 }
3544 else /* non-block mode */
3545 {
3546 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003548 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003550 if (VIsual_mode == 'V') /* linewise */
3551 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 else
3553 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003554 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3555 if (gchar_pos(top) == NUL)
3556 tocol = fromcol + 1;
3557 }
3558 }
3559 if (VIsual_mode != 'V' && lnum == bot->lnum)
3560 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003561 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003562 {
3563 fromcol = -10;
3564 tocol = MAXCOL;
3565 }
3566 else if (bot->col == MAXCOL)
3567 tocol = MAXCOL;
3568 else
3569 {
3570 pos = *bot;
3571 if (*p_sel == 'e')
3572 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3573 else
3574 {
3575 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3576 ++tocol;
3577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579 }
3580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003582 /* Check if the character under the cursor should not be inverted */
3583 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003584#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003585 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003586#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003587 )
3588 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003590 /* if inverting in this line set area_highlighting */
3591 if (fromcol >= 0)
3592 {
3593 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003594 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003596 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003597 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003598 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003599 && clip_isautosel_plus()))
3600 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003605 /*
3606 * handle 'incsearch' and ":s///c" highlighting
3607 */
3608 else if (highlight_match
3609 && wp == curwin
3610 && lnum >= curwin->w_cursor.lnum
3611 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003613 if (lnum == curwin->w_cursor.lnum)
3614 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003615 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003616 else
3617 fromcol = 0;
3618 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3619 {
3620 pos.lnum = lnum;
3621 pos.col = search_match_endcol;
3622 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3623 }
3624 else
3625 tocol = MAXCOL;
3626 /* do at least one character; happens when past end of line */
3627 if (fromcol == tocol)
3628 tocol = fromcol + 1;
3629 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003630 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633
3634#ifdef FEAT_DIFF
3635 filler_lines = diff_check(wp, lnum);
3636 if (filler_lines < 0)
3637 {
3638 if (filler_lines == -1)
3639 {
3640 if (diff_find_change(wp, lnum, &change_start, &change_end))
3641 diff_hlf = HLF_ADD; /* added line */
3642 else if (change_start == 0)
3643 diff_hlf = HLF_TXD; /* changed text */
3644 else
3645 diff_hlf = HLF_CHD; /* changed line */
3646 }
3647 else
3648 diff_hlf = HLF_ADD; /* added line */
3649 filler_lines = 0;
3650 area_highlighting = TRUE;
3651 }
3652 if (lnum == wp->w_topline)
3653 filler_lines = wp->w_topfill;
3654 filler_todo = filler_lines;
3655#endif
3656
3657#ifdef LINE_ATTR
3658# ifdef FEAT_SIGNS
3659 /* If this line has a sign with line highlighting set line_attr. */
3660 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3661 if (v != 0)
3662 line_attr = sign_get_attr((int)v, TRUE);
3663# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003664# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003666 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003667 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668# endif
3669 if (line_attr != 0)
3670 area_highlighting = TRUE;
3671#endif
3672
3673 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3674 ptr = line;
3675
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003676#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003677 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003678 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003679 /* For checking first word with a capital skip white space. */
3680 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003681 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003682
Bram Moolenaar30abd282005-06-22 22:35:10 +00003683 /* To be able to spell-check over line boundaries copy the end of the
3684 * current line into nextline[]. Above the start of the next line was
3685 * copied to nextline[SPWORDLEN]. */
3686 if (nextline[SPWORDLEN] == NUL)
3687 {
3688 /* No next line or it is empty. */
3689 nextlinecol = MAXCOL;
3690 nextline_idx = 0;
3691 }
3692 else
3693 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003694 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003695 if (v < SPWORDLEN)
3696 {
3697 /* Short line, use it completely and append the start of the
3698 * next line. */
3699 nextlinecol = 0;
3700 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003701 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003702 nextline_idx = v + 1;
3703 }
3704 else
3705 {
3706 /* Long line, use only the last SPWORDLEN bytes. */
3707 nextlinecol = v - SPWORDLEN;
3708 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3709 nextline_idx = SPWORDLEN + 1;
3710 }
3711 }
3712 }
3713#endif
3714
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003715 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003717 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003718 extra_check = TRUE;
3719 /* find start of trailing whitespace */
3720 if (lcs_trail)
3721 {
3722 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003723 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003724 --trailcol;
3725 trailcol += (colnr_T) (ptr - line);
3726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
3728
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003729 wcr_attr = get_wcr_attr(wp);
3730 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003731 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003732 win_attr = wcr_attr;
3733 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003734 }
3735#ifdef FEAT_TEXT_PROP
3736 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003737 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003738#endif
3739
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 /*
3741 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3742 * first character to be displayed.
3743 */
3744 if (wp->w_p_wrap)
3745 v = wp->w_skipcol;
3746 else
3747 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003748 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003751
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 while (vcol < v && *ptr != NUL)
3753 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003754 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003757 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 }
3759
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003760 /* When:
3761 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003762 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003763 * - 'virtualedit' is set, or
3764 * - the visual mode is active,
3765 * the end of the line may be before the start of the displayed part.
3766 */
3767 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003768#ifdef FEAT_SYN_HL
3769 wp->w_p_cuc || draw_color_col ||
3770#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003771 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003772 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774
3775 /* Handle a character that's not completely on the screen: Put ptr at
3776 * that character but skip the first few screen characters. */
3777 if (vcol > v)
3778 {
3779 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003781 /* If the character fits on the screen, don't need to skip it.
3782 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003783 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003784 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
3786
3787 /*
3788 * Adjust for when the inverted text is before the screen,
3789 * and when the start of the inverted text is before the screen.
3790 */
3791 if (tocol <= vcol)
3792 fromcol = 0;
3793 else if (fromcol >= 0 && fromcol < vcol)
3794 fromcol = vcol;
3795
3796#ifdef FEAT_LINEBREAK
3797 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3798 if (wp->w_p_wrap)
3799 need_showbreak = TRUE;
3800#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003801#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003802 /* When spell checking a word we need to figure out the start of the
3803 * word and if it's badly spelled or not. */
3804 if (has_spell)
3805 {
3806 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003807 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003808 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003809
3810 pos = wp->w_cursor;
3811 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003812 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003813 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003814
3815 /* spell_move_to() may call ml_get() and make "line" invalid */
3816 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3817 ptr = line + linecol;
3818
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003819 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003820 {
3821 /* no bad word found at line start, don't check until end of a
3822 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003823 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003824 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003825 }
3826 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003827 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003828 /* bad word found, use attributes until end of word */
3829 word_end = wp->w_cursor.col + len + 1;
3830
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003831 /* Turn index into actual attributes. */
3832 if (spell_hlf != HLF_COUNT)
3833 spell_attr = highlight_attr[spell_hlf];
3834 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003835 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003836
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003837# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003838 /* Need to restart syntax highlighting for this line. */
3839 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003840 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003841# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003842 }
3843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845
3846 /*
3847 * Correct highlighting for cursor that can't be disabled.
3848 * Avoids having to check this for each character.
3849 */
3850 if (fromcol >= 0)
3851 {
3852 if (noinvcur)
3853 {
3854 if ((colnr_T)fromcol == wp->w_virtcol)
3855 {
3856 /* highlighting starts at cursor, let it start just after the
3857 * cursor */
3858 fromcol_prev = fromcol;
3859 fromcol = -1;
3860 }
3861 else if ((colnr_T)fromcol < wp->w_virtcol)
3862 /* restart highlighting after the cursor */
3863 fromcol_prev = wp->w_virtcol;
3864 }
3865 if (fromcol >= tocol)
3866 fromcol = -1;
3867 }
3868
3869#ifdef FEAT_SEARCH_EXTRA
3870 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003871 * Handle highlighting the last used search pattern and matches.
3872 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003873 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003875 cur = wp->w_match_head;
3876 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003877 while ((cur != NULL || shl_flag == FALSE) && !number_only
3878 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003880 if (shl_flag == FALSE)
3881 {
3882 shl = &search_hl;
3883 shl_flag = TRUE;
3884 }
3885 else
3886 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003887 shl->startcol = MAXCOL;
3888 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003890 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003891 v = (long)(ptr - line);
3892 if (cur != NULL)
3893 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003894 next_search_hl(wp, shl, lnum, (colnr_T)v,
3895 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003896
3897 /* Need to get the line again, a multi-line regexp may have made it
3898 * invalid. */
3899 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3900 ptr = line + v;
3901
3902 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003904 if (shl->lnum == lnum)
3905 shl->startcol = shl->rm.startpos[0].col;
3906 else
3907 shl->startcol = 0;
3908 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3909 - shl->rm.startpos[0].lnum)
3910 shl->endcol = shl->rm.endpos[0].col;
3911 else
3912 shl->endcol = MAXCOL;
3913 /* Highlight one character for an empty match. */
3914 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003916 if (has_mbyte && line[shl->endcol] != NUL)
3917 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3918 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003919 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003921 if ((long)shl->startcol < v) /* match at leftcol */
3922 {
3923 shl->attr_cur = shl->attr;
3924 search_attr = shl->attr;
3925 }
3926 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003928 if (shl != &search_hl && cur != NULL)
3929 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931#endif
3932
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003933#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003934 // Cursor line highlighting for 'cursorline' in the current window.
3935 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003936 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003937 // Do not show the cursor line when Visual mode is active, because it's
3938 // not clear what is selected then. Do update w_last_cursorline.
3939 if (!(wp == curwin && VIsual_active))
3940 {
3941 line_attr = HL_ATTR(HLF_CUL);
3942 area_highlighting = TRUE;
3943 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003944 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003945 }
3946#endif
3947
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003948#ifdef FEAT_TEXT_PROP
3949 {
3950 char_u *prop_start;
3951
3952 text_prop_count = get_text_props(wp->w_buffer, lnum,
3953 &prop_start, FALSE);
3954 if (text_prop_count > 0)
3955 {
3956 // Make a copy of the properties, so that they are properly
3957 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003958 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003959 if (text_props != NULL)
3960 mch_memmove(text_props, prop_start,
3961 text_prop_count * sizeof(textprop_T));
3962
3963 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003964 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003965 area_highlighting = TRUE;
3966 extra_check = TRUE;
3967 }
3968 }
3969#endif
3970
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003971 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003973
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974#ifdef FEAT_RIGHTLEFT
3975 if (wp->w_p_rl)
3976 {
3977 /* Rightleft window: process the text in the normal direction, but put
3978 * it in current_ScreenLine[] from right to left. Start at the
3979 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003980 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003982 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
3984#endif
3985
3986 /*
3987 * Repeat for the whole displayed line.
3988 */
3989 for (;;)
3990 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003991#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003992 int has_match_conc = 0; // match wants to conceal
3993 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 /* Skip this quickly when working on the text. */
3996 if (draw_state != WL_LINE)
3997 {
3998#ifdef FEAT_CMDWIN
3999 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
4000 {
4001 draw_state = WL_CMDLINE;
4002 if (cmdwin_type != 0 && wp == curwin)
4003 {
4004 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004006 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004007 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004008 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
4010 }
4011#endif
4012
4013#ifdef FEAT_FOLDING
4014 if (draw_state == WL_FOLD - 1 && n_extra == 0)
4015 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01004016 int fdc = compute_foldcolumn(wp, 0);
4017
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01004019 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 {
Bram Moolenaar62706602016-12-09 19:28:48 +01004021 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004022 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004023 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01004024 p_extra_free = alloc(12 + 1);
4025
4026 if (p_extra_free != NULL)
4027 {
4028 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
4029 n_extra = fdc;
4030 p_extra_free[n_extra] = NUL;
4031 p_extra = p_extra_free;
4032 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004033 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004034 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01004035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 }
4037 }
4038#endif
4039
4040#ifdef FEAT_SIGNS
4041 if (draw_state == WL_SIGN - 1 && n_extra == 0)
4042 {
4043 draw_state = WL_SIGN;
4044 /* Show the sign column when there are any signs in this
4045 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02004046 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004048 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004050 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051# endif
4052
4053 /* Draw two cells with the sign value or blank. */
4054 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004055 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004056 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 n_extra = 2;
4058
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02004059 if (row == startrow
4060#ifdef FEAT_DIFF
4061 + filler_lines && filler_todo <= 0
4062#endif
4063 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 {
4065 text_sign = buf_getsigntype(wp->w_buffer, lnum,
4066 SIGN_TEXT);
4067# ifdef FEAT_SIGN_ICONS
4068 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
4069 SIGN_ICON);
4070 if (gui.in_use && icon_sign != 0)
4071 {
4072 /* Use the image in this position. */
4073 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004074 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075# ifdef FEAT_NETBEANS_INTG
4076 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004077 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004079 c_final = NUL;
4080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081# endif
4082 char_attr = icon_sign;
4083 }
4084 else
4085# endif
4086 if (text_sign != 0)
4087 {
4088 p_extra = sign_get_text(text_sign);
4089 if (p_extra != NULL)
4090 {
4091 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004092 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004093 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 }
4095 char_attr = sign_get_attr(text_sign, FALSE);
4096 }
4097 }
4098 }
4099 }
4100#endif
4101
4102 if (draw_state == WL_NR - 1 && n_extra == 0)
4103 {
4104 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02004105 /* Display the absolute or relative line number. After the
4106 * first fill with blanks when the 'n' flag isn't in 'cpo' */
4107 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 && (row == startrow
4109#ifdef FEAT_DIFF
4110 + filler_lines
4111#endif
4112 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
4113 {
4114 /* Draw the line number (empty space after wrapping). */
4115 if (row == startrow
4116#ifdef FEAT_DIFF
4117 + filler_lines
4118#endif
4119 )
4120 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004121 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004122 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004123
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004124 if (wp->w_p_nu && !wp->w_p_rnu)
4125 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004126 num = (long)lnum;
4127 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004128 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004129 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004130 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004131 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004132 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004133 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004134 num = lnum;
4135 fmt = "%-*ld ";
4136 }
4137 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004138
Bram Moolenaar700e7342013-01-30 12:31:36 +01004139 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004140 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 if (wp->w_skipcol > 0)
4142 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4143 *p_extra = '-';
4144#ifdef FEAT_RIGHTLEFT
4145 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004146 {
4147 char_u *p1, *p2;
4148 int t;
4149
4150 // like rl_mirror(), but keep the space at the end
4151 p2 = skiptowhite(extra) - 1;
4152 for (p1 = extra; p1 < p2; ++p1, --p2)
4153 {
4154 t = *p1;
4155 *p1 = *p2;
4156 *p2 = t;
4157 }
4158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159#endif
4160 p_extra = extra;
4161 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004162 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 }
4164 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004167 c_final = NUL;
4168 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004169 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004170 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004171#ifdef FEAT_SYN_HL
4172 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004173 * the current line differently.
4174 * TODO: Can we use CursorLine instead of CursorLineNr
4175 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004176 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004177 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004178 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 }
4182
Bram Moolenaar597a4222014-06-25 14:39:50 +02004183#ifdef FEAT_LINEBREAK
4184 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4185 && n_extra == 0 && *p_sbr != NUL)
4186 /* draw indent after showbreak value */
4187 draw_state = WL_BRI;
4188 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4189 /* After the showbreak, draw the breakindent */
4190 draw_state = WL_BRI - 1;
4191
4192 /* draw 'breakindent': indent wrapped text accordingly */
4193 if (draw_state == WL_BRI - 1 && n_extra == 0)
4194 {
4195 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004196 /* if need_showbreak is set, breakindent also applies */
4197 if (wp->w_p_bri && n_extra == 0
4198 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004199# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004200 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004201# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004202 )
4203 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004204 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004205# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004206 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004207 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004208 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004209# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004210 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4211 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004212 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004213# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004214 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004215# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004216 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004217 c_extra = ' ';
4218 n_extra = get_breakindent_win(wp,
4219 ml_get_buf(wp->w_buffer, lnum, FALSE));
4220 /* Correct end of highlighted area for 'breakindent',
4221 * required when 'linebreak' is also set. */
4222 if (tocol == vcol)
4223 tocol += n_extra;
4224 }
4225 }
4226#endif
4227
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4229 if (draw_state == WL_SBR - 1 && n_extra == 0)
4230 {
4231 draw_state = WL_SBR;
4232# ifdef FEAT_DIFF
4233 if (filler_todo > 0)
4234 {
4235 /* Draw "deleted" diff line(s). */
4236 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004239 c_final = NUL;
4240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004244 c_final = NUL;
4245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246# ifdef FEAT_RIGHTLEFT
4247 if (wp->w_p_rl)
4248 n_extra = col + 1;
4249 else
4250# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004251 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004252 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254# endif
4255# ifdef FEAT_LINEBREAK
4256 if (*p_sbr != NUL && need_showbreak)
4257 {
4258 /* Draw 'showbreak' at the start of each broken line. */
4259 p_extra = p_sbr;
4260 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004261 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004263 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004265 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 /* Correct end of highlighted area for 'showbreak',
4267 * required when 'linebreak' is also set. */
4268 if (tocol == vcol)
4269 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004270#ifdef FEAT_SYN_HL
4271 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004272 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004273 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004274 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277# endif
4278 }
4279#endif
4280
4281 if (draw_state == WL_LINE - 1 && n_extra == 0)
4282 {
4283 draw_state = WL_LINE;
4284 if (saved_n_extra)
4285 {
4286 /* Continue item from end of wrapped line. */
4287 n_extra = saved_n_extra;
4288 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004289 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 p_extra = saved_p_extra;
4291 char_attr = saved_char_attr;
4292 }
4293 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004294 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 }
4297
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004298 // When still displaying '$' of change command, stop at cursor.
4299 // When only displaying the (relative) line number and that's done,
4300 // stop here.
4301 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004302 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303#ifdef FEAT_DIFF
4304 && filler_todo <= 0
4305#endif
4306 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004307 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004309 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004310 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004311 /* Pretend we have finished updating the window. Except when
4312 * 'cursorcolumn' is set. */
4313#ifdef FEAT_SYN_HL
4314 if (wp->w_p_cuc)
4315 row = wp->w_cline_row + wp->w_cline_height;
4316 else
4317#endif
4318 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 break;
4320 }
4321
Bram Moolenaar637532b2019-01-03 21:44:40 +01004322 if (draw_state == WL_LINE && (area_highlighting
4323#ifdef FEAT_SPELL
4324 || has_spell
4325#endif
4326 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 {
4328 /* handle Visual or match highlighting in this line */
4329 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4331 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004333 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004335 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 else if (area_attr != 0
4337 && (vcol == tocol
4338 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340
4341#ifdef FEAT_SEARCH_EXTRA
4342 if (!n_extra)
4343 {
4344 /*
4345 * Check for start/end of search pattern match.
4346 * After end, check for start/end of next match.
4347 * When another match, have to check for start again.
4348 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004349 * Do this for 'search_hl' and the match list (ordered by
4350 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004352 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004353 cur = wp->w_match_head;
4354 shl_flag = FALSE;
4355 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004357 if (shl_flag == FALSE
4358 && ((cur != NULL
4359 && cur->priority > SEARCH_HL_PRIORITY)
4360 || cur == NULL))
4361 {
4362 shl = &search_hl;
4363 shl_flag = TRUE;
4364 }
4365 else
4366 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004367 if (cur != NULL)
4368 cur->pos.cur = 0;
4369 pos_inprogress = TRUE;
4370 while (shl->rm.regprog != NULL
4371 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004373 if (shl->startcol != MAXCOL
4374 && v >= (long)shl->startcol
4375 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004377 int tmp_col = v + MB_PTR2LEN(ptr);
4378
4379 if (shl->endcol < tmp_col)
4380 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004382#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004383 // Match with the "Conceal" group results in hiding
4384 // the match.
4385 if (cur != NULL
4386 && shl != &search_hl
4387 && syn_name2id((char_u *)"Conceal")
4388 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004389 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004390 has_match_conc =
4391 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004392 match_conc = cur->conceal_char;
4393 }
4394 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004395 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004398 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
4400 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004401 next_search_hl(wp, shl, lnum, (colnr_T)v,
4402 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004403 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004404 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
4406 /* Need to get the line again, a multi-line regexp
4407 * may have made it invalid. */
4408 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4409 ptr = line + v;
4410
4411 if (shl->lnum == lnum)
4412 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004413 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004415 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004417 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004419 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 {
4421 /* highlight empty match, try again after
4422 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004424 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004425 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004427 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429
4430 /* Loop to check if the match starts at the
4431 * current position */
4432 continue;
4433 }
4434 }
4435 break;
4436 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004437 if (shl != &search_hl && cur != NULL)
4438 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004440
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004441 /* Use attributes from match with highest priority among
4442 * 'search_hl' and the match list. */
4443 search_attr = search_hl.attr_cur;
4444 cur = wp->w_match_head;
4445 shl_flag = FALSE;
4446 while (cur != NULL || shl_flag == FALSE)
4447 {
4448 if (shl_flag == FALSE
4449 && ((cur != NULL
4450 && cur->priority > SEARCH_HL_PRIORITY)
4451 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004452 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004453 shl = &search_hl;
4454 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004455 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004456 else
4457 shl = &cur->hl;
4458 if (shl->attr_cur != 0)
4459 search_attr = shl->attr_cur;
4460 if (shl != &search_hl && cur != NULL)
4461 cur = cur->next;
4462 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004463 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004464 if (*ptr == NUL && (did_line_attr >= 1
4465 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004466 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468#endif
4469
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004471 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004473 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4474 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004476 if (diff_hlf == HLF_TXD && ptr - line > change_end
4477 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004479 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004480 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004481 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004484
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004485#ifdef FEAT_TEXT_PROP
4486 if (text_props != NULL)
4487 {
4488 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004489 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004490
4491 // Check if any active property ends.
4492 for (pi = 0; pi < text_props_active; ++pi)
4493 {
4494 int tpi = text_prop_idxs[pi];
4495
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004496 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004497 + text_props[tpi].tp_len)
4498 {
4499 if (pi + 1 < text_props_active)
4500 mch_memmove(text_prop_idxs + pi,
4501 text_prop_idxs + pi + 1,
4502 sizeof(int)
4503 * (text_props_active - (pi + 1)));
4504 --text_props_active;
4505 --pi;
4506 }
4507 }
4508
4509 // Add any text property that starts in this column.
4510 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004511 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004512 text_prop_idxs[text_props_active++] = text_prop_next++;
4513
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004514 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004515 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004516 if (text_props_active > 0)
4517 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004518 // Sort the properties on priority and/or starting last.
4519 // Then combine the attributes, highest priority last.
4520 current_text_props = text_props;
4521 current_buf = wp->w_buffer;
4522 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4523 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004524
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004525 for (pi = 0; pi < text_props_active; ++pi)
4526 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004527 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004528 proptype_T *pt = text_prop_type_by_id(
4529 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004530
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004531 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004532 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004533 int pt_attr = syn_id2attr(pt->pt_hl_id);
4534
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004535 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004536 text_prop_attr =
4537 hl_combine_attr(text_prop_attr, pt_attr);
4538 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004539 }
4540 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004541 }
4542 }
4543#endif
4544
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004545 /* Decide which of the highlight attributes to use. */
4546 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004547#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004548 if (area_attr != 0)
4549 char_attr = hl_combine_attr(line_attr, area_attr);
4550 else if (search_attr != 0)
4551 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004552# ifdef FEAT_TEXT_PROP
4553 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004554 {
4555 char_attr = hl_combine_attr(
4556 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4557 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004558# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004559 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004560 || vcol < fromcol || vcol_prev < fromcol_prev
4561 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004562 // Use line_attr when not in the Visual or 'incsearch' area
4563 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004564 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004565#else
4566 if (area_attr != 0)
4567 char_attr = area_attr;
4568 else if (search_attr != 0)
4569 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004570#endif
4571 else
4572 {
4573 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004574#ifdef FEAT_TEXT_PROP
4575 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004576 {
4577 if (text_prop_combine)
4578 char_attr = hl_combine_attr(
4579 syntax_attr, text_prop_attr);
4580 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004581 char_attr = hl_combine_attr(
4582 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004583 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004584 else
4585#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004586#ifdef FEAT_SYN_HL
4587 if (has_syntax)
4588 char_attr = syntax_attr;
4589 else
4590#endif
4591 char_attr = 0;
4592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004594 if (char_attr == 0)
4595 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596
4597 /*
4598 * Get the next character to put on the screen.
4599 */
4600 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004601 * The "p_extra" points to the extra stuff that is inserted to
4602 * represent special characters (non-printable stuff) and other
4603 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004604 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004605 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4606 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4608 */
4609 if (n_extra > 0)
4610 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004611 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004613 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004615 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
4617 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004618 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004619 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
4621 else
4622 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
4624 else
4625 {
4626 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (has_mbyte)
4628 {
4629 mb_c = c;
4630 if (enc_utf8)
4631 {
4632 /* If the UTF-8 character is more than one byte:
4633 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004634 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 mb_utf8 = FALSE;
4636 if (mb_l > n_extra)
4637 mb_l = 1;
4638 else if (mb_l > 1)
4639 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004640 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004642 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
4644 }
4645 else
4646 {
4647 /* if this is a DBCS character, put it in "mb_c" */
4648 mb_l = MB_BYTE2LEN(c);
4649 if (mb_l >= n_extra)
4650 mb_l = 1;
4651 else if (mb_l > 1)
4652 mb_c = (c << 8) + p_extra[1];
4653 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004654 if (mb_l == 0) /* at the NUL at end-of-line */
4655 mb_l = 1;
4656
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 /* If a double-width char doesn't fit display a '>' in the
4658 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004659 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660# ifdef FEAT_RIGHTLEFT
4661 wp->w_p_rl ? (col <= 0) :
4662# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004663 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 && (*mb_char2cells)(mb_c) == 2)
4665 {
4666 c = '>';
4667 mb_c = c;
4668 mb_l = 1;
4669 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004670 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 /* put the pointer back to output the double-width
4672 * character at the start of the next line. */
4673 ++n_extra;
4674 --p_extra;
4675 }
4676 else
4677 {
4678 n_extra -= mb_l - 1;
4679 p_extra += mb_l - 1;
4680 }
4681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 ++p_extra;
4683 }
4684 --n_extra;
4685 }
4686 else
4687 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004688#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004689 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004690#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004691
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004692 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004693 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 /*
4695 * Get a character from the line itself.
4696 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004697 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004698#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004699 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 if (has_mbyte)
4702 {
4703 mb_c = c;
4704 if (enc_utf8)
4705 {
4706 /* If the UTF-8 character is more than one byte: Decode it
4707 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004708 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 mb_utf8 = FALSE;
4710 if (mb_l > 1)
4711 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004712 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 /* Overlong encoded ASCII or ASCII with composing char
4714 * is displayed normally, except a NUL. */
4715 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004716 {
4717 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004718#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004719 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004720#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004723
4724 /* At start of the line we can have a composing char.
4725 * Draw it as a space with a composing char. */
4726 if (utf_iscomposing(mb_c))
4727 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004728 int i;
4729
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004730 for (i = Screen_mco - 1; i > 0; --i)
4731 u8cc[i] = u8cc[i - 1];
4732 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004733 mb_c = ' ';
4734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736
4737 if ((mb_l == 1 && c >= 0x80)
4738 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004739 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
4741 /*
4742 * Illegal UTF-8 byte: display as <xx>.
4743 * Non-BMP character : display as ? or fullwidth ?.
4744 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004745 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004746# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004747 if (wp->w_p_rl) /* reverse */
4748 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 p_extra = extra;
4751 c = *p_extra;
4752 mb_c = mb_ptr2char_adv(&p_extra);
4753 mb_utf8 = (c >= 0x80);
4754 n_extra = (int)STRLEN(p_extra);
4755 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004756 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 if (area_attr == 0 && search_attr == 0)
4758 {
4759 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004760 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 saved_attr2 = char_attr; /* save current attr */
4762 }
4763 }
4764 else if (mb_l == 0) /* at the NUL at end-of-line */
4765 mb_l = 1;
4766#ifdef FEAT_ARABIC
4767 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4768 {
4769 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004770 int pc, pc1, nc;
4771 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
4773 /* The idea of what is the previous and next
4774 * character depends on 'rightleft'. */
4775 if (wp->w_p_rl)
4776 {
4777 pc = prev_c;
4778 pc1 = prev_c1;
4779 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004780 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
4782 else
4783 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004784 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004786 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 }
4788 prev_c = mb_c;
4789
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004790 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 else
4793 prev_c = mb_c;
4794#endif
4795 }
4796 else /* enc_dbcs */
4797 {
4798 mb_l = MB_BYTE2LEN(c);
4799 if (mb_l == 0) /* at the NUL at end-of-line */
4800 mb_l = 1;
4801 else if (mb_l > 1)
4802 {
4803 /* We assume a second byte below 32 is illegal.
4804 * Hopefully this is OK for all double-byte encodings!
4805 */
4806 if (ptr[1] >= 32)
4807 mb_c = (c << 8) + ptr[1];
4808 else
4809 {
4810 if (ptr[1] == NUL)
4811 {
4812 /* head byte at end of line */
4813 mb_l = 1;
4814 transchar_nonprint(extra, c);
4815 }
4816 else
4817 {
4818 /* illegal tail byte */
4819 mb_l = 2;
4820 STRCPY(extra, "XX");
4821 }
4822 p_extra = extra;
4823 n_extra = (int)STRLEN(extra) - 1;
4824 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004825 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 c = *p_extra++;
4827 if (area_attr == 0 && search_attr == 0)
4828 {
4829 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004830 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 saved_attr2 = char_attr; /* save current attr */
4832 }
4833 mb_c = c;
4834 }
4835 }
4836 }
4837 /* If a double-width char doesn't fit display a '>' in the
4838 * last column; the character is displayed at the start of the
4839 * next line. */
4840 if ((
4841# ifdef FEAT_RIGHTLEFT
4842 wp->w_p_rl ? (col <= 0) :
4843# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004844 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 && (*mb_char2cells)(mb_c) == 2)
4846 {
4847 c = '>';
4848 mb_c = c;
4849 mb_utf8 = FALSE;
4850 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004851 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004852 // Put pointer back so that the character will be
4853 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004855#ifdef FEAT_CONCEAL
4856 did_decrement_ptr = TRUE;
4857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 }
4859 else if (*ptr != NUL)
4860 ptr += mb_l - 1;
4861
4862 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004863 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004864 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004865 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004868 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004869 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 c = ' ';
4871 if (area_attr == 0 && search_attr == 0)
4872 {
4873 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004874 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 saved_attr2 = char_attr; /* save current attr */
4876 }
4877 mb_c = c;
4878 mb_utf8 = FALSE;
4879 mb_l = 1;
4880 }
4881
4882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 ++ptr;
4884
4885 if (extra_check)
4886 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004887#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004888 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004889#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004890
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004891#ifdef FEAT_TERMINAL
4892 if (get_term_attr)
4893 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004894 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004895
4896 if (!attr_pri)
4897 char_attr = syntax_attr;
4898 else
4899 char_attr = hl_combine_attr(syntax_attr, char_attr);
4900 }
4901#endif
4902
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004903#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004904 // Get syntax attribute, unless still at the start of the line
4905 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004906 v = (long)(ptr - line);
4907 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
4909 /* Get the syntax attribute for the character. If there
4910 * is an error, disable syntax highlighting. */
4911 save_did_emsg = did_emsg;
4912 did_emsg = FALSE;
4913
Bram Moolenaar217ad922005-03-20 22:37:15 +00004914 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004915# ifdef FEAT_SPELL
4916 has_spell ? &can_spell :
4917# endif
4918 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919
4920 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004921 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004922 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004923 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004924 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 else
4927 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004928
4929 // combine syntax attribute with 'wincolor'
4930 if (win_attr != 0)
4931 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4932
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004933#ifdef SYN_TIME_LIMIT
4934 if (wp->w_s->b_syn_slow)
4935 has_syntax = FALSE;
4936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937
4938 /* Need to get the line again, a multi-line regexp may
4939 * have made it invalid. */
4940 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4941 ptr = line + v;
4942
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004943# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004944 // Text properties overrule syntax highlighting or combine.
4945 if (text_prop_attr == 0 || text_prop_combine)
4946# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004947 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004948 int comb_attr = syntax_attr;
4949# ifdef FEAT_TEXT_PROP
4950 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4951# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004952 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004953 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004954 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004955 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004956 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004957# ifdef FEAT_CONCEAL
4958 /* no concealing past the end of the line, it interferes
4959 * with line highlighting */
4960 if (c == NUL)
4961 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004962 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004963 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004964# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004966#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004967
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004968#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004969 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004970 * Only do this when there is no syntax highlighting, the
4971 * @Spell cluster is not used or the current syntax item
4972 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004973 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004974 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004975 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004976 if (c != 0 && (
4977# ifdef FEAT_SYN_HL
4978 !has_syntax ||
4979# endif
4980 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004981 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004982 char_u *prev_ptr, *p;
4983 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004984 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004985 if (has_mbyte)
4986 {
4987 prev_ptr = ptr - mb_l;
4988 v -= mb_l - 1;
4989 }
4990 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004991 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004992
4993 /* Use nextline[] if possible, it has the start of the
4994 * next line concatenated. */
4995 if ((prev_ptr - line) - nextlinecol >= 0)
4996 p = nextline + (prev_ptr - line) - nextlinecol;
4997 else
4998 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004999 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005000 len = spell_check(wp, p, &spell_hlf, &cap_col,
5001 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005002 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005003
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005004 /* In Insert mode only highlight a word that
5005 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005006 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005007 && (State & INSERT) != 0
5008 && wp->w_cursor.lnum == lnum
5009 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00005010 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005011 && wp->w_cursor.col < (colnr_T)word_end)
5012 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005013 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005014 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005015 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00005016
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005017 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00005018 && (p - nextline) + len > nextline_idx)
5019 {
5020 /* Remember that the good word continues at the
5021 * start of the next line. */
5022 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005023 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005024 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005025
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005026 /* Turn index into actual attributes. */
5027 if (spell_hlf != HLF_COUNT)
5028 spell_attr = highlight_attr[spell_hlf];
5029
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005030 if (cap_col > 0)
5031 {
5032 if (p != prev_ptr
5033 && (p - nextline) + cap_col >= nextline_idx)
5034 {
5035 /* Remember that the word in the next line
5036 * must start with a capital. */
5037 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005038 cap_col = (int)((p - nextline) + cap_col
5039 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005040 }
5041 else
5042 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005043 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005044 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005045 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005046 }
5047 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005048 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005049 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005050 char_attr = hl_combine_attr(char_attr, spell_attr);
5051 else
5052 char_attr = hl_combine_attr(spell_attr, char_attr);
5053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054#endif
5055#ifdef FEAT_LINEBREAK
5056 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00005057 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01005059 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01005060 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01005062 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005063 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01005064
Bram Moolenaar597a4222014-06-25 14:39:50 +02005065 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005066 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02005067 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02005068 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005069# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02005070 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005071 wp->w_buffer->b_p_vts_array) - 1;
5072# else
Bram Moolenaara3650912014-11-19 13:21:57 +01005073 n_extra = (int)wp->w_buffer->b_p_ts
5074 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005075# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01005076
Bram Moolenaar4df70292015-03-21 14:20:16 +01005077 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01005078 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005079 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005080 {
5081#ifdef FEAT_CONCEAL
5082 if (c == TAB)
5083 /* See "Tab alignment" below. */
5084 FIX_FOR_BOGUSCOLS;
5085#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005086 if (!wp->w_p_list)
5087 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 }
5090#endif
5091
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005092 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
5093 // But not when the character is followed by a composing
5094 // character (use mb_l to check that).
5095 if (wp->w_p_list
5096 && ((((c == 160 && mb_l == 1)
5097 || (mb_utf8
5098 && ((mb_c == 160 && mb_l == 2)
5099 || (mb_c == 0x202f && mb_l == 3))))
5100 && lcs_nbsp)
5101 || (c == ' '
5102 && mb_l == 1
5103 && lcs_space
5104 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005105 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005106 c = (c == ' ') ? lcs_space : lcs_nbsp;
5107 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005108 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005109 n_attr = 1;
5110 extra_attr = HL_ATTR(HLF_8);
5111 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005112 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005113 mb_c = c;
5114 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005115 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005116 mb_utf8 = TRUE;
5117 u8cc[0] = 0;
5118 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005119 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005120 else
5121 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005122 }
5123
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5125 {
5126 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005127 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
5129 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005130 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 saved_attr2 = char_attr; /* save current attr */
5132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005134 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
5136 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005137 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005138 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 else
5141 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
5143 }
5144
5145 /*
5146 * Handling of non-printable characters.
5147 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005148 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
5150 /*
5151 * when getting a character from the file, we may have to
5152 * turn it into something else on the way to putting it
5153 * into "ScreenLines".
5154 */
5155 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5156 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005157 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005158 long vcol_adjusted = vcol; /* removed showbreak length */
5159#ifdef FEAT_LINEBREAK
5160 /* only adjust the tab_len, when at the first column
5161 * after the showbreak value was drawn */
5162 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5163 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005166#ifdef FEAT_VARTABS
5167 tab_len = tabstop_padding(vcol_adjusted,
5168 wp->w_buffer->b_p_ts,
5169 wp->w_buffer->b_p_vts_array) - 1;
5170#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005171 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005172 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5173#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005174
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005175#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005176 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005177#endif
5178 /* tab amount depends on current column */
5179 n_extra = tab_len;
5180#ifdef FEAT_LINEBREAK
5181 else
5182 {
5183 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005184 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005185 int i;
5186 int saved_nextra = n_extra;
5187
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005188#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005189 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005190 /* there are characters to conceal */
5191 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005192 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5193 */
5194 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5195 && n_extra > tab_len)
5196 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005197#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005198
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005199 /* if n_extra > 0, it gives the number of chars, to
5200 * use for a tab, else we need to calculate the width
5201 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005202 len = (tab_len * mb_char2len(lcs_tab2));
5203 if (n_extra > 0)
5204 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005205 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005206 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005207 vim_memset(p, ' ', len);
5208 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005209 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005210 p_extra_free = p;
5211 for (i = 0; i < tab_len; i++)
5212 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005213 if (*p == NUL)
5214 {
5215 tab_len = i;
5216 break;
5217 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005218 mb_char2bytes(lcs_tab2, p);
5219 p += mb_char2len(lcs_tab2);
5220 n_extra += mb_char2len(lcs_tab2)
5221 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005222 }
5223 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005224#ifdef FEAT_CONCEAL
5225 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5226 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005227 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005228 n_extra -= vcol_off;
5229#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005230 }
5231#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005232#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005233 {
5234 int vc_saved = vcol_off;
5235
5236 /* Tab alignment should be identical regardless of
5237 * 'conceallevel' value. So tab compensates of all
5238 * previous concealed characters, and thus resets
5239 * vcol_off and boguscols accumulated so far in the
5240 * line. Note that the tab can be longer than
5241 * 'tabstop' when there are concealed characters. */
5242 FIX_FOR_BOGUSCOLS;
5243
5244 /* Make sure, the highlighting for the tab char will be
5245 * correctly set further below (effectively reverts the
5246 * FIX_FOR_BOGSUCOLS macro */
5247 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005248 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005249 tab_len += vc_saved;
5250 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (wp->w_p_list)
5254 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005255 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005256#ifdef FEAT_LINEBREAK
5257 if (wp->w_p_lbr)
5258 c_extra = NUL; /* using p_extra from above */
5259 else
5260#endif
5261 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005262 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005263 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005264 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005267 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
5269 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005270 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005271 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 }
5274 else
5275 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005276 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 c_extra = ' ';
5278 c = ' ';
5279 }
5280 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005281 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005282 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005283 || ((fromcol >= 0 || fromcol_prev >= 0)
5284 && tocol > vcol
5285 && VIsual_mode != Ctrl_V
5286 && (
5287# ifdef FEAT_RIGHTLEFT
5288 wp->w_p_rl ? (col >= 0) :
5289# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005290 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005291 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005292 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005293 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005294 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005296 /* Display a '$' after the line or highlight an extra
5297 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5299 /* For a diff line the highlighting continues after the
5300 * "$". */
5301 if (
5302# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005303 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304# ifdef LINE_ATTR
5305 &&
5306# endif
5307# endif
5308# ifdef LINE_ATTR
5309 line_attr == 0
5310# endif
5311 )
5312#endif
5313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 /* In virtualedit, visual selections may extend
5315 * beyond end of line. */
5316 if (area_highlighting && virtual_active()
5317 && tocol != MAXCOL && vcol < tocol)
5318 n_extra = 0;
5319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
5321 p_extra = at_end_str;
5322 n_extra = 1;
5323 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005324 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 }
5326 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005327 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005328 c = lcs_eol;
5329 else
5330 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 lcs_eol_one = -1;
5332 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005333 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005335 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 n_attr = 1;
5337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005339 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 {
5341 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005342 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005343 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 }
5345 else
5346 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
5348 else if (c != NUL)
5349 {
5350 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005351 if (n_extra == 0)
5352 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353#ifdef FEAT_RIGHTLEFT
5354 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5355 rl_mirror(p_extra); /* reverse "<12>" */
5356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005358 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005359#ifdef FEAT_LINEBREAK
5360 if (wp->w_p_lbr)
5361 {
5362 char_u *p;
5363
5364 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005365 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005366 vim_memset(p, ' ', n_extra);
5367 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5368 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005369 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005370 p_extra_free = p_extra = p;
5371 }
5372 else
5373#endif
5374 {
5375 n_extra = byte2cells(c) - 1;
5376 c = *p_extra++;
5377 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005378 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
5380 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005381 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 saved_attr2 = char_attr; /* save current attr */
5383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 else if (VIsual_active
5387 && (VIsual_mode == Ctrl_V
5388 || VIsual_mode == 'v')
5389 && virtual_active()
5390 && tocol != MAXCOL
5391 && vcol < tocol
5392 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005393#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005395#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005396 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 {
5398 c = ' ';
5399 --ptr; /* put it back at the NUL */
5400 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005401#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 else if ((
5403# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005404 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005406# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005407 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 ) && (
5411# ifdef FEAT_RIGHTLEFT
5412 wp->w_p_rl ? (col >= 0) :
5413# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005414 (col
5415# ifdef FEAT_CONCEAL
5416 - boguscols
5417# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005418 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {
5420 /* Highlight until the right side of the window */
5421 c = ' ';
5422 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005423
5424 /* Remember we do the char for line highlighting. */
5425 ++did_line_attr;
5426
5427 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005428 if (line_attr != 0 && char_attr == search_attr
5429 && (did_line_attr > 1
5430 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005431 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432# ifdef FEAT_DIFF
5433 if (diff_hlf == HLF_TXD)
5434 {
5435 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005436 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005437 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005438 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005439 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5440 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005441 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
5444# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005445# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005446 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005447 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005448 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005449 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5450 char_attr = hl_combine_attr(char_attr,
5451 HL_ATTR(HLF_CUL));
5452 }
5453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 }
5455#endif
5456 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005457
5458#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005459 if ( wp->w_p_cole > 0
5460 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005461 conceal_cursor_line(wp))
5462 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005463 && !(lnum_in_visual_area
5464 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005465 {
5466 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005467 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005468 && (syn_get_sub_char() != NUL || match_conc
5469 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005470 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005471 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005472 /* First time at this concealed item: display one
5473 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005474 if (match_conc)
5475 c = match_conc;
5476 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005477 c = syn_get_sub_char();
5478 else if (lcs_conceal != NUL)
5479 c = lcs_conceal;
5480 else
5481 c = ' ';
5482
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005483 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005484
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005485 if (n_extra > 0)
5486 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005487 vcol += n_extra;
5488 if (wp->w_p_wrap && n_extra > 0)
5489 {
5490# ifdef FEAT_RIGHTLEFT
5491 if (wp->w_p_rl)
5492 {
5493 col -= n_extra;
5494 boguscols -= n_extra;
5495 }
5496 else
5497# endif
5498 {
5499 boguscols += n_extra;
5500 col += n_extra;
5501 }
5502 }
5503 n_extra = 0;
5504 n_attr = 0;
5505 }
5506 else if (n_skip == 0)
5507 {
5508 is_concealing = TRUE;
5509 n_skip = 1;
5510 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005511 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005512 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005513 {
5514 mb_utf8 = TRUE;
5515 u8cc[0] = 0;
5516 c = 0xc0;
5517 }
5518 else
5519 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005520 }
5521 else
5522 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005523 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005524 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005525 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005526
5527 if (n_skip > 0 && did_decrement_ptr)
5528 // not showing the '>', put pointer back to avoid getting stuck
5529 ++ptr;
5530
5531#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 }
5533
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005534#ifdef FEAT_CONCEAL
5535 /* In the cursor line and we may be concealing characters: correct
5536 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005537 if (!did_wcol && draw_state == WL_LINE
5538 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005539 && conceal_cursor_line(wp)
5540 && (int)wp->w_virtcol <= vcol + n_skip)
5541 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005542# ifdef FEAT_RIGHTLEFT
5543 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005544 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005545 else
5546# endif
5547 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005548 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005549 did_wcol = TRUE;
5550 }
5551#endif
5552
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 /* Don't override visual selection highlighting. */
5554 if (n_attr > 0
5555 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005556 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 char_attr = extra_attr;
5558
Bram Moolenaar81695252004-12-29 20:58:21 +00005559#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 /* XIM don't send preedit_start and preedit_end, but they send
5561 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5562 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005563 if (p_imst == IM_ON_THE_SPOT
5564 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005565 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 && (State & INSERT)
5567 && !p_imdisable
5568 && im_is_preediting()
5569 && draw_state == WL_LINE)
5570 {
5571 colnr_T tcol;
5572
5573 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005574 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 else
5576 tcol = preedit_end_col;
5577 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5578 {
5579 if (feedback_old_attr < 0)
5580 {
5581 feedback_col = 0;
5582 feedback_old_attr = char_attr;
5583 }
5584 char_attr = im_get_feedback_attr(feedback_col);
5585 if (char_attr < 0)
5586 char_attr = feedback_old_attr;
5587 feedback_col++;
5588 }
5589 else if (feedback_old_attr >= 0)
5590 {
5591 char_attr = feedback_old_attr;
5592 feedback_old_attr = -1;
5593 feedback_col = 0;
5594 }
5595 }
5596#endif
5597 /*
5598 * Handle the case where we are in column 0 but not on the first
5599 * character of the line and the user wants us to show us a
5600 * special character (via 'listchars' option "precedes:<char>".
5601 */
5602 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005603 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5605#ifdef FEAT_DIFF
5606 && filler_todo <= 0
5607#endif
5608 && draw_state > WL_NR
5609 && c != NUL)
5610 {
5611 c = lcs_prec;
5612 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005613 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5614 {
5615 /* Double-width character being overwritten by the "precedes"
5616 * character, need to fill up half the character. */
5617 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005618 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005619 n_extra = 1;
5620 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005621 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005624 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
5626 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005627 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005628 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630 else
5631 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005632 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 {
5634 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005635 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 n_attr3 = 1;
5637 }
5638 }
5639
5640 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005641 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005643 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005644#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005645 || did_line_attr == 1
5646#endif
5647 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005649#ifdef FEAT_SEARCH_EXTRA
5650 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005651
5652 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005653 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005654 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005655#endif
5656
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005657 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 * highlight match at end of line. If it's beyond the last
5659 * char on the screen, just overwrite that one (tricky!) Not
5660 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005661#ifdef FEAT_SEARCH_EXTRA
5662 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005663 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005664 prevcol_hl_flag = TRUE;
5665 else
5666 {
5667 cur = wp->w_match_head;
5668 while (cur != NULL)
5669 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005670 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005671 {
5672 prevcol_hl_flag = TRUE;
5673 break;
5674 }
5675 cur = cur->next;
5676 }
5677 }
5678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005680 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005681 && (VIsual_mode != Ctrl_V
5682 || lnum == VIsual.lnum
5683 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005684 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685#ifdef FEAT_SEARCH_EXTRA
5686 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005687 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005688# ifdef FEAT_SYN_HL
5689 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5690 && !(wp == curwin && VIsual_active))
5691# endif
5692# ifdef FEAT_DIFF
5693 && diff_hlf == (hlf_T)0
5694# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005695# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005696 && did_line_attr <= 1
5697# endif
5698 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699#endif
5700 ))
5701 {
5702 int n = 0;
5703
5704#ifdef FEAT_RIGHTLEFT
5705 if (wp->w_p_rl)
5706 {
5707 if (col < 0)
5708 n = 1;
5709 }
5710 else
5711#endif
5712 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005713 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 n = -1;
5715 }
5716 if (n != 0)
5717 {
5718 /* At the window boundary, highlight the last character
5719 * instead (better than nothing). */
5720 off += n;
5721 col += n;
5722 }
5723 else
5724 {
5725 /* Add a blank character to highlight. */
5726 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 if (enc_utf8)
5728 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 }
5730#ifdef FEAT_SEARCH_EXTRA
5731 if (area_attr == 0)
5732 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005733 /* Use attributes from match with highest priority among
5734 * 'search_hl' and the match list. */
5735 char_attr = search_hl.attr;
5736 cur = wp->w_match_head;
5737 shl_flag = FALSE;
5738 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005739 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005740 if (shl_flag == FALSE
5741 && ((cur != NULL
5742 && cur->priority > SEARCH_HL_PRIORITY)
5743 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005744 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005745 shl = &search_hl;
5746 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005747 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005748 else
5749 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005750 if ((ptr - line) - 1 == (long)shl->startcol
5751 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005752 char_attr = shl->attr;
5753 if (shl != &search_hl && cur != NULL)
5754 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
5757#endif
5758 ScreenAttrs[off] = char_attr;
5759#ifdef FEAT_RIGHTLEFT
5760 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005763 --off;
5764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 else
5766#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005767 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005769 ++off;
5770 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005771 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005772#ifdef FEAT_SYN_HL
5773 eol_hl_off = 1;
5774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777
Bram Moolenaar91170f82006-05-05 21:15:17 +00005778 /*
5779 * At end of the text line.
5780 */
5781 if (c == NUL)
5782 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005783#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005784 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005785 if (wp->w_p_wrap)
5786 v = wp->w_skipcol;
5787 else
5788 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005789
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005790 /* check if line ends before left margin */
5791 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005792 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005793#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005794 // Get rid of the boguscols now, we want to draw until the right
5795 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005796 col -= boguscols;
5797 boguscols = 0;
5798#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005799
5800 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005801 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005802
5803 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005804 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5805 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005806 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005807 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005808 || draw_color_col
5809 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005810# ifdef FEAT_RIGHTLEFT
5811 && !wp->w_p_rl
5812# endif
5813 )
5814 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005815 int rightmost_vcol = 0;
5816 int i;
5817
5818 if (wp->w_p_cuc)
5819 rightmost_vcol = wp->w_virtcol;
5820 if (draw_color_col)
5821 /* determine rightmost colorcolumn to possibly draw */
5822 for (i = 0; color_cols[i] >= 0; ++i)
5823 if (rightmost_vcol < color_cols[i])
5824 rightmost_vcol = color_cols[i];
5825
Bram Moolenaar02631462017-09-22 15:20:32 +02005826 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005827 {
5828 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005829 if (enc_utf8)
5830 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005831 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005832 if (draw_color_col)
5833 draw_color_col = advance_color_col(VCOL_HLC,
5834 &color_cols);
5835
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005836 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005837 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005838 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005839 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005840 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005841 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005842
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005843 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005844 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005845
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005846 ++vcol;
5847 }
5848 }
5849#endif
5850
Bram Moolenaar53f81742017-09-22 14:35:51 +02005851 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005852 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 row++;
5854
5855 /*
5856 * Update w_cline_height and w_cline_folded if the cursor line was
5857 * updated (saves a call to plines() later).
5858 */
5859 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5860 {
5861 curwin->w_cline_row = startrow;
5862 curwin->w_cline_height = row - startrow;
5863#ifdef FEAT_FOLDING
5864 curwin->w_cline_folded = FALSE;
5865#endif
5866 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5867 }
5868
5869 break;
5870 }
5871
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005872 // Show "extends" character from 'listchars' if beyond the line end and
5873 // 'list' is set.
5874 if (lcs_ext != NUL
5875 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 && !wp->w_p_wrap
5877#ifdef FEAT_DIFF
5878 && filler_todo <= 0
5879#endif
5880 && (
5881#ifdef FEAT_RIGHTLEFT
5882 wp->w_p_rl ? col == 0 :
5883#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005884 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005886 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5888 {
5889 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005890 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005892 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 {
5894 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005895 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005896 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 }
5898 else
5899 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 }
5901
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005902#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005903 /* advance to the next 'colorcolumn' */
5904 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005905 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005906
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005907 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005908 * highlight the cursor position itself.
5909 * Also highlight the 'colorcolumn' if it is different than
5910 * 'cursorcolumn' */
5911 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005912 if (draw_state == WL_LINE && !lnum_in_visual_area
5913 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005914 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005915 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005916 && lnum != wp->w_cursor.lnum)
5917 {
5918 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005919 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005920 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005921 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005922 {
5923 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005924 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005925 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005926 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005927#endif
5928
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 /*
5930 * Store character to be displayed.
5931 * Skip characters that are left of the screen for 'nowrap'.
5932 */
5933 vcol_prev = vcol;
5934 if (draw_state < WL_LINE || n_skip <= 0)
5935 {
5936 /*
5937 * Store the character.
5938 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005939#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5941 {
5942 /* A double-wide character is: put first halve in left cell. */
5943 --off;
5944 --col;
5945 }
5946#endif
5947 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005949 {
5950 if ((mb_c & 0xff00) == 0x8e00)
5951 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 else if (enc_utf8)
5955 {
5956 if (mb_utf8)
5957 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005958 int i;
5959
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005961 if ((c & 0xff) == 0)
5962 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005963 for (i = 0; i < Screen_mco; ++i)
5964 {
5965 ScreenLinesC[i][off] = u8cc[i];
5966 if (u8cc[i] == 0)
5967 break;
5968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 }
5970 else
5971 ScreenLinesUC[off] = 0;
5972 }
5973 if (multi_attr)
5974 {
5975 ScreenAttrs[off] = multi_attr;
5976 multi_attr = 0;
5977 }
5978 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 ScreenAttrs[off] = char_attr;
5980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5982 {
5983 /* Need to fill two screen columns. */
5984 ++off;
5985 ++col;
5986 if (enc_utf8)
5987 /* UTF-8: Put a 0 in the second screen char. */
5988 ScreenLines[off] = 0;
5989 else
5990 /* DBCS: Put second byte in the second screen char. */
5991 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005992 if (draw_state > WL_NR
5993#ifdef FEAT_DIFF
5994 && filler_todo <= 0
5995#endif
5996 )
5997 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 /* When "tocol" is halfway a character, set it to the end of
5999 * the character, otherwise highlighting won't stop. */
6000 if (tocol == vcol)
6001 ++tocol;
6002#ifdef FEAT_RIGHTLEFT
6003 if (wp->w_p_rl)
6004 {
6005 /* now it's time to backup one cell */
6006 --off;
6007 --col;
6008 }
6009#endif
6010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011#ifdef FEAT_RIGHTLEFT
6012 if (wp->w_p_rl)
6013 {
6014 --off;
6015 --col;
6016 }
6017 else
6018#endif
6019 {
6020 ++off;
6021 ++col;
6022 }
6023 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006024#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006025 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006026 {
6027 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006028 ++vcol_off;
6029 if (n_extra > 0)
6030 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02006031 if (wp->w_p_wrap)
6032 {
6033 /*
6034 * Special voodoo required if 'wrap' is on.
6035 *
6036 * Advance the column indicator to force the line
6037 * drawing to wrap early. This will make the line
6038 * take up the same screen space when parts are concealed,
6039 * so that cursor line computations aren't messed up.
6040 *
6041 * To avoid the fictitious advance of 'col' causing
6042 * trailing junk to be written out of the screen line
6043 * we are building, 'boguscols' keeps track of the number
6044 * of bad columns we have advanced.
6045 */
6046 if (n_extra > 0)
6047 {
6048 vcol += n_extra;
6049# ifdef FEAT_RIGHTLEFT
6050 if (wp->w_p_rl)
6051 {
6052 col -= n_extra;
6053 boguscols -= n_extra;
6054 }
6055 else
6056# endif
6057 {
6058 col += n_extra;
6059 boguscols += n_extra;
6060 }
6061 n_extra = 0;
6062 n_attr = 0;
6063 }
6064
6065
Bram Moolenaar860cae12010-06-05 23:22:07 +02006066 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6067 {
6068 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006069# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02006070 if (wp->w_p_rl)
6071 {
6072 --boguscols;
6073 --col;
6074 }
6075 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01006076# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006077 {
6078 ++boguscols;
6079 ++col;
6080 }
6081 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006082
6083# ifdef FEAT_RIGHTLEFT
6084 if (wp->w_p_rl)
6085 {
6086 --boguscols;
6087 --col;
6088 }
6089 else
6090# endif
6091 {
6092 ++boguscols;
6093 ++col;
6094 }
6095 }
6096 else
6097 {
6098 if (n_extra > 0)
6099 {
6100 vcol += n_extra;
6101 n_extra = 0;
6102 n_attr = 0;
6103 }
6104 }
6105
6106 }
6107#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 else
6109 --n_skip;
6110
Bram Moolenaar64486672010-05-16 15:46:46 +02006111 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
6112 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00006113 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114#ifdef FEAT_DIFF
6115 && filler_todo <= 0
6116#endif
6117 )
6118 ++vcol;
6119
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006120#ifdef FEAT_SYN_HL
6121 if (vcol_save_attr >= 0)
6122 char_attr = vcol_save_attr;
6123#endif
6124
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 /* restore attributes after "predeces" in 'listchars' */
6126 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6127 char_attr = saved_attr3;
6128
6129 /* restore attributes after last 'listchars' or 'number' char */
6130 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6131 char_attr = saved_attr2;
6132
6133 /*
6134 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006135 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 */
6137 if ((
6138#ifdef FEAT_RIGHTLEFT
6139 wp->w_p_rl ? (col < 0) :
6140#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006141 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 && (*ptr != NUL
6143#ifdef FEAT_DIFF
6144 || filler_todo > 0
6145#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006146 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6148 )
6149 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006150#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006151 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006152 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006153 boguscols = 0;
6154#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006155 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006156 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 ++row;
6159 ++screen_row;
6160
6161 /* When not wrapping and finished diff lines, or when displayed
6162 * '$' and highlighting until last column, break here. */
6163 if ((!wp->w_p_wrap
6164#ifdef FEAT_DIFF
6165 && filler_todo <= 0
6166#endif
6167 ) || lcs_eol_one == -1)
6168 break;
6169
6170 /* When the window is too narrow draw all "@" lines. */
6171 if (draw_state != WL_LINE
6172#ifdef FEAT_DIFF
6173 && filler_todo <= 0
6174#endif
6175 )
6176 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006177 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 row = endrow;
6180 }
6181
6182 /* When line got too long for screen break here. */
6183 if (row == endrow)
6184 {
6185 ++row;
6186 break;
6187 }
6188
6189 if (screen_cur_row == screen_row - 1
6190#ifdef FEAT_DIFF
6191 && filler_todo <= 0
6192#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006193 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 {
6195 /* Remember that the line wraps, used for modeless copy. */
6196 LineWraps[screen_row - 1] = TRUE;
6197
6198 /*
6199 * Special trick to make copy/paste of wrapped lines work with
6200 * xterm/screen: write an extra character beyond the end of
6201 * the line. This will work with all terminal types
6202 * (regardless of the xn,am settings).
6203 * Only do this on a fast tty.
6204 * Only do this if the cursor is on the current line
6205 * (something has been written in it).
6206 * Don't do this for the GUI.
6207 * Don't do this for double-width characters.
6208 * Don't do this for a window not at the right screen border.
6209 */
6210 if (p_tf
6211#ifdef FEAT_GUI
6212 && !gui.in_use
6213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006215 && ((*mb_off2cells)(LineOffset[screen_row],
6216 LineOffset[screen_row] + screen_Columns)
6217 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006219 + (int)Columns - 2,
6220 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006221 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 {
6223 /* First make sure we are at the end of the screen line,
6224 * then output the same character again to let the
6225 * terminal know about the wrap. If the terminal doesn't
6226 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006227 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 screen_char(LineOffset[screen_row - 1]
6229 + (unsigned)Columns - 1,
6230 screen_row - 1, (int)(Columns - 1));
6231
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 /* When there is a multi-byte character, just output a
6233 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006234 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6235 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 out_char(' ');
6237 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 out_char(ScreenLines[LineOffset[screen_row - 1]
6239 + (Columns - 1)]);
6240 /* force a redraw of the first char on the next line */
6241 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6242 screen_start(); /* don't know where cursor is now */
6243 }
6244 }
6245
6246 col = 0;
6247 off = (unsigned)(current_ScreenLine - ScreenLines);
6248#ifdef FEAT_RIGHTLEFT
6249 if (wp->w_p_rl)
6250 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006251 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 off += col;
6253 }
6254#endif
6255
6256 /* reset the drawing state for the start of a wrapped line */
6257 draw_state = WL_START;
6258 saved_n_extra = n_extra;
6259 saved_p_extra = p_extra;
6260 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006261 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 saved_char_attr = char_attr;
6263 n_extra = 0;
6264 lcs_prec_todo = lcs_prec;
6265#ifdef FEAT_LINEBREAK
6266# ifdef FEAT_DIFF
6267 if (filler_todo <= 0)
6268# endif
6269 need_showbreak = TRUE;
6270#endif
6271#ifdef FEAT_DIFF
6272 --filler_todo;
6273 /* When the filler lines are actually below the last line of the
6274 * file, don't draw the line itself, break here. */
6275 if (filler_todo == 0 && wp->w_botfill)
6276 break;
6277#endif
6278 }
6279
6280 } /* for every character in the line */
6281
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006282#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006283 /* After an empty line check first word for capital. */
6284 if (*skipwhite(line) == NUL)
6285 {
6286 capcol_lnum = lnum + 1;
6287 cap_col = 0;
6288 }
6289#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006290#ifdef FEAT_TEXT_PROP
6291 vim_free(text_props);
6292 vim_free(text_prop_idxs);
6293#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006294
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006295 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 return row;
6297}
6298
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006299/*
6300 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006301 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006302 */
6303 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006304comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006305{
6306 int i;
6307
6308 for (i = 0; i < Screen_mco; ++i)
6309 {
6310 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6311 return TRUE;
6312 if (ScreenLinesC[i][off_from] == 0)
6313 break;
6314 }
6315 return FALSE;
6316}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006317
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318/*
6319 * Check whether the given character needs redrawing:
6320 * - the (first byte of the) character is different
6321 * - the attributes are different
6322 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006323 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 */
6325 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006326char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
6328 if (cols > 0
6329 && ((ScreenLines[off_from] != ScreenLines[off_to]
6330 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 || (enc_dbcs != 0
6332 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6333 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6334 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6335 : (cols > 1 && ScreenLines[off_from + 1]
6336 != ScreenLines[off_to + 1])))
6337 || (enc_utf8
6338 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6339 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006340 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006341 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6342 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006343 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 return TRUE;
6345 return FALSE;
6346}
6347
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006348#if defined(FEAT_TERMINAL) || defined(PROTO)
6349/*
6350 * Return the index in ScreenLines[] for the current screen line.
6351 */
6352 int
6353screen_get_current_line_off()
6354{
6355 return (int)(current_ScreenLine - ScreenLines);
6356}
6357#endif
6358
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359/*
6360 * Move one "cooked" screen line to the screen, but only the characters that
6361 * have actually changed. Handle insert/delete character.
6362 * "coloff" gives the first column on the screen for this line.
6363 * "endcol" gives the columns where valid characters are.
6364 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6365 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006366 * "flags" can have bits:
6367 * SLF_POPUP popup window
6368 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6370 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6371 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006372 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006373screen_line(
6374 int row,
6375 int coloff,
6376 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006377 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006378 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
6380 unsigned off_from;
6381 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006382 unsigned max_off_from;
6383 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 int force = FALSE; /* force update rest of the line */
6387 int redraw_this /* bool: does character need redraw? */
6388#ifdef FEAT_GUI
6389 = TRUE /* For GUI when while-loop empty */
6390#endif
6391 ;
6392 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 int clear_next = FALSE;
6394 int char_cells; /* 1: normal char */
6395 /* 2: occupies two display cells */
6396# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006398 /* Check for illegal row and col, just in case. */
6399 if (row >= Rows)
6400 row = Rows - 1;
6401 if (endcol > Columns)
6402 endcol = Columns;
6403
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404# ifdef FEAT_CLIPBOARD
6405 clip_may_clear_selection(row, row);
6406# endif
6407
6408 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6409 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006410 max_off_from = off_from + screen_Columns;
6411 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412
6413#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006414 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 {
6416 /* Clear rest first, because it's left of the text. */
6417 if (clear_width > 0)
6418 {
6419 while (col <= endcol && ScreenLines[off_to] == ' '
6420 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006421 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 {
6423 ++off_to;
6424 ++col;
6425 }
6426 if (col <= endcol)
6427 screen_fill(row, row + 1, col + coloff,
6428 endcol + coloff + 1, ' ', ' ', 0);
6429 }
6430 col = endcol + 1;
6431 off_to = LineOffset[row] + col + coloff;
6432 off_from += col;
6433 endcol = (clear_width > 0 ? clear_width : -clear_width);
6434 }
6435#endif /* FEAT_RIGHTLEFT */
6436
6437 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6438
6439 while (col < endcol)
6440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006442 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 else
6444 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445
6446 redraw_this = redraw_next;
6447 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6448 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6449
6450#ifdef FEAT_GUI
6451 /* If the next character was bold, then redraw the current character to
6452 * remove any pixels that might have spilt over into us. This only
6453 * happens in the GUI.
6454 */
6455 if (redraw_next && gui.in_use)
6456 {
6457 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006458 if (hl > HL_ALL)
6459 hl = syn_attr2attr(hl);
6460 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 redraw_this = TRUE;
6462 }
6463#endif
6464
6465 if (redraw_this)
6466 {
6467 /*
6468 * Special handling when 'xs' termcap flag set (hpterm):
6469 * Attributes for characters are stored at the position where the
6470 * cursor is when writing the highlighting code. The
6471 * start-highlighting code must be written with the cursor on the
6472 * first highlighted character. The stop-highlighting code must
6473 * be written with the cursor just after the last highlighted
6474 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006475 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 * to clear the rest of the line, and force redrawing it
6477 * completely.
6478 */
6479 if ( p_wiv
6480 && !force
6481#ifdef FEAT_GUI
6482 && !gui.in_use
6483#endif
6484 && ScreenAttrs[off_to] != 0
6485 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6486 {
6487 /*
6488 * Need to remove highlighting attributes here.
6489 */
6490 windgoto(row, col + coloff);
6491 out_str(T_CE); /* clear rest of this screen line */
6492 screen_start(); /* don't know where cursor is now */
6493 force = TRUE; /* force redraw of rest of the line */
6494 redraw_next = TRUE; /* or else next char would miss out */
6495
6496 /*
6497 * If the previous character was highlighted, need to stop
6498 * highlighting at this character.
6499 */
6500 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6501 {
6502 screen_attr = ScreenAttrs[off_to - 1];
6503 term_windgoto(row, col + coloff);
6504 screen_stop_highlight();
6505 }
6506 else
6507 screen_attr = 0; /* highlighting has stopped */
6508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (enc_dbcs != 0)
6510 {
6511 /* Check if overwriting a double-byte with a single-byte or
6512 * the other way around requires another character to be
6513 * redrawn. For UTF-8 this isn't needed, because comparing
6514 * ScreenLinesUC[] is sufficient. */
6515 if (char_cells == 1
6516 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006517 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 {
6519 /* Writing a single-cell character over a double-cell
6520 * character: need to redraw the next cell. */
6521 ScreenLines[off_to + 1] = 0;
6522 redraw_next = TRUE;
6523 }
6524 else if (char_cells == 2
6525 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006526 && (*mb_off2cells)(off_to, max_off_to) == 1
6527 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
6529 /* Writing the second half of a double-cell character over
6530 * a double-cell character: need to redraw the second
6531 * cell. */
6532 ScreenLines[off_to + 2] = 0;
6533 redraw_next = TRUE;
6534 }
6535
6536 if (enc_dbcs == DBCS_JPNU)
6537 ScreenLines2[off_to] = ScreenLines2[off_from];
6538 }
6539 /* When writing a single-width character over a double-width
6540 * character and at the end of the redrawn text, need to clear out
6541 * the right halve of the old character.
6542 * Also required when writing the right halve of a double-width
6543 * char over the left halve of an existing one. */
6544 if (has_mbyte && col + char_cells == endcol
6545 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006546 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006548 && (*mb_off2cells)(off_to, max_off_to) == 1
6549 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551
6552 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 if (enc_utf8)
6554 {
6555 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6556 if (ScreenLinesUC[off_from] != 0)
6557 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006558 int i;
6559
6560 for (i = 0; i < Screen_mco; ++i)
6561 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 }
6563 }
6564 if (char_cells == 2)
6565 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
6567#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006568 /* The bold trick makes a single column of pixels appear in the
6569 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 * character should be redrawn too. This happens for our own GUI
6571 * and for some xterms. */
6572 if (
6573# ifdef FEAT_GUI
6574 gui.in_use
6575# endif
6576# if defined(FEAT_GUI) && defined(UNIX)
6577 ||
6578# endif
6579# ifdef UNIX
6580 term_is_xterm
6581# endif
6582 )
6583 {
6584 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006585 if (hl > HL_ALL)
6586 hl = syn_attr2attr(hl);
6587 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 redraw_next = TRUE;
6589 }
6590#endif
6591 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006592
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006593 /* For simplicity set the attributes of second half of a
6594 * double-wide character equal to the first half. */
6595 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006597
6598 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 screen_char(off_to, row, col + coloff);
6602 }
6603 else if ( p_wiv
6604#ifdef FEAT_GUI
6605 && !gui.in_use
6606#endif
6607 && col + coloff > 0)
6608 {
6609 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6610 {
6611 /*
6612 * Don't output stop-highlight when moving the cursor, it will
6613 * stop the highlighting when it should continue.
6614 */
6615 screen_attr = 0;
6616 }
6617 else if (screen_attr != 0)
6618 screen_stop_highlight();
6619 }
6620
6621 off_to += CHAR_CELLS;
6622 off_from += CHAR_CELLS;
6623 col += CHAR_CELLS;
6624 }
6625
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 if (clear_next)
6627 {
6628 /* Clear the second half of a double-wide character of which the left
6629 * half was overwritten with a single-wide character. */
6630 ScreenLines[off_to] = ' ';
6631 if (enc_utf8)
6632 ScreenLinesUC[off_to] = 0;
6633 screen_char(off_to, row, col + coloff);
6634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
6636 if (clear_width > 0
6637#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006638 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639#endif
6640 )
6641 {
6642#ifdef FEAT_GUI
6643 int startCol = col;
6644#endif
6645
6646 /* blank out the rest of the line */
6647 while (col < clear_width && ScreenLines[off_to] == ' '
6648 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006649 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 {
6651 ++off_to;
6652 ++col;
6653 }
6654 if (col < clear_width)
6655 {
6656#ifdef FEAT_GUI
6657 /*
6658 * In the GUI, clearing the rest of the line may leave pixels
6659 * behind if the first character cleared was bold. Some bold
6660 * fonts spill over the left. In this case we redraw the previous
6661 * character too. If we didn't skip any blanks above, then we
6662 * only redraw if the character wasn't already redrawn anyway.
6663 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006664 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
6666 hl = ScreenAttrs[off_to];
6667 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006668 {
6669 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006670
Bram Moolenaar9c697322006-10-09 20:11:17 +00006671 if (enc_utf8)
6672 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6673 * that its width is 2. */
6674 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6675 else if (enc_dbcs != 0)
6676 {
6677 /* find previous character by counting from first
6678 * column and get its width. */
6679 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006680 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006681
6682 while (off < off_to)
6683 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006684 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006685 off += prev_cells;
6686 }
6687 }
6688
6689 if (enc_dbcs != 0 && prev_cells > 1)
6690 screen_char_2(off_to - prev_cells, row,
6691 col + coloff - prev_cells);
6692 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006693 screen_char(off_to - prev_cells, row,
6694 col + coloff - prev_cells);
6695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 }
6697#endif
6698 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6699 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 off_to += clear_width - col;
6701 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 }
6703 }
6704
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006705 if (clear_width > 0
6706#ifdef FEAT_TEXT_PROP
6707 && !(flags & SLF_POPUP) // no separator for popup window
6708#endif
6709 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006711 // For a window that has a right neighbor, draw the separator char
6712 // right of the window contents.
6713 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {
6715 int c;
6716
6717 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006718 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006719 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6720 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 || ScreenAttrs[off_to] != hl)
6722 {
6723 ScreenLines[off_to] = c;
6724 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 if (enc_utf8)
6726 {
6727 if (c >= 0x80)
6728 {
6729 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006730 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 }
6732 else
6733 ScreenLinesUC[off_to] = 0;
6734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 screen_char(off_to, row, col + coloff);
6736 }
6737 }
6738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 LineWraps[row] = FALSE;
6740 }
6741}
6742
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006743#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006745 * Mirror text "str" for right-left displaying.
6746 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006748 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006749rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750{
6751 char_u *p1, *p2;
6752 int t;
6753
6754 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6755 {
6756 t = *p1;
6757 *p1 = *p2;
6758 *p2 = t;
6759 }
6760}
6761#endif
6762
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763/*
6764 * mark all status lines for redraw; used after first :cd
6765 */
6766 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006767status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
6769 win_T *wp;
6770
Bram Moolenaar29323592016-07-24 22:04:11 +02006771 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 if (wp->w_status_height)
6773 {
6774 wp->w_redr_status = TRUE;
6775 redraw_later(VALID);
6776 }
6777}
6778
6779/*
6780 * mark all status lines of the current buffer for redraw
6781 */
6782 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006783status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 win_T *wp;
6786
Bram Moolenaar29323592016-07-24 22:04:11 +02006787 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6789 {
6790 wp->w_redr_status = TRUE;
6791 redraw_later(VALID);
6792 }
6793}
6794
6795/*
6796 * Redraw all status lines that need to be redrawn.
6797 */
6798 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006799redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800{
6801 win_T *wp;
6802
Bram Moolenaar29323592016-07-24 22:04:11 +02006803 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006805 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006806 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006807 draw_tabline();
Bram Moolenaar988c4332019-06-02 14:12:11 +02006808
6809#ifdef FEAT_TEXT_PROP
6810 // Display popup windows on top of the status lines.
6811 update_popups();
6812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814
Bram Moolenaar4033c552017-09-16 20:54:51 +02006815#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816/*
6817 * Redraw all status lines at the bottom of frame "frp".
6818 */
6819 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006820win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
6822 if (frp->fr_layout == FR_LEAF)
6823 frp->fr_win->w_redr_status = TRUE;
6824 else if (frp->fr_layout == FR_ROW)
6825 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006826 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 win_redraw_last_status(frp);
6828 }
6829 else /* frp->fr_layout == FR_COL */
6830 {
6831 frp = frp->fr_child;
6832 while (frp->fr_next != NULL)
6833 frp = frp->fr_next;
6834 win_redraw_last_status(frp);
6835 }
6836}
6837#endif
6838
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839/*
6840 * Draw the verticap separator right of window "wp" starting with line "row".
6841 */
6842 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006843draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
6845 int hl;
6846 int c;
6847
6848 if (wp->w_vsep_width)
6849 {
6850 /* draw the vertical separator right of this window */
6851 c = fillchar_vsep(&hl);
6852 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6853 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6854 c, ' ', hl);
6855 }
6856}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
6858#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006859static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860
6861/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006862 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 */
6864 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006865status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
6867 int len = 0;
6868
6869#ifdef FEAT_MENU
6870 int emenu = (xp->xp_context == EXPAND_MENUS
6871 || xp->xp_context == EXPAND_MENUNAMES);
6872
6873 /* Check for menu separators - replace with '|'. */
6874 if (emenu && menu_is_separator(s))
6875 return 1;
6876#endif
6877
6878 while (*s != NUL)
6879 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006880 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006881 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006882 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 }
6884
6885 return len;
6886}
6887
6888/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006889 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006890 * These are backslashes used for escaping. Do show backslashes in help tags.
6891 */
6892 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006893skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006894{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006895 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006896#ifdef FEAT_MENU
6897 || ((xp->xp_context == EXPAND_MENUS
6898 || xp->xp_context == EXPAND_MENUNAMES)
6899 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6900#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006901 )
6902 {
6903#ifndef BACKSLASH_IN_FILENAME
6904 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6905 return 2;
6906#endif
6907 return 1;
6908 }
6909 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006910}
6911
6912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 * Show wildchar matches in the status line.
6914 * Show at least the "match" item.
6915 * We start at item 'first_match' in the list and show all matches that fit.
6916 *
6917 * If inversion is possible we use it. Else '=' characters are used.
6918 */
6919 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006920win_redr_status_matches(
6921 expand_T *xp,
6922 int num_matches,
6923 char_u **matches, /* list of matches */
6924 int match,
6925 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926{
6927#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6928 int row;
6929 char_u *buf;
6930 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006931 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 int fillchar;
6933 int attr;
6934 int i;
6935 int highlight = TRUE;
6936 char_u *selstart = NULL;
6937 int selstart_col = 0;
6938 char_u *selend = NULL;
6939 static int first_match = 0;
6940 int add_left = FALSE;
6941 char_u *s;
6942#ifdef FEAT_MENU
6943 int emenu;
6944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946
6947 if (matches == NULL) /* interrupted completion? */
6948 return;
6949
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006950 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006951 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006952 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006953 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 if (buf == NULL)
6955 return;
6956
6957 if (match == -1) /* don't show match but original text */
6958 {
6959 match = 0;
6960 highlight = FALSE;
6961 }
6962 /* count 1 for the ending ">" */
6963 clen = status_match_len(xp, L_MATCH(match)) + 3;
6964 if (match == 0)
6965 first_match = 0;
6966 else if (match < first_match)
6967 {
6968 /* jumping left, as far as we can go */
6969 first_match = match;
6970 add_left = TRUE;
6971 }
6972 else
6973 {
6974 /* check if match fits on the screen */
6975 for (i = first_match; i < match; ++i)
6976 clen += status_match_len(xp, L_MATCH(i)) + 2;
6977 if (first_match > 0)
6978 clen += 2;
6979 /* jumping right, put match at the left */
6980 if ((long)clen > Columns)
6981 {
6982 first_match = match;
6983 /* if showing the last match, we can add some on the left */
6984 clen = 2;
6985 for (i = match; i < num_matches; ++i)
6986 {
6987 clen += status_match_len(xp, L_MATCH(i)) + 2;
6988 if ((long)clen >= Columns)
6989 break;
6990 }
6991 if (i == num_matches)
6992 add_left = TRUE;
6993 }
6994 }
6995 if (add_left)
6996 while (first_match > 0)
6997 {
6998 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6999 if ((long)clen >= Columns)
7000 break;
7001 --first_match;
7002 }
7003
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007004 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005
7006 if (first_match == 0)
7007 {
7008 *buf = NUL;
7009 len = 0;
7010 }
7011 else
7012 {
7013 STRCPY(buf, "< ");
7014 len = 2;
7015 }
7016 clen = len;
7017
7018 i = first_match;
7019 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
7020 {
7021 if (i == match)
7022 {
7023 selstart = buf + len;
7024 selstart_col = clen;
7025 }
7026
7027 s = L_MATCH(i);
7028 /* Check for menu separators - replace with '|' */
7029#ifdef FEAT_MENU
7030 emenu = (xp->xp_context == EXPAND_MENUS
7031 || xp->xp_context == EXPAND_MENUNAMES);
7032 if (emenu && menu_is_separator(s))
7033 {
7034 STRCPY(buf + len, transchar('|'));
7035 l = (int)STRLEN(buf + len);
7036 len += l;
7037 clen += l;
7038 }
7039 else
7040#endif
7041 for ( ; *s != NUL; ++s)
7042 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007043 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007045 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {
7047 STRNCPY(buf + len, s, l);
7048 s += l - 1;
7049 len += l;
7050 }
7051 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {
7053 STRCPY(buf + len, transchar_byte(*s));
7054 len += (int)STRLEN(buf + len);
7055 }
7056 }
7057 if (i == match)
7058 selend = buf + len;
7059
7060 *(buf + len++) = ' ';
7061 *(buf + len++) = ' ';
7062 clen += 2;
7063 if (++i == num_matches)
7064 break;
7065 }
7066
7067 if (i != num_matches)
7068 {
7069 *(buf + len++) = '>';
7070 ++clen;
7071 }
7072
7073 buf[len] = NUL;
7074
7075 row = cmdline_row - 1;
7076 if (row >= 0)
7077 {
7078 if (wild_menu_showing == 0)
7079 {
7080 if (msg_scrolled > 0)
7081 {
7082 /* Put the wildmenu just above the command line. If there is
7083 * no room, scroll the screen one line up. */
7084 if (cmdline_row == Rows - 1)
7085 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02007086 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 ++msg_scrolled;
7088 }
7089 else
7090 {
7091 ++cmdline_row;
7092 ++row;
7093 }
7094 wild_menu_showing = WM_SCROLLED;
7095 }
7096 else
7097 {
7098 /* Create status line if needed by setting 'laststatus' to 2.
7099 * Set 'winminheight' to zero to avoid that the window is
7100 * resized. */
7101 if (lastwin->w_status_height == 0)
7102 {
7103 save_p_ls = p_ls;
7104 save_p_wmh = p_wmh;
7105 p_ls = 2;
7106 p_wmh = 0;
7107 last_status(FALSE);
7108 }
7109 wild_menu_showing = WM_SHOWN;
7110 }
7111 }
7112
7113 screen_puts(buf, row, 0, attr);
7114 if (selstart != NULL && highlight)
7115 {
7116 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007117 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 }
7119
7120 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7121 }
7122
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 vim_free(buf);
7125}
7126#endif
7127
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128/*
7129 * Redraw the status line of window wp.
7130 *
7131 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007132 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7133 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007135 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007136win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137{
7138 int row;
7139 char_u *p;
7140 int len;
7141 int fillchar;
7142 int attr;
7143 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007144 static int busy = FALSE;
7145
7146 /* It's possible to get here recursively when 'statusline' (indirectly)
7147 * invokes ":redrawstatus". Simply ignore the call then. */
7148 if (busy)
7149 return;
7150 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
7152 wp->w_redr_status = FALSE;
7153 if (wp->w_status_height == 0)
7154 {
7155 /* no status line, can only be last window */
7156 redraw_cmdline = TRUE;
7157 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007158 else if (!redrawing()
7159#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007160 // don't update status line when popup menu is visible and may be
7161 // drawn over it, unless it will be redrawn later
7162 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007163#endif
7164 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 {
7166 /* Don't redraw right now, do it later. */
7167 wp->w_redr_status = TRUE;
7168 }
7169#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007170 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 {
7172 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007173 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 }
7175#endif
7176 else
7177 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007178 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007180 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 p = NameBuff;
7182 len = (int)STRLEN(p);
7183
Bram Moolenaard85f2712017-07-28 21:51:57 +02007184 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185#ifdef FEAT_QUICKFIX
7186 || wp->w_p_pvw
7187#endif
7188 || bufIsChanged(wp->w_buffer)
7189 || wp->w_buffer->b_p_ro)
7190 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007191 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007193 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 len += (int)STRLEN(p + len);
7195 }
7196#ifdef FEAT_QUICKFIX
7197 if (wp->w_p_pvw)
7198 {
7199 STRCPY(p + len, _("[Preview]"));
7200 len += (int)STRLEN(p + len);
7201 }
7202#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007203 if (bufIsChanged(wp->w_buffer)
7204#ifdef FEAT_TERMINAL
7205 && !bt_terminal(wp->w_buffer)
7206#endif
7207 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 {
7209 STRCPY(p + len, "[+]");
7210 len += 3;
7211 }
7212 if (wp->w_buffer->b_p_ro)
7213 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007214 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007215 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 }
7217
Bram Moolenaar02631462017-09-22 15:20:32 +02007218 this_ru_col = ru_col - (Columns - wp->w_width);
7219 if (this_ru_col < (wp->w_width + 1) / 2)
7220 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 if (this_ru_col <= 1)
7222 {
7223 p = (char_u *)"<"; /* No room for file name! */
7224 len = 1;
7225 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007226 else if (has_mbyte)
7227 {
7228 int clen = 0, i;
7229
7230 /* Count total number of display cells. */
7231 clen = mb_string2cells(p, -1);
7232
7233 /* Find first character that will fit.
7234 * Going from start to end is much faster for DBCS. */
7235 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7236 i += (*mb_ptr2len)(p + i))
7237 clen -= (*mb_ptr2cells)(p + i);
7238 len = clen;
7239 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007241 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007243 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
7245
Bram Moolenaara12a1612019-01-24 16:39:02 +01007246 }
7247 else if (len > this_ru_col - 1)
7248 {
7249 p += len - (this_ru_col - 1);
7250 *p = '<';
7251 len = this_ru_col - 1;
7252 }
7253
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007255 screen_puts(p, row, wp->w_wincol, attr);
7256 screen_fill(row, row + 1, len + wp->w_wincol,
7257 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007259 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7261 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007262 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
7264#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007265 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266#endif
7267 }
7268
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 /*
7270 * May need to draw the character below the vertical separator.
7271 */
7272 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7273 {
7274 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007275 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 else
7277 fillchar = fillchar_vsep(&attr);
7278 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7279 attr);
7280 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007281 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282}
7283
Bram Moolenaar238a5642006-02-21 22:12:05 +00007284#ifdef FEAT_STL_OPT
7285/*
7286 * Redraw the status line according to 'statusline' and take care of any
7287 * errors encountered.
7288 */
7289 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007290redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007291{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007292 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007293 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007294
7295 /* When called recursively return. This can happen when the statusline
7296 * contains an expression that triggers a redraw. */
7297 if (entered)
7298 return;
7299 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007300
Bram Moolenaara742e082016-04-05 21:10:38 +02007301 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007302 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007303 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007304 {
7305 /* When there is an error disable the statusline, otherwise the
7306 * display is messed up with errors and a redraw triggers the problem
7307 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007308 set_string_option_direct((char_u *)"statusline", -1,
7309 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007310 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007311 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007312 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007313 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007314}
7315#endif
7316
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317/*
7318 * Return TRUE if the status line of window "wp" is connected to the status
7319 * line of the window right of it. If not, then it's a vertical separator.
7320 * Only call if (wp->w_vsep_width != 0).
7321 */
7322 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007323stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
7325 frame_T *fr;
7326
7327 fr = wp->w_frame;
7328 while (fr->fr_parent != NULL)
7329 {
7330 if (fr->fr_parent->fr_layout == FR_COL)
7331 {
7332 if (fr->fr_next != NULL)
7333 break;
7334 }
7335 else
7336 {
7337 if (fr->fr_next != NULL)
7338 return TRUE;
7339 }
7340 fr = fr->fr_parent;
7341 }
7342 return FALSE;
7343}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346/*
7347 * Get the value to show for the language mappings, active 'keymap'.
7348 */
7349 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007350get_keymap_str(
7351 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007352 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007353 char_u *buf, /* buffer for the result */
7354 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355{
7356 char_u *p;
7357
7358 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7359 return FALSE;
7360
7361 {
7362#ifdef FEAT_EVAL
7363 buf_T *old_curbuf = curbuf;
7364 win_T *old_curwin = curwin;
7365 char_u *s;
7366
7367 curbuf = wp->w_buffer;
7368 curwin = wp;
7369 STRCPY(buf, "b:keymap_name"); /* must be writable */
7370 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007371 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 --emsg_skip;
7373 curbuf = old_curbuf;
7374 curwin = old_curwin;
7375 if (p == NULL || *p == NUL)
7376#endif
7377 {
7378#ifdef FEAT_KEYMAP
7379 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7380 p = wp->w_buffer->b_p_keymap;
7381 else
7382#endif
7383 p = (char_u *)"lang";
7384 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007385 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 buf[0] = NUL;
7387#ifdef FEAT_EVAL
7388 vim_free(s);
7389#endif
7390 }
7391 return buf[0] != NUL;
7392}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393
7394#if defined(FEAT_STL_OPT) || defined(PROTO)
7395/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007396 * Redraw the status line or ruler of window "wp".
7397 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 */
7399 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007400win_redr_custom(
7401 win_T *wp,
7402 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007404 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 int attr;
7406 int curattr;
7407 int row;
7408 int col = 0;
7409 int maxwidth;
7410 int width;
7411 int n;
7412 int len;
7413 int fillchar;
7414 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007415 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007417 struct stl_hlrec hltab[STL_MAX_ITEM];
7418 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007419 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007420 win_T *ewp;
7421 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422
Bram Moolenaar1d633412013-12-11 15:52:01 +01007423 /* There is a tiny chance that this gets called recursively: When
7424 * redrawing a status line triggers redrawing the ruler or tabline.
7425 * Avoid trouble by not allowing recursion. */
7426 if (entered)
7427 return;
7428 entered = TRUE;
7429
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007431 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007433 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007434 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007435 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007436 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007437 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007438 maxwidth = Columns;
7439# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007440 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007443 else
7444 {
7445 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007446 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007447 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007448
7449 if (draw_ruler)
7450 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007451 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007452 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007453 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007454 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007455 if (*++stl == '-')
7456 stl++;
7457 if (atoi((char *)stl))
7458 while (VIM_ISDIGIT(*stl))
7459 stl++;
7460 if (*stl++ != '(')
7461 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007462 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007463 col = ru_col - (Columns - wp->w_width);
7464 if (col < (wp->w_width + 1) / 2)
7465 col = (wp->w_width + 1) / 2;
7466 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007467 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007468 {
7469 row = Rows - 1;
7470 --maxwidth; /* writing in last column may cause scrolling */
7471 fillchar = ' ';
7472 attr = 0;
7473 }
7474
7475# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007476 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007477# endif
7478 }
7479 else
7480 {
7481 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007482 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007483 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007484 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007485# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007486 use_sandbox = was_set_insecurely((char_u *)"statusline",
7487 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007488# endif
7489 }
7490
Bram Moolenaar53f81742017-09-22 14:35:51 +02007491 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007492 }
7493
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007495 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496
Bram Moolenaar61452852011-02-01 18:01:11 +01007497 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7498 * the cursor away and back. */
7499 ewp = wp == NULL ? curwin : wp;
7500 p_crb_save = ewp->w_p_crb;
7501 ewp->w_p_crb = FALSE;
7502
Bram Moolenaar362f3562009-11-03 16:20:34 +00007503 /* Make a copy, because the statusline may include a function call that
7504 * might change the option value and free the memory. */
7505 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007506 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007507 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007508 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007509 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007510 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007512 /* Make all characters printable. */
7513 p = transstr(buf);
7514 if (p != NULL)
7515 {
7516 vim_strncpy(buf, p, sizeof(buf) - 1);
7517 vim_free(p);
7518 }
7519
7520 /* fill up with "fillchar" */
7521 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007522 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 ++width;
7526 }
7527 buf[len] = NUL;
7528
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007529 /*
7530 * Draw each snippet with the specified highlighting.
7531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 curattr = attr;
7533 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007534 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007536 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 screen_puts_len(p, len, row, col, curattr);
7538 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007539 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007541 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007543 else if (hltab[n].userhl < 0)
7544 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007545#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007546 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7547 && wp->w_status_height != 0)
7548 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007549 else if (wp != NULL && bt_terminal(wp->w_buffer)
7550 && wp->w_status_height != 0)
7551 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007552#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007553 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007554 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007556 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 }
7558 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007559
7560 if (wp == NULL)
7561 {
7562 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7563 col = 0;
7564 len = 0;
7565 p = buf;
7566 fillchar = 0;
7567 for (n = 0; tabtab[n].start != NULL; n++)
7568 {
7569 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7570 while (col < len)
7571 TabPageIdxs[col++] = fillchar;
7572 p = tabtab[n].start;
7573 fillchar = tabtab[n].userhl;
7574 }
7575 while (col < Columns)
7576 TabPageIdxs[col++] = fillchar;
7577 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007578
7579theend:
7580 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581}
7582
7583#endif /* FEAT_STL_OPT */
7584
7585/*
7586 * Output a single character directly to the screen and update ScreenLines.
7587 */
7588 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007589screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 char_u buf[MB_MAXBYTES + 1];
7592
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007593 if (has_mbyte)
7594 buf[(*mb_char2bytes)(c, buf)] = NUL;
7595 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007596 {
7597 buf[0] = c;
7598 buf[1] = NUL;
7599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 screen_puts(buf, row, col, attr);
7601}
7602
7603/*
7604 * Get a single character directly from ScreenLines into "bytes[]".
7605 * Also return its attribute in *attrp;
7606 */
7607 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007608screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
7610 unsigned off;
7611
7612 /* safety check */
7613 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7614 {
7615 off = LineOffset[row] + col;
7616 *attrp = ScreenAttrs[off];
7617 bytes[0] = ScreenLines[off];
7618 bytes[1] = NUL;
7619
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 if (enc_utf8 && ScreenLinesUC[off] != 0)
7621 bytes[utfc_char2bytes(off, bytes)] = NUL;
7622 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7623 {
7624 bytes[0] = ScreenLines[off];
7625 bytes[1] = ScreenLines2[off];
7626 bytes[2] = NUL;
7627 }
7628 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7629 {
7630 bytes[1] = ScreenLines[off + 1];
7631 bytes[2] = NUL;
7632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 }
7634}
7635
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007636/*
7637 * Return TRUE if composing characters for screen posn "off" differs from
7638 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007639 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007640 */
7641 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007642screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007643{
7644 int i;
7645
7646 for (i = 0; i < Screen_mco; ++i)
7647 {
7648 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7649 return TRUE;
7650 if (u8cc[i] == 0)
7651 break;
7652 }
7653 return FALSE;
7654}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007655
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656/*
7657 * Put string '*text' on the screen at position 'row' and 'col', with
7658 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7659 * Note: only outputs within one row, message is truncated at screen boundary!
7660 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7661 */
7662 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007663screen_puts(
7664 char_u *text,
7665 int row,
7666 int col,
7667 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668{
7669 screen_puts_len(text, -1, row, col, attr);
7670}
7671
7672/*
7673 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7674 * a NUL.
7675 */
7676 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007677screen_puts_len(
7678 char_u *text,
7679 int textlen,
7680 int row,
7681 int col,
7682 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683{
7684 unsigned off;
7685 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007686 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007688 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 int mbyte_blen = 1;
7690 int mbyte_cells = 1;
7691 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007692 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007694#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007696 int pc, nc, nc1;
7697 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007699 int force_redraw_this;
7700 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007701 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702
7703 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7704 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007705 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706
Bram Moolenaarc236c162008-07-13 17:41:49 +00007707 /* When drawing over the right halve of a double-wide char clear out the
7708 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007709 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007710#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007711 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007712#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007713 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007714 {
7715 ScreenLines[off - 1] = ' ';
7716 ScreenAttrs[off - 1] = 0;
7717 if (enc_utf8)
7718 {
7719 ScreenLinesUC[off - 1] = 0;
7720 ScreenLinesC[0][off - 1] = 0;
7721 }
7722 /* redraw the previous cell, make it empty */
7723 screen_char(off - 1, row, col - 1);
7724 /* force the cell at "col" to be redrawn */
7725 force_redraw_next = TRUE;
7726 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007727
Bram Moolenaar367329b2007-08-30 11:53:22 +00007728 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007729 while (col < screen_Columns
7730 && (len < 0 || (int)(ptr - text) < len)
7731 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {
7733 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 /* check if this is the first byte of a multibyte */
7735 if (has_mbyte)
7736 {
7737 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007738 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007740 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7742 mbyte_cells = 1;
7743 else if (enc_dbcs != 0)
7744 mbyte_cells = mbyte_blen;
7745 else /* enc_utf8 */
7746 {
7747 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007748 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 (int)((text + len) - ptr));
7750 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007751 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007753#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7755 {
7756 /* Do Arabic shaping. */
7757 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7758 {
7759 /* Past end of string to be displayed. */
7760 nc = NUL;
7761 nc1 = NUL;
7762 }
7763 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007764 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007765 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7766 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007767 nc1 = pcc[0];
7768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 pc = prev_c;
7770 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007771 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 }
7773 else
7774 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007775#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007776 if (col + mbyte_cells > screen_Columns)
7777 {
7778 /* Only 1 cell left, but character requires 2 cells:
7779 * display a '>' in the last column to avoid wrapping. */
7780 c = '>';
7781 mbyte_cells = 1;
7782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 }
7784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007786 force_redraw_this = force_redraw_next;
7787 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007788
7789 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 || (mbyte_cells == 2
7791 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7792 || (enc_dbcs == DBCS_JPNU
7793 && c == 0x8e
7794 && ScreenLines2[off] != ptr[1])
7795 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007796 && (ScreenLinesUC[off] !=
7797 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7798 || (ScreenLinesUC[off] != 0
7799 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007801 || exmode_active;
7802
Bram Moolenaara12a1612019-01-24 16:39:02 +01007803 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 {
7805#if defined(FEAT_GUI) || defined(UNIX)
7806 /* The bold trick makes a single row of pixels appear in the next
7807 * character. When a bold character is removed, the next
7808 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007809 * and for some xterms. */
7810 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811# ifdef FEAT_GUI
7812 gui.in_use
7813# endif
7814# if defined(FEAT_GUI) && defined(UNIX)
7815 ||
7816# endif
7817# ifdef UNIX
7818 term_is_xterm
7819# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007820 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007822 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007824 if (n > HL_ALL)
7825 n = syn_attr2attr(n);
7826 if (n & HL_BOLD)
7827 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 }
7829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 /* When at the end of the text and overwriting a two-cell
7831 * character with a one-cell character, need to clear the next
7832 * cell. Also when overwriting the left halve of a two-cell char
7833 * with the right halve of a two-cell char. Do this only once
7834 * (mb_off2cells() may return 2 on the right halve). */
7835 if (clear_next_cell)
7836 clear_next_cell = FALSE;
7837 else if (has_mbyte
7838 && (len < 0 ? ptr[mbyte_blen] == NUL
7839 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007840 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007842 && (*mb_off2cells)(off, max_off) == 1
7843 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 clear_next_cell = TRUE;
7845
7846 /* Make sure we never leave a second byte of a double-byte behind,
7847 * it confuses mb_off2cells(). */
7848 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007849 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007851 && (*mb_off2cells)(off, max_off) == 1
7852 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 ScreenLines[off] = c;
7855 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 if (enc_utf8)
7857 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007858 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 ScreenLinesUC[off] = 0;
7860 else
7861 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007862 int i;
7863
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007865 for (i = 0; i < Screen_mco; ++i)
7866 {
7867 ScreenLinesC[i][off] = u8cc[i];
7868 if (u8cc[i] == 0)
7869 break;
7870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 }
7872 if (mbyte_cells == 2)
7873 {
7874 ScreenLines[off + 1] = 0;
7875 ScreenAttrs[off + 1] = attr;
7876 }
7877 screen_char(off, row, col);
7878 }
7879 else if (mbyte_cells == 2)
7880 {
7881 ScreenLines[off + 1] = ptr[1];
7882 ScreenAttrs[off + 1] = attr;
7883 screen_char_2(off, row, col);
7884 }
7885 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7886 {
7887 ScreenLines2[off] = ptr[1];
7888 screen_char(off, row, col);
7889 }
7890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 screen_char(off, row, col);
7892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 if (has_mbyte)
7894 {
7895 off += mbyte_cells;
7896 col += mbyte_cells;
7897 ptr += mbyte_blen;
7898 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007899 {
7900 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007902 len = -1;
7903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 }
7905 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {
7907 ++off;
7908 ++col;
7909 ++ptr;
7910 }
7911 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007912
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007913 /* If we detected the next character needs to be redrawn, but the text
7914 * doesn't extend up to there, update the character here. */
7915 if (force_redraw_next && col < screen_Columns)
7916 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007917 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7918 screen_char_2(off, row, col);
7919 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007920 screen_char(off, row, col);
7921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922}
7923
7924#ifdef FEAT_SEARCH_EXTRA
7925/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007926 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 */
7928 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007929start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930{
7931 if (p_hls && !no_hlsearch)
7932 {
7933 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007934 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007935# ifdef FEAT_RELTIME
7936 /* Set the time limit to 'redrawtime'. */
7937 profile_setlimit(p_rdt, &search_hl.tm);
7938# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 }
7940}
7941
7942/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007943 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 */
7945 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007946end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947{
7948 if (search_hl.rm.regprog != NULL)
7949 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007950 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 search_hl.rm.regprog = NULL;
7952 }
7953}
7954
7955/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007956 * Init for calling prepare_search_hl().
7957 */
7958 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007959init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007960{
7961 matchitem_T *cur;
7962
7963 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7964 * match */
7965 cur = wp->w_match_head;
7966 while (cur != NULL)
7967 {
7968 cur->hl.rm = cur->match;
7969 if (cur->hlg_id == 0)
7970 cur->hl.attr = 0;
7971 else
7972 cur->hl.attr = syn_id2attr(cur->hlg_id);
7973 cur->hl.buf = wp->w_buffer;
7974 cur->hl.lnum = 0;
7975 cur->hl.first_lnum = 0;
7976# ifdef FEAT_RELTIME
7977 /* Set the time limit to 'redrawtime'. */
7978 profile_setlimit(p_rdt, &(cur->hl.tm));
7979# endif
7980 cur = cur->next;
7981 }
7982 search_hl.buf = wp->w_buffer;
7983 search_hl.lnum = 0;
7984 search_hl.first_lnum = 0;
7985 /* time limit is set at the toplevel, for all windows */
7986}
7987
7988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 * Advance to the match in window "wp" line "lnum" or past it.
7990 */
7991 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007992prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007994 matchitem_T *cur; /* points to the match list */
7995 match_T *shl; /* points to search_hl or a match */
7996 int shl_flag; /* flag to indicate whether search_hl
7997 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007998 int pos_inprogress; /* marks that position match search is
7999 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 int n;
8001
8002 /*
8003 * When using a multi-line pattern, start searching at the top
8004 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008005 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008007 cur = wp->w_match_head;
8008 shl_flag = FALSE;
8009 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008011 if (shl_flag == FALSE)
8012 {
8013 shl = &search_hl;
8014 shl_flag = TRUE;
8015 }
8016 else
8017 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 if (shl->rm.regprog != NULL
8019 && shl->lnum == 0
8020 && re_multiline(shl->rm.regprog))
8021 {
8022 if (shl->first_lnum == 0)
8023 {
8024# ifdef FEAT_FOLDING
8025 for (shl->first_lnum = lnum;
8026 shl->first_lnum > wp->w_topline; --shl->first_lnum)
8027 if (hasFoldingWin(wp, shl->first_lnum - 1,
8028 NULL, NULL, TRUE, NULL))
8029 break;
8030# else
8031 shl->first_lnum = wp->w_topline;
8032# endif
8033 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008034 if (cur != NULL)
8035 cur->pos.cur = 0;
8036 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008038 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
8039 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02008041 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
8042 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008043 pos_inprogress = cur == NULL || cur->pos.cur == 0
8044 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 if (shl->lnum != 0)
8046 {
8047 shl->first_lnum = shl->lnum
8048 + shl->rm.endpos[0].lnum
8049 - shl->rm.startpos[0].lnum;
8050 n = shl->rm.endpos[0].col;
8051 }
8052 else
8053 {
8054 ++shl->first_lnum;
8055 n = 0;
8056 }
8057 }
8058 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008059 if (shl != &search_hl && cur != NULL)
8060 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 }
8062}
8063
8064/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008065 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 * Uses shl->buf.
8067 * Sets shl->lnum and shl->rm contents.
8068 * Note: Assumes a previous match is always before "lnum", unless
8069 * shl->lnum is zero.
8070 * Careful: Any pointers for buffer lines will become invalid.
8071 */
8072 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008073next_search_hl(
8074 win_T *win,
8075 match_T *shl, /* points to search_hl or a match */
8076 linenr_T lnum,
8077 colnr_T mincol, /* minimal column for a match */
8078 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079{
8080 linenr_T l;
8081 colnr_T matchcol;
8082 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008083 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008085 // for :{range}s/pat only highlight inside the range
8086 if (lnum < search_first_line || lnum > search_last_line)
8087 {
8088 shl->lnum = 0;
8089 return;
8090 }
8091
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 if (shl->lnum != 0)
8093 {
8094 /* Check for three situations:
8095 * 1. If the "lnum" is below a previous match, start a new search.
8096 * 2. If the previous match includes "mincol", use it.
8097 * 3. Continue after the previous match.
8098 */
8099 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8100 if (lnum > l)
8101 shl->lnum = 0;
8102 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8103 return;
8104 }
8105
8106 /*
8107 * Repeat searching for a match until one is found that includes "mincol"
8108 * or none is found in this line.
8109 */
8110 called_emsg = FALSE;
8111 for (;;)
8112 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008113#ifdef FEAT_RELTIME
8114 /* Stop searching after passing the time limit. */
8115 if (profile_passed_limit(&(shl->tm)))
8116 {
8117 shl->lnum = 0; /* no match found in time */
8118 break;
8119 }
8120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 /* Three situations:
8122 * 1. No useful previous match: search from start of line.
8123 * 2. Not Vi compatible or empty match: continue at next character.
8124 * Break the loop if this is beyond the end of the line.
8125 * 3. Vi compatible searching: continue at end of previous match.
8126 */
8127 if (shl->lnum == 0)
8128 matchcol = 0;
8129 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8130 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008131 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008133 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008134
8135 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008136 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008137 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008139 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 shl->lnum = 0;
8141 break;
8142 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008143 if (has_mbyte)
8144 matchcol += mb_ptr2len(ml);
8145 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008146 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
8148 else
8149 matchcol = shl->rm.endpos[0].col;
8150
8151 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008152 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008154 /* Remember whether shl->rm is using a copy of the regprog in
8155 * cur->match. */
8156 int regprog_is_copy = (shl != &search_hl && cur != NULL
8157 && shl == &cur->hl
8158 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008159 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008160
Bram Moolenaarb3414592014-06-17 17:48:32 +02008161 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8162 matchcol,
8163#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008164 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008165#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008166 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008167#endif
8168 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008169 /* Copy the regprog, in case it got freed and recompiled. */
8170 if (regprog_is_copy)
8171 cur->match.regprog = cur->hl.rm.regprog;
8172
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008173 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008174 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008175 /* Error while handling regexp: stop using this regexp. */
8176 if (shl == &search_hl)
8177 {
8178 /* don't free regprog in the match list, it's a copy */
8179 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008180 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008181 }
8182 shl->rm.regprog = NULL;
8183 shl->lnum = 0;
8184 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8185 message */
8186 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008187 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008188 }
8189 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008190 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008191 else
8192 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 if (nmatched == 0)
8194 {
8195 shl->lnum = 0; /* no match found */
8196 break;
8197 }
8198 if (shl->rm.startpos[0].lnum > 0
8199 || shl->rm.startpos[0].col >= mincol
8200 || nmatched > 1
8201 || shl->rm.endpos[0].col > mincol)
8202 {
8203 shl->lnum += shl->rm.startpos[0].lnum;
8204 break; /* useful match found */
8205 }
8206 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008207
8208 // Restore called_emsg for assert_fails().
8209 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211
Bram Moolenaar85077472016-10-16 14:35:48 +02008212/*
8213 * If there is a match fill "shl" and return one.
8214 * Return zero otherwise.
8215 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008216 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008217next_search_hl_pos(
8218 match_T *shl, /* points to a match */
8219 linenr_T lnum,
8220 posmatch_T *posmatch, /* match positions */
8221 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008222{
8223 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008224 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008225
Bram Moolenaarb3414592014-06-17 17:48:32 +02008226 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8227 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008228 llpos_T *pos = &posmatch->pos[i];
8229
8230 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008231 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008232 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008233 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008234 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008235 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008236 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008237 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008238 /* if this match comes before the one at "found" then swap
8239 * them */
8240 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008241 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008242 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008243
Bram Moolenaar85077472016-10-16 14:35:48 +02008244 *pos = posmatch->pos[found];
8245 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008246 }
8247 }
8248 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008249 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008250 }
8251 }
8252 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008253 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008254 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008255 colnr_T start = posmatch->pos[found].col == 0
8256 ? 0 : posmatch->pos[found].col - 1;
8257 colnr_T end = posmatch->pos[found].col == 0
8258 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008259
Bram Moolenaar85077472016-10-16 14:35:48 +02008260 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008261 shl->rm.startpos[0].lnum = 0;
8262 shl->rm.startpos[0].col = start;
8263 shl->rm.endpos[0].lnum = 0;
8264 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008265 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008266 posmatch->cur = found + 1;
8267 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008268 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008269 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008270}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008271#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008272
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008274screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275{
8276 attrentry_T *aep = NULL;
8277
8278 screen_attr = attr;
8279 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008280#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 && termcap_active
8282#endif
8283 )
8284 {
8285#ifdef FEAT_GUI
8286 if (gui.in_use)
8287 {
8288 char buf[20];
8289
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008290 /* The GUI handles this internally. */
8291 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 OUT_STR(buf);
8293 }
8294 else
8295#endif
8296 {
8297 if (attr > HL_ALL) /* special HL attr. */
8298 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008299 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 aep = syn_cterm_attr2entry(attr);
8301 else
8302 aep = syn_term_attr2entry(attr);
8303 if (aep == NULL) /* did ":syntax clear" */
8304 attr = 0;
8305 else
8306 attr = aep->ae_attr;
8307 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008308 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008310 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008311#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008312 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8313 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8314 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008315#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008316 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008317 /* If the Normal FG color has BOLD attribute and the new HL
8318 * has a FG color defined, clear BOLD. */
8319 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008320 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008322 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008323 out_str(T_UCS);
8324 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008325 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8326 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008328 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008330 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008332 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008333 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334
8335 /*
8336 * Output the color or start string after bold etc., in case the
8337 * bold etc. override the color setting.
8338 */
8339 if (aep != NULL)
8340 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008341#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008342 /* When 'termguicolors' is set but fg or bg is unset,
8343 * fall back to the cterm colors. This helps for SpellBad,
8344 * where the GUI uses a red undercurl. */
8345 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008347 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008348 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008349 }
8350 else
8351#endif
8352 if (t_colors > 1)
8353 {
8354 if (aep->ae_u.cterm.fg_color)
8355 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8356 }
8357#ifdef FEAT_TERMGUICOLORS
8358 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8359 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008360 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008361 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 }
8363 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008364#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008365 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008367 if (aep->ae_u.cterm.bg_color)
8368 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8369 }
8370
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008371 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008372 {
8373 if (aep->ae_u.term.start != NULL)
8374 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 }
8376 }
8377 }
8378 }
8379}
8380
8381 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008382screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383{
8384 int do_ME = FALSE; /* output T_ME code */
8385
8386 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008387#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 && termcap_active
8389#endif
8390 )
8391 {
8392#ifdef FEAT_GUI
8393 if (gui.in_use)
8394 {
8395 char buf[20];
8396
8397 /* use internal GUI code */
8398 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8399 OUT_STR(buf);
8400 }
8401 else
8402#endif
8403 {
8404 if (screen_attr > HL_ALL) /* special HL attr. */
8405 {
8406 attrentry_T *aep;
8407
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008408 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 {
8410 /*
8411 * Assume that t_me restores the original colors!
8412 */
8413 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008414 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008415#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008416 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8417 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8418 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008419#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008420 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008421#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008422 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8423 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8424 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008425#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008426 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 do_ME = TRUE;
8428 }
8429 else
8430 {
8431 aep = syn_term_attr2entry(screen_attr);
8432 if (aep != NULL && aep->ae_u.term.stop != NULL)
8433 {
8434 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8435 do_ME = TRUE;
8436 else
8437 out_str(aep->ae_u.term.stop);
8438 }
8439 }
8440 if (aep == NULL) /* did ":syntax clear" */
8441 screen_attr = 0;
8442 else
8443 screen_attr = aep->ae_attr;
8444 }
8445
8446 /*
8447 * Often all ending-codes are equal to T_ME. Avoid outputting the
8448 * same sequence several times.
8449 */
8450 if (screen_attr & HL_STANDOUT)
8451 {
8452 if (STRCMP(T_SE, T_ME) == 0)
8453 do_ME = TRUE;
8454 else
8455 out_str(T_SE);
8456 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008457 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008458 {
8459 if (STRCMP(T_UCE, T_ME) == 0)
8460 do_ME = TRUE;
8461 else
8462 out_str(T_UCE);
8463 }
8464 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008465 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 {
8467 if (STRCMP(T_UE, T_ME) == 0)
8468 do_ME = TRUE;
8469 else
8470 out_str(T_UE);
8471 }
8472 if (screen_attr & HL_ITALIC)
8473 {
8474 if (STRCMP(T_CZR, T_ME) == 0)
8475 do_ME = TRUE;
8476 else
8477 out_str(T_CZR);
8478 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008479 if (screen_attr & HL_STRIKETHROUGH)
8480 {
8481 if (STRCMP(T_STE, T_ME) == 0)
8482 do_ME = TRUE;
8483 else
8484 out_str(T_STE);
8485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8487 out_str(T_ME);
8488
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008489#ifdef FEAT_TERMGUICOLORS
8490 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008492 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008493 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008494 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008495 term_bg_rgb_color(cterm_normal_bg_gui_color);
8496 }
8497 else
8498#endif
8499 {
8500 if (t_colors > 1)
8501 {
8502 /* set Normal cterm colors */
8503 if (cterm_normal_fg_color != 0)
8504 term_fg_color(cterm_normal_fg_color - 1);
8505 if (cterm_normal_bg_color != 0)
8506 term_bg_color(cterm_normal_bg_color - 1);
8507 if (cterm_normal_fg_bold)
8508 out_str(T_MD);
8509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 }
8511 }
8512 }
8513 screen_attr = 0;
8514}
8515
8516/*
8517 * Reset the colors for a cterm. Used when leaving Vim.
8518 * The machine specific code may override this again.
8519 */
8520 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008521reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008523 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 {
8525 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008526#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008527 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8528 || cterm_normal_bg_gui_color != INVALCOLOR)
8529 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008530#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 {
8534 out_str(T_OP);
8535 screen_attr = -1;
8536 }
8537 if (cterm_normal_fg_bold)
8538 {
8539 out_str(T_ME);
8540 screen_attr = -1;
8541 }
8542 }
8543}
8544
8545/*
8546 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8547 * using the attributes from ScreenAttrs["off"].
8548 */
8549 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008550screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551{
8552 int attr;
8553
8554 /* Check for illegal values, just in case (could happen just after
8555 * resizing). */
8556 if (row >= screen_Rows || col >= screen_Columns)
8557 return;
8558
Bram Moolenaarae654382019-01-17 21:09:05 +01008559#ifdef FEAT_INS_EXPAND
8560 if (pum_under_menu(row, col))
8561 return;
8562#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008563 /* Outputting a character in the last cell on the screen may scroll the
8564 * screen up. Only do it when the "xn" termcap property is set, otherwise
8565 * mark the character invalid (update it when scrolled up). */
8566 if (*T_XN == NUL
8567 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568#ifdef FEAT_RIGHTLEFT
8569 /* account for first command-line character in rightleft mode */
8570 && !cmdmsg_rl
8571#endif
8572 )
8573 {
8574 ScreenAttrs[off] = (sattr_T)-1;
8575 return;
8576 }
8577
8578 /*
8579 * Stop highlighting first, so it's easier to move the cursor.
8580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 if (screen_char_attr != 0)
8582 attr = screen_char_attr;
8583 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 attr = ScreenAttrs[off];
8585 if (screen_attr != attr)
8586 screen_stop_highlight();
8587
8588 windgoto(row, col);
8589
8590 if (screen_attr != attr)
8591 screen_start_highlight(attr);
8592
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 if (enc_utf8 && ScreenLinesUC[off] != 0)
8594 {
8595 char_u buf[MB_MAXBYTES + 1];
8596
Bram Moolenaarcb070082016-04-02 22:14:51 +02008597 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008598 {
8599 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008600#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008601 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008602#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008603 )
8604 {
8605 /* Clear the two screen cells. If the character is actually
8606 * single width it won't change the second cell. */
8607 out_str((char_u *)" ");
8608 term_windgoto(row, col);
8609 }
8610 /* not sure where the cursor is after drawing the ambiguous width
8611 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008612 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008613 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008614 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008616
8617 /* Convert the UTF-8 character to bytes and write it. */
8618 buf[utfc_char2bytes(off, buf)] = NUL;
8619 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 }
8621 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 /* double-byte character in single-width cell */
8626 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8627 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 }
8629
8630 screen_cur_col++;
8631}
8632
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633/*
8634 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8635 * on the screen at position 'row' and 'col'.
8636 * The attributes of the first byte is used for all. This is required to
8637 * output the two bytes of a double-byte character with nothing in between.
8638 */
8639 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008640screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
8642 /* Check for illegal values (could be wrong when screen was resized). */
8643 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8644 return;
8645
8646 /* Outputting the last character on the screen may scrollup the screen.
8647 * Don't to it! Mark the character invalid (update it when scrolled up) */
8648 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8649 {
8650 ScreenAttrs[off] = (sattr_T)-1;
8651 return;
8652 }
8653
8654 /* Output the first byte normally (positions the cursor), then write the
8655 * second byte directly. */
8656 screen_char(off, row, col);
8657 out_char(ScreenLines[off + 1]);
8658 ++screen_cur_col;
8659}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661/*
8662 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8663 * This uses the contents of ScreenLines[] and doesn't change it.
8664 */
8665 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008666screen_draw_rectangle(
8667 int row,
8668 int col,
8669 int height,
8670 int width,
8671 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672{
8673 int r, c;
8674 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008675 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008677 /* Can't use ScreenLines unless initialized */
8678 if (ScreenLines == NULL)
8679 return;
8680
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 if (invert)
8682 screen_char_attr = HL_INVERSE;
8683 for (r = row; r < row + height; ++r)
8684 {
8685 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008686 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 for (c = col; c < col + width; ++c)
8688 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008689 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 {
8691 screen_char_2(off + c, r, c);
8692 ++c;
8693 }
8694 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 {
8696 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008697 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 }
8700 }
8701 }
8702 screen_char_attr = 0;
8703}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705/*
8706 * Redraw the characters for a vertically split window.
8707 */
8708 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008709redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710{
8711 int col;
8712 int width;
8713
8714# ifdef FEAT_CLIPBOARD
8715 clip_may_clear_selection(row, end - 1);
8716# endif
8717
8718 if (wp == NULL)
8719 {
8720 col = 0;
8721 width = Columns;
8722 }
8723 else
8724 {
8725 col = wp->w_wincol;
8726 width = wp->w_width;
8727 }
8728 screen_draw_rectangle(row, col, end - row, width, FALSE);
8729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008731 static void
8732space_to_screenline(int off, int attr)
8733{
8734 ScreenLines[off] = ' ';
8735 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008736 if (enc_utf8)
8737 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008738}
8739
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740/*
8741 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8742 * with character 'c1' in first column followed by 'c2' in the other columns.
8743 * Use attributes 'attr'.
8744 */
8745 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008746screen_fill(
8747 int start_row,
8748 int end_row,
8749 int start_col,
8750 int end_col,
8751 int c1,
8752 int c2,
8753 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754{
8755 int row;
8756 int col;
8757 int off;
8758 int end_off;
8759 int did_delete;
8760 int c;
8761 int norm_term;
8762#if defined(FEAT_GUI) || defined(UNIX)
8763 int force_next = FALSE;
8764#endif
8765
8766 if (end_row > screen_Rows) /* safety check */
8767 end_row = screen_Rows;
8768 if (end_col > screen_Columns) /* safety check */
8769 end_col = screen_Columns;
8770 if (ScreenLines == NULL
8771 || start_row >= end_row
8772 || start_col >= end_col) /* nothing to do */
8773 return;
8774
8775 /* it's a "normal" terminal when not in a GUI or cterm */
8776 norm_term = (
8777#ifdef FEAT_GUI
8778 !gui.in_use &&
8779#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008780 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 for (row = start_row; row < end_row; ++row)
8782 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008783 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008784#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008785 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008786#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008787 )
8788 {
8789 /* When drawing over the right halve of a double-wide char clear
8790 * out the left halve. When drawing over the left halve of a
8791 * double wide-char clear out the right halve. Only needed in a
8792 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008793 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008794 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008795 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008796 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 /*
8799 * Try to use delete-line termcap code, when no attributes or in a
8800 * "normal" terminal, where a bold/italic space is just a
8801 * space.
8802 */
8803 did_delete = FALSE;
8804 if (c2 == ' '
8805 && end_col == Columns
8806 && can_clear(T_CE)
8807 && (attr == 0
8808 || (norm_term
8809 && attr <= HL_ALL
8810 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8811 {
8812 /*
8813 * check if we really need to clear something
8814 */
8815 col = start_col;
8816 if (c1 != ' ') /* don't clear first char */
8817 ++col;
8818
8819 off = LineOffset[row] + col;
8820 end_off = LineOffset[row] + end_col;
8821
8822 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 if (enc_utf8)
8824 while (off < end_off && ScreenLines[off] == ' '
8825 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8826 ++off;
8827 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 while (off < end_off && ScreenLines[off] == ' '
8829 && ScreenAttrs[off] == 0)
8830 ++off;
8831 if (off < end_off) /* something to be cleared */
8832 {
8833 col = off - LineOffset[row];
8834 screen_stop_highlight();
8835 term_windgoto(row, col);/* clear rest of this screen line */
8836 out_str(T_CE);
8837 screen_start(); /* don't know where cursor is now */
8838 col = end_col - col;
8839 while (col--) /* clear chars in ScreenLines */
8840 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008841 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 ++off;
8843 }
8844 }
8845 did_delete = TRUE; /* the chars are cleared now */
8846 }
8847
8848 off = LineOffset[row] + start_col;
8849 c = c1;
8850 for (col = start_col; col < end_col; ++col)
8851 {
8852 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008853 || (enc_utf8 && (int)ScreenLinesUC[off]
8854 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 || ScreenAttrs[off] != attr
8856#if defined(FEAT_GUI) || defined(UNIX)
8857 || force_next
8858#endif
8859 )
8860 {
8861#if defined(FEAT_GUI) || defined(UNIX)
8862 /* The bold trick may make a single row of pixels appear in
8863 * the next character. When a bold character is removed, the
8864 * next character should be redrawn too. This happens for our
8865 * own GUI and for some xterms. */
8866 if (
8867# ifdef FEAT_GUI
8868 gui.in_use
8869# endif
8870# if defined(FEAT_GUI) && defined(UNIX)
8871 ||
8872# endif
8873# ifdef UNIX
8874 term_is_xterm
8875# endif
8876 )
8877 {
8878 if (ScreenLines[off] != ' '
8879 && (ScreenAttrs[off] > HL_ALL
8880 || ScreenAttrs[off] & HL_BOLD))
8881 force_next = TRUE;
8882 else
8883 force_next = FALSE;
8884 }
8885#endif
8886 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 if (enc_utf8)
8888 {
8889 if (c >= 0x80)
8890 {
8891 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008892 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 }
8894 else
8895 ScreenLinesUC[off] = 0;
8896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 ScreenAttrs[off] = attr;
8898 if (!did_delete || c != ' ')
8899 screen_char(off, row, col);
8900 }
8901 ++off;
8902 if (col == start_col)
8903 {
8904 if (did_delete)
8905 break;
8906 c = c2;
8907 }
8908 }
8909 if (end_col == Columns)
8910 LineWraps[row] = FALSE;
8911 if (row == Rows - 1) /* overwritten the command line */
8912 {
8913 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008914 if (start_col == 0 && end_col == Columns
8915 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008917 if (start_col == 0)
8918 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 }
8920 }
8921}
8922
8923/*
8924 * Check if there should be a delay. Used before clearing or redrawing the
8925 * screen or the command line.
8926 */
8927 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008928check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929{
8930 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8931 && !did_wait_return
8932 && emsg_silent == 0)
8933 {
8934 out_flush();
8935 ui_delay(1000L, TRUE);
8936 emsg_on_display = FALSE;
8937 if (check_msg_scroll)
8938 msg_scroll = FALSE;
8939 }
8940}
8941
8942/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008943 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8944 */
8945 static void
8946clear_TabPageIdxs(void)
8947{
8948 int scol;
8949
8950 for (scol = 0; scol < Columns; ++scol)
8951 TabPageIdxs[scol] = 0;
8952}
8953
8954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008956 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 * Returns TRUE if there is a valid screen to write to.
8958 * Returns FALSE when starting up and screen not initialized yet.
8959 */
8960 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008961screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008963 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 return (ScreenLines != NULL);
8965}
8966
8967/*
8968 * Resize the shell to Rows and Columns.
8969 * Allocate ScreenLines[] and associated items.
8970 *
8971 * There may be some time between setting Rows and Columns and (re)allocating
8972 * ScreenLines[]. This happens when starting up and when (manually) changing
8973 * the shell size. Always use screen_Rows and screen_Columns to access items
8974 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8975 * final size of the shell is needed.
8976 */
8977 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008978screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
8980 int new_row, old_row;
8981#ifdef FEAT_GUI
8982 int old_Rows;
8983#endif
8984 win_T *wp;
8985 int outofmem = FALSE;
8986 int len;
8987 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008989 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008991 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 sattr_T *new_ScreenAttrs;
8993 unsigned *new_LineOffset;
8994 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008995 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008996 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008998 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008999 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009001retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 /*
9003 * Allocation of the screen buffers is done only when the size changes and
9004 * when Rows and Columns have been set and we have started doing full
9005 * screen stuff.
9006 */
9007 if ((ScreenLines != NULL
9008 && Rows == screen_Rows
9009 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 && enc_utf8 == (ScreenLinesUC != NULL)
9011 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01009012 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 || Rows == 0
9014 || Columns == 0
9015 || (!full_screen && ScreenLines == NULL))
9016 return;
9017
9018 /*
9019 * It's possible that we produce an out-of-memory message below, which
9020 * will cause this function to be called again. To break the loop, just
9021 * return here.
9022 */
9023 if (entered)
9024 return;
9025 entered = TRUE;
9026
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009027 /*
9028 * Note that the window sizes are updated before reallocating the arrays,
9029 * thus we must not redraw here!
9030 */
9031 ++RedrawingDisabled;
9032
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 win_new_shellsize(); /* fit the windows in the new sized shell */
9034
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 comp_col(); /* recompute columns for shown command and ruler */
9036
9037 /*
9038 * We're changing the size of the screen.
9039 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9040 * - Move lines from the old arrays into the new arrays, clear extra
9041 * lines (unless the screen is going to be cleared).
9042 * - Free the old arrays.
9043 *
9044 * If anything fails, make ScreenLines NULL, so we don't do anything!
9045 * Continuing with the old ScreenLines may result in a crash, because the
9046 * size is wrong.
9047 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009048 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009050 if (aucmd_win != NULL)
9051 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009052#ifdef FEAT_TEXT_PROP
9053 // global popup windows
9054 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9055 win_free_lsize(wp);
9056 // tab-local popup windows
9057 FOR_ALL_TABPAGES(tp)
9058 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9059 win_free_lsize(wp);
9060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009062 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01009063 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 if (enc_utf8)
9065 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009066 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009067 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009068 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
9069 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 }
9071 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009072 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
9073 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
9074 new_LineOffset = LALLOC_MULT(unsigned, Rows);
9075 new_LineWraps = LALLOC_MULT(char_u, Rows);
9076 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009078 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 {
9080 if (win_alloc_lines(wp) == FAIL)
9081 {
9082 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009083 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 }
9085 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009086 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9087 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009088 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009089#ifdef FEAT_TEXT_PROP
9090 // global popup windows
9091 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9092 if (win_alloc_lines(wp) == FAIL)
9093 {
9094 outofmem = TRUE;
9095 goto give_up;
9096 }
9097 // tab-local popup windows
9098 FOR_ALL_TABPAGES(tp)
9099 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9100 if (win_alloc_lines(wp) == FAIL)
9101 {
9102 outofmem = TRUE;
9103 goto give_up;
9104 }
9105#endif
9106
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009107give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009109 for (i = 0; i < p_mco; ++i)
9110 if (new_ScreenLinesC[i] == NULL)
9111 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009113 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 || new_ScreenAttrs == NULL
9116 || new_LineOffset == NULL
9117 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009118 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 || outofmem)
9120 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009121 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009122 {
9123 /* guess the size */
9124 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9125
9126 /* Remember we did this to avoid getting outofmem messages over
9127 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009128 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009129 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009130 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009131 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009132 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009133 VIM_CLEAR(new_ScreenLinesC[i]);
9134 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009135 VIM_CLEAR(new_ScreenAttrs);
9136 VIM_CLEAR(new_LineOffset);
9137 VIM_CLEAR(new_LineWraps);
9138 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 }
9140 else
9141 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009142 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009143
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 for (new_row = 0; new_row < Rows; ++new_row)
9145 {
9146 new_LineOffset[new_row] = new_row * Columns;
9147 new_LineWraps[new_row] = FALSE;
9148
9149 /*
9150 * If the screen is not going to be cleared, copy as much as
9151 * possible from the old screen to the new one and clear the rest
9152 * (used when resizing the window at the "--more--" prompt or when
9153 * executing an external command, for the GUI).
9154 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009155 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 {
9157 (void)vim_memset(new_ScreenLines + new_row * Columns,
9158 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 if (enc_utf8)
9160 {
9161 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9162 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009163 for (i = 0; i < p_mco; ++i)
9164 (void)vim_memset(new_ScreenLinesC[i]
9165 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 0, (size_t)Columns * sizeof(u8char_T));
9167 }
9168 if (enc_dbcs == DBCS_JPNU)
9169 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9170 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9172 0, (size_t)Columns * sizeof(sattr_T));
9173 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009174 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 {
9176 if (screen_Columns < Columns)
9177 len = screen_Columns;
9178 else
9179 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009180 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009181 * may be invalid now. Also when p_mco changes. */
9182 if (!(enc_utf8 && ScreenLinesUC == NULL)
9183 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009184 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9185 ScreenLines + LineOffset[old_row],
9186 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009187 if (enc_utf8 && ScreenLinesUC != NULL
9188 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 {
9190 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9191 ScreenLinesUC + LineOffset[old_row],
9192 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009193 for (i = 0; i < p_mco; ++i)
9194 mch_memmove(new_ScreenLinesC[i]
9195 + new_LineOffset[new_row],
9196 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 (size_t)len * sizeof(u8char_T));
9198 }
9199 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9200 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9201 ScreenLines2 + LineOffset[old_row],
9202 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9204 ScreenAttrs + LineOffset[old_row],
9205 (size_t)len * sizeof(sattr_T));
9206 }
9207 }
9208 }
9209 /* Use the last line of the screen for the current line. */
9210 current_ScreenLine = new_ScreenLines + Rows * Columns;
9211 }
9212
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009213 free_screenlines();
9214
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009217 for (i = 0; i < p_mco; ++i)
9218 ScreenLinesC[i] = new_ScreenLinesC[i];
9219 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 ScreenAttrs = new_ScreenAttrs;
9222 LineOffset = new_LineOffset;
9223 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009224 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225
9226 /* It's important that screen_Rows and screen_Columns reflect the actual
9227 * size of ScreenLines[]. Set them before calling anything. */
9228#ifdef FEAT_GUI
9229 old_Rows = screen_Rows;
9230#endif
9231 screen_Rows = Rows;
9232 screen_Columns = Columns;
9233
9234 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009235 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237#ifdef FEAT_GUI
9238 else if (gui.in_use
9239 && !gui.starting
9240 && ScreenLines != NULL
9241 && old_Rows != Rows)
9242 {
9243 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9244 /*
9245 * Adjust the position of the cursor, for when executing an external
9246 * command.
9247 */
9248 if (msg_row >= Rows) /* Rows got smaller */
9249 msg_row = Rows - 1; /* put cursor at last row */
9250 else if (Rows > old_Rows) /* Rows got bigger */
9251 msg_row += Rows - old_Rows; /* put cursor in same place */
9252 if (msg_col >= Columns) /* Columns got smaller */
9253 msg_col = Columns - 1; /* put cursor at last column */
9254 }
9255#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009256 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009259 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009260
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009261 /*
9262 * Do not apply autocommands more than 3 times to avoid an endless loop
9263 * in case applying autocommands always changes Rows or Columns.
9264 */
9265 if (starting == 0 && ++retry_count <= 3)
9266 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009267 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009268 /* In rare cases, autocommands may have altered Rows or Columns,
9269 * jump back to check if we need to allocate the screen again. */
9270 goto retry;
9271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272}
9273
9274 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009275free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009276{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009277 int i;
9278
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009279 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009280 for (i = 0; i < Screen_mco; ++i)
9281 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009282 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009283 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009284 vim_free(ScreenAttrs);
9285 vim_free(LineOffset);
9286 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009287 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009288}
9289
9290 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009291screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292{
9293 check_for_delay(FALSE);
9294 screenalloc(FALSE); /* allocate screen buffers if size changed */
9295 screenclear2(); /* clear the screen */
9296}
9297
9298 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009299screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300{
9301 int i;
9302
9303 if (starting == NO_SCREEN || ScreenLines == NULL
9304#ifdef FEAT_GUI
9305 || (gui.in_use && gui.starting)
9306#endif
9307 )
9308 return;
9309
9310#ifdef FEAT_GUI
9311 if (!gui.in_use)
9312#endif
9313 screen_attr = -1; /* force setting the Normal colors */
9314 screen_stop_highlight(); /* don't want highlighting here */
9315
9316#ifdef FEAT_CLIPBOARD
9317 /* disable selection without redrawing it */
9318 clip_scroll_selection(9999);
9319#endif
9320
9321 /* blank out ScreenLines */
9322 for (i = 0; i < Rows; ++i)
9323 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009324 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 LineWraps[i] = FALSE;
9326 }
9327
9328 if (can_clear(T_CL))
9329 {
9330 out_str(T_CL); /* clear the display */
9331 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009332 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 }
9334 else
9335 {
9336 /* can't clear the screen, mark all chars with invalid attributes */
9337 for (i = 0; i < Rows; ++i)
9338 lineinvalid(LineOffset[i], (int)Columns);
9339 clear_cmdline = TRUE;
9340 }
9341
9342 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9343
9344 win_rest_invalid(firstwin);
9345 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009346 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 if (must_redraw == CLEAR) /* no need to clear again */
9348 must_redraw = NOT_VALID;
9349 compute_cmdrow();
9350 msg_row = cmdline_row; /* put cursor on last line for messages */
9351 msg_col = 0;
9352 screen_start(); /* don't know where cursor is now */
9353 msg_scrolled = 0; /* can't scroll back */
9354 msg_didany = FALSE;
9355 msg_didout = FALSE;
9356}
9357
9358/*
9359 * Clear one line in ScreenLines.
9360 */
9361 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009362lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363{
9364 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 if (enc_utf8)
9366 (void)vim_memset(ScreenLinesUC + off, 0,
9367 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009368 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369}
9370
9371/*
9372 * Mark one line in ScreenLines invalid by setting the attributes to an
9373 * invalid value.
9374 */
9375 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009376lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377{
9378 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9379}
9380
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381/*
9382 * Copy part of a Screenline for vertically split window "wp".
9383 */
9384 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009385linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
9387 unsigned off_to = LineOffset[to] + wp->w_wincol;
9388 unsigned off_from = LineOffset[from] + wp->w_wincol;
9389
9390 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9391 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 if (enc_utf8)
9393 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009394 int i;
9395
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9397 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009398 for (i = 0; i < p_mco; ++i)
9399 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9400 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 }
9402 if (enc_dbcs == DBCS_JPNU)
9403 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9404 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9406 wp->w_width * sizeof(sattr_T));
9407}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408
9409/*
9410 * Return TRUE if clearing with term string "p" would work.
9411 * It can't work when the string is empty or it won't set the right background.
9412 */
9413 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009414can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415{
9416 return (*p != NUL && (t_colors <= 1
9417#ifdef FEAT_GUI
9418 || gui.in_use
9419#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009420#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009421 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009422 || (!p_tgc && cterm_normal_bg_color == 0)
9423#else
9424 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009425#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009426 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427}
9428
9429/*
9430 * Reset cursor position. Use whenever cursor was moved because of outputting
9431 * something directly to the screen (shell commands) or a terminal control
9432 * code.
9433 */
9434 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009435screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436{
9437 screen_cur_row = screen_cur_col = 9999;
9438}
9439
9440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 * Move the cursor to position "row","col" in the screen.
9442 * This tries to find the most efficient way to move, minimizing the number of
9443 * characters sent to the terminal.
9444 */
9445 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009446windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009448 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 int i;
9450 int plan;
9451 int cost;
9452 int wouldbe_col;
9453 int noinvcurs;
9454 char_u *bs;
9455 int goto_cost;
9456 int attr;
9457
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009458#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9460
9461#define PLAN_LE 1
9462#define PLAN_CR 2
9463#define PLAN_NL 3
9464#define PLAN_WRITE 4
9465 /* Can't use ScreenLines unless initialized */
9466 if (ScreenLines == NULL)
9467 return;
9468
9469 if (col != screen_cur_col || row != screen_cur_row)
9470 {
9471 /* Check for valid position. */
9472 if (row < 0) /* window without text lines? */
9473 row = 0;
9474 if (row >= screen_Rows)
9475 row = screen_Rows - 1;
9476 if (col >= screen_Columns)
9477 col = screen_Columns - 1;
9478
9479 /* check if no cursor movement is allowed in highlight mode */
9480 if (screen_attr && *T_MS == NUL)
9481 noinvcurs = HIGHL_COST;
9482 else
9483 noinvcurs = 0;
9484 goto_cost = GOTO_COST + noinvcurs;
9485
9486 /*
9487 * Plan how to do the positioning:
9488 * 1. Use CR to move it to column 0, same row.
9489 * 2. Use T_LE to move it a few columns to the left.
9490 * 3. Use NL to move a few lines down, column 0.
9491 * 4. Move a few columns to the right with T_ND or by writing chars.
9492 *
9493 * Don't do this if the cursor went beyond the last column, the cursor
9494 * position is unknown then (some terminals wrap, some don't )
9495 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009496 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 * characters to move the cursor to the right.
9498 */
9499 if (row >= screen_cur_row && screen_cur_col < Columns)
9500 {
9501 /*
9502 * If the cursor is in the same row, bigger col, we can use CR
9503 * or T_LE.
9504 */
9505 bs = NULL; /* init for GCC */
9506 attr = screen_attr;
9507 if (row == screen_cur_row && col < screen_cur_col)
9508 {
9509 /* "le" is preferred over "bc", because "bc" is obsolete */
9510 if (*T_LE)
9511 bs = T_LE; /* "cursor left" */
9512 else
9513 bs = T_BC; /* "backspace character (old) */
9514 if (*bs)
9515 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9516 else
9517 cost = 999;
9518 if (col + 1 < cost) /* using CR is less characters */
9519 {
9520 plan = PLAN_CR;
9521 wouldbe_col = 0;
9522 cost = 1; /* CR is just one character */
9523 }
9524 else
9525 {
9526 plan = PLAN_LE;
9527 wouldbe_col = col;
9528 }
9529 if (noinvcurs) /* will stop highlighting */
9530 {
9531 cost += noinvcurs;
9532 attr = 0;
9533 }
9534 }
9535
9536 /*
9537 * If the cursor is above where we want to be, we can use CR LF.
9538 */
9539 else if (row > screen_cur_row)
9540 {
9541 plan = PLAN_NL;
9542 wouldbe_col = 0;
9543 cost = (row - screen_cur_row) * 2; /* CR LF */
9544 if (noinvcurs) /* will stop highlighting */
9545 {
9546 cost += noinvcurs;
9547 attr = 0;
9548 }
9549 }
9550
9551 /*
9552 * If the cursor is in the same row, smaller col, just use write.
9553 */
9554 else
9555 {
9556 plan = PLAN_WRITE;
9557 wouldbe_col = screen_cur_col;
9558 cost = 0;
9559 }
9560
9561 /*
9562 * Check if any characters that need to be written have the
9563 * correct attributes. Also avoid UTF-8 characters.
9564 */
9565 i = col - wouldbe_col;
9566 if (i > 0)
9567 cost += i;
9568 if (cost < goto_cost && i > 0)
9569 {
9570 /*
9571 * Check if the attributes are correct without additionally
9572 * stopping highlighting.
9573 */
9574 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9575 while (i && *p++ == attr)
9576 --i;
9577 if (i != 0)
9578 {
9579 /*
9580 * Try if it works when highlighting is stopped here.
9581 */
9582 if (*--p == 0)
9583 {
9584 cost += noinvcurs;
9585 while (i && *p++ == 0)
9586 --i;
9587 }
9588 if (i != 0)
9589 cost = 999; /* different attributes, don't do it */
9590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 if (enc_utf8)
9592 {
9593 /* Don't use an UTF-8 char for positioning, it's slow. */
9594 for (i = wouldbe_col; i < col; ++i)
9595 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9596 {
9597 cost = 999;
9598 break;
9599 }
9600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 }
9602
9603 /*
9604 * We can do it without term_windgoto()!
9605 */
9606 if (cost < goto_cost)
9607 {
9608 if (plan == PLAN_LE)
9609 {
9610 if (noinvcurs)
9611 screen_stop_highlight();
9612 while (screen_cur_col > col)
9613 {
9614 out_str(bs);
9615 --screen_cur_col;
9616 }
9617 }
9618 else if (plan == PLAN_CR)
9619 {
9620 if (noinvcurs)
9621 screen_stop_highlight();
9622 out_char('\r');
9623 screen_cur_col = 0;
9624 }
9625 else if (plan == PLAN_NL)
9626 {
9627 if (noinvcurs)
9628 screen_stop_highlight();
9629 while (screen_cur_row < row)
9630 {
9631 out_char('\n');
9632 ++screen_cur_row;
9633 }
9634 screen_cur_col = 0;
9635 }
9636
9637 i = col - screen_cur_col;
9638 if (i > 0)
9639 {
9640 /*
9641 * Use cursor-right if it's one character only. Avoids
9642 * removing a line of pixels from the last bold char, when
9643 * using the bold trick in the GUI.
9644 */
9645 if (T_ND[0] != NUL && T_ND[1] == NUL)
9646 {
9647 while (i-- > 0)
9648 out_char(*T_ND);
9649 }
9650 else
9651 {
9652 int off;
9653
9654 off = LineOffset[row] + screen_cur_col;
9655 while (i-- > 0)
9656 {
9657 if (ScreenAttrs[off] != screen_attr)
9658 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 if (enc_dbcs == DBCS_JPNU
9662 && ScreenLines[off] == 0x8e)
9663 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 ++off;
9665 }
9666 }
9667 }
9668 }
9669 }
9670 else
9671 cost = 999;
9672
9673 if (cost >= goto_cost)
9674 {
9675 if (noinvcurs)
9676 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009677 if (row == screen_cur_row && (col > screen_cur_col)
9678 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 term_cursor_right(col - screen_cur_col);
9680 else
9681 term_windgoto(row, col);
9682 }
9683 screen_cur_row = row;
9684 screen_cur_col = col;
9685 }
9686}
9687
9688/*
9689 * Set cursor to its position in the current window.
9690 */
9691 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009692setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009694 setcursor_mayforce(FALSE);
9695}
9696
9697/*
9698 * Set cursor to its position in the current window.
9699 * When "force" is TRUE also when not redrawing.
9700 */
9701 void
9702setcursor_mayforce(int force)
9703{
9704 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 {
9706 validate_cursor();
9707 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009708 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009710 /* With 'rightleft' set and the cursor on a double-wide
9711 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009712 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9713 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009714 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009715 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716#endif
9717 curwin->w_wcol));
9718 }
9719}
9720
9721
9722/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009723 * Insert 'line_count' lines at 'row' in window 'wp'.
9724 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9725 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 * scrolling.
9727 * Returns FAIL if the lines are not inserted, OK for success.
9728 */
9729 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009730win_ins_lines(
9731 win_T *wp,
9732 int row,
9733 int line_count,
9734 int invalid,
9735 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736{
9737 int did_delete;
9738 int nextrow;
9739 int lastrow;
9740 int retval;
9741
9742 if (invalid)
9743 wp->w_lines_valid = 0;
9744
9745 if (wp->w_height < 5)
9746 return FAIL;
9747
9748 if (line_count > wp->w_height - row)
9749 line_count = wp->w_height - row;
9750
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009751 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 if (retval != MAYBE)
9753 return retval;
9754
9755 /*
9756 * If there is a next window or a status line, we first try to delete the
9757 * lines at the bottom to avoid messing what is after the window.
9758 * If this fails and there are following windows, don't do anything to avoid
9759 * messing up those windows, better just redraw.
9760 */
9761 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 if (wp->w_next != NULL || wp->w_status_height)
9763 {
9764 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009765 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 did_delete = TRUE;
9767 else if (wp->w_next)
9768 return FAIL;
9769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 /*
9771 * if no lines deleted, blank the lines that will end up below the window
9772 */
9773 if (!did_delete)
9774 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009777 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 lastrow = nextrow + line_count;
9779 if (lastrow > Rows)
9780 lastrow = Rows;
9781 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009782 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 ' ', ' ', 0);
9784 }
9785
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009786 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 == FAIL)
9788 {
9789 /* deletion will have messed up other windows */
9790 if (did_delete)
9791 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 win_rest_invalid(W_NEXT(wp));
9794 }
9795 return FAIL;
9796 }
9797
9798 return OK;
9799}
9800
9801/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009802 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9804 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9805 * scrolling
9806 * Return OK for success, FAIL if the lines are not deleted.
9807 */
9808 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009809win_del_lines(
9810 win_T *wp,
9811 int row,
9812 int line_count,
9813 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009814 int mayclear,
9815 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816{
9817 int retval;
9818
9819 if (invalid)
9820 wp->w_lines_valid = 0;
9821
9822 if (line_count > wp->w_height - row)
9823 line_count = wp->w_height - row;
9824
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009825 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 if (retval != MAYBE)
9827 return retval;
9828
9829 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009830 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 return FAIL;
9832
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 /*
9834 * If there are windows or status lines below, try to put them at the
9835 * correct place. If we can't do that, they have to be redrawn.
9836 */
9837 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9838 {
9839 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009840 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 {
9842 wp->w_redr_status = TRUE;
9843 win_rest_invalid(wp->w_next);
9844 }
9845 }
9846 /*
9847 * If this is the last window and there is no status line, redraw the
9848 * command line later.
9849 */
9850 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 redraw_cmdline = TRUE;
9852 return OK;
9853}
9854
9855/*
9856 * Common code for win_ins_lines() and win_del_lines().
9857 * Returns OK or FAIL when the work has been done.
9858 * Returns MAYBE when not finished yet.
9859 */
9860 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009861win_do_lines(
9862 win_T *wp,
9863 int row,
9864 int line_count,
9865 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009866 int del,
9867 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868{
9869 int retval;
9870
9871 if (!redrawing() || line_count <= 0)
9872 return FAIL;
9873
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009874 /* When inserting lines would result in loss of command output, just redraw
9875 * the lines. */
9876 if (no_win_do_lines_ins && !del)
9877 return FAIL;
9878
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009880 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009882 if (!no_win_do_lines_ins)
9883 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 return FAIL;
9885 }
9886
9887 /*
9888 * Delete all remaining lines
9889 */
9890 if (row + line_count >= wp->w_height)
9891 {
9892 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009893 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 ' ', ' ', 0);
9895 return OK;
9896 }
9897
9898 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009899 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009901 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009903 if (!no_win_do_lines_ins)
9904 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905
9906 /*
9907 * If the terminal can set a scroll region, use that.
9908 * Always do this in a vertically split window. This will redraw from
9909 * ScreenLines[] when t_CV isn't defined. That's faster than using
9910 * win_line().
9911 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009912 * a character in the lower right corner of the scroll region may cause a
9913 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009915 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 scroll_region_set(wp, row);
9919 if (del)
9920 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009921 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 else
9923 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009924 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 scroll_region_reset();
9927 return retval;
9928 }
9929
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9931 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932
9933 return MAYBE;
9934}
9935
9936/*
9937 * window 'wp' and everything after it is messed up, mark it for redraw
9938 */
9939 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009940win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 {
9944 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 wp->w_redr_status = TRUE;
9946 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 }
9948 redraw_cmdline = TRUE;
9949}
9950
9951/*
9952 * The rest of the routines in this file perform screen manipulations. The
9953 * given operation is performed physically on the screen. The corresponding
9954 * change is also made to the internal screen image. In this way, the editor
9955 * anticipates the effect of editing changes on the appearance of the screen.
9956 * That way, when we call screenupdate a complete redraw isn't usually
9957 * necessary. Another advantage is that we can keep adding code to anticipate
9958 * screen changes, and in the meantime, everything still works.
9959 */
9960
9961/*
9962 * types for inserting or deleting lines
9963 */
9964#define USE_T_CAL 1
9965#define USE_T_CDL 2
9966#define USE_T_AL 3
9967#define USE_T_CE 4
9968#define USE_T_DL 5
9969#define USE_T_SR 6
9970#define USE_NL 7
9971#define USE_T_CD 8
9972#define USE_REDRAW 9
9973
9974/*
9975 * insert lines on the screen and update ScreenLines[]
9976 * 'end' is the line after the scrolled part. Normally it is Rows.
9977 * When scrolling region used 'off' is the offset from the top for the region.
9978 * 'row' and 'end' are relative to the start of the region.
9979 *
9980 * return FAIL for failure, OK for success.
9981 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009982 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009983screen_ins_lines(
9984 int off,
9985 int row,
9986 int line_count,
9987 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009988 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009989 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990{
9991 int i;
9992 int j;
9993 unsigned temp;
9994 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009995 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 int type;
9997 int result_empty;
9998 int can_ce = can_clear(T_CE);
9999
10000 /*
10001 * FAIL if
10002 * - there is no valid screen
10003 * - the screen has to be redrawn completely
10004 * - the line count is less than one
10005 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010006 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010008 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
10009#ifdef FEAT_CLIPBOARD
10010 || (clip_star.state != SELECT_CLEARED
10011 && redrawing_for_callback > 0)
10012#endif
10013 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 return FAIL;
10015
10016 /*
10017 * There are seven ways to insert lines:
10018 * 0. When in a vertically split window and t_CV isn't set, redraw the
10019 * characters from ScreenLines[].
10020 * 1. Use T_CD (clear to end of display) if it exists and the result of
10021 * the insert is just empty lines
10022 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10023 * present or line_count > 1. It looks better if we do all the inserts
10024 * at once.
10025 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10026 * insert is just empty lines and T_CE is not present or line_count >
10027 * 1.
10028 * 4. Use T_AL (insert line) if it exists.
10029 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10030 * just empty lines.
10031 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10032 * just empty lines.
10033 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10034 * the 'da' flag is not set or we have clear line capability.
10035 * 8. redraw the characters from ScreenLines[].
10036 *
10037 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10038 * the scrollbar for the window. It does have insert line, use that if it
10039 * exists.
10040 */
10041 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10043 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010044 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 type = USE_T_CD;
10046 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10047 type = USE_T_CAL;
10048 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10049 type = USE_T_CDL;
10050 else if (*T_AL != NUL)
10051 type = USE_T_AL;
10052 else if (can_ce && result_empty)
10053 type = USE_T_CE;
10054 else if (*T_DL != NUL && result_empty)
10055 type = USE_T_DL;
10056 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10057 type = USE_T_SR;
10058 else
10059 return FAIL;
10060
10061 /*
10062 * For clearing the lines screen_del_lines() is used. This will also take
10063 * care of t_db if necessary.
10064 */
10065 if (type == USE_T_CD || type == USE_T_CDL ||
10066 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010067 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068
10069 /*
10070 * If text is retained below the screen, first clear or delete as many
10071 * lines at the bottom of the window as are about to be inserted so that
10072 * the deleted lines won't later surface during a screen_del_lines.
10073 */
10074 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010075 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076
10077#ifdef FEAT_CLIPBOARD
10078 /* Remove a modeless selection when inserting lines halfway the screen
10079 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010080 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010081 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 else
10083 clip_scroll_selection(-line_count);
10084#endif
10085
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086#ifdef FEAT_GUI
10087 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10088 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010089 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090#endif
10091
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010092 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10093 cursor_col = wp->w_wincol;
10094
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 if (*T_CCS != NUL) /* cursor relative to region */
10096 cursor_row = row;
10097 else
10098 cursor_row = row + off;
10099
10100 /*
10101 * Shift LineOffset[] line_count down to reflect the inserted lines.
10102 * Clear the inserted lines in ScreenLines[].
10103 */
10104 row += off;
10105 end += off;
10106 for (i = 0; i < line_count; ++i)
10107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 if (wp != NULL && wp->w_width != Columns)
10109 {
10110 /* need to copy part of a line */
10111 j = end - 1 - i;
10112 while ((j -= line_count) >= row)
10113 linecopy(j + line_count, j, wp);
10114 j += line_count;
10115 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010116 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10117 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 else
10119 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10120 LineWraps[j] = FALSE;
10121 }
10122 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 {
10124 j = end - 1 - i;
10125 temp = LineOffset[j];
10126 while ((j -= line_count) >= row)
10127 {
10128 LineOffset[j + line_count] = LineOffset[j];
10129 LineWraps[j + line_count] = LineWraps[j];
10130 }
10131 LineOffset[j + line_count] = temp;
10132 LineWraps[j + line_count] = FALSE;
10133 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010134 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 else
10136 lineinvalid(temp, (int)Columns);
10137 }
10138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139
10140 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010141 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010142 if (clear_attr != 0)
10143 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 /* redraw the characters */
10146 if (type == USE_REDRAW)
10147 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010148 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 {
10150 term_append_lines(line_count);
10151 screen_start(); /* don't know where cursor is now */
10152 }
10153 else
10154 {
10155 for (i = 0; i < line_count; i++)
10156 {
10157 if (type == USE_T_AL)
10158 {
10159 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010160 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 out_str(T_AL);
10162 }
10163 else /* type == USE_T_SR */
10164 out_str(T_SR);
10165 screen_start(); /* don't know where cursor is now */
10166 }
10167 }
10168
10169 /*
10170 * With scroll-reverse and 'da' flag set we need to clear the lines that
10171 * have been scrolled down into the region.
10172 */
10173 if (type == USE_T_SR && *T_DA)
10174 {
10175 for (i = 0; i < line_count; ++i)
10176 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010177 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 out_str(T_CE);
10179 screen_start(); /* don't know where cursor is now */
10180 }
10181 }
10182
10183#ifdef FEAT_GUI
10184 gui_can_update_cursor();
10185 if (gui.in_use)
10186 out_flush(); /* always flush after a scroll */
10187#endif
10188 return OK;
10189}
10190
10191/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010192 * Delete lines on the screen and update ScreenLines[].
10193 * "end" is the line after the scrolled part. Normally it is Rows.
10194 * When scrolling region used "off" is the offset from the top for the region.
10195 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 *
10197 * Return OK for success, FAIL if the lines are not deleted.
10198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010200screen_del_lines(
10201 int off,
10202 int row,
10203 int line_count,
10204 int end,
10205 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010206 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010207 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208{
10209 int j;
10210 int i;
10211 unsigned temp;
10212 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010213 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 int cursor_end;
10215 int result_empty; /* result is empty until end of region */
10216 int can_delete; /* deleting line codes can be used */
10217 int type;
10218
10219 /*
10220 * FAIL if
10221 * - there is no valid screen
10222 * - the screen has to be redrawn completely
10223 * - the line count is less than one
10224 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010225 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010227 if (!screen_valid(TRUE) || line_count <= 0
10228 || (!force && line_count > p_ttyscroll)
10229#ifdef FEAT_CLIPBOARD
10230 || (clip_star.state != SELECT_CLEARED
10231 && redrawing_for_callback > 0)
10232#endif
10233 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 return FAIL;
10235
10236 /*
10237 * Check if the rest of the current region will become empty.
10238 */
10239 result_empty = row + line_count >= end;
10240
10241 /*
10242 * We can delete lines only when 'db' flag not set or when 'ce' option
10243 * available.
10244 */
10245 can_delete = (*T_DB == NUL || can_clear(T_CE));
10246
10247 /*
10248 * There are six ways to delete lines:
10249 * 0. When in a vertically split window and t_CV isn't set, redraw the
10250 * characters from ScreenLines[].
10251 * 1. Use T_CD if it exists and the result is empty.
10252 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10253 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10254 * none of the other ways work.
10255 * 4. Use T_CE (erase line) if the result is empty.
10256 * 5. Use T_DL (delete line) if it exists.
10257 * 6. redraw the characters from ScreenLines[].
10258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10260 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010261 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 type = USE_T_CD;
10263#if defined(__BEOS__) && defined(BEOS_DR8)
10264 /*
10265 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10266 * its internal termcap... this works okay for tests which test *T_DB !=
10267 * NUL. It has the disadvantage that the user cannot use any :set t_*
10268 * command to get T_DB (back) to empty_option, only :set term=... will do
10269 * the trick...
10270 * Anyway, this hack will hopefully go away with the next OS release.
10271 * (Olaf Seibert)
10272 */
10273 else if (row == 0 && T_DB == empty_option
10274 && (line_count == 1 || *T_CDL == NUL))
10275#else
10276 else if (row == 0 && (
10277#ifndef AMIGA
10278 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10279 * up, so use delete-line command */
10280 line_count == 1 ||
10281#endif
10282 *T_CDL == NUL))
10283#endif
10284 type = USE_NL;
10285 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10286 type = USE_T_CDL;
10287 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010288 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 type = USE_T_CE;
10290 else if (*T_DL != NUL && can_delete)
10291 type = USE_T_DL;
10292 else if (*T_CDL != NUL && can_delete)
10293 type = USE_T_CDL;
10294 else
10295 return FAIL;
10296
10297#ifdef FEAT_CLIPBOARD
10298 /* Remove a modeless selection when deleting lines halfway the screen or
10299 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010300 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010301 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 else
10303 clip_scroll_selection(line_count);
10304#endif
10305
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306#ifdef FEAT_GUI
10307 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10308 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010309 gui_dont_update_cursor(gui.cursor_row >= row + off
10310 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311#endif
10312
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010313 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10314 cursor_col = wp->w_wincol;
10315
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 if (*T_CCS != NUL) /* cursor relative to region */
10317 {
10318 cursor_row = row;
10319 cursor_end = end;
10320 }
10321 else
10322 {
10323 cursor_row = row + off;
10324 cursor_end = end + off;
10325 }
10326
10327 /*
10328 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10329 * Clear the inserted lines in ScreenLines[].
10330 */
10331 row += off;
10332 end += off;
10333 for (i = 0; i < line_count; ++i)
10334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 if (wp != NULL && wp->w_width != Columns)
10336 {
10337 /* need to copy part of a line */
10338 j = row + i;
10339 while ((j += line_count) <= end - 1)
10340 linecopy(j - line_count, j, wp);
10341 j -= line_count;
10342 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010343 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10344 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 else
10346 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10347 LineWraps[j] = FALSE;
10348 }
10349 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 {
10351 /* whole width, moving the line pointers is faster */
10352 j = row + i;
10353 temp = LineOffset[j];
10354 while ((j += line_count) <= end - 1)
10355 {
10356 LineOffset[j - line_count] = LineOffset[j];
10357 LineWraps[j - line_count] = LineWraps[j];
10358 }
10359 LineOffset[j - line_count] = temp;
10360 LineWraps[j - line_count] = FALSE;
10361 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010362 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 else
10364 lineinvalid(temp, (int)Columns);
10365 }
10366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010368 if (screen_attr != clear_attr)
10369 screen_stop_highlight();
10370 if (clear_attr != 0)
10371 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 /* redraw the characters */
10374 if (type == USE_REDRAW)
10375 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010376 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010378 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 out_str(T_CD);
10380 screen_start(); /* don't know where cursor is now */
10381 }
10382 else if (type == USE_T_CDL)
10383 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010384 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 term_delete_lines(line_count);
10386 screen_start(); /* don't know where cursor is now */
10387 }
10388 /*
10389 * Deleting lines at top of the screen or scroll region: Just scroll
10390 * the whole screen (scroll region) up by outputting newlines on the
10391 * last line.
10392 */
10393 else if (type == USE_NL)
10394 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010395 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 for (i = line_count; --i >= 0; )
10397 out_char('\n'); /* cursor will remain on same line */
10398 }
10399 else
10400 {
10401 for (i = line_count; --i >= 0; )
10402 {
10403 if (type == USE_T_DL)
10404 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010405 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 out_str(T_DL); /* delete a line */
10407 }
10408 else /* type == USE_T_CE */
10409 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010410 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 out_str(T_CE); /* erase a line */
10412 }
10413 screen_start(); /* don't know where cursor is now */
10414 }
10415 }
10416
10417 /*
10418 * If the 'db' flag is set, we need to clear the lines that have been
10419 * scrolled up at the bottom of the region.
10420 */
10421 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10422 {
10423 for (i = line_count; i > 0; --i)
10424 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010425 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 out_str(T_CE); /* erase a line */
10427 screen_start(); /* don't know where cursor is now */
10428 }
10429 }
10430
10431#ifdef FEAT_GUI
10432 gui_can_update_cursor();
10433 if (gui.in_use)
10434 out_flush(); /* always flush after a scroll */
10435#endif
10436
10437 return OK;
10438}
10439
10440/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010441 * Return TRUE when postponing displaying the mode message: when not redrawing
10442 * or inside a mapping.
10443 */
10444 int
10445skip_showmode()
10446{
10447 // Call char_avail() only when we are going to show something, because it
10448 // takes a bit of time. redrawing() may also call char_avail_avail().
10449 if (global_busy
10450 || msg_silent != 0
10451 || !redrawing()
10452 || (char_avail() && !KeyTyped))
10453 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010454 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010455 return TRUE;
10456 }
10457 return FALSE;
10458}
10459
10460/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010461 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 *
10463 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10464 * If clear_cmdline is FALSE there may be a message there that needs to be
10465 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010466 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 * Return the length of the message (0 if no message).
10468 */
10469 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010470showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471{
10472 int need_clear;
10473 int length = 0;
10474 int do_mode;
10475 int attr;
10476 int nwr_save;
10477#ifdef FEAT_INS_EXPAND
10478 int sub_attr;
10479#endif
10480
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010481 do_mode = ((p_smd && msg_silent == 0)
10482 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010483 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010484 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010485 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010487 if (skip_showmode())
10488 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489
10490 nwr_save = need_wait_return;
10491
10492 /* wait a bit before overwriting an important message */
10493 check_for_delay(FALSE);
10494
10495 /* if the cmdline is more than one line high, erase top lines */
10496 need_clear = clear_cmdline;
10497 if (clear_cmdline && cmdline_row < Rows - 1)
10498 msg_clr_cmdline(); /* will reset clear_cmdline */
10499
10500 /* Position on the last line in the window, column 0 */
10501 msg_pos_mode();
10502 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010503 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 if (do_mode)
10505 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010506 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010508 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010509# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010510 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010511# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010512 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010513# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010514 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010515# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010516 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010518 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519# endif
10520#endif
10521#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10522 if (gui.in_use)
10523 {
10524 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010525 {
10526 /* HANGUL */
10527 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010528 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010529 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010530 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 }
10533#endif
10534#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010535 /* CTRL-X in Insert mode */
10536 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 {
10538 /* These messages can get long, avoid a wrap in a narrow
10539 * window. Prefer showing edit_submode_extra. */
10540 length = (Rows - msg_row) * Columns - 3;
10541 if (edit_submode_extra != NULL)
10542 length -= vim_strsize(edit_submode_extra);
10543 if (length > 0)
10544 {
10545 if (edit_submode_pre != NULL)
10546 length -= vim_strsize(edit_submode_pre);
10547 if (length - vim_strsize(edit_submode) > 0)
10548 {
10549 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010550 msg_puts_attr((char *)edit_submode_pre, attr);
10551 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 }
10553 if (edit_submode_extra != NULL)
10554 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010555 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010557 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 else
10559 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010560 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 }
10562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 }
10564 else
10565#endif
10566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010568 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010569 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010570 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 else if (State & INSERT)
10572 {
10573#ifdef FEAT_RIGHTLEFT
10574 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010575 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010577 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010579 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010580 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010582 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010584 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585#ifdef FEAT_RIGHTLEFT
10586 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010587 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588#endif
10589#ifdef FEAT_KEYMAP
10590 if (State & LANGMAP)
10591 {
10592# ifdef FEAT_ARABIC
10593 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010594 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 else
10596# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010597 if (get_keymap_str(curwin, (char_u *)" (%s)",
10598 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010599 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 }
10601#endif
10602 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010603 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 if (VIsual_active)
10606 {
10607 char *p;
10608
10609 /* Don't concatenate separate words to avoid translation
10610 * problems. */
10611 switch ((VIsual_select ? 4 : 0)
10612 + (VIsual_mode == Ctrl_V) * 2
10613 + (VIsual_mode == 'V'))
10614 {
10615 case 0: p = N_(" VISUAL"); break;
10616 case 1: p = N_(" VISUAL LINE"); break;
10617 case 2: p = N_(" VISUAL BLOCK"); break;
10618 case 4: p = N_(" SELECT"); break;
10619 case 5: p = N_(" SELECT LINE"); break;
10620 default: p = N_(" SELECT BLOCK"); break;
10621 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010622 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010624 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010626
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 need_clear = TRUE;
10628 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010629 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630#ifdef FEAT_INS_EXPAND
10631 && edit_submode == NULL /* otherwise it gets too long */
10632#endif
10633 )
10634 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010635 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 need_clear = TRUE;
10637 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010638
10639 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010640 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 msg_clr_eos();
10642 msg_didout = FALSE; /* overwrite this message */
10643 length = msg_col;
10644 msg_col = 0;
10645 need_wait_return = nwr_save; /* never ask for hit-return for this */
10646 }
10647 else if (clear_cmdline && msg_silent == 0)
10648 /* Clear the whole command line. Will reset "clear_cmdline". */
10649 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010650 else if (redraw_mode)
10651 {
10652 msg_pos_mode();
10653 msg_clr_eos();
10654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655
10656#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 /* In Visual mode the size of the selected area must be redrawn. */
10658 if (VIsual_active)
10659 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660
10661 /* If the last window has no status line, the ruler is after the mode
10662 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010663 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010664 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665#endif
10666 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010667 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 clear_cmdline = FALSE;
10669
10670 return length;
10671}
10672
10673/*
10674 * Position for a mode message.
10675 */
10676 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010677msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678{
10679 msg_col = 0;
10680 msg_row = Rows - 1;
10681}
10682
10683/*
10684 * Delete mode message. Used when ESC is typed which is expected to end
10685 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010686 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 */
10688 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010689unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690{
10691 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010692 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 */
10694 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10695 redraw_cmdline = TRUE; /* delete mode later */
10696 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010697 clearmode();
10698}
10699
10700/*
10701 * Clear the mode message.
10702 */
10703 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010704clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010705{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010706 int save_msg_row = msg_row;
10707 int save_msg_col = msg_col;
10708
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010709 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010710 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010711 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010712 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010713
10714 msg_col = save_msg_col;
10715 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716}
10717
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010718 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010719recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010720{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010721 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010722 if (!shortmess(SHM_RECORDING))
10723 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010724 char s[4];
10725
10726 sprintf(s, " @%c", reg_recording);
10727 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010728 }
10729}
10730
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010731/*
10732 * Draw the tab pages line at the top of the Vim window.
10733 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010734 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010735draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010736{
10737 int tabcount = 0;
10738 tabpage_T *tp;
10739 int tabwidth;
10740 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010741 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010742 int attr;
10743 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010744 win_T *cwp;
10745 int wincount;
10746 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010747 int c;
10748 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010749 int attr_sel = HL_ATTR(HLF_TPS);
10750 int attr_nosel = HL_ATTR(HLF_TP);
10751 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010752 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010753 int room;
10754 int use_sep_chars = (t_colors < 8
10755#ifdef FEAT_GUI
10756 && !gui.in_use
10757#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010758#ifdef FEAT_TERMGUICOLORS
10759 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010760#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010761 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010762
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010763 if (ScreenLines == NULL)
10764 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010765 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010766
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010767#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010768 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010769 if (gui_use_tabline())
10770 {
10771 gui_update_tabline();
10772 return;
10773 }
10774#endif
10775
10776 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010777 return;
10778
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010779#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010780 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010781
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010782 /* Use the 'tabline' option if it's set. */
10783 if (*p_tal != NUL)
10784 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010785 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010786
10787 /* Check for an error. If there is one we would loop in redrawing the
10788 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010789 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010790 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010791 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010792 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010793 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010794 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010795 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010796 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010797#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010798 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010799 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010800 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010801
Bram Moolenaar238a5642006-02-21 22:12:05 +000010802 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10803 if (tabwidth < 6)
10804 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010805
Bram Moolenaar238a5642006-02-21 22:12:05 +000010806 attr = attr_nosel;
10807 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010808 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10809 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010810 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010811 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010812
Bram Moolenaar238a5642006-02-21 22:12:05 +000010813 if (tp->tp_topframe == topframe)
10814 attr = attr_sel;
10815 if (use_sep_chars && col > 0)
10816 screen_putchar('|', 0, col++, attr);
10817
10818 if (tp->tp_topframe != topframe)
10819 attr = attr_nosel;
10820
10821 screen_putchar(' ', 0, col++, attr);
10822
10823 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010824 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010825 cwp = curwin;
10826 wp = firstwin;
10827 }
10828 else
10829 {
10830 cwp = tp->tp_curwin;
10831 wp = tp->tp_firstwin;
10832 }
10833
10834 modified = FALSE;
10835 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10836 if (bufIsChanged(wp->w_buffer))
10837 modified = TRUE;
10838 if (modified || wincount > 1)
10839 {
10840 if (wincount > 1)
10841 {
10842 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010843 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010844 if (col + len >= Columns - 3)
10845 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010846 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010847#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010848 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010849#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010850 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010851#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010852 );
10853 col += len;
10854 }
10855 if (modified)
10856 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10857 screen_putchar(' ', 0, col++, attr);
10858 }
10859
10860 room = scol - col + tabwidth - 1;
10861 if (room > 0)
10862 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010863 /* Get buffer name in NameBuff[] */
10864 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010865 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010866 len = vim_strsize(NameBuff);
10867 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010868 if (has_mbyte)
10869 while (len > room)
10870 {
10871 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010872 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010873 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010874 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010875 {
10876 p += len - room;
10877 len = room;
10878 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010879 if (len > Columns - col - 1)
10880 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010881
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010882 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010883 col += len;
10884 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010885 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010886
10887 /* Store the tab page number in TabPageIdxs[], so that
10888 * jump_to_mouse() knows where each one is. */
10889 ++tabcount;
10890 while (scol < col)
10891 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010892 }
10893
Bram Moolenaar238a5642006-02-21 22:12:05 +000010894 if (use_sep_chars)
10895 c = '_';
10896 else
10897 c = ' ';
10898 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010899
10900 /* Put an "X" for closing the current tab if there are several. */
10901 if (first_tabpage->tp_next != NULL)
10902 {
10903 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10904 TabPageIdxs[Columns - 1] = -999;
10905 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010906 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010907
10908 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10909 * set. */
10910 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010911}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010912
10913/*
10914 * Get buffer name for "buf" into NameBuff[].
10915 * Takes care of special buffer names and translates special characters.
10916 */
10917 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010918get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010919{
10920 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010921 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010922 else
10923 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10924 trans_characters(NameBuff, MAXPATHL);
10925}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010926
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927/*
10928 * Get the character to use in a status line. Get its attributes in "*attr".
10929 */
10930 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010931fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932{
10933 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010934
10935#ifdef FEAT_TERMINAL
10936 if (bt_terminal(wp->w_buffer))
10937 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010938 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010939 {
10940 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010941 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010942 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010943 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010944 {
10945 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010946 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010947 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010948 }
10949 else
10950#endif
10951 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010953 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 fill = fill_stl;
10955 }
10956 else
10957 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010958 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 fill = fill_stlnc;
10960 }
10961 /* Use fill when there is highlighting, and highlighting of current
10962 * window differs, or the fillchars differ, or this is not the
10963 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010964 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010965 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 || (fill_stl != fill_stlnc)))
10967 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010968 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 return '^';
10970 return '=';
10971}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973/*
10974 * Get the character to use in a separator between vertically split windows.
10975 * Get its attributes in "*attr".
10976 */
10977 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010978fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010980 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 if (*attr == 0 && fill_vert == ' ')
10982 return '|';
10983 else
10984 return fill_vert;
10985}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986
10987/*
10988 * Return TRUE if redrawing should currently be done.
10989 */
10990 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010991redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010993#ifdef FEAT_EVAL
10994 if (disable_redraw_for_testing)
10995 return 0;
10996 else
10997#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010998 return ((!RedrawingDisabled
10999#ifdef FEAT_EVAL
11000 || ignore_redraw_flag_for_testing
11001#endif
11002 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003}
11004
11005/*
11006 * Return TRUE if printing messages should currently be done.
11007 */
11008 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011009messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010{
11011 return (!(p_lz && char_avail() && !KeyTyped));
11012}
11013
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011014#ifdef FEAT_MENU
11015/*
11016 * Draw the window toolbar.
11017 */
11018 static void
11019redraw_win_toolbar(win_T *wp)
11020{
11021 vimmenu_T *menu;
11022 int item_idx = 0;
11023 int item_count = 0;
11024 int col = 0;
11025 int next_col;
11026 int off = (int)(current_ScreenLine - ScreenLines);
11027 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
11028 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
11029
11030 vim_free(wp->w_winbar_items);
11031 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11032 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011033 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011034
11035 /* TODO: use fewer spaces if there is not enough room */
11036 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011037 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011038 {
11039 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011040 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011041 break;
11042 if (col > 1)
11043 {
11044 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011045 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011046 break;
11047 }
11048
11049 wp->w_winbar_items[item_idx].wb_startcol = col;
11050 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011051 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011052 break;
11053
11054 next_col = text_to_screenline(wp, menu->name, col);
11055 while (col < next_col)
11056 {
11057 ScreenAttrs[off + col] = button_attr;
11058 ++col;
11059 }
11060 wp->w_winbar_items[item_idx].wb_endcol = col;
11061 wp->w_winbar_items[item_idx].wb_menu = menu;
11062 ++item_idx;
11063
Bram Moolenaar02631462017-09-22 15:20:32 +020011064 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011065 break;
11066 space_to_screenline(off + col, button_attr);
11067 ++col;
11068 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011069 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011070 {
11071 space_to_screenline(off + col, fill_attr);
11072 ++col;
11073 }
11074 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11075
Bram Moolenaar02631462017-09-22 15:20:32 +020011076 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011077 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011078}
11079#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011080
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081/*
11082 * Show current status info in ruler and various other places
11083 * If always is FALSE, only show ruler if position has changed.
11084 */
11085 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011086showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087{
11088 if (!always && !redrawing())
11089 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011090#ifdef FEAT_INS_EXPAND
11091 if (pum_visible())
11092 {
11093 /* Don't redraw right now, do it later. */
11094 curwin->w_redr_status = TRUE;
11095 return;
11096 }
11097#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011098#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011099 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011100 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 else
11102#endif
11103#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011104 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105#endif
11106
11107#ifdef FEAT_TITLE
11108 if (need_maketitle
11109# ifdef FEAT_STL_OPT
11110 || (p_icon && (stl_syntax & STL_IN_ICON))
11111 || (p_title && (stl_syntax & STL_IN_TITLE))
11112# endif
11113 )
11114 maketitle();
11115#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011116 /* Redraw the tab pages line if needed. */
11117 if (redraw_tabline)
11118 draw_tabline();
Bram Moolenaar988c4332019-06-02 14:12:11 +020011119
11120#ifdef FEAT_TEXT_PROP
11121 // Display popup windows on top of everything.
11122 update_popups();
11123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124}
11125
11126#ifdef FEAT_CMDL_INFO
11127 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011128win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011130#define RULER_BUF_LEN 70
11131 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 int row;
11133 int fillchar;
11134 int attr;
11135 int empty_line = FALSE;
11136 colnr_T virtcol;
11137 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011138 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 int this_ru_col;
11141 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011142 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143
11144 /* If 'ruler' off or redrawing disabled, don't do anything */
11145 if (!p_ru)
11146 return;
11147
11148 /*
11149 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11150 * after deleting lines, before cursor.lnum is corrected.
11151 */
11152 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11153 return;
11154
11155#ifdef FEAT_INS_EXPAND
11156 /* Don't draw the ruler while doing insert-completion, it might overwrite
11157 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 if (edit_submode != NULL)
11160 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011161 // Don't draw the ruler when the popup menu is visible, it may overlap.
11162 // Except when the popup menu will be redrawn anyway.
11163 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011164 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165#endif
11166
11167#ifdef FEAT_STL_OPT
11168 if (*p_ruf)
11169 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011170 int save_called_emsg = called_emsg;
11171
11172 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011174 if (called_emsg)
11175 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011176 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011177 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 return;
11179 }
11180#endif
11181
11182 /*
11183 * Check if not in Insert mode and the line is empty (will show "0-1").
11184 */
11185 if (!(State & INSERT)
11186 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11187 empty_line = TRUE;
11188
11189 /*
11190 * Only draw the ruler when something changed.
11191 */
11192 validate_virtcol_win(wp);
11193 if ( redraw_cmdline
11194 || always
11195 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11196 || wp->w_cursor.col != wp->w_ru_cursor.col
11197 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 || wp->w_topline != wp->w_ru_topline
11200 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11201#ifdef FEAT_DIFF
11202 || wp->w_topfill != wp->w_ru_topfill
11203#endif
11204 || empty_line != wp->w_ru_empty)
11205 {
11206 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 if (wp->w_status_height)
11208 {
11209 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011210 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011211 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011212 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 }
11214 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 {
11216 row = Rows - 1;
11217 fillchar = ' ';
11218 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 width = Columns;
11220 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 }
11222
11223 /* In list mode virtcol needs to be recomputed */
11224 virtcol = wp->w_virtcol;
11225 if (wp->w_p_list && lcs_tab1 == NUL)
11226 {
11227 wp->w_p_list = FALSE;
11228 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11229 wp->w_p_list = TRUE;
11230 }
11231
11232 /*
11233 * Some sprintfs return the length, some return a pointer.
11234 * To avoid portability problems we use strlen() here.
11235 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011236 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11238 ? 0L
11239 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011240 len = STRLEN(buffer);
11241 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11243 (int)virtcol + 1);
11244
11245 /*
11246 * Add a "50%" if there is room for it.
11247 * On the last line, don't print in the last column (scrolls the
11248 * screen up on some terminals).
11249 */
11250 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011251 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 this_ru_col = ru_col - (Columns - width);
11256 if (this_ru_col < 0)
11257 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 /* Never use more than half the window/screen width, leave the other
11259 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011260 if (this_ru_col < (width + 1) / 2)
11261 this_ru_col = (width + 1) / 2;
11262 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011264 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011265 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 if (has_mbyte)
11268 i += (*mb_char2bytes)(fillchar, buffer + i);
11269 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 buffer[i++] = fillchar;
11271 ++o;
11272 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011273 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 }
11275 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 if (has_mbyte)
11277 {
11278 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011279 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 {
11281 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011282 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 {
11284 buffer[i] = NUL;
11285 break;
11286 }
11287 }
11288 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011289 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011290 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291
Bram Moolenaar4033c552017-09-16 20:54:51 +020011292 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 i = redraw_cmdline;
11294 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011295 this_ru_col + off + (int)STRLEN(buffer),
11296 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 fillchar, fillchar, attr);
11298 /* don't redraw the cmdline because of showing the ruler */
11299 redraw_cmdline = i;
11300 wp->w_ru_cursor = wp->w_cursor;
11301 wp->w_ru_virtcol = wp->w_virtcol;
11302 wp->w_ru_empty = empty_line;
11303 wp->w_ru_topline = wp->w_topline;
11304 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11305#ifdef FEAT_DIFF
11306 wp->w_ru_topfill = wp->w_topfill;
11307#endif
11308 }
11309}
11310#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011311
11312#if defined(FEAT_LINEBREAK) || defined(PROTO)
11313/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011314 * Return the width of the 'number' and 'relativenumber' column.
11315 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011316 * Otherwise it depends on 'numberwidth' and the line count.
11317 */
11318 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011319number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011320{
11321 int n;
11322 linenr_T lnum;
11323
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011324 if (wp->w_p_rnu && !wp->w_p_nu)
11325 /* cursor line shows "0" */
11326 lnum = wp->w_height;
11327 else
11328 /* cursor line shows absolute line number */
11329 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011330
Bram Moolenaar6b314672015-03-20 15:42:10 +010011331 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011332 return wp->w_nrwidth_width;
11333 wp->w_nrwidth_line_count = lnum;
11334
11335 n = 0;
11336 do
11337 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011338 lnum /= 10;
11339 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011340 } while (lnum > 0);
11341
11342 /* 'numberwidth' gives the minimal width plus one */
11343 if (n < wp->w_p_nuw - 1)
11344 n = wp->w_p_nuw - 1;
11345
11346 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011347 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011348 return n;
11349}
11350#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011351
Bram Moolenaar113e1072019-01-20 15:30:40 +010011352#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011353/*
11354 * Return the current cursor column. This is the actual position on the
11355 * screen. First column is 0.
11356 */
11357 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011358screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011359{
11360 return screen_cur_col;
11361}
11362
11363/*
11364 * Return the current cursor row. This is the actual position on the screen.
11365 * First row is 0.
11366 */
11367 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011368screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011369{
11370 return screen_cur_row;
11371}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011372#endif