blob: f179618166a478b33debf839e237156abe32b68d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200124#ifdef FEAT_TEXT_PROP
125static void update_popups(void);
126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200128static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100129static void win_draw_end(win_T *wp, int c1, int c2, int draw_margin, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100131static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
132static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
133static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200135static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200141# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void start_search_hl(void);
143static void end_search_hl(void);
144static void init_search_hl(win_T *wp);
145static void prepare_search_hl(win_T *wp, linenr_T lnum);
146static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
147static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200152static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200154static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void win_rest_invalid(win_T *wp);
156static void msg_pos_mode(void);
157static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200158static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200160#ifdef FEAT_MENU
161static void redraw_win_toolbar(win_T *wp);
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/* Ugly global: overrule attribute used by screen_char() */
171static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200173#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
174/* Can limit syntax highlight time to 'redrawtime'. */
175# define SYN_TIME_LIMIT 1
176#endif
177
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200178#ifdef FEAT_RIGHTLEFT
179# define HAS_RIGHTLEFT(x) x
180#else
181# define HAS_RIGHTLEFT(x) FALSE
182#endif
183
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200184// flags for screen_line()
185#define SLF_RIGHTLEFT 1
186#define SLF_POPUP 2
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100191 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200204 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100243 // This may be needed when switching tabs.
244 if (must_redraw < type)
245 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246}
247
248/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000249 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 */
251 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100252redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
254 redraw_buf_later(curbuf, type);
255}
256
257 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100258redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259{
260 win_T *wp;
261
262 FOR_ALL_WINDOWS(wp)
263 {
264 if (wp->w_buffer == buf)
265 redraw_win_later(wp, type);
266 }
267}
268
Bram Moolenaar113e1072019-01-20 15:30:40 +0100269#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200270 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100271redraw_buf_line_later(buf_T *buf, linenr_T lnum)
272{
273 win_T *wp;
274
275 FOR_ALL_WINDOWS(wp)
276 if (wp->w_buffer == buf && lnum >= wp->w_topline
277 && lnum < wp->w_botline)
278 redrawWinline(wp, lnum);
279}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100280#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100281
Bram Moolenaar113e1072019-01-20 15:30:40 +0100282#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100283 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200284redraw_buf_and_status_later(buf_T *buf, int type)
285{
286 win_T *wp;
287
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200288#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200289 if (wild_menu_showing != 0)
290 /* Don't redraw while the command line completion is displayed, it
291 * would disappear. */
292 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200293#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200294 FOR_ALL_WINDOWS(wp)
295 {
296 if (wp->w_buffer == buf)
297 {
298 redraw_win_later(wp, type);
299 wp->w_redr_status = TRUE;
300 }
301 }
302}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100303#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200304
Bram Moolenaar113e1072019-01-20 15:30:40 +0100305#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200307 * Redraw as soon as possible. When the command line is not scrolled redraw
308 * right away and restore what was on the command line.
309 * Return a code indicating what happened.
310 */
311 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100312redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313{
314 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 int r;
317 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200318 schar_T *screenline; /* copy from ScreenLines[] */
319 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200321 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200323 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200324
325 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200326 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200327 return ret;
328
329 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 rows = screen_Rows - cmdline_row;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200331 screenline = LALLOC_MULT(schar_T, rows * cols);
332 screenattr = LALLOC_MULT(sattr_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200333 if (screenline == NULL || screenattr == NULL)
334 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (enc_utf8)
336 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200337 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200338 if (screenlineUC == NULL)
339 ret = 2;
340 for (i = 0; i < p_mco; ++i)
341 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200342 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenlineC[i] == NULL)
344 ret = 2;
345 }
346 }
347 if (enc_dbcs == DBCS_JPNU)
348 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200349 screenline2 = LALLOC_MULT(schar_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 if (screenline2 == NULL)
351 ret = 2;
352 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353
354 if (ret != 2)
355 {
356 /* Save the text displayed in the command line area. */
357 for (r = 0; r < rows; ++r)
358 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 (size_t)cols * sizeof(schar_T));
362 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 if (enc_utf8)
366 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenlineC[i] + r * cols,
372 ScreenLinesC[i] + LineOffset[cmdline_row + r],
373 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374 }
375 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200376 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200379 }
380
381 update_screen(0);
382 ret = 3;
383
384 if (must_redraw == 0)
385 {
386 int off = (int)(current_ScreenLine - ScreenLines);
387
388 /* Restore the text displayed in the command line area. */
389 for (r = 0; r < rows; ++r)
390 {
391 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 screenline + r * cols,
393 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenattr + r * cols,
396 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 if (enc_utf8)
398 {
399 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenlineUC + r * cols,
401 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 for (i = 0; i < p_mco; ++i)
403 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200404 screenlineC[i] + r * cols,
405 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 }
407 if (enc_dbcs == DBCS_JPNU)
408 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenline2 + r * cols,
410 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200411 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200412 }
413 ret = 4;
414 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200415 }
416
417 vim_free(screenline);
418 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200419 if (enc_utf8)
420 {
421 vim_free(screenlineUC);
422 for (i = 0; i < p_mco; ++i)
423 vim_free(screenlineC[i]);
424 }
425 if (enc_dbcs == DBCS_JPNU)
426 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200427
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200428 /* Show the intro message when appropriate. */
429 maybe_intro_message();
430
431 setcursor();
432
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433 return ret;
434}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100435#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200436
437/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100438 * Invoked after an asynchronous callback is called.
439 * If an echo command was used the cursor needs to be put back where
440 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200441 * If "call_update_screen" is FALSE don't call update_screen() when at the
442 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100443 */
444 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200445redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100446{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200447 ++redrawing_for_callback;
448
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100449 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200450 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100451 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200452 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 // Don't redraw when in prompt_for_number().
454 if (cmdline_row > 0)
455 {
456 // Redrawing only works when the screen didn't scroll. Don't clear
457 // wildmenu entries.
458 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200459#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200460 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200461#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 && call_update_screen)
463 update_screen(0);
464
465 // Redraw in the same position, so that the user can continue
466 // editing the command.
467 redrawcmdline_ex(FALSE);
468 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200469 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200470 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100471 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200473 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100474 setcursor();
475 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100478 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200479 // Don't update the cursor when it is blinking and off to avoid
480 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100481 out_flush_cursor(FALSE, FALSE);
482 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100483#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100484 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200485
486 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100487}
488
489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Changed something in the current window, at buffer line "lnum", that
491 * requires that line and possibly other lines to be redrawn.
492 * Used when entering/leaving Insert mode with the cursor on a folded line.
493 * Used to remove the "$" from a change command.
494 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
495 * may become invalid and the whole window will have to be redrawn.
496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100498redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200499 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100500 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200502 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
503 wp->w_redraw_top = lnum;
504 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
505 wp->w_redraw_bot = lnum;
506 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507}
508
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200509/*
510 * To be called when "updating_screen" was set before and now the postponed
511 * side effects may take place.
512 */
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200513 void
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200514after_updating_screen(int may_resize_shell UNUSED)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200515{
516 updating_screen = FALSE;
517#ifdef FEAT_GUI
518 if (may_resize_shell)
519 gui_may_resize_shell();
520#endif
521#ifdef FEAT_TERMINAL
522 term_check_channel_closed_recently();
523#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200524
525#ifdef HAVE_DROP_FILE
526 // If handle_drop() was called while updating_screen was TRUE need to
527 // handle the drop now.
528 handle_any_postponed_drop();
529#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200530}
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200533 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 */
535 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100536update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 redraw_curbuf_later(type);
539 update_screen(type);
540}
541
542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 * Based on the current value of curwin->w_topline, transfer a screenfull
544 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200545 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200547 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200548update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200550 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 win_T *wp;
552 static int did_intro = FALSE;
553#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
554 int did_one;
555#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200556#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200557 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200558 int gui_cursor_col = 0;
559 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200560#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200561 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000563 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200565 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200567 if (type == VALID_NO_UPDATE)
568 {
569 no_update = TRUE;
570 type = 0;
571 }
572
Bram Moolenaara3347722019-05-11 21:14:24 +0200573#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200574 {
575 buf_T *buf;
576
577 // Before updating the screen, notify any listeners of changed text.
578 FOR_ALL_BUFFERS(buf)
579 invoke_listeners(buf);
580 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200581#endif
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (must_redraw)
584 {
585 if (type < must_redraw) /* use maximal type */
586 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000587
588 /* must_redraw is reset here, so that when we run into some weird
589 * reason to redraw while busy redrawing (e.g., asynchronous
590 * scrolling), or update_topline() in win_update() will cause a
591 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 must_redraw = 0;
593 }
594
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200595 /* May need to update w_lines[]. */
596 if (curwin->w_lines_valid == 0 && type < NOT_VALID
597#ifdef FEAT_TERMINAL
598 && !term_do_update_window(curwin)
599#endif
600 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 type = NOT_VALID;
602
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000603 /* Postpone the redrawing when it's not needed and when being called
604 * recursively. */
605 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {
607 redraw_later(type); /* remember type for next time */
608 must_redraw = type;
609 if (type > INVERTED_ALL)
610 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200611 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200613#ifdef FEAT_TEXT_PROP
614 // TODO: avoid redrawing everything when there is a popup window.
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200615 if (popup_any_visible())
Bram Moolenaarca2f7032019-06-02 15:56:15 +0200616 {
617 if (type < NOT_VALID)
618 type = NOT_VALID;
619 FOR_ALL_WINDOWS(wp)
620 wp->w_redr_type = NOT_VALID;
621 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
624 updating_screen = TRUE;
625#ifdef FEAT_SYN_HL
626 ++display_tick; /* let syntax code know we're in a next round of
627 * display updating */
628#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200629 if (no_update)
630 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632 /*
633 * if the screen was scrolled up when displaying a message, scroll it down
634 */
635 if (msg_scrolled)
636 {
637 clear_cmdline = TRUE;
638 if (msg_scrolled > Rows - 5) /* clearing is faster */
639 type = CLEAR;
640 else if (type != CLEAR)
641 {
642 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200643 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
644 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 type = CLEAR;
646 FOR_ALL_WINDOWS(wp)
647 {
648 if (W_WINROW(wp) < msg_scrolled)
649 {
650 if (W_WINROW(wp) + wp->w_height > msg_scrolled
651 && wp->w_redr_type < REDRAW_TOP
652 && wp->w_lines_valid > 0
653 && wp->w_topline == wp->w_lines[0].wl_lnum)
654 {
655 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
656 wp->w_redr_type = REDRAW_TOP;
657 }
658 else
659 {
660 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200661 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
662 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
665 }
666 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200667 if (!no_update)
668 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000669 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671 msg_scrolled = 0;
672 need_wait_return = FALSE;
673 }
674
675 /* reset cmdline_row now (may have been changed temporarily) */
676 compute_cmdrow();
677
678 /* Check for changed highlighting */
679 if (need_highlight_changed)
680 highlight_changed();
681
682 if (type == CLEAR) /* first clear screen */
683 {
684 screenclear(); /* will reset clear_cmdline */
685 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200686 /* must_redraw may be set indirectly, avoid another redraw later */
687 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
689
690 if (clear_cmdline) /* going to clear cmdline (done below) */
691 check_for_delay(FALSE);
692
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000693#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200694 /* Force redraw when width of 'number' or 'relativenumber' column
695 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000696 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200697 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
698 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000699 curwin->w_redr_type = NOT_VALID;
700#endif
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 /*
703 * Only start redrawing if there is really something to do.
704 */
705 if (type == INVERTED)
706 update_curswant();
707 if (curwin->w_redr_type < type
708 && !((type == VALID
709 && curwin->w_lines[0].wl_valid
710#ifdef FEAT_DIFF
711 && curwin->w_topfill == curwin->w_old_topfill
712 && curwin->w_botfill == curwin->w_old_botfill
713#endif
714 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000716 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
718 && curwin->w_old_visual_mode == VIsual_mode
719 && (curwin->w_valid & VALID_VIRTCOL)
720 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 ))
722 curwin->w_redr_type = type;
723
Bram Moolenaar5a305422006-04-28 22:38:25 +0000724 /* Redraw the tab pages line if needed. */
725 if (redraw_tabline || type >= NOT_VALID)
726 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728#ifdef FEAT_SYN_HL
729 /*
730 * Correct stored syntax highlighting info for changes in each displayed
731 * buffer. Each buffer must only be done once.
732 */
733 FOR_ALL_WINDOWS(wp)
734 {
735 if (wp->w_buffer->b_mod_set)
736 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 win_T *wwp;
738
739 /* Check if we already did this buffer. */
740 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
741 if (wwp->w_buffer == wp->w_buffer)
742 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200743 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 syn_stack_apply_changes(wp->w_buffer);
745 }
746 }
747#endif
748
749 /*
750 * Go from top to bottom through the windows, redrawing the ones that need
751 * it.
752 */
753#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
754 did_one = FALSE;
755#endif
756#ifdef FEAT_SEARCH_EXTRA
757 search_hl.rm.regprog = NULL;
758#endif
759 FOR_ALL_WINDOWS(wp)
760 {
761 if (wp->w_redr_type != 0)
762 {
763 cursor_off();
764#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
765 if (!did_one)
766 {
767 did_one = TRUE;
768# ifdef FEAT_SEARCH_EXTRA
769 start_search_hl();
770# endif
771# ifdef FEAT_CLIPBOARD
772 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200773 if (clip_star.available && clip_isautosel_star())
774 clip_update_selection(&clip_star);
775 if (clip_plus.available && clip_isautosel_plus())
776 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777# endif
778#ifdef FEAT_GUI
779 /* Remove the cursor before starting to do anything, because
780 * scrolling may make it difficult to redraw the text under
781 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200782 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200783 {
784 gui_cursor_col = gui.cursor_col;
785 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200787 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789#endif
790 }
791#endif
792 win_update(wp);
793 }
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 /* redraw status line after the window to minimize cursor movement */
796 if (wp->w_redr_status)
797 {
798 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200799 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
802#if defined(FEAT_SEARCH_EXTRA)
803 end_search_hl();
804#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100805#ifdef FEAT_INS_EXPAND
806 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200807 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 /* Reset b_mod_set flags. Going through all windows is probably faster
811 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200812 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200815 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
817 /* Clear or redraw the command line. Done last, because scrolling may
818 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200819 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 showmode();
821
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200822 if (no_update)
823 --no_win_do_lines_ins;
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200826 if (!did_intro)
827 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 did_intro = TRUE;
829
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200830#ifdef FEAT_TEXT_PROP
Bram Moolenaar988c4332019-06-02 14:12:11 +0200831 // Display popup windows on top of the windows.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200832 update_popups();
833#endif
834
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835#ifdef FEAT_GUI
836 /* Redraw the cursor and update the scrollbars when all screen updating is
837 * done. */
838 if (gui.in_use)
839 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200840 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200841 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100842 mch_disable_flush();
843 out_flush(); /* required before updating the cursor */
844 mch_enable_flush();
845
Bram Moolenaar144445d2016-07-08 21:41:54 +0200846 /* Put the GUI position where the cursor was, gui_update_cursor()
847 * uses that. */
848 gui.col = gui_cursor_col;
849 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200850 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100852 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200853 screen_cur_col = gui.col;
854 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200855 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100856 else
857 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 gui_update_scrollbars(FALSE);
859 }
860#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200861 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862}
863
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100864#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100865/*
866 * Prepare for updating one or more windows.
867 * Caller must check for "updating_screen" already set to avoid recursiveness.
868 */
869 static void
870update_prepare(void)
871{
872 cursor_off();
873 updating_screen = TRUE;
874#ifdef FEAT_GUI
875 /* Remove the cursor before starting to do anything, because scrolling may
876 * make it difficult to redraw the text under it. */
877 if (gui.in_use)
878 gui_undraw_cursor();
879#endif
880#ifdef FEAT_SEARCH_EXTRA
881 start_search_hl();
882#endif
883}
884
885/*
886 * Finish updating one or more windows.
887 */
888 static void
889update_finish(void)
890{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200891 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100892 showmode();
893
894# ifdef FEAT_SEARCH_EXTRA
895 end_search_hl();
896# endif
897
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200898 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100899
900# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100901 /* Redraw the cursor and update the scrollbars when all screen updating is
902 * done. */
903 if (gui.in_use)
904 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100905 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100906 gui_update_scrollbars(FALSE);
907 }
908# endif
909}
910#endif
911
Bram Moolenaar860cae12010-06-05 23:22:07 +0200912#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200913/*
914 * Return TRUE if the cursor line in window "wp" may be concealed, according
915 * to the 'concealcursor' option.
916 */
917 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100918conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200919{
920 int c;
921
922 if (*wp->w_p_cocu == NUL)
923 return FALSE;
924 if (get_real_state() & VISUAL)
925 c = 'v';
926 else if (State & INSERT)
927 c = 'i';
928 else if (State & NORMAL)
929 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200930 else if (State & CMDLINE)
931 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200932 else
933 return FALSE;
934 return vim_strchr(wp->w_p_cocu, c) != NULL;
935}
936
937/*
938 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
939 */
940 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200941conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200942{
943 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
944 {
945 need_cursor_line_redraw = TRUE;
946 /* Need to recompute cursor column, e.g., when starting Visual mode
947 * without concealing. */
948 curs_columns(TRUE);
949 }
950}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951#endif
952
Bram Moolenaar113e1072019-01-20 15:30:40 +0100953#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100955update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
957 win_T *wp;
958 int doit = FALSE;
959
960# ifdef FEAT_FOLDING
961 win_foldinfo.fi_level = 0;
962# endif
963
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100964 // update/delete a specific sign
965 redraw_buf_line_later(buf, lnum);
966
967 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (wp->w_redr_type != 0)
970 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100972 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200973 * happening (recursive call), messages on the screen or still starting up.
974 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100975 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200976 || State == ASKMORE || State == HITRETURN
977 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100978#ifdef FEAT_GUI
979 || gui.starting
980#endif
981 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 return;
983
984 /* update all windows that need updating */
985 update_prepare();
986
Bram Moolenaar29323592016-07-24 22:04:11 +0200987 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
989 if (wp->w_redr_type != 0)
990 win_update(wp);
991 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200992 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
Bram Moolenaar988c4332019-06-02 14:12:11 +0200995#ifdef FEAT_TEXT_PROP
996 // Display popup windows on top of the others.
997 update_popups();
998#endif
999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 update_finish();
1001}
1002#endif
1003
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001004/*
1005 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
1006 * window then get the "Pmenu" highlight attribute.
1007 */
1008 static int
1009get_wcr_attr(win_T *wp)
1010{
1011 int wcr_attr = 0;
1012
1013 if (*wp->w_p_wcr != NUL)
1014 wcr_attr = syn_name2attr(wp->w_p_wcr);
1015#ifdef FEAT_TEXT_PROP
1016 if (bt_popup(wp->w_buffer) && wcr_attr == 0)
1017 wcr_attr = HL_ATTR(HLF_PNI);
1018#endif
1019 return wcr_attr;
1020}
1021
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001022#ifdef FEAT_TEXT_PROP
1023/*
1024 * Return a string of "len" spaces in IObuff.
1025 */
1026 static char_u *
1027get_spaces(int len)
1028{
1029 vim_memset(IObuff, ' ', (size_t)len);
1030 IObuff[len] = NUL;
1031 return IObuff;
1032}
1033
1034 static void
1035update_popups(void)
1036{
1037 win_T *wp;
1038 int top_off;
1039 int left_off;
1040 int total_width;
1041 int total_height;
1042 int popup_attr;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001043 int border_attr[4];
1044 int border_char[8] = {'-', '|', '-', '|', '+', '+', '+', '+', };
1045 char_u buf[MB_MAXBYTES];
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001046 int row;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001047 int i;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001048
1049 // Find the window with the lowest zindex that hasn't been updated yet,
1050 // so that the window with a higher zindex is drawn later, thus goes on
1051 // top.
1052 // TODO: don't redraw every popup every time.
1053 popup_reset_handled();
1054 while ((wp = find_next_popup(TRUE)) != NULL)
1055 {
1056 // Recompute the position if the text changed.
1057 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1058 popup_adjust_position(wp);
1059
1060 // adjust w_winrow and w_wincol for border and padding, since
1061 // win_update() doesn't handle them.
1062 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1063 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1064 wp->w_winrow += top_off;
1065 wp->w_wincol += left_off;
1066
1067 // Draw the popup text.
1068 win_update(wp);
1069
1070 wp->w_winrow -= top_off;
1071 wp->w_wincol -= left_off;
1072
1073 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1074 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
1075 total_height = wp->w_popup_border[0] + wp->w_popup_padding[0]
1076 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1077 popup_attr = get_wcr_attr(wp);
1078
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001079 if (enc_utf8)
1080 {
Bram Moolenaar790498b2019-06-01 22:15:29 +02001081 border_char[0] = border_char[2] = 0x2550;
1082 border_char[1] = border_char[3] = 0x2551;
1083 border_char[4] = 0x2554;
1084 border_char[5] = 0x2557;
1085 border_char[6] = 0x255d;
1086 border_char[7] = 0x255a;
1087 }
1088 for (i = 0; i < 8; ++i)
1089 if (wp->w_border_char[i] != 0)
1090 border_char[i] = wp->w_border_char[i];
1091
1092 for (i = 0; i < 4; ++i)
1093 {
1094 border_attr[i] = popup_attr;
1095 if (wp->w_border_highlight[i] != NULL)
1096 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001097 }
1098
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001099 if (wp->w_popup_border[0] > 0)
1100 {
1101 // top border
1102 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1103 wp->w_wincol,
1104 wp->w_wincol + total_width,
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001105 wp->w_popup_border[3] != 0
Bram Moolenaar790498b2019-06-01 22:15:29 +02001106 ? border_char[4] : border_char[0],
1107 border_char[0], border_attr[0]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001108 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001109 {
1110 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1111 screen_puts(buf, wp->w_winrow,
1112 wp->w_wincol + total_width - 1, border_attr[1]);
1113 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001114 }
1115
1116 if (wp->w_popup_padding[0] > 0)
1117 {
1118 // top padding
1119 row = wp->w_winrow + wp->w_popup_border[0];
1120 screen_fill(row, row + wp->w_popup_padding[0],
1121 wp->w_wincol + wp->w_popup_border[3],
1122 wp->w_wincol + total_width - wp->w_popup_border[1],
1123 ' ', ' ', popup_attr);
1124 }
1125
1126 for (row = wp->w_winrow + wp->w_popup_border[0];
1127 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1128 ++row)
1129 {
1130 // left border
1131 if (wp->w_popup_border[3] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001132 {
1133 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1134 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1135 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001136 // left padding
1137 if (wp->w_popup_padding[3] > 0)
1138 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1139 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1140 // right border
1141 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001142 {
1143 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1144 screen_puts(buf, row,
1145 wp->w_wincol + total_width - 1, border_attr[1]);
1146 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001147 // right padding
1148 if (wp->w_popup_padding[1] > 0)
1149 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1150 wp->w_wincol + wp->w_popup_border[3]
1151 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1152 }
1153
1154 if (wp->w_popup_padding[2] > 0)
1155 {
1156 // bottom padding
1157 row = wp->w_winrow + wp->w_popup_border[0]
1158 + wp->w_popup_padding[0] + wp->w_height;
1159 screen_fill(row, row + wp->w_popup_padding[2],
1160 wp->w_wincol + wp->w_popup_border[3],
1161 wp->w_wincol + total_width - wp->w_popup_border[1],
1162 ' ', ' ', popup_attr);
1163 }
1164
1165 if (wp->w_popup_border[2] > 0)
1166 {
1167 // bottom border
1168 row = wp->w_winrow + total_height - 1;
1169 screen_fill(row , row + 1,
1170 wp->w_wincol,
1171 wp->w_wincol + total_width,
Bram Moolenaar790498b2019-06-01 22:15:29 +02001172 wp->w_popup_border[3] != 0
1173 ? border_char[7] : border_char[2],
1174 border_char[2], border_attr[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001175 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001176 {
1177 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1178 screen_puts(buf, row,
1179 wp->w_wincol + total_width - 1, border_attr[2]);
1180 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001181 }
1182 }
1183}
1184#endif
1185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186#if defined(FEAT_GUI) || defined(PROTO)
1187/*
1188 * Update a single window, its status line and maybe the command line msg.
1189 * Used for the GUI scrollbar.
1190 */
1191 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001192updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001194 /* return if already busy updating */
1195 if (updating_screen)
1196 return;
1197
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 update_prepare();
1199
1200#ifdef FEAT_CLIPBOARD
1201 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001202 if (clip_star.available && clip_isautosel_star())
1203 clip_update_selection(&clip_star);
1204 if (clip_plus.available && clip_isautosel_plus())
1205 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001207
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001209
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001210 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001211 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001212 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (wp->w_redr_status
1215# ifdef FEAT_CMDL_INFO
1216 || p_ru
1217# endif
1218# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001219 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220# endif
1221 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001222 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaar988c4332019-06-02 14:12:11 +02001224#ifdef FEAT_TEXT_PROP
1225 // Display popup windows on top of everything.
1226 update_popups();
1227#endif
1228
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 update_finish();
1230}
1231#endif
1232
1233/*
1234 * Update a single window.
1235 *
1236 * This may cause the windows below it also to be redrawn (when clearing the
1237 * screen or scrolling lines).
1238 *
1239 * How the window is redrawn depends on wp->w_redr_type. Each type also
1240 * implies the one below it.
1241 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001242 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1244 * INVERTED redraw the changed part of the Visual area
1245 * INVERTED_ALL redraw the whole Visual area
1246 * VALID 1. scroll up/down to adjust for a changed w_topline
1247 * 2. update lines at the top when scrolled down
1248 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001249 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 * b_mod_top and b_mod_bot.
1251 * - if wp->w_redraw_top non-zero, redraw lines between
1252 * wp->w_redraw_top and wp->w_redr_bot.
1253 * - continue redrawing when syntax status is invalid.
1254 * 4. if scrolled up, update lines at the bottom.
1255 * This results in three areas that may need updating:
1256 * top: from first row to top_end (when scrolled down)
1257 * mid: from mid_start to mid_end (update inversion or changed text)
1258 * bot: from bot_start to last row (when scrolled up)
1259 */
1260 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001261win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262{
1263 buf_T *buf = wp->w_buffer;
1264 int type;
1265 int top_end = 0; /* Below last row of the top area that needs
1266 updating. 0 when no top area updating. */
1267 int mid_start = 999;/* first row of the mid area that needs
1268 updating. 999 when no mid area updating. */
1269 int mid_end = 0; /* Below last row of the mid area that needs
1270 updating. 0 when no mid area updating. */
1271 int bot_start = 999;/* first row of the bot area that needs
1272 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 int scrolled_down = FALSE; /* TRUE when scrolled down when
1274 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001276 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 int top_to_mod = FALSE; /* redraw above mod_top */
1278#endif
1279
1280 int row; /* current window row to display */
1281 linenr_T lnum; /* current buffer lnum to display */
1282 int idx; /* current index in w_lines[] */
1283 int srow; /* starting row of the current line */
1284
1285 int eof = FALSE; /* if TRUE, we hit the end of the file */
1286 int didline = FALSE; /* if TRUE, we finished the last line */
1287 int i;
1288 long j;
1289 static int recursive = FALSE; /* being called recursively */
1290 int old_botline = wp->w_botline;
1291#ifdef FEAT_FOLDING
1292 long fold_count;
1293#endif
1294#ifdef FEAT_SYN_HL
1295 /* remember what happened to the previous line, to know if
1296 * check_visual_highlight() can be used */
1297#define DID_NONE 1 /* didn't update a line */
1298#define DID_LINE 2 /* updated a normal line */
1299#define DID_FOLD 3 /* updated a folded line */
1300 int did_update = DID_NONE;
1301 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1302#endif
1303 linenr_T mod_top = 0;
1304 linenr_T mod_bot = 0;
1305#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1306 int save_got_int;
1307#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001308#ifdef SYN_TIME_LIMIT
1309 proftime_T syntax_tm;
1310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311
1312 type = wp->w_redr_type;
1313
1314 if (type == NOT_VALID)
1315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 wp->w_lines_valid = 0;
1318 }
1319
1320 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001321 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 {
1323 wp->w_redr_type = 0;
1324 return;
1325 }
1326
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 /* Window is zero-width: Only need to draw the separator. */
1328 if (wp->w_width == 0)
1329 {
1330 /* draw the vertical separator right of this window */
1331 draw_vsep_win(wp, 0);
1332 wp->w_redr_type = 0;
1333 return;
1334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001336#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001337 // If this window contains a terminal, redraw works completely differently.
1338 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001339 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001340 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001341# ifdef FEAT_MENU
1342 /* Draw the window toolbar, if there is one. */
1343 if (winbar_height(wp) > 0)
1344 redraw_win_toolbar(wp);
1345# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001346 wp->w_redr_type = 0;
1347 return;
1348 }
1349#endif
1350
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001352 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353#endif
1354
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001355#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001356 /* Force redraw when width of 'number' or 'relativenumber' column
1357 * changes. */
1358 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001359 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001360 {
1361 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001362 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001363 }
1364 else
1365#endif
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1368 {
1369 /*
1370 * When there are both inserted/deleted lines and specific lines to be
1371 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1372 * everything (only happens when redrawing is off for while).
1373 */
1374 type = NOT_VALID;
1375 }
1376 else
1377 {
1378 /*
1379 * Set mod_top to the first line that needs displaying because of
1380 * changes. Set mod_bot to the first line after the changes.
1381 */
1382 mod_top = wp->w_redraw_top;
1383 if (wp->w_redraw_bot != 0)
1384 mod_bot = wp->w_redraw_bot + 1;
1385 else
1386 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (buf->b_mod_set)
1388 {
1389 if (mod_top == 0 || mod_top > buf->b_mod_top)
1390 {
1391 mod_top = buf->b_mod_top;
1392#ifdef FEAT_SYN_HL
1393 /* Need to redraw lines above the change that may be included
1394 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001395 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001397 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 if (mod_top < 1)
1399 mod_top = 1;
1400 }
1401#endif
1402 }
1403 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1404 mod_bot = buf->b_mod_bot;
1405
1406#ifdef FEAT_SEARCH_EXTRA
1407 /* When 'hlsearch' is on and using a multi-line search pattern, a
1408 * change in one line may make the Search highlighting in a
1409 * previous line invalid. Simple solution: redraw all visible
1410 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001411 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001413 if (search_hl.rm.regprog != NULL
1414 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001416 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001417 {
1418 cur = wp->w_match_head;
1419 while (cur != NULL)
1420 {
1421 if (cur->match.regprog != NULL
1422 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001423 {
1424 top_to_mod = TRUE;
1425 break;
1426 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001427 cur = cur->next;
1428 }
1429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430#endif
1431 }
1432#ifdef FEAT_FOLDING
1433 if (mod_top != 0 && hasAnyFolding(wp))
1434 {
1435 linenr_T lnumt, lnumb;
1436
1437 /*
1438 * A change in a line can cause lines above it to become folded or
1439 * unfolded. Find the top most buffer line that may be affected.
1440 * If the line was previously folded and displayed, get the first
1441 * line of that fold. If the line is folded now, get the first
1442 * folded line. Use the minimum of these two.
1443 */
1444
1445 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1446 * the line below it. If there is no valid entry, use w_topline.
1447 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1448 * to this line. If there is no valid entry, use MAXLNUM. */
1449 lnumt = wp->w_topline;
1450 lnumb = MAXLNUM;
1451 for (i = 0; i < wp->w_lines_valid; ++i)
1452 if (wp->w_lines[i].wl_valid)
1453 {
1454 if (wp->w_lines[i].wl_lastlnum < mod_top)
1455 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1456 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1457 {
1458 lnumb = wp->w_lines[i].wl_lnum;
1459 /* When there is a fold column it might need updating
1460 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001461 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 ++lnumb;
1463 }
1464 }
1465
1466 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1467 if (mod_top > lnumt)
1468 mod_top = lnumt;
1469
1470 /* Now do the same for the bottom line (one above mod_bot). */
1471 --mod_bot;
1472 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1473 ++mod_bot;
1474 if (mod_bot < lnumb)
1475 mod_bot = lnumb;
1476 }
1477#endif
1478
1479 /* When a change starts above w_topline and the end is below
1480 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001481 * If the end of the change is above w_topline: do like no change was
1482 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 if (mod_top != 0 && mod_top < wp->w_topline)
1484 {
1485 if (mod_bot > wp->w_topline)
1486 mod_top = wp->w_topline;
1487#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001488 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 top_end = 1;
1490#endif
1491 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001492
1493 /* When line numbers are displayed need to redraw all lines below
1494 * inserted/deleted lines. */
1495 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1496 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001498 wp->w_redraw_top = 0; // reset for next time
1499 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500
1501 /*
1502 * When only displaying the lines at the top, set top_end. Used when
1503 * window has scrolled down for msg_scrolled.
1504 */
1505 if (type == REDRAW_TOP)
1506 {
1507 j = 0;
1508 for (i = 0; i < wp->w_lines_valid; ++i)
1509 {
1510 j += wp->w_lines[i].wl_size;
1511 if (j >= wp->w_upd_rows)
1512 {
1513 top_end = j;
1514 break;
1515 }
1516 }
1517 if (top_end == 0)
1518 /* not found (cannot happen?): redraw everything */
1519 type = NOT_VALID;
1520 else
1521 /* top area defined, the rest is VALID */
1522 type = VALID;
1523 }
1524
Bram Moolenaar367329b2007-08-30 11:53:22 +00001525 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001526 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1527 * non-zero and thus not FALSE) will indicate that screenclear() was not
1528 * called. */
1529 if (screen_cleared)
1530 screen_cleared = MAYBE;
1531
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 /*
1533 * If there are no changes on the screen that require a complete redraw,
1534 * handle three cases:
1535 * 1: we are off the top of the screen by a few lines: scroll down
1536 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1537 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1538 * w_lines[] that needs updating.
1539 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001540 if ((type == VALID || type == SOME_VALID
1541 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542#ifdef FEAT_DIFF
1543 && !wp->w_botfill && !wp->w_old_botfill
1544#endif
1545 )
1546 {
1547 if (mod_top != 0 && wp->w_topline == mod_top)
1548 {
1549 /*
1550 * w_topline is the first changed line, the scrolling will be done
1551 * further down.
1552 */
1553 }
1554 else if (wp->w_lines[0].wl_valid
1555 && (wp->w_topline < wp->w_lines[0].wl_lnum
1556#ifdef FEAT_DIFF
1557 || (wp->w_topline == wp->w_lines[0].wl_lnum
1558 && wp->w_topfill > wp->w_old_topfill)
1559#endif
1560 ))
1561 {
1562 /*
1563 * New topline is above old topline: May scroll down.
1564 */
1565#ifdef FEAT_FOLDING
1566 if (hasAnyFolding(wp))
1567 {
1568 linenr_T ln;
1569
1570 /* count the number of lines we are off, counting a sequence
1571 * of folded lines as one */
1572 j = 0;
1573 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1574 {
1575 ++j;
1576 if (j >= wp->w_height - 2)
1577 break;
1578 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1579 }
1580 }
1581 else
1582#endif
1583 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1584 if (j < wp->w_height - 2) /* not too far off */
1585 {
1586 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1587#ifdef FEAT_DIFF
1588 /* insert extra lines for previously invisible filler lines */
1589 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1590 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1591 - wp->w_old_topfill;
1592#endif
1593 if (i < wp->w_height - 2) /* less than a screen off */
1594 {
1595 /*
1596 * Try to insert the correct number of lines.
1597 * If not the last window, delete the lines at the bottom.
1598 * win_ins_lines may fail when the terminal can't do it.
1599 */
1600 if (i > 0)
1601 check_for_delay(FALSE);
1602 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1603 {
1604 if (wp->w_lines_valid != 0)
1605 {
1606 /* Need to update rows that are new, stop at the
1607 * first one that scrolled down. */
1608 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
1611 /* Move the entries that were scrolled, disable
1612 * the entries for the lines to be redrawn. */
1613 if ((wp->w_lines_valid += j) > wp->w_height)
1614 wp->w_lines_valid = wp->w_height;
1615 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1616 wp->w_lines[idx] = wp->w_lines[idx - j];
1617 while (idx >= 0)
1618 wp->w_lines[idx--].wl_valid = FALSE;
1619 }
1620 }
1621 else
1622 mid_start = 0; /* redraw all lines */
1623 }
1624 else
1625 mid_start = 0; /* redraw all lines */
1626 }
1627 else
1628 mid_start = 0; /* redraw all lines */
1629 }
1630 else
1631 {
1632 /*
1633 * New topline is at or below old topline: May scroll up.
1634 * When topline didn't change, find first entry in w_lines[] that
1635 * needs updating.
1636 */
1637
1638 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1639 j = -1;
1640 row = 0;
1641 for (i = 0; i < wp->w_lines_valid; i++)
1642 {
1643 if (wp->w_lines[i].wl_valid
1644 && wp->w_lines[i].wl_lnum == wp->w_topline)
1645 {
1646 j = i;
1647 break;
1648 }
1649 row += wp->w_lines[i].wl_size;
1650 }
1651 if (j == -1)
1652 {
1653 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1654 * lines */
1655 mid_start = 0;
1656 }
1657 else
1658 {
1659 /*
1660 * Try to delete the correct number of lines.
1661 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1662 */
1663#ifdef FEAT_DIFF
1664 /* If the topline didn't change, delete old filler lines,
1665 * otherwise delete filler lines of the new topline... */
1666 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1667 row += wp->w_old_topfill;
1668 else
1669 row += diff_check_fill(wp, wp->w_topline);
1670 /* ... but don't delete new filler lines. */
1671 row -= wp->w_topfill;
1672#endif
1673 if (row > 0)
1674 {
1675 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001676 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1677 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 bot_start = wp->w_height - row;
1679 else
1680 mid_start = 0; /* redraw all lines */
1681 }
1682 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1683 {
1684 /*
1685 * Skip the lines (below the deleted lines) that are still
1686 * valid and don't need redrawing. Copy their info
1687 * upwards, to compensate for the deleted lines. Set
1688 * bot_start to the first row that needs redrawing.
1689 */
1690 bot_start = 0;
1691 idx = 0;
1692 for (;;)
1693 {
1694 wp->w_lines[idx] = wp->w_lines[j];
1695 /* stop at line that didn't fit, unless it is still
1696 * valid (no lines deleted) */
1697 if (row > 0 && bot_start + row
1698 + (int)wp->w_lines[j].wl_size > wp->w_height)
1699 {
1700 wp->w_lines_valid = idx + 1;
1701 break;
1702 }
1703 bot_start += wp->w_lines[idx++].wl_size;
1704
1705 /* stop at the last valid entry in w_lines[].wl_size */
1706 if (++j >= wp->w_lines_valid)
1707 {
1708 wp->w_lines_valid = idx;
1709 break;
1710 }
1711 }
1712#ifdef FEAT_DIFF
1713 /* Correct the first entry for filler lines at the top
1714 * when it won't get updated below. */
1715 if (wp->w_p_diff && bot_start > 0)
1716 wp->w_lines[0].wl_size =
1717 plines_win_nofill(wp, wp->w_topline, TRUE)
1718 + wp->w_topfill;
1719#endif
1720 }
1721 }
1722 }
1723
1724 /* When starting redraw in the first line, redraw all lines. When
1725 * there is only one window it's probably faster to clear the screen
1726 * first. */
1727 if (mid_start == 0)
1728 {
1729 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001730 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001731 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001732 /* Clear the screen when it was not done by win_del_lines() or
1733 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1734 * then. */
1735 if (screen_cleared != TRUE)
1736 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001737 /* The screen was cleared, redraw the tab pages line. */
1738 if (redraw_tabline)
1739 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001742
1743 /* When win_del_lines() or win_ins_lines() caused the screen to be
1744 * cleared (only happens for the first window) or when screenclear()
1745 * was called directly above, "must_redraw" will have been set to
1746 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1747 if (screen_cleared == TRUE)
1748 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750 else
1751 {
1752 /* Not VALID or INVERTED: redraw all lines. */
1753 mid_start = 0;
1754 mid_end = wp->w_height;
1755 }
1756
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001757 if (type == SOME_VALID)
1758 {
1759 /* SOME_VALID: redraw all lines. */
1760 mid_start = 0;
1761 mid_end = wp->w_height;
1762 type = NOT_VALID;
1763 }
1764
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 /* check if we are updating or removing the inverted part */
1766 if ((VIsual_active && buf == curwin->w_buffer)
1767 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1768 {
1769 linenr_T from, to;
1770
1771 if (VIsual_active)
1772 {
1773 if (VIsual_active
1774 && (VIsual_mode != wp->w_old_visual_mode
1775 || type == INVERTED_ALL))
1776 {
1777 /*
1778 * If the type of Visual selection changed, redraw the whole
1779 * selection. Also when the ownership of the X selection is
1780 * gained or lost.
1781 */
1782 if (curwin->w_cursor.lnum < VIsual.lnum)
1783 {
1784 from = curwin->w_cursor.lnum;
1785 to = VIsual.lnum;
1786 }
1787 else
1788 {
1789 from = VIsual.lnum;
1790 to = curwin->w_cursor.lnum;
1791 }
1792 /* redraw more when the cursor moved as well */
1793 if (wp->w_old_cursor_lnum < from)
1794 from = wp->w_old_cursor_lnum;
1795 if (wp->w_old_cursor_lnum > to)
1796 to = wp->w_old_cursor_lnum;
1797 if (wp->w_old_visual_lnum < from)
1798 from = wp->w_old_visual_lnum;
1799 if (wp->w_old_visual_lnum > to)
1800 to = wp->w_old_visual_lnum;
1801 }
1802 else
1803 {
1804 /*
1805 * Find the line numbers that need to be updated: The lines
1806 * between the old cursor position and the current cursor
1807 * position. Also check if the Visual position changed.
1808 */
1809 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1810 {
1811 from = curwin->w_cursor.lnum;
1812 to = wp->w_old_cursor_lnum;
1813 }
1814 else
1815 {
1816 from = wp->w_old_cursor_lnum;
1817 to = curwin->w_cursor.lnum;
1818 if (from == 0) /* Visual mode just started */
1819 from = to;
1820 }
1821
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001822 if (VIsual.lnum != wp->w_old_visual_lnum
1823 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 {
1825 if (wp->w_old_visual_lnum < from
1826 && wp->w_old_visual_lnum != 0)
1827 from = wp->w_old_visual_lnum;
1828 if (wp->w_old_visual_lnum > to)
1829 to = wp->w_old_visual_lnum;
1830 if (VIsual.lnum < from)
1831 from = VIsual.lnum;
1832 if (VIsual.lnum > to)
1833 to = VIsual.lnum;
1834 }
1835 }
1836
1837 /*
1838 * If in block mode and changed column or curwin->w_curswant:
1839 * update all lines.
1840 * First compute the actual start and end column.
1841 */
1842 if (VIsual_mode == Ctrl_V)
1843 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001844 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001845#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001846 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
Bram Moolenaar404406a2014-10-09 13:24:43 +02001848 if (curwin->w_p_lbr)
1849 ve_flags = VE_ALL;
1850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001852#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001853 ve_flags = save_ve_flags;
1854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 ++toc;
1856 if (curwin->w_curswant == MAXCOL)
1857 toc = MAXCOL;
1858
1859 if (fromc != wp->w_old_cursor_fcol
1860 || toc != wp->w_old_cursor_lcol)
1861 {
1862 if (from > VIsual.lnum)
1863 from = VIsual.lnum;
1864 if (to < VIsual.lnum)
1865 to = VIsual.lnum;
1866 }
1867 wp->w_old_cursor_fcol = fromc;
1868 wp->w_old_cursor_lcol = toc;
1869 }
1870 }
1871 else
1872 {
1873 /* Use the line numbers of the old Visual area. */
1874 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1875 {
1876 from = wp->w_old_cursor_lnum;
1877 to = wp->w_old_visual_lnum;
1878 }
1879 else
1880 {
1881 from = wp->w_old_visual_lnum;
1882 to = wp->w_old_cursor_lnum;
1883 }
1884 }
1885
1886 /*
1887 * There is no need to update lines above the top of the window.
1888 */
1889 if (from < wp->w_topline)
1890 from = wp->w_topline;
1891
1892 /*
1893 * If we know the value of w_botline, use it to restrict the update to
1894 * the lines that are visible in the window.
1895 */
1896 if (wp->w_valid & VALID_BOTLINE)
1897 {
1898 if (from >= wp->w_botline)
1899 from = wp->w_botline - 1;
1900 if (to >= wp->w_botline)
1901 to = wp->w_botline - 1;
1902 }
1903
1904 /*
1905 * Find the minimal part to be updated.
1906 * Watch out for scrolling that made entries in w_lines[] invalid.
1907 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1908 * top_end; need to redraw from top_end to the "to" line.
1909 * A middle mouse click with a Visual selection may change the text
1910 * above the Visual area and reset wl_valid, do count these for
1911 * mid_end (in srow).
1912 */
1913 if (mid_start > 0)
1914 {
1915 lnum = wp->w_topline;
1916 idx = 0;
1917 srow = 0;
1918 if (scrolled_down)
1919 mid_start = top_end;
1920 else
1921 mid_start = 0;
1922 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1923 {
1924 if (wp->w_lines[idx].wl_valid)
1925 mid_start += wp->w_lines[idx].wl_size;
1926 else if (!scrolled_down)
1927 srow += wp->w_lines[idx].wl_size;
1928 ++idx;
1929# ifdef FEAT_FOLDING
1930 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1931 lnum = wp->w_lines[idx].wl_lnum;
1932 else
1933# endif
1934 ++lnum;
1935 }
1936 srow += mid_start;
1937 mid_end = wp->w_height;
1938 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1939 {
1940 if (wp->w_lines[idx].wl_valid
1941 && wp->w_lines[idx].wl_lnum >= to + 1)
1942 {
1943 /* Only update until first row of this line */
1944 mid_end = srow;
1945 break;
1946 }
1947 srow += wp->w_lines[idx].wl_size;
1948 }
1949 }
1950 }
1951
1952 if (VIsual_active && buf == curwin->w_buffer)
1953 {
1954 wp->w_old_visual_mode = VIsual_mode;
1955 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1956 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001957 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 wp->w_old_curswant = curwin->w_curswant;
1959 }
1960 else
1961 {
1962 wp->w_old_visual_mode = 0;
1963 wp->w_old_cursor_lnum = 0;
1964 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001965 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
1968#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1969 /* reset got_int, otherwise regexp won't work */
1970 save_got_int = got_int;
1971 got_int = 0;
1972#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001973#ifdef SYN_TIME_LIMIT
1974 /* Set the time limit to 'redrawtime'. */
1975 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001976 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978#ifdef FEAT_FOLDING
1979 win_foldinfo.fi_level = 0;
1980#endif
1981
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001982#ifdef FEAT_MENU
1983 /*
1984 * Draw the window toolbar, if there is one.
1985 * TODO: only when needed.
1986 */
1987 if (winbar_height(wp) > 0)
1988 redraw_win_toolbar(wp);
1989#endif
1990
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 /*
1992 * Update all the window rows.
1993 */
1994 idx = 0; /* first entry in w_lines[].wl_size */
1995 row = 0;
1996 srow = 0;
1997 lnum = wp->w_topline; /* first line shown in window */
1998 for (;;)
1999 {
2000 /* stop updating when reached the end of the window (check for _past_
2001 * the end of the window is at the end of the loop) */
2002 if (row == wp->w_height)
2003 {
2004 didline = TRUE;
2005 break;
2006 }
2007
2008 /* stop updating when hit the end of the file */
2009 if (lnum > buf->b_ml.ml_line_count)
2010 {
2011 eof = TRUE;
2012 break;
2013 }
2014
2015 /* Remember the starting row of the line that is going to be dealt
2016 * with. It is used further down when the line doesn't fit. */
2017 srow = row;
2018
2019 /*
2020 * Update a line when it is in an area that needs updating, when it
2021 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002022 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 * win_del_lines(), check if the current line includes it.
2024 * When syntax folding is being used, the saved syntax states will
2025 * already have been updated, we can't see where the syntax state is
2026 * the same again, just update until the end of the window.
2027 */
2028 if (row < top_end
2029 || (row >= mid_start && row < mid_end)
2030#ifdef FEAT_SEARCH_EXTRA
2031 || top_to_mod
2032#endif
2033 || idx >= wp->w_lines_valid
2034 || (row + wp->w_lines[idx].wl_size > bot_start)
2035 || (mod_top != 0
2036 && (lnum == mod_top
2037 || (lnum >= mod_top
2038 && (lnum < mod_bot
2039#ifdef FEAT_SYN_HL
2040 || did_update == DID_FOLD
2041 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02002042 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 && (
2044# ifdef FEAT_FOLDING
2045 (foldmethodIsSyntax(wp)
2046 && hasAnyFolding(wp)) ||
2047# endif
2048 syntax_check_changed(lnum)))
2049#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002050#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02002051 /* match in fixed position might need redraw
2052 * if lines were inserted or deleted */
2053 || (wp->w_match_head != NULL
2054 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 )))))
2057 {
2058#ifdef FEAT_SEARCH_EXTRA
2059 if (lnum == mod_top)
2060 top_to_mod = FALSE;
2061#endif
2062
2063 /*
2064 * When at start of changed lines: May scroll following lines
2065 * up or down to minimize redrawing.
2066 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002067 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 */
2069 if (lnum == mod_top
2070 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002071 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
2073 int old_rows = 0;
2074 int new_rows = 0;
2075 int xtra_rows;
2076 linenr_T l;
2077
2078 /* Count the old number of window rows, using w_lines[], which
2079 * should still contain the sizes for the lines as they are
2080 * currently displayed. */
2081 for (i = idx; i < wp->w_lines_valid; ++i)
2082 {
2083 /* Only valid lines have a meaningful wl_lnum. Invalid
2084 * lines are part of the changed area. */
2085 if (wp->w_lines[i].wl_valid
2086 && wp->w_lines[i].wl_lnum == mod_bot)
2087 break;
2088 old_rows += wp->w_lines[i].wl_size;
2089#ifdef FEAT_FOLDING
2090 if (wp->w_lines[i].wl_valid
2091 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2092 {
2093 /* Must have found the last valid entry above mod_bot.
2094 * Add following invalid entries. */
2095 ++i;
2096 while (i < wp->w_lines_valid
2097 && !wp->w_lines[i].wl_valid)
2098 old_rows += wp->w_lines[i++].wl_size;
2099 break;
2100 }
2101#endif
2102 }
2103
2104 if (i >= wp->w_lines_valid)
2105 {
2106 /* We can't find a valid line below the changed lines,
2107 * need to redraw until the end of the window.
2108 * Inserting/deleting lines has no use. */
2109 bot_start = 0;
2110 }
2111 else
2112 {
2113 /* Able to count old number of rows: Count new window
2114 * rows, and may insert/delete lines */
2115 j = idx;
2116 for (l = lnum; l < mod_bot; ++l)
2117 {
2118#ifdef FEAT_FOLDING
2119 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2120 ++new_rows;
2121 else
2122#endif
2123#ifdef FEAT_DIFF
2124 if (l == wp->w_topline)
2125 new_rows += plines_win_nofill(wp, l, TRUE)
2126 + wp->w_topfill;
2127 else
2128#endif
2129 new_rows += plines_win(wp, l, TRUE);
2130 ++j;
2131 if (new_rows > wp->w_height - row - 2)
2132 {
2133 /* it's getting too much, must redraw the rest */
2134 new_rows = 9999;
2135 break;
2136 }
2137 }
2138 xtra_rows = new_rows - old_rows;
2139 if (xtra_rows < 0)
2140 {
2141 /* May scroll text up. If there is not enough
2142 * remaining text or scrolling fails, must redraw the
2143 * rest. If scrolling works, must redraw the text
2144 * below the scrolled text. */
2145 if (row - xtra_rows >= wp->w_height - 2)
2146 mod_bot = MAXLNUM;
2147 else
2148 {
2149 check_for_delay(FALSE);
2150 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002151 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 mod_bot = MAXLNUM;
2153 else
2154 bot_start = wp->w_height + xtra_rows;
2155 }
2156 }
2157 else if (xtra_rows > 0)
2158 {
2159 /* May scroll text down. If there is not enough
2160 * remaining text of scrolling fails, must redraw the
2161 * rest. */
2162 if (row + xtra_rows >= wp->w_height - 2)
2163 mod_bot = MAXLNUM;
2164 else
2165 {
2166 check_for_delay(FALSE);
2167 if (win_ins_lines(wp, row + old_rows,
2168 xtra_rows, FALSE, FALSE) == FAIL)
2169 mod_bot = MAXLNUM;
2170 else if (top_end > row + old_rows)
2171 /* Scrolled the part at the top that requires
2172 * updating down. */
2173 top_end += xtra_rows;
2174 }
2175 }
2176
2177 /* When not updating the rest, may need to move w_lines[]
2178 * entries. */
2179 if (mod_bot != MAXLNUM && i != j)
2180 {
2181 if (j < i)
2182 {
2183 int x = row + new_rows;
2184
2185 /* move entries in w_lines[] upwards */
2186 for (;;)
2187 {
2188 /* stop at last valid entry in w_lines[] */
2189 if (i >= wp->w_lines_valid)
2190 {
2191 wp->w_lines_valid = j;
2192 break;
2193 }
2194 wp->w_lines[j] = wp->w_lines[i];
2195 /* stop at a line that won't fit */
2196 if (x + (int)wp->w_lines[j].wl_size
2197 > wp->w_height)
2198 {
2199 wp->w_lines_valid = j + 1;
2200 break;
2201 }
2202 x += wp->w_lines[j++].wl_size;
2203 ++i;
2204 }
2205 if (bot_start > x)
2206 bot_start = x;
2207 }
2208 else /* j > i */
2209 {
2210 /* move entries in w_lines[] downwards */
2211 j -= i;
2212 wp->w_lines_valid += j;
2213 if (wp->w_lines_valid > wp->w_height)
2214 wp->w_lines_valid = wp->w_height;
2215 for (i = wp->w_lines_valid; i - j >= idx; --i)
2216 wp->w_lines[i] = wp->w_lines[i - j];
2217
2218 /* The w_lines[] entries for inserted lines are
2219 * now invalid, but wl_size may be used above.
2220 * Reset to zero. */
2221 while (i >= idx)
2222 {
2223 wp->w_lines[i].wl_size = 0;
2224 wp->w_lines[i--].wl_valid = FALSE;
2225 }
2226 }
2227 }
2228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
2230
2231#ifdef FEAT_FOLDING
2232 /*
2233 * When lines are folded, display one line for all of them.
2234 * Otherwise, display normally (can be several display lines when
2235 * 'wrap' is on).
2236 */
2237 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2238 if (fold_count != 0)
2239 {
2240 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2241 ++row;
2242 --fold_count;
2243 wp->w_lines[idx].wl_folded = TRUE;
2244 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2245# ifdef FEAT_SYN_HL
2246 did_update = DID_FOLD;
2247# endif
2248 }
2249 else
2250#endif
2251 if (idx < wp->w_lines_valid
2252 && wp->w_lines[idx].wl_valid
2253 && wp->w_lines[idx].wl_lnum == lnum
2254 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002255 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar17146962019-05-30 00:12:11 +02002256#ifdef FEAT_TEXT_PROP
2257 && !bt_popup(wp->w_buffer)
2258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 && srow + wp->w_lines[idx].wl_size > wp->w_height
2260#ifdef FEAT_DIFF
2261 && diff_check_fill(wp, lnum) == 0
2262#endif
2263 )
2264 {
2265 /* This line is not going to fit. Don't draw anything here,
2266 * will draw "@ " lines below. */
2267 row = wp->w_height + 1;
2268 }
2269 else
2270 {
2271#ifdef FEAT_SEARCH_EXTRA
2272 prepare_search_hl(wp, lnum);
2273#endif
2274#ifdef FEAT_SYN_HL
2275 /* Let the syntax stuff know we skipped a few lines. */
2276 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002277 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 syntax_end_parsing(syntax_last_parsed + 1);
2279#endif
2280
2281 /*
2282 * Display one line.
2283 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002284 row = win_line(wp, lnum, srow, wp->w_height,
2285 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287#ifdef FEAT_FOLDING
2288 wp->w_lines[idx].wl_folded = FALSE;
2289 wp->w_lines[idx].wl_lastlnum = lnum;
2290#endif
2291#ifdef FEAT_SYN_HL
2292 did_update = DID_LINE;
2293 syntax_last_parsed = lnum;
2294#endif
2295 }
2296
2297 wp->w_lines[idx].wl_lnum = lnum;
2298 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002299
2300 /* Past end of the window or end of the screen. Note that after
2301 * resizing wp->w_height may be end up too big. That's a problem
2302 * elsewhere, but prevent a crash here. */
2303 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
2305 /* we may need the size of that too long line later on */
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 = plines_win(wp, lnum, TRUE);
2308 ++idx;
2309 break;
2310 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002311 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 wp->w_lines[idx].wl_size = row - srow;
2313 ++idx;
2314#ifdef FEAT_FOLDING
2315 lnum += fold_count + 1;
2316#else
2317 ++lnum;
2318#endif
2319 }
2320 else
2321 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002322 if (wp->w_p_rnu)
2323 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002324#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002325 // 'relativenumber' set: The text doesn't need to be drawn, but
2326 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002327 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2328 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002329 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002330 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002331#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002332 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002333 }
2334
2335 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 row += wp->w_lines[idx++].wl_size;
2337 if (row > wp->w_height) /* past end of screen */
2338 break;
2339#ifdef FEAT_FOLDING
2340 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2341#else
2342 ++lnum;
2343#endif
2344#ifdef FEAT_SYN_HL
2345 did_update = DID_NONE;
2346#endif
2347 }
2348
2349 if (lnum > buf->b_ml.ml_line_count)
2350 {
2351 eof = TRUE;
2352 break;
2353 }
2354 }
2355 /*
2356 * End of loop over all window lines.
2357 */
2358
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002359#ifdef FEAT_VTP
2360 /* Rewrite the character at the end of the screen line. */
2361 if (use_vtp())
2362 {
2363 int i;
2364
2365 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002366 if (enc_utf8)
2367 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2368 LineOffset[i] + screen_Columns) > 1)
2369 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2370 else
2371 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2372 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002373 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2374 }
2375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
2377 if (idx > wp->w_lines_valid)
2378 wp->w_lines_valid = idx;
2379
2380#ifdef FEAT_SYN_HL
2381 /*
2382 * Let the syntax stuff know we stop parsing here.
2383 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002384 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 syntax_end_parsing(syntax_last_parsed + 1);
2386#endif
2387
2388 /*
2389 * If we didn't hit the end of the file, and we didn't finish the last
2390 * line we were working on, then the line didn't fit.
2391 */
2392 wp->w_empty_rows = 0;
2393#ifdef FEAT_DIFF
2394 wp->w_filler_rows = 0;
2395#endif
2396 if (!eof && !didline)
2397 {
2398 if (lnum == wp->w_topline)
2399 {
2400 /*
2401 * Single line that does not fit!
2402 * Don't overwrite it, it can be edited.
2403 */
2404 wp->w_botline = lnum + 1;
2405 }
2406#ifdef FEAT_DIFF
2407 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2408 {
2409 /* Window ends in filler lines. */
2410 wp->w_botline = lnum;
2411 wp->w_filler_rows = wp->w_height - srow;
2412 }
2413#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002414#ifdef FEAT_TEXT_PROP
2415 else if (bt_popup(wp->w_buffer))
2416 {
2417 // popup line that doesn't fit is left as-is
2418 wp->w_botline = lnum;
2419 }
2420#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002421 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2422 {
2423 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2424
2425 /*
2426 * Last line isn't finished: Display "@@@" in the last screen line.
2427 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002428 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002429 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002430 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002431 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002432 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002433 set_empty_rows(wp, srow);
2434 wp->w_botline = lnum;
2435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2437 {
2438 /*
2439 * Last line isn't finished: Display "@@@" at the end.
2440 */
2441 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2442 W_WINROW(wp) + wp->w_height,
2443 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002444 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 set_empty_rows(wp, srow);
2446 wp->w_botline = lnum;
2447 }
2448 else
2449 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002450 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 wp->w_botline = lnum;
2452 }
2453 }
2454 else
2455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if (eof) /* we hit the end of the file */
2458 {
2459 wp->w_botline = buf->b_ml.ml_line_count + 1;
2460#ifdef FEAT_DIFF
2461 j = diff_check_fill(wp, wp->w_botline);
2462 if (j > 0 && !wp->w_botfill)
2463 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002464 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 if (char2cells(fill_diff) > 1)
2466 i = '-';
2467 else
2468 i = fill_diff;
2469 if (row + j > wp->w_height)
2470 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002471 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 row += j;
2473 }
2474#endif
2475 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002476 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 wp->w_botline = lnum;
2478
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002479 // Make sure the rest of the screen is blank
2480 // put '~'s on rows that aren't part of the file.
Bram Moolenaar17146962019-05-30 00:12:11 +02002481 win_draw_end(wp,
2482#ifdef FEAT_TEXT_PROP
2483 bt_popup(wp->w_buffer) ? ' ' :
2484#endif
2485 '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 }
2487
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002488#ifdef SYN_TIME_LIMIT
2489 syn_set_timeout(NULL);
2490#endif
2491
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 /* Reset the type of redrawing required, the window has been updated. */
2493 wp->w_redr_type = 0;
2494#ifdef FEAT_DIFF
2495 wp->w_old_topfill = wp->w_topfill;
2496 wp->w_old_botfill = wp->w_botfill;
2497#endif
2498
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002499 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 {
2501 /*
2502 * There is a trick with w_botline. If we invalidate it on each
2503 * change that might modify it, this will cause a lot of expensive
2504 * calls to plines() in update_topline() each time. Therefore the
2505 * value of w_botline is often approximated, and this value is used to
2506 * compute the value of w_topline. If the value of w_botline was
2507 * wrong, check that the value of w_topline is correct (cursor is on
2508 * the visible part of the text). If it's not, we need to redraw
2509 * again. Mostly this just means scrolling up a few lines, so it
2510 * doesn't look too bad. Only do this for the current window (where
2511 * changes are relevant).
2512 */
2513 wp->w_valid |= VALID_BOTLINE;
2514 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2515 {
2516 recursive = TRUE;
2517 curwin->w_valid &= ~VALID_TOPLINE;
2518 update_topline(); /* may invalidate w_botline again */
2519 if (must_redraw != 0)
2520 {
2521 /* Don't update for changes in buffer again. */
2522 i = curbuf->b_mod_set;
2523 curbuf->b_mod_set = FALSE;
2524 win_update(curwin);
2525 must_redraw = 0;
2526 curbuf->b_mod_set = i;
2527 }
2528 recursive = FALSE;
2529 }
2530 }
2531
2532#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2533 /* restore got_int, unless CTRL-C was hit while redrawing */
2534 if (!got_int)
2535 got_int = save_got_int;
2536#endif
2537}
2538
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002540 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2541 * Return the new offset.
2542 */
2543 static int
2544screen_fill_end(
2545 win_T *wp,
2546 int c1,
2547 int c2,
2548 int off,
2549 int width,
2550 int row,
2551 int endrow,
2552 int attr)
2553{
2554 int nn = off + width;
2555
2556 if (nn > wp->w_width)
2557 nn = wp->w_width;
2558#ifdef FEAT_RIGHTLEFT
2559 if (wp->w_p_rl)
2560 {
2561 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2562 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2563 c1, c2, attr);
2564 }
2565 else
2566#endif
2567 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2568 wp->w_wincol + off, (int)wp->w_wincol + nn,
2569 c1, c2, attr);
2570 return nn;
2571}
2572
2573/*
2574 * Clear lines near the end the window and mark the unused lines with "c1".
2575 * use "c2" as the filler character.
2576 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 */
2578 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002579win_draw_end(
2580 win_T *wp,
2581 int c1,
2582 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002583 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002584 int row,
2585 int endrow,
2586 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002589 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002590 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002591
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002592 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002593
2594 if (draw_margin)
2595 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002596#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002597 int fdc = compute_foldcolumn(wp, 0);
2598
2599 if (fdc > 0)
2600 // draw the fold column
2601 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002602 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002603#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002604#ifdef FEAT_SIGNS
2605 if (signcolumn_on(wp))
2606 // draw the sign column
2607 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002608 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002609#endif
2610 if ((wp->w_p_nu || wp->w_p_rnu)
2611 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2612 // draw the number column
2613 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002614 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
2617#ifdef FEAT_RIGHTLEFT
2618 if (wp->w_p_rl)
2619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002621 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002622 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002624 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002625 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
2627 else
2628#endif
2629 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002631 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002632 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002634
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 set_empty_rows(wp, row);
2636}
2637
Bram Moolenaar1a384422010-07-14 19:53:30 +02002638#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002639/*
2640 * Advance **color_cols and return TRUE when there are columns to draw.
2641 */
2642 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002643advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002644{
2645 while (**color_cols >= 0 && vcol > **color_cols)
2646 ++*color_cols;
2647 return (**color_cols >= 0);
2648}
2649#endif
2650
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002651#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2652/*
2653 * Copy "text" to ScreenLines using "attr".
2654 * Returns the next screen column.
2655 */
2656 static int
2657text_to_screenline(win_T *wp, char_u *text, int col)
2658{
2659 int off = (int)(current_ScreenLine - ScreenLines);
2660
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002661 if (has_mbyte)
2662 {
2663 int cells;
2664 int u8c, u8cc[MAX_MCO];
2665 int i;
2666 int idx;
2667 int c_len;
2668 char_u *p;
2669# ifdef FEAT_ARABIC
2670 int prev_c = 0; /* previous Arabic character */
2671 int prev_c1 = 0; /* first composing char for prev_c */
2672# endif
2673
2674# ifdef FEAT_RIGHTLEFT
2675 if (wp->w_p_rl)
2676 idx = off;
2677 else
2678# endif
2679 idx = off + col;
2680
2681 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2682 for (p = text; *p != NUL; )
2683 {
2684 cells = (*mb_ptr2cells)(p);
2685 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002686 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002687# ifdef FEAT_RIGHTLEFT
2688 - (wp->w_p_rl ? col : 0)
2689# endif
2690 )
2691 break;
2692 ScreenLines[idx] = *p;
2693 if (enc_utf8)
2694 {
2695 u8c = utfc_ptr2char(p, u8cc);
2696 if (*p < 0x80 && u8cc[0] == 0)
2697 {
2698 ScreenLinesUC[idx] = 0;
2699#ifdef FEAT_ARABIC
2700 prev_c = u8c;
2701#endif
2702 }
2703 else
2704 {
2705#ifdef FEAT_ARABIC
2706 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2707 {
2708 /* Do Arabic shaping. */
2709 int pc, pc1, nc;
2710 int pcc[MAX_MCO];
2711 int firstbyte = *p;
2712
2713 /* The idea of what is the previous and next
2714 * character depends on 'rightleft'. */
2715 if (wp->w_p_rl)
2716 {
2717 pc = prev_c;
2718 pc1 = prev_c1;
2719 nc = utf_ptr2char(p + c_len);
2720 prev_c1 = u8cc[0];
2721 }
2722 else
2723 {
2724 pc = utfc_ptr2char(p + c_len, pcc);
2725 nc = prev_c;
2726 pc1 = pcc[0];
2727 }
2728 prev_c = u8c;
2729
2730 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2731 pc, pc1, nc);
2732 ScreenLines[idx] = firstbyte;
2733 }
2734 else
2735 prev_c = u8c;
2736#endif
2737 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002738 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002739 for (i = 0; i < Screen_mco; ++i)
2740 {
2741 ScreenLinesC[i][idx] = u8cc[i];
2742 if (u8cc[i] == 0)
2743 break;
2744 }
2745 }
2746 if (cells > 1)
2747 ScreenLines[idx + 1] = 0;
2748 }
2749 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2750 /* double-byte single width character */
2751 ScreenLines2[idx] = p[1];
2752 else if (cells > 1)
2753 /* double-width character */
2754 ScreenLines[idx + 1] = p[1];
2755 col += cells;
2756 idx += cells;
2757 p += c_len;
2758 }
2759 }
2760 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002761 {
2762 int len = (int)STRLEN(text);
2763
Bram Moolenaar02631462017-09-22 15:20:32 +02002764 if (len > wp->w_width - col)
2765 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002766 if (len > 0)
2767 {
2768#ifdef FEAT_RIGHTLEFT
2769 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002770 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002771 else
2772#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002773 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002774 col += len;
2775 }
2776 }
2777 return col;
2778}
2779#endif
2780
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781#ifdef FEAT_FOLDING
2782/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002783 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2784 * space is available for window "wp", minus "col".
2785 */
2786 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002787compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002788{
2789 int fdc = wp->w_p_fdc;
2790 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002791 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002792
2793 if (fdc > wwidth - (col + wmw))
2794 fdc = wwidth - (col + wmw);
2795 return fdc;
2796}
2797
2798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 * Display one folded line.
2800 */
2801 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002802fold_line(
2803 win_T *wp,
2804 long fold_count,
2805 foldinfo_T *foldinfo,
2806 linenr_T lnum,
2807 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002809 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 pos_T *top, *bot;
2811 linenr_T lnume = lnum + fold_count - 1;
2812 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002813 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 int col;
2816 int txtcol;
2817 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002818 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819
2820 /* Build the fold line:
2821 * 1. Add the cmdwin_type for the command-line window
2822 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002823 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 * 4. Compose the text
2825 * 5. Add the text
2826 * 6. set highlighting for the Visual area an other text
2827 */
2828 col = 0;
2829
2830 /*
2831 * 1. Add the cmdwin_type for the command-line window
2832 * Ignores 'rightleft', this window is never right-left.
2833 */
2834#ifdef FEAT_CMDWIN
2835 if (cmdwin_type != 0 && wp == curwin)
2836 {
2837 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002838 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 if (enc_utf8)
2840 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 ++col;
2842 }
2843#endif
2844
2845 /*
2846 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002847 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002849 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 if (fdc > 0)
2851 {
2852 fill_foldcolumn(buf, wp, TRUE, lnum);
2853#ifdef FEAT_RIGHTLEFT
2854 if (wp->w_p_rl)
2855 {
2856 int i;
2857
Bram Moolenaar02631462017-09-22 15:20:32 +02002858 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002859 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 /* reverse the fold column */
2861 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002862 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
2864 else
2865#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002866 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 col += fdc;
2868 }
2869
2870#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002871# define RL_MEMSET(p, v, l) \
2872 do { \
2873 if (wp->w_p_rl) \
2874 for (ri = 0; ri < l; ++ri) \
2875 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2876 else \
2877 for (ri = 0; ri < l; ++ri) \
2878 ScreenAttrs[off + (p) + ri] = v; \
2879 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002881# define RL_MEMSET(p, v, l) \
2882 do { \
2883 for (ri = 0; ri < l; ++ri) \
2884 ScreenAttrs[off + (p) + ri] = v; \
2885 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886#endif
2887
Bram Moolenaar64486672010-05-16 15:46:46 +02002888 /* Set all attributes of the 'number' or 'relativenumber' column and the
2889 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002890 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891
2892#ifdef FEAT_SIGNS
2893 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002894 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002896 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 if (len > 0)
2898 {
2899 if (len > 2)
2900 len = 2;
2901# ifdef FEAT_RIGHTLEFT
2902 if (wp->w_p_rl)
2903 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002904 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002905 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 else
2907# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002908 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 col += len;
2910 }
2911 }
2912#endif
2913
2914 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002915 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002917 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002919 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 if (len > 0)
2921 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002922 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002923 long num;
2924 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002925
2926 if (len > w + 1)
2927 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002928
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002929 if (wp->w_p_nu && !wp->w_p_rnu)
2930 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002931 num = (long)lnum;
2932 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002933 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002934 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002935 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002936 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002937 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002938 /* 'number' + 'relativenumber': cursor line shows absolute
2939 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002940 num = lnum;
2941 fmt = "%-*ld ";
2942 }
2943 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002944
Bram Moolenaar700e7342013-01-30 12:31:36 +01002945 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946#ifdef FEAT_RIGHTLEFT
2947 if (wp->w_p_rl)
2948 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002949 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002950 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 else
2952#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002953 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 col += len;
2955 }
2956 }
2957
2958 /*
2959 * 4. Compose the folded-line string with 'foldtext', if set.
2960 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002961 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
2963 txtcol = col; /* remember where text starts */
2964
2965 /*
2966 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2967 * Right-left text is put in columns 0 - number-col, normal text is put
2968 * in columns number-col - window-width.
2969 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002970 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972 /* Fill the rest of the line with the fold filler */
2973#ifdef FEAT_RIGHTLEFT
2974 if (wp->w_p_rl)
2975 col -= txtcol;
2976#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002977 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978#ifdef FEAT_RIGHTLEFT
2979 - (wp->w_p_rl ? txtcol : 0)
2980#endif
2981 )
2982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 if (enc_utf8)
2984 {
2985 if (fill_fold >= 0x80)
2986 {
2987 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002988 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002989 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 }
2991 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002994 ScreenLines[off + col] = fill_fold;
2995 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002996 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002998 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002999 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 }
3001
3002 if (text != buf)
3003 vim_free(text);
3004
3005 /*
3006 * 6. set highlighting for the Visual area an other text.
3007 * If all folded lines are in the Visual area, highlight the line.
3008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3010 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003011 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 {
3013 /* Visual is after curwin->w_cursor */
3014 top = &curwin->w_cursor;
3015 bot = &VIsual;
3016 }
3017 else
3018 {
3019 /* Visual is before curwin->w_cursor */
3020 top = &VIsual;
3021 bot = &curwin->w_cursor;
3022 }
3023 if (lnum >= top->lnum
3024 && lnume <= bot->lnum
3025 && (VIsual_mode != 'v'
3026 || ((lnum > top->lnum
3027 || (lnum == top->lnum
3028 && top->col == 0))
3029 && (lnume < bot->lnum
3030 || (lnume == bot->lnum
3031 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003032 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 {
3034 if (VIsual_mode == Ctrl_V)
3035 {
3036 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02003037 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02003039 if (wp->w_old_cursor_lcol != MAXCOL
3040 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02003041 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 len = wp->w_old_cursor_lcol;
3043 else
Bram Moolenaar02631462017-09-22 15:20:32 +02003044 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003045 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003046 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 }
3048 }
3049 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003050 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02003052 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 }
3055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003057#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003058 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
3059 if (wp->w_p_cc_cols)
3060 {
3061 int i = 0;
3062 int j = wp->w_p_cc_cols[i];
3063 int old_txtcol = txtcol;
3064
3065 while (j > -1)
3066 {
3067 txtcol += j;
3068 if (wp->w_p_wrap)
3069 txtcol -= wp->w_skipcol;
3070 else
3071 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003072 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003073 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003074 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003075 txtcol = old_txtcol;
3076 j = wp->w_p_cc_cols[++i];
3077 }
3078 }
3079
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003080 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00003081 if (wp->w_p_cuc)
3082 {
3083 txtcol += wp->w_virtcol;
3084 if (wp->w_p_wrap)
3085 txtcol -= wp->w_skipcol;
3086 else
3087 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003088 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00003089 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003090 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00003091 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
Bram Moolenaar02631462017-09-22 15:20:32 +02003094 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003095 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096
3097 /*
3098 * Update w_cline_height and w_cline_folded if the cursor line was
3099 * updated (saves a call to plines() later).
3100 */
3101 if (wp == curwin
3102 && lnum <= curwin->w_cursor.lnum
3103 && lnume >= curwin->w_cursor.lnum)
3104 {
3105 curwin->w_cline_row = row;
3106 curwin->w_cline_height = 1;
3107 curwin->w_cline_folded = TRUE;
3108 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
3109 }
3110}
3111
3112/*
3113 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
3114 */
3115 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003116copy_text_attr(
3117 int off,
3118 char_u *buf,
3119 int len,
3120 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003122 int i;
3123
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 if (enc_utf8)
3126 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003127 for (i = 0; i < len; ++i)
3128 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129}
3130
3131/*
3132 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003133 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 */
3135 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003136fill_foldcolumn(
3137 char_u *p,
3138 win_T *wp,
3139 int closed, /* TRUE of FALSE */
3140 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
3142 int i = 0;
3143 int level;
3144 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003145 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003146 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
3148 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003149 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 level = win_foldinfo.fi_level;
3152 if (level > 0)
3153 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003154 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003155 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003156
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 /* If the column is too narrow, we start at the lowest level that
3158 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003159 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 if (first_level < 1)
3161 first_level = 1;
3162
Bram Moolenaar1c934292015-01-27 16:39:29 +01003163 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
3165 if (win_foldinfo.fi_lnum == lnum
3166 && first_level + i >= win_foldinfo.fi_low_level)
3167 p[i] = '-';
3168 else if (first_level == 1)
3169 p[i] = '|';
3170 else if (first_level + i <= 9)
3171 p[i] = '0' + first_level + i;
3172 else
3173 p[i] = '>';
3174 if (first_level + i == level)
3175 break;
3176 }
3177 }
3178 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003179 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180}
3181#endif /* FEAT_FOLDING */
3182
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003183#ifdef FEAT_TEXT_PROP
3184static textprop_T *current_text_props = NULL;
3185static buf_T *current_buf = NULL;
3186
3187 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003188text_prop_compare(const void *s1, const void *s2)
3189{
3190 int idx1, idx2;
3191 proptype_T *pt1, *pt2;
3192 colnr_T col1, col2;
3193
3194 idx1 = *(int *)s1;
3195 idx2 = *(int *)s2;
3196 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3197 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3198 if (pt1 == pt2)
3199 return 0;
3200 if (pt1 == NULL)
3201 return -1;
3202 if (pt2 == NULL)
3203 return 1;
3204 if (pt1->pt_priority != pt2->pt_priority)
3205 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3206 col1 = current_text_props[idx1].tp_col;
3207 col2 = current_text_props[idx2].tp_col;
3208 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3209}
3210#endif
3211
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212/*
3213 * Display line "lnum" of window 'wp' on the screen.
3214 * Start at row "startrow", stop when "endrow" is reached.
3215 * wp->w_virtcol needs to be valid.
3216 *
3217 * Return the number of last row the line occupies.
3218 */
3219 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003220win_line(
3221 win_T *wp,
3222 linenr_T lnum,
3223 int startrow,
3224 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003225 int nochange UNUSED, // not updating for changed text
3226 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003228 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3230 int c = 0; /* init for GCC */
3231 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003232#ifdef FEAT_LINEBREAK
3233 long vcol_sbr = -1; /* virtual column after showbreak */
3234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 long vcol_prev = -1; /* "vcol" of previous character */
3236 char_u *line; /* current line */
3237 char_u *ptr; /* current position in "line" */
3238 int row; /* row in the window, excl w_winrow */
3239 int screen_row; /* row on the screen, incl w_winrow */
3240
3241 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3242 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003243 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003244 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003246 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 int extra_attr = 0; /* attributes when n_extra != 0 */
3248 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3249 displaying lcs_eol at end-of-line */
3250 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3251 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3252
3253 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3254 int saved_n_extra = 0;
3255 char_u *saved_p_extra = NULL;
3256 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003257 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 int saved_char_attr = 0;
3259
3260 int n_attr = 0; /* chars with special attr */
3261 int saved_attr2 = 0; /* char_attr saved for n_attr */
3262 int n_attr3 = 0; /* chars with overruling special attr */
3263 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3264
3265 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3266
3267 int fromcol, tocol; /* start/end of inverting */
3268 int fromcol_prev = -2; /* start of inverting after cursor */
3269 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003271 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 pos_T pos;
3273 long v;
3274
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003275 int char_attr = 0; // attributes for next character
3276 int attr_pri = FALSE; // char_attr has priority
3277 int area_highlighting = FALSE; // Visual or incsearch highlighting
3278 // in this line
3279 int vi_attr = 0; // attributes for Visual and incsearch
3280 // highlighting
3281 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003282 int win_attr = 0; // background for whole window, except
3283 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003284 int area_attr = 0; // attributes desired by highlighting
3285 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003287 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 int syntax_attr = 0; /* attributes desired by syntax */
3289 int has_syntax = FALSE; /* this buffer has syntax highl. */
3290 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003291 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003292 int draw_color_col = FALSE; /* highlight colorcolumn */
3293 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003294#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003295#ifdef FEAT_TEXT_PROP
3296 int text_prop_count;
3297 int text_prop_next = 0; // next text property to use
3298 textprop_T *text_props = NULL;
3299 int *text_prop_idxs = NULL;
3300 int text_props_active = 0;
3301 proptype_T *text_prop_type = NULL;
3302 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003303 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003304#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003305#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003306 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003307# define SPWORDLEN 150
3308 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003309 int nextlinecol = 0; /* column where nextline[] starts */
3310 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003311 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003312 int spell_attr = 0; /* attributes desired by spelling */
3313 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003314 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3315 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003316 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003317 static int cap_col = -1; /* column to check for Cap word */
3318 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003319 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003321 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 int multi_attr = 0; /* attributes desired by multibyte */
3323 int mb_l = 1; /* multi-byte byte length */
3324 int mb_c = 0; /* decoded multi-byte character */
3325 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003326 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#ifdef FEAT_DIFF
3328 int filler_lines; /* nr of filler lines to be drawn */
3329 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003330 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 int change_start = MAXCOL; /* first col of changed area */
3332 int change_end = -1; /* last col of changed area */
3333#endif
3334 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3335#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003336 int need_showbreak = FALSE; /* overlong line, skipping first x
3337 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003339#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003340 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003342 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343#endif
3344#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003345 matchitem_T *cur; /* points to the match list */
3346 match_T *shl; /* points to search_hl or a match */
3347 int shl_flag; /* flag to indicate whether search_hl
3348 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003349 int pos_inprogress; /* marks that position match search is
3350 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003351 int prevcol_hl_flag; /* flag to indicate whether prevcol
3352 equals startcol of search_hl or one
3353 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354#endif
3355#ifdef FEAT_ARABIC
3356 int prev_c = 0; /* previous Arabic character */
3357 int prev_c1 = 0; /* first composing char for prev_c */
3358#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003359#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003360 int did_line_attr = 0;
3361#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003362#ifdef FEAT_TERMINAL
3363 int get_term_attr = FALSE;
3364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
3366 /* draw_state: items that are drawn in sequence: */
3367#define WL_START 0 /* nothing done yet */
3368#ifdef FEAT_CMDWIN
3369# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3370#else
3371# define WL_CMDLINE WL_START
3372#endif
3373#ifdef FEAT_FOLDING
3374# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3375#else
3376# define WL_FOLD WL_CMDLINE
3377#endif
3378#ifdef FEAT_SIGNS
3379# define WL_SIGN WL_FOLD + 1 /* column for signs */
3380#else
3381# define WL_SIGN WL_FOLD /* column for signs */
3382#endif
3383#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003384#ifdef FEAT_LINEBREAK
3385# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003387# define WL_BRI WL_NR
3388#endif
3389#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3390# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3391#else
3392# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393#endif
3394#define WL_LINE WL_SBR + 1 /* text in the line */
3395 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003396#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 int feedback_col = 0;
3398 int feedback_old_attr = -1;
3399#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003400 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar860cae12010-06-05 23:22:07 +02003402#ifdef FEAT_CONCEAL
3403 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003404 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003405 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003406 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003407 int is_concealing = FALSE;
3408 int boguscols = 0; /* nonexistent columns added to force
3409 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003410 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003411 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003412 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003413 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003414# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003415# define FIX_FOR_BOGUSCOLS \
3416 { \
3417 n_extra += vcol_off; \
3418 vcol -= vcol_off; \
3419 vcol_off = 0; \
3420 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003421 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003422 boguscols = 0; \
3423 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003424#else
3425# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
3428 if (startrow > endrow) /* past the end already! */
3429 return startrow;
3430
3431 row = startrow;
3432 screen_row = row + W_WINROW(wp);
3433
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003434 if (!number_only)
3435 {
3436 /*
3437 * To speed up the loop below, set extra_check when there is linebreak,
3438 * trailing white space and/or syntax processing to be done.
3439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003441 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442#endif
3443#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003444 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003445# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003446 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003447# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003448 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003450 /* Prepare for syntax highlighting in this line. When there is an
3451 * error, stop syntax highlighting. */
3452 save_did_emsg = did_emsg;
3453 did_emsg = FALSE;
3454 syntax_start(wp, lnum);
3455 if (did_emsg)
3456 wp->w_s->b_syn_error = TRUE;
3457 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003458 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003459 did_emsg = save_did_emsg;
3460#ifdef SYN_TIME_LIMIT
3461 if (!wp->w_s->b_syn_slow)
3462#endif
3463 {
3464 has_syntax = TRUE;
3465 extra_check = TRUE;
3466 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003469
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003470 /* Check for columns to display for 'colorcolumn'. */
3471 color_cols = wp->w_p_cc_cols;
3472 if (color_cols != NULL)
3473 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003474#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003475
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003476#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003477 if (term_show_buffer(wp->w_buffer))
3478 {
3479 extra_check = TRUE;
3480 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003481 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003482 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003483#endif
3484
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003485#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003486 if (wp->w_p_spell
3487 && *wp->w_s->b_p_spl != NUL
3488 && wp->w_s->b_langp.ga_len > 0
3489 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003490 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003491 /* Prepare for spell checking. */
3492 has_spell = TRUE;
3493 extra_check = TRUE;
3494
Bram Moolenaar7701f302018-10-02 21:20:32 +02003495 /* Get the start of the next line, so that words that wrap to the
3496 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003497 * Trick: skip a few chars for C/shell/Vim comments */
3498 nextline[SPWORDLEN] = NUL;
3499 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3500 {
3501 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3502 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3503 }
3504
Bram Moolenaar7701f302018-10-02 21:20:32 +02003505 /* When a word wrapped from the previous line the start of the
3506 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003507 if (lnum == checked_lnum)
3508 cur_checked_col = checked_col;
3509 checked_lnum = 0;
3510
3511 /* When there was a sentence end in the previous line may require a
3512 * word starting with capital in this line. In line 1 always check
3513 * the first word. */
3514 if (lnum != capcol_lnum)
3515 cap_col = -1;
3516 if (lnum == 1)
3517 cap_col = 0;
3518 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520#endif
3521
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003522 /*
3523 * handle visual active in this window
3524 */
3525 fromcol = -10;
3526 tocol = MAXCOL;
3527 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003529 /* Visual is after curwin->w_cursor */
3530 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003532 top = &curwin->w_cursor;
3533 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003535 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003537 top = &VIsual;
3538 bot = &curwin->w_cursor;
3539 }
3540 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3541 if (VIsual_mode == Ctrl_V) /* block mode */
3542 {
3543 if (lnum_in_visual_area)
3544 {
3545 fromcol = wp->w_old_cursor_fcol;
3546 tocol = wp->w_old_cursor_lcol;
3547 }
3548 }
3549 else /* non-block mode */
3550 {
3551 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003553 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003555 if (VIsual_mode == 'V') /* linewise */
3556 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 else
3558 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003559 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3560 if (gchar_pos(top) == NUL)
3561 tocol = fromcol + 1;
3562 }
3563 }
3564 if (VIsual_mode != 'V' && lnum == bot->lnum)
3565 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003566 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003567 {
3568 fromcol = -10;
3569 tocol = MAXCOL;
3570 }
3571 else if (bot->col == MAXCOL)
3572 tocol = MAXCOL;
3573 else
3574 {
3575 pos = *bot;
3576 if (*p_sel == 'e')
3577 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3578 else
3579 {
3580 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3581 ++tocol;
3582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
3584 }
3585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003587 /* Check if the character under the cursor should not be inverted */
3588 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003589#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003590 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003591#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003592 )
3593 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003595 /* if inverting in this line set area_highlighting */
3596 if (fromcol >= 0)
3597 {
3598 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003599 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003601 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003602 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003603 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003604 && clip_isautosel_plus()))
3605 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003610 /*
3611 * handle 'incsearch' and ":s///c" highlighting
3612 */
3613 else if (highlight_match
3614 && wp == curwin
3615 && lnum >= curwin->w_cursor.lnum
3616 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003618 if (lnum == curwin->w_cursor.lnum)
3619 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003620 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003621 else
3622 fromcol = 0;
3623 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3624 {
3625 pos.lnum = lnum;
3626 pos.col = search_match_endcol;
3627 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3628 }
3629 else
3630 tocol = MAXCOL;
3631 /* do at least one character; happens when past end of line */
3632 if (fromcol == tocol)
3633 tocol = fromcol + 1;
3634 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003635 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 }
3638
3639#ifdef FEAT_DIFF
3640 filler_lines = diff_check(wp, lnum);
3641 if (filler_lines < 0)
3642 {
3643 if (filler_lines == -1)
3644 {
3645 if (diff_find_change(wp, lnum, &change_start, &change_end))
3646 diff_hlf = HLF_ADD; /* added line */
3647 else if (change_start == 0)
3648 diff_hlf = HLF_TXD; /* changed text */
3649 else
3650 diff_hlf = HLF_CHD; /* changed line */
3651 }
3652 else
3653 diff_hlf = HLF_ADD; /* added line */
3654 filler_lines = 0;
3655 area_highlighting = TRUE;
3656 }
3657 if (lnum == wp->w_topline)
3658 filler_lines = wp->w_topfill;
3659 filler_todo = filler_lines;
3660#endif
3661
3662#ifdef LINE_ATTR
3663# ifdef FEAT_SIGNS
3664 /* If this line has a sign with line highlighting set line_attr. */
3665 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3666 if (v != 0)
3667 line_attr = sign_get_attr((int)v, TRUE);
3668# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003669# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003671 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003672 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673# endif
3674 if (line_attr != 0)
3675 area_highlighting = TRUE;
3676#endif
3677
3678 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3679 ptr = line;
3680
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003681#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003682 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003683 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003684 /* For checking first word with a capital skip white space. */
3685 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003686 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003687
Bram Moolenaar30abd282005-06-22 22:35:10 +00003688 /* To be able to spell-check over line boundaries copy the end of the
3689 * current line into nextline[]. Above the start of the next line was
3690 * copied to nextline[SPWORDLEN]. */
3691 if (nextline[SPWORDLEN] == NUL)
3692 {
3693 /* No next line or it is empty. */
3694 nextlinecol = MAXCOL;
3695 nextline_idx = 0;
3696 }
3697 else
3698 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003699 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003700 if (v < SPWORDLEN)
3701 {
3702 /* Short line, use it completely and append the start of the
3703 * next line. */
3704 nextlinecol = 0;
3705 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003706 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003707 nextline_idx = v + 1;
3708 }
3709 else
3710 {
3711 /* Long line, use only the last SPWORDLEN bytes. */
3712 nextlinecol = v - SPWORDLEN;
3713 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3714 nextline_idx = SPWORDLEN + 1;
3715 }
3716 }
3717 }
3718#endif
3719
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003720 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003722 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003723 extra_check = TRUE;
3724 /* find start of trailing whitespace */
3725 if (lcs_trail)
3726 {
3727 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003728 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003729 --trailcol;
3730 trailcol += (colnr_T) (ptr - line);
3731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
3733
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003734 wcr_attr = get_wcr_attr(wp);
3735 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003736 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003737 win_attr = wcr_attr;
3738 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003739 }
3740#ifdef FEAT_TEXT_PROP
3741 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003742 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003743#endif
3744
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 /*
3746 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3747 * first character to be displayed.
3748 */
3749 if (wp->w_p_wrap)
3750 v = wp->w_skipcol;
3751 else
3752 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003753 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003756
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 while (vcol < v && *ptr != NUL)
3758 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003759 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003762 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
3764
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003765 /* When:
3766 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003767 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003768 * - 'virtualedit' is set, or
3769 * - the visual mode is active,
3770 * the end of the line may be before the start of the displayed part.
3771 */
3772 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003773#ifdef FEAT_SYN_HL
3774 wp->w_p_cuc || draw_color_col ||
3775#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003776 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003777 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779
3780 /* Handle a character that's not completely on the screen: Put ptr at
3781 * that character but skip the first few screen characters. */
3782 if (vcol > v)
3783 {
3784 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003786 /* If the character fits on the screen, don't need to skip it.
3787 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003788 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003789 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791
3792 /*
3793 * Adjust for when the inverted text is before the screen,
3794 * and when the start of the inverted text is before the screen.
3795 */
3796 if (tocol <= vcol)
3797 fromcol = 0;
3798 else if (fromcol >= 0 && fromcol < vcol)
3799 fromcol = vcol;
3800
3801#ifdef FEAT_LINEBREAK
3802 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3803 if (wp->w_p_wrap)
3804 need_showbreak = TRUE;
3805#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003806#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003807 /* When spell checking a word we need to figure out the start of the
3808 * word and if it's badly spelled or not. */
3809 if (has_spell)
3810 {
3811 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003812 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003813 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003814
3815 pos = wp->w_cursor;
3816 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003817 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003818 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003819
3820 /* spell_move_to() may call ml_get() and make "line" invalid */
3821 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3822 ptr = line + linecol;
3823
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003824 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003825 {
3826 /* no bad word found at line start, don't check until end of a
3827 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003828 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003829 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003830 }
3831 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003832 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003833 /* bad word found, use attributes until end of word */
3834 word_end = wp->w_cursor.col + len + 1;
3835
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003836 /* Turn index into actual attributes. */
3837 if (spell_hlf != HLF_COUNT)
3838 spell_attr = highlight_attr[spell_hlf];
3839 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003840 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003841
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003842# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003843 /* Need to restart syntax highlighting for this line. */
3844 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003845 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003846# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003847 }
3848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 }
3850
3851 /*
3852 * Correct highlighting for cursor that can't be disabled.
3853 * Avoids having to check this for each character.
3854 */
3855 if (fromcol >= 0)
3856 {
3857 if (noinvcur)
3858 {
3859 if ((colnr_T)fromcol == wp->w_virtcol)
3860 {
3861 /* highlighting starts at cursor, let it start just after the
3862 * cursor */
3863 fromcol_prev = fromcol;
3864 fromcol = -1;
3865 }
3866 else if ((colnr_T)fromcol < wp->w_virtcol)
3867 /* restart highlighting after the cursor */
3868 fromcol_prev = wp->w_virtcol;
3869 }
3870 if (fromcol >= tocol)
3871 fromcol = -1;
3872 }
3873
3874#ifdef FEAT_SEARCH_EXTRA
3875 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003876 * Handle highlighting the last used search pattern and matches.
3877 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003878 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003880 cur = wp->w_match_head;
3881 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003882 while ((cur != NULL || shl_flag == FALSE) && !number_only
3883 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003885 if (shl_flag == FALSE)
3886 {
3887 shl = &search_hl;
3888 shl_flag = TRUE;
3889 }
3890 else
3891 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003892 shl->startcol = MAXCOL;
3893 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003895 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003896 v = (long)(ptr - line);
3897 if (cur != NULL)
3898 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003899 next_search_hl(wp, shl, lnum, (colnr_T)v,
3900 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003901
3902 /* Need to get the line again, a multi-line regexp may have made it
3903 * invalid. */
3904 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3905 ptr = line + v;
3906
3907 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003909 if (shl->lnum == lnum)
3910 shl->startcol = shl->rm.startpos[0].col;
3911 else
3912 shl->startcol = 0;
3913 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3914 - shl->rm.startpos[0].lnum)
3915 shl->endcol = shl->rm.endpos[0].col;
3916 else
3917 shl->endcol = MAXCOL;
3918 /* Highlight one character for an empty match. */
3919 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003921 if (has_mbyte && line[shl->endcol] != NUL)
3922 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3923 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003924 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003926 if ((long)shl->startcol < v) /* match at leftcol */
3927 {
3928 shl->attr_cur = shl->attr;
3929 search_attr = shl->attr;
3930 }
3931 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003933 if (shl != &search_hl && cur != NULL)
3934 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
3936#endif
3937
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003938#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003939 // Cursor line highlighting for 'cursorline' in the current window.
3940 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003941 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003942 // Do not show the cursor line when Visual mode is active, because it's
3943 // not clear what is selected then. Do update w_last_cursorline.
3944 if (!(wp == curwin && VIsual_active))
3945 {
3946 line_attr = HL_ATTR(HLF_CUL);
3947 area_highlighting = TRUE;
3948 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003949 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003950 }
3951#endif
3952
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003953#ifdef FEAT_TEXT_PROP
3954 {
3955 char_u *prop_start;
3956
3957 text_prop_count = get_text_props(wp->w_buffer, lnum,
3958 &prop_start, FALSE);
3959 if (text_prop_count > 0)
3960 {
3961 // Make a copy of the properties, so that they are properly
3962 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003963 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003964 if (text_props != NULL)
3965 mch_memmove(text_props, prop_start,
3966 text_prop_count * sizeof(textprop_T));
3967
3968 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003969 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003970 area_highlighting = TRUE;
3971 extra_check = TRUE;
3972 }
3973 }
3974#endif
3975
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003976 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003978
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979#ifdef FEAT_RIGHTLEFT
3980 if (wp->w_p_rl)
3981 {
3982 /* Rightleft window: process the text in the normal direction, but put
3983 * it in current_ScreenLine[] from right to left. Start at the
3984 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003985 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003987 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989#endif
3990
3991 /*
3992 * Repeat for the whole displayed line.
3993 */
3994 for (;;)
3995 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003996#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003997 int has_match_conc = 0; // match wants to conceal
3998 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 /* Skip this quickly when working on the text. */
4001 if (draw_state != WL_LINE)
4002 {
4003#ifdef FEAT_CMDWIN
4004 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
4005 {
4006 draw_state = WL_CMDLINE;
4007 if (cmdwin_type != 0 && wp == curwin)
4008 {
4009 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004011 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004012 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004013 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015 }
4016#endif
4017
4018#ifdef FEAT_FOLDING
4019 if (draw_state == WL_FOLD - 1 && n_extra == 0)
4020 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01004021 int fdc = compute_foldcolumn(wp, 0);
4022
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01004024 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
Bram Moolenaar62706602016-12-09 19:28:48 +01004026 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004027 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004028 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01004029 p_extra_free = alloc(12 + 1);
4030
4031 if (p_extra_free != NULL)
4032 {
4033 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
4034 n_extra = fdc;
4035 p_extra_free[n_extra] = NUL;
4036 p_extra = p_extra_free;
4037 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004038 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004039 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01004040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 }
4042 }
4043#endif
4044
4045#ifdef FEAT_SIGNS
4046 if (draw_state == WL_SIGN - 1 && n_extra == 0)
4047 {
4048 draw_state = WL_SIGN;
4049 /* Show the sign column when there are any signs in this
4050 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02004051 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004053 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004055 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056# endif
4057
4058 /* Draw two cells with the sign value or blank. */
4059 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004060 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004061 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 n_extra = 2;
4063
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02004064 if (row == startrow
4065#ifdef FEAT_DIFF
4066 + filler_lines && filler_todo <= 0
4067#endif
4068 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
4070 text_sign = buf_getsigntype(wp->w_buffer, lnum,
4071 SIGN_TEXT);
4072# ifdef FEAT_SIGN_ICONS
4073 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
4074 SIGN_ICON);
4075 if (gui.in_use && icon_sign != 0)
4076 {
4077 /* Use the image in this position. */
4078 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004079 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080# ifdef FEAT_NETBEANS_INTG
4081 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004082 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004084 c_final = NUL;
4085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086# endif
4087 char_attr = icon_sign;
4088 }
4089 else
4090# endif
4091 if (text_sign != 0)
4092 {
4093 p_extra = sign_get_text(text_sign);
4094 if (p_extra != NULL)
4095 {
4096 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004097 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004098 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
4100 char_attr = sign_get_attr(text_sign, FALSE);
4101 }
4102 }
4103 }
4104 }
4105#endif
4106
4107 if (draw_state == WL_NR - 1 && n_extra == 0)
4108 {
4109 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02004110 /* Display the absolute or relative line number. After the
4111 * first fill with blanks when the 'n' flag isn't in 'cpo' */
4112 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 && (row == startrow
4114#ifdef FEAT_DIFF
4115 + filler_lines
4116#endif
4117 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
4118 {
4119 /* Draw the line number (empty space after wrapping). */
4120 if (row == startrow
4121#ifdef FEAT_DIFF
4122 + filler_lines
4123#endif
4124 )
4125 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004126 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004127 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004128
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004129 if (wp->w_p_nu && !wp->w_p_rnu)
4130 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004131 num = (long)lnum;
4132 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004133 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004134 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004135 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004136 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004137 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004138 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004139 num = lnum;
4140 fmt = "%-*ld ";
4141 }
4142 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004143
Bram Moolenaar700e7342013-01-30 12:31:36 +01004144 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004145 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 if (wp->w_skipcol > 0)
4147 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4148 *p_extra = '-';
4149#ifdef FEAT_RIGHTLEFT
4150 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004151 {
4152 char_u *p1, *p2;
4153 int t;
4154
4155 // like rl_mirror(), but keep the space at the end
4156 p2 = skiptowhite(extra) - 1;
4157 for (p1 = extra; p1 < p2; ++p1, --p2)
4158 {
4159 t = *p1;
4160 *p1 = *p2;
4161 *p2 = t;
4162 }
4163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164#endif
4165 p_extra = extra;
4166 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004167 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 }
4169 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004172 c_final = NUL;
4173 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004174 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004175 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004176#ifdef FEAT_SYN_HL
4177 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004178 * the current line differently.
4179 * TODO: Can we use CursorLine instead of CursorLineNr
4180 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004181 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004182 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004183 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186 }
4187
Bram Moolenaar597a4222014-06-25 14:39:50 +02004188#ifdef FEAT_LINEBREAK
4189 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4190 && n_extra == 0 && *p_sbr != NUL)
4191 /* draw indent after showbreak value */
4192 draw_state = WL_BRI;
4193 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4194 /* After the showbreak, draw the breakindent */
4195 draw_state = WL_BRI - 1;
4196
4197 /* draw 'breakindent': indent wrapped text accordingly */
4198 if (draw_state == WL_BRI - 1 && n_extra == 0)
4199 {
4200 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004201 /* if need_showbreak is set, breakindent also applies */
4202 if (wp->w_p_bri && n_extra == 0
4203 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004204# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004205 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004206# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004207 )
4208 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004209 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004210# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004211 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004212 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004213 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004214# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004215 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4216 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004217 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004218# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004219 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004220# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004221 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004222 c_extra = ' ';
4223 n_extra = get_breakindent_win(wp,
4224 ml_get_buf(wp->w_buffer, lnum, FALSE));
4225 /* Correct end of highlighted area for 'breakindent',
4226 * required when 'linebreak' is also set. */
4227 if (tocol == vcol)
4228 tocol += n_extra;
4229 }
4230 }
4231#endif
4232
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4234 if (draw_state == WL_SBR - 1 && n_extra == 0)
4235 {
4236 draw_state = WL_SBR;
4237# ifdef FEAT_DIFF
4238 if (filler_todo > 0)
4239 {
4240 /* Draw "deleted" diff line(s). */
4241 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004244 c_final = NUL;
4245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004249 c_final = NUL;
4250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251# ifdef FEAT_RIGHTLEFT
4252 if (wp->w_p_rl)
4253 n_extra = col + 1;
4254 else
4255# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004256 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004257 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259# endif
4260# ifdef FEAT_LINEBREAK
4261 if (*p_sbr != NUL && need_showbreak)
4262 {
4263 /* Draw 'showbreak' at the start of each broken line. */
4264 p_extra = p_sbr;
4265 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004266 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004268 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004270 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 /* Correct end of highlighted area for 'showbreak',
4272 * required when 'linebreak' is also set. */
4273 if (tocol == vcol)
4274 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004275#ifdef FEAT_SYN_HL
4276 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004277 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004278 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004279 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282# endif
4283 }
4284#endif
4285
4286 if (draw_state == WL_LINE - 1 && n_extra == 0)
4287 {
4288 draw_state = WL_LINE;
4289 if (saved_n_extra)
4290 {
4291 /* Continue item from end of wrapped line. */
4292 n_extra = saved_n_extra;
4293 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004294 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 p_extra = saved_p_extra;
4296 char_attr = saved_char_attr;
4297 }
4298 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004299 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 }
4302
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004303 // When still displaying '$' of change command, stop at cursor.
4304 // When only displaying the (relative) line number and that's done,
4305 // stop here.
4306 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004307 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308#ifdef FEAT_DIFF
4309 && filler_todo <= 0
4310#endif
4311 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004312 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004314 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004315 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004316 /* Pretend we have finished updating the window. Except when
4317 * 'cursorcolumn' is set. */
4318#ifdef FEAT_SYN_HL
4319 if (wp->w_p_cuc)
4320 row = wp->w_cline_row + wp->w_cline_height;
4321 else
4322#endif
4323 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 break;
4325 }
4326
Bram Moolenaar637532b2019-01-03 21:44:40 +01004327 if (draw_state == WL_LINE && (area_highlighting
4328#ifdef FEAT_SPELL
4329 || has_spell
4330#endif
4331 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 {
4333 /* handle Visual or match highlighting in this line */
4334 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4336 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004338 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004340 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 else if (area_attr != 0
4342 && (vcol == tocol
4343 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
4346#ifdef FEAT_SEARCH_EXTRA
4347 if (!n_extra)
4348 {
4349 /*
4350 * Check for start/end of search pattern match.
4351 * After end, check for start/end of next match.
4352 * When another match, have to check for start again.
4353 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004354 * Do this for 'search_hl' and the match list (ordered by
4355 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004357 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004358 cur = wp->w_match_head;
4359 shl_flag = FALSE;
4360 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004362 if (shl_flag == FALSE
4363 && ((cur != NULL
4364 && cur->priority > SEARCH_HL_PRIORITY)
4365 || cur == NULL))
4366 {
4367 shl = &search_hl;
4368 shl_flag = TRUE;
4369 }
4370 else
4371 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004372 if (cur != NULL)
4373 cur->pos.cur = 0;
4374 pos_inprogress = TRUE;
4375 while (shl->rm.regprog != NULL
4376 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004378 if (shl->startcol != MAXCOL
4379 && v >= (long)shl->startcol
4380 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004382 int tmp_col = v + MB_PTR2LEN(ptr);
4383
4384 if (shl->endcol < tmp_col)
4385 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004387#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004388 // Match with the "Conceal" group results in hiding
4389 // the match.
4390 if (cur != NULL
4391 && shl != &search_hl
4392 && syn_name2id((char_u *)"Conceal")
4393 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004394 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004395 has_match_conc =
4396 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004397 match_conc = cur->conceal_char;
4398 }
4399 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004400 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004403 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 {
4405 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004406 next_search_hl(wp, shl, lnum, (colnr_T)v,
4407 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004408 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004409 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
4411 /* Need to get the line again, a multi-line regexp
4412 * may have made it invalid. */
4413 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4414 ptr = line + v;
4415
4416 if (shl->lnum == lnum)
4417 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004418 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004420 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004422 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004424 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
4426 /* highlight empty match, try again after
4427 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004429 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004430 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004432 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434
4435 /* Loop to check if the match starts at the
4436 * current position */
4437 continue;
4438 }
4439 }
4440 break;
4441 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004442 if (shl != &search_hl && cur != NULL)
4443 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004445
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004446 /* Use attributes from match with highest priority among
4447 * 'search_hl' and the match list. */
4448 search_attr = search_hl.attr_cur;
4449 cur = wp->w_match_head;
4450 shl_flag = FALSE;
4451 while (cur != NULL || shl_flag == FALSE)
4452 {
4453 if (shl_flag == FALSE
4454 && ((cur != NULL
4455 && cur->priority > SEARCH_HL_PRIORITY)
4456 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004457 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004458 shl = &search_hl;
4459 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004460 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004461 else
4462 shl = &cur->hl;
4463 if (shl->attr_cur != 0)
4464 search_attr = shl->attr_cur;
4465 if (shl != &search_hl && cur != NULL)
4466 cur = cur->next;
4467 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004468 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004469 if (*ptr == NUL && (did_line_attr >= 1
4470 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004471 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473#endif
4474
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004476 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004478 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4479 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004481 if (diff_hlf == HLF_TXD && ptr - line > change_end
4482 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004484 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004485 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004486 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004489
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004490#ifdef FEAT_TEXT_PROP
4491 if (text_props != NULL)
4492 {
4493 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004494 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004495
4496 // Check if any active property ends.
4497 for (pi = 0; pi < text_props_active; ++pi)
4498 {
4499 int tpi = text_prop_idxs[pi];
4500
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004501 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004502 + text_props[tpi].tp_len)
4503 {
4504 if (pi + 1 < text_props_active)
4505 mch_memmove(text_prop_idxs + pi,
4506 text_prop_idxs + pi + 1,
4507 sizeof(int)
4508 * (text_props_active - (pi + 1)));
4509 --text_props_active;
4510 --pi;
4511 }
4512 }
4513
4514 // Add any text property that starts in this column.
4515 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004516 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004517 text_prop_idxs[text_props_active++] = text_prop_next++;
4518
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004519 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004520 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004521 if (text_props_active > 0)
4522 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004523 // Sort the properties on priority and/or starting last.
4524 // Then combine the attributes, highest priority last.
4525 current_text_props = text_props;
4526 current_buf = wp->w_buffer;
4527 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4528 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004529
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004530 for (pi = 0; pi < text_props_active; ++pi)
4531 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004532 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004533 proptype_T *pt = text_prop_type_by_id(
4534 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004535
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004536 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004537 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004538 int pt_attr = syn_id2attr(pt->pt_hl_id);
4539
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004540 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004541 text_prop_attr =
4542 hl_combine_attr(text_prop_attr, pt_attr);
4543 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004544 }
4545 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004546 }
4547 }
4548#endif
4549
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004550 /* Decide which of the highlight attributes to use. */
4551 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004552#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004553 if (area_attr != 0)
4554 char_attr = hl_combine_attr(line_attr, area_attr);
4555 else if (search_attr != 0)
4556 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004557# ifdef FEAT_TEXT_PROP
4558 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004559 {
4560 char_attr = hl_combine_attr(
4561 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4562 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004563# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004564 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004565 || vcol < fromcol || vcol_prev < fromcol_prev
4566 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004567 // Use line_attr when not in the Visual or 'incsearch' area
4568 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004569 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004570#else
4571 if (area_attr != 0)
4572 char_attr = area_attr;
4573 else if (search_attr != 0)
4574 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004575#endif
4576 else
4577 {
4578 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004579#ifdef FEAT_TEXT_PROP
4580 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004581 {
4582 if (text_prop_combine)
4583 char_attr = hl_combine_attr(
4584 syntax_attr, text_prop_attr);
4585 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004586 char_attr = hl_combine_attr(
4587 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004588 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004589 else
4590#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004591#ifdef FEAT_SYN_HL
4592 if (has_syntax)
4593 char_attr = syntax_attr;
4594 else
4595#endif
4596 char_attr = 0;
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004599 if (char_attr == 0)
4600 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
4602 /*
4603 * Get the next character to put on the screen.
4604 */
4605 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004606 * The "p_extra" points to the extra stuff that is inserted to
4607 * represent special characters (non-printable stuff) and other
4608 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004609 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004610 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4611 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4613 */
4614 if (n_extra > 0)
4615 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004616 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004618 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004620 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
4622 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004623 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004624 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626 else
4627 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629 else
4630 {
4631 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 if (has_mbyte)
4633 {
4634 mb_c = c;
4635 if (enc_utf8)
4636 {
4637 /* If the UTF-8 character is more than one byte:
4638 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004639 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 mb_utf8 = FALSE;
4641 if (mb_l > n_extra)
4642 mb_l = 1;
4643 else if (mb_l > 1)
4644 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004645 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004647 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 }
4649 }
4650 else
4651 {
4652 /* if this is a DBCS character, put it in "mb_c" */
4653 mb_l = MB_BYTE2LEN(c);
4654 if (mb_l >= n_extra)
4655 mb_l = 1;
4656 else if (mb_l > 1)
4657 mb_c = (c << 8) + p_extra[1];
4658 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004659 if (mb_l == 0) /* at the NUL at end-of-line */
4660 mb_l = 1;
4661
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 /* If a double-width char doesn't fit display a '>' in the
4663 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004664 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665# ifdef FEAT_RIGHTLEFT
4666 wp->w_p_rl ? (col <= 0) :
4667# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004668 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 && (*mb_char2cells)(mb_c) == 2)
4670 {
4671 c = '>';
4672 mb_c = c;
4673 mb_l = 1;
4674 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004675 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 /* put the pointer back to output the double-width
4677 * character at the start of the next line. */
4678 ++n_extra;
4679 --p_extra;
4680 }
4681 else
4682 {
4683 n_extra -= mb_l - 1;
4684 p_extra += mb_l - 1;
4685 }
4686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 ++p_extra;
4688 }
4689 --n_extra;
4690 }
4691 else
4692 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004693#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004694 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004695#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004696
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004697 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004698 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 /*
4700 * Get a character from the line itself.
4701 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004702 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004703#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004704 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 if (has_mbyte)
4707 {
4708 mb_c = c;
4709 if (enc_utf8)
4710 {
4711 /* If the UTF-8 character is more than one byte: Decode it
4712 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004713 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 mb_utf8 = FALSE;
4715 if (mb_l > 1)
4716 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004717 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 /* Overlong encoded ASCII or ASCII with composing char
4719 * is displayed normally, except a NUL. */
4720 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004721 {
4722 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004723#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004724 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004725#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004728
4729 /* At start of the line we can have a composing char.
4730 * Draw it as a space with a composing char. */
4731 if (utf_iscomposing(mb_c))
4732 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004733 int i;
4734
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004735 for (i = Screen_mco - 1; i > 0; --i)
4736 u8cc[i] = u8cc[i - 1];
4737 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004738 mb_c = ' ';
4739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
4741
4742 if ((mb_l == 1 && c >= 0x80)
4743 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004744 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 {
4746 /*
4747 * Illegal UTF-8 byte: display as <xx>.
4748 * Non-BMP character : display as ? or fullwidth ?.
4749 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004750 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004751# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004752 if (wp->w_p_rl) /* reverse */
4753 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004754# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 p_extra = extra;
4756 c = *p_extra;
4757 mb_c = mb_ptr2char_adv(&p_extra);
4758 mb_utf8 = (c >= 0x80);
4759 n_extra = (int)STRLEN(p_extra);
4760 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004761 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 if (area_attr == 0 && search_attr == 0)
4763 {
4764 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004765 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 saved_attr2 = char_attr; /* save current attr */
4767 }
4768 }
4769 else if (mb_l == 0) /* at the NUL at end-of-line */
4770 mb_l = 1;
4771#ifdef FEAT_ARABIC
4772 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4773 {
4774 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004775 int pc, pc1, nc;
4776 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777
4778 /* The idea of what is the previous and next
4779 * character depends on 'rightleft'. */
4780 if (wp->w_p_rl)
4781 {
4782 pc = prev_c;
4783 pc1 = prev_c1;
4784 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004785 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
4787 else
4788 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004789 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004791 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
4793 prev_c = mb_c;
4794
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004795 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 else
4798 prev_c = mb_c;
4799#endif
4800 }
4801 else /* enc_dbcs */
4802 {
4803 mb_l = MB_BYTE2LEN(c);
4804 if (mb_l == 0) /* at the NUL at end-of-line */
4805 mb_l = 1;
4806 else if (mb_l > 1)
4807 {
4808 /* We assume a second byte below 32 is illegal.
4809 * Hopefully this is OK for all double-byte encodings!
4810 */
4811 if (ptr[1] >= 32)
4812 mb_c = (c << 8) + ptr[1];
4813 else
4814 {
4815 if (ptr[1] == NUL)
4816 {
4817 /* head byte at end of line */
4818 mb_l = 1;
4819 transchar_nonprint(extra, c);
4820 }
4821 else
4822 {
4823 /* illegal tail byte */
4824 mb_l = 2;
4825 STRCPY(extra, "XX");
4826 }
4827 p_extra = extra;
4828 n_extra = (int)STRLEN(extra) - 1;
4829 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004830 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 c = *p_extra++;
4832 if (area_attr == 0 && search_attr == 0)
4833 {
4834 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004835 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 saved_attr2 = char_attr; /* save current attr */
4837 }
4838 mb_c = c;
4839 }
4840 }
4841 }
4842 /* If a double-width char doesn't fit display a '>' in the
4843 * last column; the character is displayed at the start of the
4844 * next line. */
4845 if ((
4846# ifdef FEAT_RIGHTLEFT
4847 wp->w_p_rl ? (col <= 0) :
4848# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004849 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 && (*mb_char2cells)(mb_c) == 2)
4851 {
4852 c = '>';
4853 mb_c = c;
4854 mb_utf8 = FALSE;
4855 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004856 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004857 // Put pointer back so that the character will be
4858 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004860#ifdef FEAT_CONCEAL
4861 did_decrement_ptr = TRUE;
4862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 }
4864 else if (*ptr != NUL)
4865 ptr += mb_l - 1;
4866
4867 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004868 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004869 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004870 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004873 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004874 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 c = ' ';
4876 if (area_attr == 0 && search_attr == 0)
4877 {
4878 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004879 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 saved_attr2 = char_attr; /* save current attr */
4881 }
4882 mb_c = c;
4883 mb_utf8 = FALSE;
4884 mb_l = 1;
4885 }
4886
4887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 ++ptr;
4889
4890 if (extra_check)
4891 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004892#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004893 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004894#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004895
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004896#ifdef FEAT_TERMINAL
4897 if (get_term_attr)
4898 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004899 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004900
4901 if (!attr_pri)
4902 char_attr = syntax_attr;
4903 else
4904 char_attr = hl_combine_attr(syntax_attr, char_attr);
4905 }
4906#endif
4907
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004908#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004909 // Get syntax attribute, unless still at the start of the line
4910 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004911 v = (long)(ptr - line);
4912 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 {
4914 /* Get the syntax attribute for the character. If there
4915 * is an error, disable syntax highlighting. */
4916 save_did_emsg = did_emsg;
4917 did_emsg = FALSE;
4918
Bram Moolenaar217ad922005-03-20 22:37:15 +00004919 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004920# ifdef FEAT_SPELL
4921 has_spell ? &can_spell :
4922# endif
4923 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924
4925 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004926 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004927 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004928 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004929 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 else
4932 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004933
4934 // combine syntax attribute with 'wincolor'
4935 if (win_attr != 0)
4936 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4937
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004938#ifdef SYN_TIME_LIMIT
4939 if (wp->w_s->b_syn_slow)
4940 has_syntax = FALSE;
4941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942
4943 /* Need to get the line again, a multi-line regexp may
4944 * have made it invalid. */
4945 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4946 ptr = line + v;
4947
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004948# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004949 // Text properties overrule syntax highlighting or combine.
4950 if (text_prop_attr == 0 || text_prop_combine)
4951# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004952 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004953 int comb_attr = syntax_attr;
4954# ifdef FEAT_TEXT_PROP
4955 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4956# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004957 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004958 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004959 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004960 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004961 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004962# ifdef FEAT_CONCEAL
4963 /* no concealing past the end of the line, it interferes
4964 * with line highlighting */
4965 if (c == NUL)
4966 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004967 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004968 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004969# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004971#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004972
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004973#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004974 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004975 * Only do this when there is no syntax highlighting, the
4976 * @Spell cluster is not used or the current syntax item
4977 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004978 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004979 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004980 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004981 if (c != 0 && (
4982# ifdef FEAT_SYN_HL
4983 !has_syntax ||
4984# endif
4985 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004986 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004987 char_u *prev_ptr, *p;
4988 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004989 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004990 if (has_mbyte)
4991 {
4992 prev_ptr = ptr - mb_l;
4993 v -= mb_l - 1;
4994 }
4995 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004996 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004997
4998 /* Use nextline[] if possible, it has the start of the
4999 * next line concatenated. */
5000 if ((prev_ptr - line) - nextlinecol >= 0)
5001 p = nextline + (prev_ptr - line) - nextlinecol;
5002 else
5003 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005004 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005005 len = spell_check(wp, p, &spell_hlf, &cap_col,
5006 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005007 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005008
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005009 /* In Insert mode only highlight a word that
5010 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005011 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005012 && (State & INSERT) != 0
5013 && wp->w_cursor.lnum == lnum
5014 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00005015 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005016 && wp->w_cursor.col < (colnr_T)word_end)
5017 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005018 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005019 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005020 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00005021
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005022 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00005023 && (p - nextline) + len > nextline_idx)
5024 {
5025 /* Remember that the good word continues at the
5026 * start of the next line. */
5027 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005028 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005029 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005030
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005031 /* Turn index into actual attributes. */
5032 if (spell_hlf != HLF_COUNT)
5033 spell_attr = highlight_attr[spell_hlf];
5034
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005035 if (cap_col > 0)
5036 {
5037 if (p != prev_ptr
5038 && (p - nextline) + cap_col >= nextline_idx)
5039 {
5040 /* Remember that the word in the next line
5041 * must start with a capital. */
5042 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005043 cap_col = (int)((p - nextline) + cap_col
5044 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005045 }
5046 else
5047 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005048 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005049 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005050 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005051 }
5052 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005053 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005054 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005055 char_attr = hl_combine_attr(char_attr, spell_attr);
5056 else
5057 char_attr = hl_combine_attr(spell_attr, char_attr);
5058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059#endif
5060#ifdef FEAT_LINEBREAK
5061 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00005062 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01005064 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01005065 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01005067 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005068 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01005069
Bram Moolenaar597a4222014-06-25 14:39:50 +02005070 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005071 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02005072 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02005073 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005074# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02005075 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005076 wp->w_buffer->b_p_vts_array) - 1;
5077# else
Bram Moolenaara3650912014-11-19 13:21:57 +01005078 n_extra = (int)wp->w_buffer->b_p_ts
5079 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005080# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01005081
Bram Moolenaar4df70292015-03-21 14:20:16 +01005082 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01005083 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005084 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005085 {
5086#ifdef FEAT_CONCEAL
5087 if (c == TAB)
5088 /* See "Tab alignment" below. */
5089 FIX_FOR_BOGUSCOLS;
5090#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005091 if (!wp->w_p_list)
5092 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095#endif
5096
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005097 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
5098 // But not when the character is followed by a composing
5099 // character (use mb_l to check that).
5100 if (wp->w_p_list
5101 && ((((c == 160 && mb_l == 1)
5102 || (mb_utf8
5103 && ((mb_c == 160 && mb_l == 2)
5104 || (mb_c == 0x202f && mb_l == 3))))
5105 && lcs_nbsp)
5106 || (c == ' '
5107 && mb_l == 1
5108 && lcs_space
5109 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005110 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005111 c = (c == ' ') ? lcs_space : lcs_nbsp;
5112 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005113 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005114 n_attr = 1;
5115 extra_attr = HL_ATTR(HLF_8);
5116 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005117 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005118 mb_c = c;
5119 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005120 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005121 mb_utf8 = TRUE;
5122 u8cc[0] = 0;
5123 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005124 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005125 else
5126 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005127 }
5128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5130 {
5131 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005132 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
5134 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005135 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 saved_attr2 = char_attr; /* save current attr */
5137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005139 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
5141 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005142 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005143 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 else
5146 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 }
5148 }
5149
5150 /*
5151 * Handling of non-printable characters.
5152 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005153 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
5155 /*
5156 * when getting a character from the file, we may have to
5157 * turn it into something else on the way to putting it
5158 * into "ScreenLines".
5159 */
5160 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5161 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005162 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005163 long vcol_adjusted = vcol; /* removed showbreak length */
5164#ifdef FEAT_LINEBREAK
5165 /* only adjust the tab_len, when at the first column
5166 * after the showbreak value was drawn */
5167 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5168 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005171#ifdef FEAT_VARTABS
5172 tab_len = tabstop_padding(vcol_adjusted,
5173 wp->w_buffer->b_p_ts,
5174 wp->w_buffer->b_p_vts_array) - 1;
5175#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005176 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005177 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5178#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005179
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005180#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005181 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005182#endif
5183 /* tab amount depends on current column */
5184 n_extra = tab_len;
5185#ifdef FEAT_LINEBREAK
5186 else
5187 {
5188 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005189 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005190 int i;
5191 int saved_nextra = n_extra;
5192
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005193#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005194 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005195 /* there are characters to conceal */
5196 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005197 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5198 */
5199 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5200 && n_extra > tab_len)
5201 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005202#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005203
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005204 /* if n_extra > 0, it gives the number of chars, to
5205 * use for a tab, else we need to calculate the width
5206 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005207 len = (tab_len * mb_char2len(lcs_tab2));
5208 if (n_extra > 0)
5209 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005210 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005211 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005212 vim_memset(p, ' ', len);
5213 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005214 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005215 p_extra_free = p;
5216 for (i = 0; i < tab_len; i++)
5217 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005218 if (*p == NUL)
5219 {
5220 tab_len = i;
5221 break;
5222 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005223 mb_char2bytes(lcs_tab2, p);
5224 p += mb_char2len(lcs_tab2);
5225 n_extra += mb_char2len(lcs_tab2)
5226 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005227 }
5228 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005229#ifdef FEAT_CONCEAL
5230 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5231 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005232 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005233 n_extra -= vcol_off;
5234#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005235 }
5236#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005237#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005238 {
5239 int vc_saved = vcol_off;
5240
5241 /* Tab alignment should be identical regardless of
5242 * 'conceallevel' value. So tab compensates of all
5243 * previous concealed characters, and thus resets
5244 * vcol_off and boguscols accumulated so far in the
5245 * line. Note that the tab can be longer than
5246 * 'tabstop' when there are concealed characters. */
5247 FIX_FOR_BOGUSCOLS;
5248
5249 /* Make sure, the highlighting for the tab char will be
5250 * correctly set further below (effectively reverts the
5251 * FIX_FOR_BOGSUCOLS macro */
5252 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005253 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005254 tab_len += vc_saved;
5255 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 if (wp->w_p_list)
5259 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005260 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005261#ifdef FEAT_LINEBREAK
5262 if (wp->w_p_lbr)
5263 c_extra = NUL; /* using p_extra from above */
5264 else
5265#endif
5266 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005267 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005268 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005269 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005272 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
5274 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005275 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005276 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279 else
5280 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005281 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 c_extra = ' ';
5283 c = ' ';
5284 }
5285 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005286 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005287 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005288 || ((fromcol >= 0 || fromcol_prev >= 0)
5289 && tocol > vcol
5290 && VIsual_mode != Ctrl_V
5291 && (
5292# ifdef FEAT_RIGHTLEFT
5293 wp->w_p_rl ? (col >= 0) :
5294# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005295 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005296 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005297 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005298 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005299 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005301 /* Display a '$' after the line or highlight an extra
5302 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5304 /* For a diff line the highlighting continues after the
5305 * "$". */
5306 if (
5307# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005308 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309# ifdef LINE_ATTR
5310 &&
5311# endif
5312# endif
5313# ifdef LINE_ATTR
5314 line_attr == 0
5315# endif
5316 )
5317#endif
5318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 /* In virtualedit, visual selections may extend
5320 * beyond end of line. */
5321 if (area_highlighting && virtual_active()
5322 && tocol != MAXCOL && vcol < tocol)
5323 n_extra = 0;
5324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
5326 p_extra = at_end_str;
5327 n_extra = 1;
5328 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005329 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
5331 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005332 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005333 c = lcs_eol;
5334 else
5335 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 lcs_eol_one = -1;
5337 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005338 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005340 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 n_attr = 1;
5342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005344 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
5346 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005347 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005348 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
5350 else
5351 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 }
5353 else if (c != NUL)
5354 {
5355 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005356 if (n_extra == 0)
5357 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358#ifdef FEAT_RIGHTLEFT
5359 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5360 rl_mirror(p_extra); /* reverse "<12>" */
5361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005363 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005364#ifdef FEAT_LINEBREAK
5365 if (wp->w_p_lbr)
5366 {
5367 char_u *p;
5368
5369 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005370 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005371 vim_memset(p, ' ', n_extra);
5372 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5373 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005374 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005375 p_extra_free = p_extra = p;
5376 }
5377 else
5378#endif
5379 {
5380 n_extra = byte2cells(c) - 1;
5381 c = *p_extra++;
5382 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005383 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
5385 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005386 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 saved_attr2 = char_attr; /* save current attr */
5388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 else if (VIsual_active
5392 && (VIsual_mode == Ctrl_V
5393 || VIsual_mode == 'v')
5394 && virtual_active()
5395 && tocol != MAXCOL
5396 && vcol < tocol
5397 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005398#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005400#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005401 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 {
5403 c = ' ';
5404 --ptr; /* put it back at the NUL */
5405 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005406#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 else if ((
5408# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005409 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005411# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005412 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 ) && (
5416# ifdef FEAT_RIGHTLEFT
5417 wp->w_p_rl ? (col >= 0) :
5418# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005419 (col
5420# ifdef FEAT_CONCEAL
5421 - boguscols
5422# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005423 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 {
5425 /* Highlight until the right side of the window */
5426 c = ' ';
5427 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005428
5429 /* Remember we do the char for line highlighting. */
5430 ++did_line_attr;
5431
5432 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005433 if (line_attr != 0 && char_attr == search_attr
5434 && (did_line_attr > 1
5435 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005436 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437# ifdef FEAT_DIFF
5438 if (diff_hlf == HLF_TXD)
5439 {
5440 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005441 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005442 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005443 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005444 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5445 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005446 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 }
5449# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005450# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005451 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005452 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005453 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005454 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5455 char_attr = hl_combine_attr(char_attr,
5456 HL_ATTR(HLF_CUL));
5457 }
5458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 }
5460#endif
5461 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005462
5463#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005464 if ( wp->w_p_cole > 0
5465 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005466 conceal_cursor_line(wp))
5467 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005468 && !(lnum_in_visual_area
5469 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005470 {
5471 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005472 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005473 && (syn_get_sub_char() != NUL || match_conc
5474 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005475 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005476 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005477 /* First time at this concealed item: display one
5478 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005479 if (match_conc)
5480 c = match_conc;
5481 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005482 c = syn_get_sub_char();
5483 else if (lcs_conceal != NUL)
5484 c = lcs_conceal;
5485 else
5486 c = ' ';
5487
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005488 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005489
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005490 if (n_extra > 0)
5491 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005492 vcol += n_extra;
5493 if (wp->w_p_wrap && n_extra > 0)
5494 {
5495# ifdef FEAT_RIGHTLEFT
5496 if (wp->w_p_rl)
5497 {
5498 col -= n_extra;
5499 boguscols -= n_extra;
5500 }
5501 else
5502# endif
5503 {
5504 boguscols += n_extra;
5505 col += n_extra;
5506 }
5507 }
5508 n_extra = 0;
5509 n_attr = 0;
5510 }
5511 else if (n_skip == 0)
5512 {
5513 is_concealing = TRUE;
5514 n_skip = 1;
5515 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005516 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005517 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005518 {
5519 mb_utf8 = TRUE;
5520 u8cc[0] = 0;
5521 c = 0xc0;
5522 }
5523 else
5524 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005525 }
5526 else
5527 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005528 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005529 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005530 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005531
5532 if (n_skip > 0 && did_decrement_ptr)
5533 // not showing the '>', put pointer back to avoid getting stuck
5534 ++ptr;
5535
5536#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 }
5538
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005539#ifdef FEAT_CONCEAL
5540 /* In the cursor line and we may be concealing characters: correct
5541 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005542 if (!did_wcol && draw_state == WL_LINE
5543 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005544 && conceal_cursor_line(wp)
5545 && (int)wp->w_virtcol <= vcol + n_skip)
5546 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005547# ifdef FEAT_RIGHTLEFT
5548 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005549 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005550 else
5551# endif
5552 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005553 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005554 did_wcol = TRUE;
5555 }
5556#endif
5557
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 /* Don't override visual selection highlighting. */
5559 if (n_attr > 0
5560 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005561 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 char_attr = extra_attr;
5563
Bram Moolenaar81695252004-12-29 20:58:21 +00005564#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 /* XIM don't send preedit_start and preedit_end, but they send
5566 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5567 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005568 if (p_imst == IM_ON_THE_SPOT
5569 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005570 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 && (State & INSERT)
5572 && !p_imdisable
5573 && im_is_preediting()
5574 && draw_state == WL_LINE)
5575 {
5576 colnr_T tcol;
5577
5578 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005579 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 else
5581 tcol = preedit_end_col;
5582 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5583 {
5584 if (feedback_old_attr < 0)
5585 {
5586 feedback_col = 0;
5587 feedback_old_attr = char_attr;
5588 }
5589 char_attr = im_get_feedback_attr(feedback_col);
5590 if (char_attr < 0)
5591 char_attr = feedback_old_attr;
5592 feedback_col++;
5593 }
5594 else if (feedback_old_attr >= 0)
5595 {
5596 char_attr = feedback_old_attr;
5597 feedback_old_attr = -1;
5598 feedback_col = 0;
5599 }
5600 }
5601#endif
5602 /*
5603 * Handle the case where we are in column 0 but not on the first
5604 * character of the line and the user wants us to show us a
5605 * special character (via 'listchars' option "precedes:<char>".
5606 */
5607 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005608 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5610#ifdef FEAT_DIFF
5611 && filler_todo <= 0
5612#endif
5613 && draw_state > WL_NR
5614 && c != NUL)
5615 {
5616 c = lcs_prec;
5617 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005618 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5619 {
5620 /* Double-width character being overwritten by the "precedes"
5621 * character, need to fill up half the character. */
5622 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005623 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005624 n_extra = 1;
5625 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005626 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005629 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 {
5631 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005632 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005633 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
5635 else
5636 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005637 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
5639 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005640 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 n_attr3 = 1;
5642 }
5643 }
5644
5645 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005646 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005648 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005649#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005650 || did_line_attr == 1
5651#endif
5652 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005654#ifdef FEAT_SEARCH_EXTRA
5655 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005656
5657 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005658 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005659 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005660#endif
5661
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005662 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 * highlight match at end of line. If it's beyond the last
5664 * char on the screen, just overwrite that one (tricky!) Not
5665 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005666#ifdef FEAT_SEARCH_EXTRA
5667 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005668 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005669 prevcol_hl_flag = TRUE;
5670 else
5671 {
5672 cur = wp->w_match_head;
5673 while (cur != NULL)
5674 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005675 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005676 {
5677 prevcol_hl_flag = TRUE;
5678 break;
5679 }
5680 cur = cur->next;
5681 }
5682 }
5683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005685 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005686 && (VIsual_mode != Ctrl_V
5687 || lnum == VIsual.lnum
5688 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005689 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690#ifdef FEAT_SEARCH_EXTRA
5691 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005692 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005693# ifdef FEAT_SYN_HL
5694 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5695 && !(wp == curwin && VIsual_active))
5696# endif
5697# ifdef FEAT_DIFF
5698 && diff_hlf == (hlf_T)0
5699# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005700# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005701 && did_line_attr <= 1
5702# endif
5703 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704#endif
5705 ))
5706 {
5707 int n = 0;
5708
5709#ifdef FEAT_RIGHTLEFT
5710 if (wp->w_p_rl)
5711 {
5712 if (col < 0)
5713 n = 1;
5714 }
5715 else
5716#endif
5717 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005718 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 n = -1;
5720 }
5721 if (n != 0)
5722 {
5723 /* At the window boundary, highlight the last character
5724 * instead (better than nothing). */
5725 off += n;
5726 col += n;
5727 }
5728 else
5729 {
5730 /* Add a blank character to highlight. */
5731 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 if (enc_utf8)
5733 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 }
5735#ifdef FEAT_SEARCH_EXTRA
5736 if (area_attr == 0)
5737 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005738 /* Use attributes from match with highest priority among
5739 * 'search_hl' and the match list. */
5740 char_attr = search_hl.attr;
5741 cur = wp->w_match_head;
5742 shl_flag = FALSE;
5743 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005744 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005745 if (shl_flag == FALSE
5746 && ((cur != NULL
5747 && cur->priority > SEARCH_HL_PRIORITY)
5748 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005749 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005750 shl = &search_hl;
5751 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005752 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005753 else
5754 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005755 if ((ptr - line) - 1 == (long)shl->startcol
5756 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005757 char_attr = shl->attr;
5758 if (shl != &search_hl && cur != NULL)
5759 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
5762#endif
5763 ScreenAttrs[off] = char_attr;
5764#ifdef FEAT_RIGHTLEFT
5765 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005766 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005768 --off;
5769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 else
5771#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005772 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005774 ++off;
5775 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005776 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005777#ifdef FEAT_SYN_HL
5778 eol_hl_off = 1;
5779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
Bram Moolenaar91170f82006-05-05 21:15:17 +00005783 /*
5784 * At end of the text line.
5785 */
5786 if (c == NUL)
5787 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005788#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005789 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005790 if (wp->w_p_wrap)
5791 v = wp->w_skipcol;
5792 else
5793 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005794
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005795 /* check if line ends before left margin */
5796 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005797 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005798#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005799 // Get rid of the boguscols now, we want to draw until the right
5800 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005801 col -= boguscols;
5802 boguscols = 0;
5803#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005804
5805 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005806 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005807
5808 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005809 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5810 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005811 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005812 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005813 || draw_color_col
5814 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005815# ifdef FEAT_RIGHTLEFT
5816 && !wp->w_p_rl
5817# endif
5818 )
5819 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005820 int rightmost_vcol = 0;
5821 int i;
5822
5823 if (wp->w_p_cuc)
5824 rightmost_vcol = wp->w_virtcol;
5825 if (draw_color_col)
5826 /* determine rightmost colorcolumn to possibly draw */
5827 for (i = 0; color_cols[i] >= 0; ++i)
5828 if (rightmost_vcol < color_cols[i])
5829 rightmost_vcol = color_cols[i];
5830
Bram Moolenaar02631462017-09-22 15:20:32 +02005831 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005832 {
5833 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005834 if (enc_utf8)
5835 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005836 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005837 if (draw_color_col)
5838 draw_color_col = advance_color_col(VCOL_HLC,
5839 &color_cols);
5840
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005841 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005842 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005843 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005844 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005845 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005846 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005847
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005848 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005849 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005850
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005851 ++vcol;
5852 }
5853 }
5854#endif
5855
Bram Moolenaar53f81742017-09-22 14:35:51 +02005856 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005857 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 row++;
5859
5860 /*
5861 * Update w_cline_height and w_cline_folded if the cursor line was
5862 * updated (saves a call to plines() later).
5863 */
5864 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5865 {
5866 curwin->w_cline_row = startrow;
5867 curwin->w_cline_height = row - startrow;
5868#ifdef FEAT_FOLDING
5869 curwin->w_cline_folded = FALSE;
5870#endif
5871 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5872 }
5873
5874 break;
5875 }
5876
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005877 // Show "extends" character from 'listchars' if beyond the line end and
5878 // 'list' is set.
5879 if (lcs_ext != NUL
5880 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 && !wp->w_p_wrap
5882#ifdef FEAT_DIFF
5883 && filler_todo <= 0
5884#endif
5885 && (
5886#ifdef FEAT_RIGHTLEFT
5887 wp->w_p_rl ? col == 0 :
5888#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005889 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005891 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5893 {
5894 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005895 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005897 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {
5899 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005900 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005901 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 }
5903 else
5904 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 }
5906
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005907#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005908 /* advance to the next 'colorcolumn' */
5909 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005910 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005911
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005912 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005913 * highlight the cursor position itself.
5914 * Also highlight the 'colorcolumn' if it is different than
5915 * 'cursorcolumn' */
5916 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005917 if (draw_state == WL_LINE && !lnum_in_visual_area
5918 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005919 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005920 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005921 && lnum != wp->w_cursor.lnum)
5922 {
5923 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005924 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005925 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005926 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005927 {
5928 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005929 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005930 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005931 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005932#endif
5933
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 /*
5935 * Store character to be displayed.
5936 * Skip characters that are left of the screen for 'nowrap'.
5937 */
5938 vcol_prev = vcol;
5939 if (draw_state < WL_LINE || n_skip <= 0)
5940 {
5941 /*
5942 * Store the character.
5943 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005944#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5946 {
5947 /* A double-wide character is: put first halve in left cell. */
5948 --off;
5949 --col;
5950 }
5951#endif
5952 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005954 {
5955 if ((mb_c & 0xff00) == 0x8e00)
5956 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 else if (enc_utf8)
5960 {
5961 if (mb_utf8)
5962 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005963 int i;
5964
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005966 if ((c & 0xff) == 0)
5967 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005968 for (i = 0; i < Screen_mco; ++i)
5969 {
5970 ScreenLinesC[i][off] = u8cc[i];
5971 if (u8cc[i] == 0)
5972 break;
5973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 }
5975 else
5976 ScreenLinesUC[off] = 0;
5977 }
5978 if (multi_attr)
5979 {
5980 ScreenAttrs[off] = multi_attr;
5981 multi_attr = 0;
5982 }
5983 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 ScreenAttrs[off] = char_attr;
5985
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5987 {
5988 /* Need to fill two screen columns. */
5989 ++off;
5990 ++col;
5991 if (enc_utf8)
5992 /* UTF-8: Put a 0 in the second screen char. */
5993 ScreenLines[off] = 0;
5994 else
5995 /* DBCS: Put second byte in the second screen char. */
5996 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005997 if (draw_state > WL_NR
5998#ifdef FEAT_DIFF
5999 && filler_todo <= 0
6000#endif
6001 )
6002 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 /* When "tocol" is halfway a character, set it to the end of
6004 * the character, otherwise highlighting won't stop. */
6005 if (tocol == vcol)
6006 ++tocol;
6007#ifdef FEAT_RIGHTLEFT
6008 if (wp->w_p_rl)
6009 {
6010 /* now it's time to backup one cell */
6011 --off;
6012 --col;
6013 }
6014#endif
6015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016#ifdef FEAT_RIGHTLEFT
6017 if (wp->w_p_rl)
6018 {
6019 --off;
6020 --col;
6021 }
6022 else
6023#endif
6024 {
6025 ++off;
6026 ++col;
6027 }
6028 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006029#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006030 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006031 {
6032 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006033 ++vcol_off;
6034 if (n_extra > 0)
6035 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02006036 if (wp->w_p_wrap)
6037 {
6038 /*
6039 * Special voodoo required if 'wrap' is on.
6040 *
6041 * Advance the column indicator to force the line
6042 * drawing to wrap early. This will make the line
6043 * take up the same screen space when parts are concealed,
6044 * so that cursor line computations aren't messed up.
6045 *
6046 * To avoid the fictitious advance of 'col' causing
6047 * trailing junk to be written out of the screen line
6048 * we are building, 'boguscols' keeps track of the number
6049 * of bad columns we have advanced.
6050 */
6051 if (n_extra > 0)
6052 {
6053 vcol += n_extra;
6054# ifdef FEAT_RIGHTLEFT
6055 if (wp->w_p_rl)
6056 {
6057 col -= n_extra;
6058 boguscols -= n_extra;
6059 }
6060 else
6061# endif
6062 {
6063 col += n_extra;
6064 boguscols += n_extra;
6065 }
6066 n_extra = 0;
6067 n_attr = 0;
6068 }
6069
6070
Bram Moolenaar860cae12010-06-05 23:22:07 +02006071 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6072 {
6073 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006074# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02006075 if (wp->w_p_rl)
6076 {
6077 --boguscols;
6078 --col;
6079 }
6080 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01006081# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006082 {
6083 ++boguscols;
6084 ++col;
6085 }
6086 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006087
6088# ifdef FEAT_RIGHTLEFT
6089 if (wp->w_p_rl)
6090 {
6091 --boguscols;
6092 --col;
6093 }
6094 else
6095# endif
6096 {
6097 ++boguscols;
6098 ++col;
6099 }
6100 }
6101 else
6102 {
6103 if (n_extra > 0)
6104 {
6105 vcol += n_extra;
6106 n_extra = 0;
6107 n_attr = 0;
6108 }
6109 }
6110
6111 }
6112#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 else
6114 --n_skip;
6115
Bram Moolenaar64486672010-05-16 15:46:46 +02006116 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
6117 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00006118 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119#ifdef FEAT_DIFF
6120 && filler_todo <= 0
6121#endif
6122 )
6123 ++vcol;
6124
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006125#ifdef FEAT_SYN_HL
6126 if (vcol_save_attr >= 0)
6127 char_attr = vcol_save_attr;
6128#endif
6129
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 /* restore attributes after "predeces" in 'listchars' */
6131 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6132 char_attr = saved_attr3;
6133
6134 /* restore attributes after last 'listchars' or 'number' char */
6135 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6136 char_attr = saved_attr2;
6137
6138 /*
6139 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006140 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 */
6142 if ((
6143#ifdef FEAT_RIGHTLEFT
6144 wp->w_p_rl ? (col < 0) :
6145#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006146 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 && (*ptr != NUL
6148#ifdef FEAT_DIFF
6149 || filler_todo > 0
6150#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006151 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6153 )
6154 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006155#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006156 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006157 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006158 boguscols = 0;
6159#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006160 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006161 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 ++row;
6164 ++screen_row;
6165
6166 /* When not wrapping and finished diff lines, or when displayed
6167 * '$' and highlighting until last column, break here. */
6168 if ((!wp->w_p_wrap
6169#ifdef FEAT_DIFF
6170 && filler_todo <= 0
6171#endif
6172 ) || lcs_eol_one == -1)
6173 break;
6174
6175 /* When the window is too narrow draw all "@" lines. */
6176 if (draw_state != WL_LINE
6177#ifdef FEAT_DIFF
6178 && filler_todo <= 0
6179#endif
6180 )
6181 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006182 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 row = endrow;
6185 }
6186
6187 /* When line got too long for screen break here. */
6188 if (row == endrow)
6189 {
6190 ++row;
6191 break;
6192 }
6193
6194 if (screen_cur_row == screen_row - 1
6195#ifdef FEAT_DIFF
6196 && filler_todo <= 0
6197#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006198 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 {
6200 /* Remember that the line wraps, used for modeless copy. */
6201 LineWraps[screen_row - 1] = TRUE;
6202
6203 /*
6204 * Special trick to make copy/paste of wrapped lines work with
6205 * xterm/screen: write an extra character beyond the end of
6206 * the line. This will work with all terminal types
6207 * (regardless of the xn,am settings).
6208 * Only do this on a fast tty.
6209 * Only do this if the cursor is on the current line
6210 * (something has been written in it).
6211 * Don't do this for the GUI.
6212 * Don't do this for double-width characters.
6213 * Don't do this for a window not at the right screen border.
6214 */
6215 if (p_tf
6216#ifdef FEAT_GUI
6217 && !gui.in_use
6218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006220 && ((*mb_off2cells)(LineOffset[screen_row],
6221 LineOffset[screen_row] + screen_Columns)
6222 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006224 + (int)Columns - 2,
6225 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006226 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 {
6228 /* First make sure we are at the end of the screen line,
6229 * then output the same character again to let the
6230 * terminal know about the wrap. If the terminal doesn't
6231 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006232 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 screen_char(LineOffset[screen_row - 1]
6234 + (unsigned)Columns - 1,
6235 screen_row - 1, (int)(Columns - 1));
6236
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 /* When there is a multi-byte character, just output a
6238 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006239 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6240 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 out_char(' ');
6242 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 out_char(ScreenLines[LineOffset[screen_row - 1]
6244 + (Columns - 1)]);
6245 /* force a redraw of the first char on the next line */
6246 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6247 screen_start(); /* don't know where cursor is now */
6248 }
6249 }
6250
6251 col = 0;
6252 off = (unsigned)(current_ScreenLine - ScreenLines);
6253#ifdef FEAT_RIGHTLEFT
6254 if (wp->w_p_rl)
6255 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006256 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 off += col;
6258 }
6259#endif
6260
6261 /* reset the drawing state for the start of a wrapped line */
6262 draw_state = WL_START;
6263 saved_n_extra = n_extra;
6264 saved_p_extra = p_extra;
6265 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006266 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 saved_char_attr = char_attr;
6268 n_extra = 0;
6269 lcs_prec_todo = lcs_prec;
6270#ifdef FEAT_LINEBREAK
6271# ifdef FEAT_DIFF
6272 if (filler_todo <= 0)
6273# endif
6274 need_showbreak = TRUE;
6275#endif
6276#ifdef FEAT_DIFF
6277 --filler_todo;
6278 /* When the filler lines are actually below the last line of the
6279 * file, don't draw the line itself, break here. */
6280 if (filler_todo == 0 && wp->w_botfill)
6281 break;
6282#endif
6283 }
6284
6285 } /* for every character in the line */
6286
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006287#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006288 /* After an empty line check first word for capital. */
6289 if (*skipwhite(line) == NUL)
6290 {
6291 capcol_lnum = lnum + 1;
6292 cap_col = 0;
6293 }
6294#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006295#ifdef FEAT_TEXT_PROP
6296 vim_free(text_props);
6297 vim_free(text_prop_idxs);
6298#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006299
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006300 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 return row;
6302}
6303
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006304/*
6305 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006306 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006307 */
6308 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006309comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006310{
6311 int i;
6312
6313 for (i = 0; i < Screen_mco; ++i)
6314 {
6315 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6316 return TRUE;
6317 if (ScreenLinesC[i][off_from] == 0)
6318 break;
6319 }
6320 return FALSE;
6321}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006322
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323/*
6324 * Check whether the given character needs redrawing:
6325 * - the (first byte of the) character is different
6326 * - the attributes are different
6327 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006328 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 */
6330 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006331char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
6333 if (cols > 0
6334 && ((ScreenLines[off_from] != ScreenLines[off_to]
6335 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 || (enc_dbcs != 0
6337 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6338 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6339 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6340 : (cols > 1 && ScreenLines[off_from + 1]
6341 != ScreenLines[off_to + 1])))
6342 || (enc_utf8
6343 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6344 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006345 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006346 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6347 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006348 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 return TRUE;
6350 return FALSE;
6351}
6352
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006353#if defined(FEAT_TERMINAL) || defined(PROTO)
6354/*
6355 * Return the index in ScreenLines[] for the current screen line.
6356 */
6357 int
6358screen_get_current_line_off()
6359{
6360 return (int)(current_ScreenLine - ScreenLines);
6361}
6362#endif
6363
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364/*
6365 * Move one "cooked" screen line to the screen, but only the characters that
6366 * have actually changed. Handle insert/delete character.
6367 * "coloff" gives the first column on the screen for this line.
6368 * "endcol" gives the columns where valid characters are.
6369 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6370 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006371 * "flags" can have bits:
6372 * SLF_POPUP popup window
6373 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6375 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6376 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006377 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006378screen_line(
6379 int row,
6380 int coloff,
6381 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006382 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006383 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
6385 unsigned off_from;
6386 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006387 unsigned max_off_from;
6388 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 int force = FALSE; /* force update rest of the line */
6392 int redraw_this /* bool: does character need redraw? */
6393#ifdef FEAT_GUI
6394 = TRUE /* For GUI when while-loop empty */
6395#endif
6396 ;
6397 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 int clear_next = FALSE;
6399 int char_cells; /* 1: normal char */
6400 /* 2: occupies two display cells */
6401# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006403 /* Check for illegal row and col, just in case. */
6404 if (row >= Rows)
6405 row = Rows - 1;
6406 if (endcol > Columns)
6407 endcol = Columns;
6408
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409# ifdef FEAT_CLIPBOARD
6410 clip_may_clear_selection(row, row);
6411# endif
6412
6413 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6414 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006415 max_off_from = off_from + screen_Columns;
6416 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417
6418#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006419 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 {
6421 /* Clear rest first, because it's left of the text. */
6422 if (clear_width > 0)
6423 {
6424 while (col <= endcol && ScreenLines[off_to] == ' '
6425 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006426 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 {
6428 ++off_to;
6429 ++col;
6430 }
6431 if (col <= endcol)
6432 screen_fill(row, row + 1, col + coloff,
6433 endcol + coloff + 1, ' ', ' ', 0);
6434 }
6435 col = endcol + 1;
6436 off_to = LineOffset[row] + col + coloff;
6437 off_from += col;
6438 endcol = (clear_width > 0 ? clear_width : -clear_width);
6439 }
6440#endif /* FEAT_RIGHTLEFT */
6441
6442 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6443
6444 while (col < endcol)
6445 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006447 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 else
6449 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
6451 redraw_this = redraw_next;
6452 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6453 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6454
6455#ifdef FEAT_GUI
6456 /* If the next character was bold, then redraw the current character to
6457 * remove any pixels that might have spilt over into us. This only
6458 * happens in the GUI.
6459 */
6460 if (redraw_next && gui.in_use)
6461 {
6462 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006463 if (hl > HL_ALL)
6464 hl = syn_attr2attr(hl);
6465 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 redraw_this = TRUE;
6467 }
6468#endif
6469
6470 if (redraw_this)
6471 {
6472 /*
6473 * Special handling when 'xs' termcap flag set (hpterm):
6474 * Attributes for characters are stored at the position where the
6475 * cursor is when writing the highlighting code. The
6476 * start-highlighting code must be written with the cursor on the
6477 * first highlighted character. The stop-highlighting code must
6478 * be written with the cursor just after the last highlighted
6479 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006480 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 * to clear the rest of the line, and force redrawing it
6482 * completely.
6483 */
6484 if ( p_wiv
6485 && !force
6486#ifdef FEAT_GUI
6487 && !gui.in_use
6488#endif
6489 && ScreenAttrs[off_to] != 0
6490 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6491 {
6492 /*
6493 * Need to remove highlighting attributes here.
6494 */
6495 windgoto(row, col + coloff);
6496 out_str(T_CE); /* clear rest of this screen line */
6497 screen_start(); /* don't know where cursor is now */
6498 force = TRUE; /* force redraw of rest of the line */
6499 redraw_next = TRUE; /* or else next char would miss out */
6500
6501 /*
6502 * If the previous character was highlighted, need to stop
6503 * highlighting at this character.
6504 */
6505 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6506 {
6507 screen_attr = ScreenAttrs[off_to - 1];
6508 term_windgoto(row, col + coloff);
6509 screen_stop_highlight();
6510 }
6511 else
6512 screen_attr = 0; /* highlighting has stopped */
6513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 if (enc_dbcs != 0)
6515 {
6516 /* Check if overwriting a double-byte with a single-byte or
6517 * the other way around requires another character to be
6518 * redrawn. For UTF-8 this isn't needed, because comparing
6519 * ScreenLinesUC[] is sufficient. */
6520 if (char_cells == 1
6521 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006522 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 {
6524 /* Writing a single-cell character over a double-cell
6525 * character: need to redraw the next cell. */
6526 ScreenLines[off_to + 1] = 0;
6527 redraw_next = TRUE;
6528 }
6529 else if (char_cells == 2
6530 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006531 && (*mb_off2cells)(off_to, max_off_to) == 1
6532 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
6534 /* Writing the second half of a double-cell character over
6535 * a double-cell character: need to redraw the second
6536 * cell. */
6537 ScreenLines[off_to + 2] = 0;
6538 redraw_next = TRUE;
6539 }
6540
6541 if (enc_dbcs == DBCS_JPNU)
6542 ScreenLines2[off_to] = ScreenLines2[off_from];
6543 }
6544 /* When writing a single-width character over a double-width
6545 * character and at the end of the redrawn text, need to clear out
6546 * the right halve of the old character.
6547 * Also required when writing the right halve of a double-width
6548 * char over the left halve of an existing one. */
6549 if (has_mbyte && col + char_cells == endcol
6550 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006551 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006553 && (*mb_off2cells)(off_to, max_off_to) == 1
6554 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 if (enc_utf8)
6559 {
6560 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6561 if (ScreenLinesUC[off_from] != 0)
6562 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006563 int i;
6564
6565 for (i = 0; i < Screen_mco; ++i)
6566 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 }
6568 }
6569 if (char_cells == 2)
6570 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
6572#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006573 /* The bold trick makes a single column of pixels appear in the
6574 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 * character should be redrawn too. This happens for our own GUI
6576 * and for some xterms. */
6577 if (
6578# ifdef FEAT_GUI
6579 gui.in_use
6580# endif
6581# if defined(FEAT_GUI) && defined(UNIX)
6582 ||
6583# endif
6584# ifdef UNIX
6585 term_is_xterm
6586# endif
6587 )
6588 {
6589 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006590 if (hl > HL_ALL)
6591 hl = syn_attr2attr(hl);
6592 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 redraw_next = TRUE;
6594 }
6595#endif
6596 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006597
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006598 /* For simplicity set the attributes of second half of a
6599 * double-wide character equal to the first half. */
6600 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006602
6603 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 screen_char(off_to, row, col + coloff);
6607 }
6608 else if ( p_wiv
6609#ifdef FEAT_GUI
6610 && !gui.in_use
6611#endif
6612 && col + coloff > 0)
6613 {
6614 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6615 {
6616 /*
6617 * Don't output stop-highlight when moving the cursor, it will
6618 * stop the highlighting when it should continue.
6619 */
6620 screen_attr = 0;
6621 }
6622 else if (screen_attr != 0)
6623 screen_stop_highlight();
6624 }
6625
6626 off_to += CHAR_CELLS;
6627 off_from += CHAR_CELLS;
6628 col += CHAR_CELLS;
6629 }
6630
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 if (clear_next)
6632 {
6633 /* Clear the second half of a double-wide character of which the left
6634 * half was overwritten with a single-wide character. */
6635 ScreenLines[off_to] = ' ';
6636 if (enc_utf8)
6637 ScreenLinesUC[off_to] = 0;
6638 screen_char(off_to, row, col + coloff);
6639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640
6641 if (clear_width > 0
6642#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006643 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644#endif
6645 )
6646 {
6647#ifdef FEAT_GUI
6648 int startCol = col;
6649#endif
6650
6651 /* blank out the rest of the line */
6652 while (col < clear_width && ScreenLines[off_to] == ' '
6653 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006654 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {
6656 ++off_to;
6657 ++col;
6658 }
6659 if (col < clear_width)
6660 {
6661#ifdef FEAT_GUI
6662 /*
6663 * In the GUI, clearing the rest of the line may leave pixels
6664 * behind if the first character cleared was bold. Some bold
6665 * fonts spill over the left. In this case we redraw the previous
6666 * character too. If we didn't skip any blanks above, then we
6667 * only redraw if the character wasn't already redrawn anyway.
6668 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006669 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 {
6671 hl = ScreenAttrs[off_to];
6672 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006673 {
6674 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006675
Bram Moolenaar9c697322006-10-09 20:11:17 +00006676 if (enc_utf8)
6677 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6678 * that its width is 2. */
6679 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6680 else if (enc_dbcs != 0)
6681 {
6682 /* find previous character by counting from first
6683 * column and get its width. */
6684 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006685 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006686
6687 while (off < off_to)
6688 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006689 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006690 off += prev_cells;
6691 }
6692 }
6693
6694 if (enc_dbcs != 0 && prev_cells > 1)
6695 screen_char_2(off_to - prev_cells, row,
6696 col + coloff - prev_cells);
6697 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006698 screen_char(off_to - prev_cells, row,
6699 col + coloff - prev_cells);
6700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 }
6702#endif
6703 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6704 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 off_to += clear_width - col;
6706 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 }
6708 }
6709
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006710 if (clear_width > 0
6711#ifdef FEAT_TEXT_PROP
6712 && !(flags & SLF_POPUP) // no separator for popup window
6713#endif
6714 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006716 // For a window that has a right neighbor, draw the separator char
6717 // right of the window contents.
6718 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 {
6720 int c;
6721
6722 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006723 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006724 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6725 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 || ScreenAttrs[off_to] != hl)
6727 {
6728 ScreenLines[off_to] = c;
6729 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 if (enc_utf8)
6731 {
6732 if (c >= 0x80)
6733 {
6734 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006735 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 }
6737 else
6738 ScreenLinesUC[off_to] = 0;
6739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 screen_char(off_to, row, col + coloff);
6741 }
6742 }
6743 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 LineWraps[row] = FALSE;
6745 }
6746}
6747
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006748#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006750 * Mirror text "str" for right-left displaying.
6751 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006753 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006754rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755{
6756 char_u *p1, *p2;
6757 int t;
6758
6759 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6760 {
6761 t = *p1;
6762 *p1 = *p2;
6763 *p2 = t;
6764 }
6765}
6766#endif
6767
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768/*
6769 * mark all status lines for redraw; used after first :cd
6770 */
6771 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006772status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773{
6774 win_T *wp;
6775
Bram Moolenaar29323592016-07-24 22:04:11 +02006776 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 if (wp->w_status_height)
6778 {
6779 wp->w_redr_status = TRUE;
6780 redraw_later(VALID);
6781 }
6782}
6783
6784/*
6785 * mark all status lines of the current buffer for redraw
6786 */
6787 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006788status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789{
6790 win_T *wp;
6791
Bram Moolenaar29323592016-07-24 22:04:11 +02006792 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6794 {
6795 wp->w_redr_status = TRUE;
6796 redraw_later(VALID);
6797 }
6798}
6799
6800/*
6801 * Redraw all status lines that need to be redrawn.
6802 */
6803 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006804redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
6806 win_T *wp;
6807
Bram Moolenaar29323592016-07-24 22:04:11 +02006808 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006810 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006811 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006812 draw_tabline();
Bram Moolenaar988c4332019-06-02 14:12:11 +02006813
6814#ifdef FEAT_TEXT_PROP
6815 // Display popup windows on top of the status lines.
6816 update_popups();
6817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819
Bram Moolenaar4033c552017-09-16 20:54:51 +02006820#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821/*
6822 * Redraw all status lines at the bottom of frame "frp".
6823 */
6824 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006825win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826{
6827 if (frp->fr_layout == FR_LEAF)
6828 frp->fr_win->w_redr_status = TRUE;
6829 else if (frp->fr_layout == FR_ROW)
6830 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006831 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 win_redraw_last_status(frp);
6833 }
6834 else /* frp->fr_layout == FR_COL */
6835 {
6836 frp = frp->fr_child;
6837 while (frp->fr_next != NULL)
6838 frp = frp->fr_next;
6839 win_redraw_last_status(frp);
6840 }
6841}
6842#endif
6843
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844/*
6845 * Draw the verticap separator right of window "wp" starting with line "row".
6846 */
6847 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006848draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850 int hl;
6851 int c;
6852
6853 if (wp->w_vsep_width)
6854 {
6855 /* draw the vertical separator right of this window */
6856 c = fillchar_vsep(&hl);
6857 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6858 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6859 c, ' ', hl);
6860 }
6861}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862
6863#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006864static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865
6866/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006867 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 */
6869 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006870status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871{
6872 int len = 0;
6873
6874#ifdef FEAT_MENU
6875 int emenu = (xp->xp_context == EXPAND_MENUS
6876 || xp->xp_context == EXPAND_MENUNAMES);
6877
6878 /* Check for menu separators - replace with '|'. */
6879 if (emenu && menu_is_separator(s))
6880 return 1;
6881#endif
6882
6883 while (*s != NUL)
6884 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006885 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006886 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006887 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 }
6889
6890 return len;
6891}
6892
6893/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006894 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006895 * These are backslashes used for escaping. Do show backslashes in help tags.
6896 */
6897 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006898skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006899{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006900 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006901#ifdef FEAT_MENU
6902 || ((xp->xp_context == EXPAND_MENUS
6903 || xp->xp_context == EXPAND_MENUNAMES)
6904 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6905#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006906 )
6907 {
6908#ifndef BACKSLASH_IN_FILENAME
6909 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6910 return 2;
6911#endif
6912 return 1;
6913 }
6914 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006915}
6916
6917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 * Show wildchar matches in the status line.
6919 * Show at least the "match" item.
6920 * We start at item 'first_match' in the list and show all matches that fit.
6921 *
6922 * If inversion is possible we use it. Else '=' characters are used.
6923 */
6924 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006925win_redr_status_matches(
6926 expand_T *xp,
6927 int num_matches,
6928 char_u **matches, /* list of matches */
6929 int match,
6930 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931{
6932#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6933 int row;
6934 char_u *buf;
6935 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006936 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 int fillchar;
6938 int attr;
6939 int i;
6940 int highlight = TRUE;
6941 char_u *selstart = NULL;
6942 int selstart_col = 0;
6943 char_u *selend = NULL;
6944 static int first_match = 0;
6945 int add_left = FALSE;
6946 char_u *s;
6947#ifdef FEAT_MENU
6948 int emenu;
6949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
6952 if (matches == NULL) /* interrupted completion? */
6953 return;
6954
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006955 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006956 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006957 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006958 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 if (buf == NULL)
6960 return;
6961
6962 if (match == -1) /* don't show match but original text */
6963 {
6964 match = 0;
6965 highlight = FALSE;
6966 }
6967 /* count 1 for the ending ">" */
6968 clen = status_match_len(xp, L_MATCH(match)) + 3;
6969 if (match == 0)
6970 first_match = 0;
6971 else if (match < first_match)
6972 {
6973 /* jumping left, as far as we can go */
6974 first_match = match;
6975 add_left = TRUE;
6976 }
6977 else
6978 {
6979 /* check if match fits on the screen */
6980 for (i = first_match; i < match; ++i)
6981 clen += status_match_len(xp, L_MATCH(i)) + 2;
6982 if (first_match > 0)
6983 clen += 2;
6984 /* jumping right, put match at the left */
6985 if ((long)clen > Columns)
6986 {
6987 first_match = match;
6988 /* if showing the last match, we can add some on the left */
6989 clen = 2;
6990 for (i = match; i < num_matches; ++i)
6991 {
6992 clen += status_match_len(xp, L_MATCH(i)) + 2;
6993 if ((long)clen >= Columns)
6994 break;
6995 }
6996 if (i == num_matches)
6997 add_left = TRUE;
6998 }
6999 }
7000 if (add_left)
7001 while (first_match > 0)
7002 {
7003 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
7004 if ((long)clen >= Columns)
7005 break;
7006 --first_match;
7007 }
7008
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007009 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010
7011 if (first_match == 0)
7012 {
7013 *buf = NUL;
7014 len = 0;
7015 }
7016 else
7017 {
7018 STRCPY(buf, "< ");
7019 len = 2;
7020 }
7021 clen = len;
7022
7023 i = first_match;
7024 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
7025 {
7026 if (i == match)
7027 {
7028 selstart = buf + len;
7029 selstart_col = clen;
7030 }
7031
7032 s = L_MATCH(i);
7033 /* Check for menu separators - replace with '|' */
7034#ifdef FEAT_MENU
7035 emenu = (xp->xp_context == EXPAND_MENUS
7036 || xp->xp_context == EXPAND_MENUNAMES);
7037 if (emenu && menu_is_separator(s))
7038 {
7039 STRCPY(buf + len, transchar('|'));
7040 l = (int)STRLEN(buf + len);
7041 len += l;
7042 clen += l;
7043 }
7044 else
7045#endif
7046 for ( ; *s != NUL; ++s)
7047 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007048 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007050 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {
7052 STRNCPY(buf + len, s, l);
7053 s += l - 1;
7054 len += l;
7055 }
7056 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {
7058 STRCPY(buf + len, transchar_byte(*s));
7059 len += (int)STRLEN(buf + len);
7060 }
7061 }
7062 if (i == match)
7063 selend = buf + len;
7064
7065 *(buf + len++) = ' ';
7066 *(buf + len++) = ' ';
7067 clen += 2;
7068 if (++i == num_matches)
7069 break;
7070 }
7071
7072 if (i != num_matches)
7073 {
7074 *(buf + len++) = '>';
7075 ++clen;
7076 }
7077
7078 buf[len] = NUL;
7079
7080 row = cmdline_row - 1;
7081 if (row >= 0)
7082 {
7083 if (wild_menu_showing == 0)
7084 {
7085 if (msg_scrolled > 0)
7086 {
7087 /* Put the wildmenu just above the command line. If there is
7088 * no room, scroll the screen one line up. */
7089 if (cmdline_row == Rows - 1)
7090 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02007091 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 ++msg_scrolled;
7093 }
7094 else
7095 {
7096 ++cmdline_row;
7097 ++row;
7098 }
7099 wild_menu_showing = WM_SCROLLED;
7100 }
7101 else
7102 {
7103 /* Create status line if needed by setting 'laststatus' to 2.
7104 * Set 'winminheight' to zero to avoid that the window is
7105 * resized. */
7106 if (lastwin->w_status_height == 0)
7107 {
7108 save_p_ls = p_ls;
7109 save_p_wmh = p_wmh;
7110 p_ls = 2;
7111 p_wmh = 0;
7112 last_status(FALSE);
7113 }
7114 wild_menu_showing = WM_SHOWN;
7115 }
7116 }
7117
7118 screen_puts(buf, row, 0, attr);
7119 if (selstart != NULL && highlight)
7120 {
7121 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007122 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 }
7124
7125 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7126 }
7127
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 vim_free(buf);
7130}
7131#endif
7132
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133/*
7134 * Redraw the status line of window wp.
7135 *
7136 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007137 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7138 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007140 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007141win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
7143 int row;
7144 char_u *p;
7145 int len;
7146 int fillchar;
7147 int attr;
7148 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007149 static int busy = FALSE;
7150
7151 /* It's possible to get here recursively when 'statusline' (indirectly)
7152 * invokes ":redrawstatus". Simply ignore the call then. */
7153 if (busy)
7154 return;
7155 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156
7157 wp->w_redr_status = FALSE;
7158 if (wp->w_status_height == 0)
7159 {
7160 /* no status line, can only be last window */
7161 redraw_cmdline = TRUE;
7162 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007163 else if (!redrawing()
7164#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007165 // don't update status line when popup menu is visible and may be
7166 // drawn over it, unless it will be redrawn later
7167 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007168#endif
7169 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {
7171 /* Don't redraw right now, do it later. */
7172 wp->w_redr_status = TRUE;
7173 }
7174#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007175 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {
7177 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007178 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 }
7180#endif
7181 else
7182 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007183 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007185 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 p = NameBuff;
7187 len = (int)STRLEN(p);
7188
Bram Moolenaard85f2712017-07-28 21:51:57 +02007189 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190#ifdef FEAT_QUICKFIX
7191 || wp->w_p_pvw
7192#endif
7193 || bufIsChanged(wp->w_buffer)
7194 || wp->w_buffer->b_p_ro)
7195 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007196 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007198 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 len += (int)STRLEN(p + len);
7200 }
7201#ifdef FEAT_QUICKFIX
7202 if (wp->w_p_pvw)
7203 {
7204 STRCPY(p + len, _("[Preview]"));
7205 len += (int)STRLEN(p + len);
7206 }
7207#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007208 if (bufIsChanged(wp->w_buffer)
7209#ifdef FEAT_TERMINAL
7210 && !bt_terminal(wp->w_buffer)
7211#endif
7212 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
7214 STRCPY(p + len, "[+]");
7215 len += 3;
7216 }
7217 if (wp->w_buffer->b_p_ro)
7218 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007219 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007220 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 }
7222
Bram Moolenaar02631462017-09-22 15:20:32 +02007223 this_ru_col = ru_col - (Columns - wp->w_width);
7224 if (this_ru_col < (wp->w_width + 1) / 2)
7225 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 if (this_ru_col <= 1)
7227 {
7228 p = (char_u *)"<"; /* No room for file name! */
7229 len = 1;
7230 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007231 else if (has_mbyte)
7232 {
7233 int clen = 0, i;
7234
7235 /* Count total number of display cells. */
7236 clen = mb_string2cells(p, -1);
7237
7238 /* Find first character that will fit.
7239 * Going from start to end is much faster for DBCS. */
7240 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7241 i += (*mb_ptr2len)(p + i))
7242 clen -= (*mb_ptr2cells)(p + i);
7243 len = clen;
7244 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007246 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007248 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 }
7250
Bram Moolenaara12a1612019-01-24 16:39:02 +01007251 }
7252 else if (len > this_ru_col - 1)
7253 {
7254 p += len - (this_ru_col - 1);
7255 *p = '<';
7256 len = this_ru_col - 1;
7257 }
7258
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007260 screen_puts(p, row, wp->w_wincol, attr);
7261 screen_fill(row, row + 1, len + wp->w_wincol,
7262 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007264 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7266 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007267 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
7269#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007270 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271#endif
7272 }
7273
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 /*
7275 * May need to draw the character below the vertical separator.
7276 */
7277 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7278 {
7279 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007280 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 else
7282 fillchar = fillchar_vsep(&attr);
7283 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7284 attr);
7285 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007286 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287}
7288
Bram Moolenaar238a5642006-02-21 22:12:05 +00007289#ifdef FEAT_STL_OPT
7290/*
7291 * Redraw the status line according to 'statusline' and take care of any
7292 * errors encountered.
7293 */
7294 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007295redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007296{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007297 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007298 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007299
7300 /* When called recursively return. This can happen when the statusline
7301 * contains an expression that triggers a redraw. */
7302 if (entered)
7303 return;
7304 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007305
Bram Moolenaara742e082016-04-05 21:10:38 +02007306 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007307 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007308 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007309 {
7310 /* When there is an error disable the statusline, otherwise the
7311 * display is messed up with errors and a redraw triggers the problem
7312 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007313 set_string_option_direct((char_u *)"statusline", -1,
7314 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007315 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007316 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007317 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007318 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007319}
7320#endif
7321
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322/*
7323 * Return TRUE if the status line of window "wp" is connected to the status
7324 * line of the window right of it. If not, then it's a vertical separator.
7325 * Only call if (wp->w_vsep_width != 0).
7326 */
7327 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007328stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329{
7330 frame_T *fr;
7331
7332 fr = wp->w_frame;
7333 while (fr->fr_parent != NULL)
7334 {
7335 if (fr->fr_parent->fr_layout == FR_COL)
7336 {
7337 if (fr->fr_next != NULL)
7338 break;
7339 }
7340 else
7341 {
7342 if (fr->fr_next != NULL)
7343 return TRUE;
7344 }
7345 fr = fr->fr_parent;
7346 }
7347 return FALSE;
7348}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351/*
7352 * Get the value to show for the language mappings, active 'keymap'.
7353 */
7354 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007355get_keymap_str(
7356 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007357 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007358 char_u *buf, /* buffer for the result */
7359 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360{
7361 char_u *p;
7362
7363 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7364 return FALSE;
7365
7366 {
7367#ifdef FEAT_EVAL
7368 buf_T *old_curbuf = curbuf;
7369 win_T *old_curwin = curwin;
7370 char_u *s;
7371
7372 curbuf = wp->w_buffer;
7373 curwin = wp;
7374 STRCPY(buf, "b:keymap_name"); /* must be writable */
7375 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007376 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 --emsg_skip;
7378 curbuf = old_curbuf;
7379 curwin = old_curwin;
7380 if (p == NULL || *p == NUL)
7381#endif
7382 {
7383#ifdef FEAT_KEYMAP
7384 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7385 p = wp->w_buffer->b_p_keymap;
7386 else
7387#endif
7388 p = (char_u *)"lang";
7389 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007390 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 buf[0] = NUL;
7392#ifdef FEAT_EVAL
7393 vim_free(s);
7394#endif
7395 }
7396 return buf[0] != NUL;
7397}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398
7399#if defined(FEAT_STL_OPT) || defined(PROTO)
7400/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007401 * Redraw the status line or ruler of window "wp".
7402 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 */
7404 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007405win_redr_custom(
7406 win_T *wp,
7407 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007409 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 int attr;
7411 int curattr;
7412 int row;
7413 int col = 0;
7414 int maxwidth;
7415 int width;
7416 int n;
7417 int len;
7418 int fillchar;
7419 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007420 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007422 struct stl_hlrec hltab[STL_MAX_ITEM];
7423 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007424 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007425 win_T *ewp;
7426 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427
Bram Moolenaar1d633412013-12-11 15:52:01 +01007428 /* There is a tiny chance that this gets called recursively: When
7429 * redrawing a status line triggers redrawing the ruler or tabline.
7430 * Avoid trouble by not allowing recursion. */
7431 if (entered)
7432 return;
7433 entered = TRUE;
7434
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007436 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007438 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007439 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007440 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007441 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007442 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007443 maxwidth = Columns;
7444# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007445 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007448 else
7449 {
7450 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007451 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007452 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007453
7454 if (draw_ruler)
7455 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007456 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007457 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007458 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007459 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007460 if (*++stl == '-')
7461 stl++;
7462 if (atoi((char *)stl))
7463 while (VIM_ISDIGIT(*stl))
7464 stl++;
7465 if (*stl++ != '(')
7466 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007467 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007468 col = ru_col - (Columns - wp->w_width);
7469 if (col < (wp->w_width + 1) / 2)
7470 col = (wp->w_width + 1) / 2;
7471 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007472 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007473 {
7474 row = Rows - 1;
7475 --maxwidth; /* writing in last column may cause scrolling */
7476 fillchar = ' ';
7477 attr = 0;
7478 }
7479
7480# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007481 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007482# endif
7483 }
7484 else
7485 {
7486 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007487 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007488 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007489 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007490# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007491 use_sandbox = was_set_insecurely((char_u *)"statusline",
7492 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007493# endif
7494 }
7495
Bram Moolenaar53f81742017-09-22 14:35:51 +02007496 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007497 }
7498
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007500 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501
Bram Moolenaar61452852011-02-01 18:01:11 +01007502 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7503 * the cursor away and back. */
7504 ewp = wp == NULL ? curwin : wp;
7505 p_crb_save = ewp->w_p_crb;
7506 ewp->w_p_crb = FALSE;
7507
Bram Moolenaar362f3562009-11-03 16:20:34 +00007508 /* Make a copy, because the statusline may include a function call that
7509 * might change the option value and free the memory. */
7510 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007511 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007512 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007513 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007514 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007515 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007517 /* Make all characters printable. */
7518 p = transstr(buf);
7519 if (p != NULL)
7520 {
7521 vim_strncpy(buf, p, sizeof(buf) - 1);
7522 vim_free(p);
7523 }
7524
7525 /* fill up with "fillchar" */
7526 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007527 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 ++width;
7531 }
7532 buf[len] = NUL;
7533
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007534 /*
7535 * Draw each snippet with the specified highlighting.
7536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 curattr = attr;
7538 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007539 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007541 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 screen_puts_len(p, len, row, col, curattr);
7543 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007544 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007546 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007548 else if (hltab[n].userhl < 0)
7549 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007550#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007551 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7552 && wp->w_status_height != 0)
7553 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007554 else if (wp != NULL && bt_terminal(wp->w_buffer)
7555 && wp->w_status_height != 0)
7556 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007557#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007558 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007559 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007561 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 }
7563 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007564
7565 if (wp == NULL)
7566 {
7567 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7568 col = 0;
7569 len = 0;
7570 p = buf;
7571 fillchar = 0;
7572 for (n = 0; tabtab[n].start != NULL; n++)
7573 {
7574 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7575 while (col < len)
7576 TabPageIdxs[col++] = fillchar;
7577 p = tabtab[n].start;
7578 fillchar = tabtab[n].userhl;
7579 }
7580 while (col < Columns)
7581 TabPageIdxs[col++] = fillchar;
7582 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007583
7584theend:
7585 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586}
7587
7588#endif /* FEAT_STL_OPT */
7589
7590/*
7591 * Output a single character directly to the screen and update ScreenLines.
7592 */
7593 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007594screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 char_u buf[MB_MAXBYTES + 1];
7597
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007598 if (has_mbyte)
7599 buf[(*mb_char2bytes)(c, buf)] = NUL;
7600 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007601 {
7602 buf[0] = c;
7603 buf[1] = NUL;
7604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 screen_puts(buf, row, col, attr);
7606}
7607
7608/*
7609 * Get a single character directly from ScreenLines into "bytes[]".
7610 * Also return its attribute in *attrp;
7611 */
7612 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007613screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614{
7615 unsigned off;
7616
7617 /* safety check */
7618 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7619 {
7620 off = LineOffset[row] + col;
7621 *attrp = ScreenAttrs[off];
7622 bytes[0] = ScreenLines[off];
7623 bytes[1] = NUL;
7624
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 if (enc_utf8 && ScreenLinesUC[off] != 0)
7626 bytes[utfc_char2bytes(off, bytes)] = NUL;
7627 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7628 {
7629 bytes[0] = ScreenLines[off];
7630 bytes[1] = ScreenLines2[off];
7631 bytes[2] = NUL;
7632 }
7633 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7634 {
7635 bytes[1] = ScreenLines[off + 1];
7636 bytes[2] = NUL;
7637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 }
7639}
7640
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007641/*
7642 * Return TRUE if composing characters for screen posn "off" differs from
7643 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007644 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007645 */
7646 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007647screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007648{
7649 int i;
7650
7651 for (i = 0; i < Screen_mco; ++i)
7652 {
7653 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7654 return TRUE;
7655 if (u8cc[i] == 0)
7656 break;
7657 }
7658 return FALSE;
7659}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007660
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661/*
7662 * Put string '*text' on the screen at position 'row' and 'col', with
7663 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7664 * Note: only outputs within one row, message is truncated at screen boundary!
7665 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7666 */
7667 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007668screen_puts(
7669 char_u *text,
7670 int row,
7671 int col,
7672 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673{
7674 screen_puts_len(text, -1, row, col, attr);
7675}
7676
7677/*
7678 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7679 * a NUL.
7680 */
7681 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007682screen_puts_len(
7683 char_u *text,
7684 int textlen,
7685 int row,
7686 int col,
7687 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688{
7689 unsigned off;
7690 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007691 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007693 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 int mbyte_blen = 1;
7695 int mbyte_cells = 1;
7696 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007697 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007699#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007701 int pc, nc, nc1;
7702 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007704 int force_redraw_this;
7705 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007706 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707
7708 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7709 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007710 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711
Bram Moolenaarc236c162008-07-13 17:41:49 +00007712 /* When drawing over the right halve of a double-wide char clear out the
7713 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007714 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007715#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007716 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007717#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007718 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007719 {
7720 ScreenLines[off - 1] = ' ';
7721 ScreenAttrs[off - 1] = 0;
7722 if (enc_utf8)
7723 {
7724 ScreenLinesUC[off - 1] = 0;
7725 ScreenLinesC[0][off - 1] = 0;
7726 }
7727 /* redraw the previous cell, make it empty */
7728 screen_char(off - 1, row, col - 1);
7729 /* force the cell at "col" to be redrawn */
7730 force_redraw_next = TRUE;
7731 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007732
Bram Moolenaar367329b2007-08-30 11:53:22 +00007733 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007734 while (col < screen_Columns
7735 && (len < 0 || (int)(ptr - text) < len)
7736 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {
7738 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 /* check if this is the first byte of a multibyte */
7740 if (has_mbyte)
7741 {
7742 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007743 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007745 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7747 mbyte_cells = 1;
7748 else if (enc_dbcs != 0)
7749 mbyte_cells = mbyte_blen;
7750 else /* enc_utf8 */
7751 {
7752 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007753 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 (int)((text + len) - ptr));
7755 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007756 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007758#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7760 {
7761 /* Do Arabic shaping. */
7762 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7763 {
7764 /* Past end of string to be displayed. */
7765 nc = NUL;
7766 nc1 = NUL;
7767 }
7768 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007769 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007770 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7771 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007772 nc1 = pcc[0];
7773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 pc = prev_c;
7775 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007776 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 }
7778 else
7779 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007780#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007781 if (col + mbyte_cells > screen_Columns)
7782 {
7783 /* Only 1 cell left, but character requires 2 cells:
7784 * display a '>' in the last column to avoid wrapping. */
7785 c = '>';
7786 mbyte_cells = 1;
7787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 }
7789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007791 force_redraw_this = force_redraw_next;
7792 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007793
7794 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 || (mbyte_cells == 2
7796 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7797 || (enc_dbcs == DBCS_JPNU
7798 && c == 0x8e
7799 && ScreenLines2[off] != ptr[1])
7800 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007801 && (ScreenLinesUC[off] !=
7802 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7803 || (ScreenLinesUC[off] != 0
7804 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007806 || exmode_active;
7807
Bram Moolenaara12a1612019-01-24 16:39:02 +01007808 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 {
7810#if defined(FEAT_GUI) || defined(UNIX)
7811 /* The bold trick makes a single row of pixels appear in the next
7812 * character. When a bold character is removed, the next
7813 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007814 * and for some xterms. */
7815 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816# ifdef FEAT_GUI
7817 gui.in_use
7818# endif
7819# if defined(FEAT_GUI) && defined(UNIX)
7820 ||
7821# endif
7822# ifdef UNIX
7823 term_is_xterm
7824# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007825 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007827 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007829 if (n > HL_ALL)
7830 n = syn_attr2attr(n);
7831 if (n & HL_BOLD)
7832 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 }
7834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 /* When at the end of the text and overwriting a two-cell
7836 * character with a one-cell character, need to clear the next
7837 * cell. Also when overwriting the left halve of a two-cell char
7838 * with the right halve of a two-cell char. Do this only once
7839 * (mb_off2cells() may return 2 on the right halve). */
7840 if (clear_next_cell)
7841 clear_next_cell = FALSE;
7842 else if (has_mbyte
7843 && (len < 0 ? ptr[mbyte_blen] == NUL
7844 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007845 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007847 && (*mb_off2cells)(off, max_off) == 1
7848 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 clear_next_cell = TRUE;
7850
7851 /* Make sure we never leave a second byte of a double-byte behind,
7852 * it confuses mb_off2cells(). */
7853 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007854 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007856 && (*mb_off2cells)(off, max_off) == 1
7857 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 ScreenLines[off] = c;
7860 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 if (enc_utf8)
7862 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007863 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 ScreenLinesUC[off] = 0;
7865 else
7866 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007867 int i;
7868
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007870 for (i = 0; i < Screen_mco; ++i)
7871 {
7872 ScreenLinesC[i][off] = u8cc[i];
7873 if (u8cc[i] == 0)
7874 break;
7875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 }
7877 if (mbyte_cells == 2)
7878 {
7879 ScreenLines[off + 1] = 0;
7880 ScreenAttrs[off + 1] = attr;
7881 }
7882 screen_char(off, row, col);
7883 }
7884 else if (mbyte_cells == 2)
7885 {
7886 ScreenLines[off + 1] = ptr[1];
7887 ScreenAttrs[off + 1] = attr;
7888 screen_char_2(off, row, col);
7889 }
7890 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7891 {
7892 ScreenLines2[off] = ptr[1];
7893 screen_char(off, row, col);
7894 }
7895 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 screen_char(off, row, col);
7897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 if (has_mbyte)
7899 {
7900 off += mbyte_cells;
7901 col += mbyte_cells;
7902 ptr += mbyte_blen;
7903 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007904 {
7905 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007907 len = -1;
7908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 }
7910 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {
7912 ++off;
7913 ++col;
7914 ++ptr;
7915 }
7916 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007917
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007918 /* If we detected the next character needs to be redrawn, but the text
7919 * doesn't extend up to there, update the character here. */
7920 if (force_redraw_next && col < screen_Columns)
7921 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007922 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7923 screen_char_2(off, row, col);
7924 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007925 screen_char(off, row, col);
7926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927}
7928
7929#ifdef FEAT_SEARCH_EXTRA
7930/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007931 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 */
7933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007934start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
7936 if (p_hls && !no_hlsearch)
7937 {
7938 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007939 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007940# ifdef FEAT_RELTIME
7941 /* Set the time limit to 'redrawtime'. */
7942 profile_setlimit(p_rdt, &search_hl.tm);
7943# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 }
7945}
7946
7947/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007948 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 */
7950 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007951end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952{
7953 if (search_hl.rm.regprog != NULL)
7954 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007955 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 search_hl.rm.regprog = NULL;
7957 }
7958}
7959
7960/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007961 * Init for calling prepare_search_hl().
7962 */
7963 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007964init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007965{
7966 matchitem_T *cur;
7967
7968 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7969 * match */
7970 cur = wp->w_match_head;
7971 while (cur != NULL)
7972 {
7973 cur->hl.rm = cur->match;
7974 if (cur->hlg_id == 0)
7975 cur->hl.attr = 0;
7976 else
7977 cur->hl.attr = syn_id2attr(cur->hlg_id);
7978 cur->hl.buf = wp->w_buffer;
7979 cur->hl.lnum = 0;
7980 cur->hl.first_lnum = 0;
7981# ifdef FEAT_RELTIME
7982 /* Set the time limit to 'redrawtime'. */
7983 profile_setlimit(p_rdt, &(cur->hl.tm));
7984# endif
7985 cur = cur->next;
7986 }
7987 search_hl.buf = wp->w_buffer;
7988 search_hl.lnum = 0;
7989 search_hl.first_lnum = 0;
7990 /* time limit is set at the toplevel, for all windows */
7991}
7992
7993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 * Advance to the match in window "wp" line "lnum" or past it.
7995 */
7996 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007997prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007999 matchitem_T *cur; /* points to the match list */
8000 match_T *shl; /* points to search_hl or a match */
8001 int shl_flag; /* flag to indicate whether search_hl
8002 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008003 int pos_inprogress; /* marks that position match search is
8004 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 int n;
8006
8007 /*
8008 * When using a multi-line pattern, start searching at the top
8009 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008010 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008012 cur = wp->w_match_head;
8013 shl_flag = FALSE;
8014 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008016 if (shl_flag == FALSE)
8017 {
8018 shl = &search_hl;
8019 shl_flag = TRUE;
8020 }
8021 else
8022 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 if (shl->rm.regprog != NULL
8024 && shl->lnum == 0
8025 && re_multiline(shl->rm.regprog))
8026 {
8027 if (shl->first_lnum == 0)
8028 {
8029# ifdef FEAT_FOLDING
8030 for (shl->first_lnum = lnum;
8031 shl->first_lnum > wp->w_topline; --shl->first_lnum)
8032 if (hasFoldingWin(wp, shl->first_lnum - 1,
8033 NULL, NULL, TRUE, NULL))
8034 break;
8035# else
8036 shl->first_lnum = wp->w_topline;
8037# endif
8038 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008039 if (cur != NULL)
8040 cur->pos.cur = 0;
8041 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008043 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
8044 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02008046 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
8047 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008048 pos_inprogress = cur == NULL || cur->pos.cur == 0
8049 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 if (shl->lnum != 0)
8051 {
8052 shl->first_lnum = shl->lnum
8053 + shl->rm.endpos[0].lnum
8054 - shl->rm.startpos[0].lnum;
8055 n = shl->rm.endpos[0].col;
8056 }
8057 else
8058 {
8059 ++shl->first_lnum;
8060 n = 0;
8061 }
8062 }
8063 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008064 if (shl != &search_hl && cur != NULL)
8065 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 }
8067}
8068
8069/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008070 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 * Uses shl->buf.
8072 * Sets shl->lnum and shl->rm contents.
8073 * Note: Assumes a previous match is always before "lnum", unless
8074 * shl->lnum is zero.
8075 * Careful: Any pointers for buffer lines will become invalid.
8076 */
8077 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008078next_search_hl(
8079 win_T *win,
8080 match_T *shl, /* points to search_hl or a match */
8081 linenr_T lnum,
8082 colnr_T mincol, /* minimal column for a match */
8083 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 linenr_T l;
8086 colnr_T matchcol;
8087 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008088 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008090 // for :{range}s/pat only highlight inside the range
8091 if (lnum < search_first_line || lnum > search_last_line)
8092 {
8093 shl->lnum = 0;
8094 return;
8095 }
8096
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 if (shl->lnum != 0)
8098 {
8099 /* Check for three situations:
8100 * 1. If the "lnum" is below a previous match, start a new search.
8101 * 2. If the previous match includes "mincol", use it.
8102 * 3. Continue after the previous match.
8103 */
8104 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8105 if (lnum > l)
8106 shl->lnum = 0;
8107 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8108 return;
8109 }
8110
8111 /*
8112 * Repeat searching for a match until one is found that includes "mincol"
8113 * or none is found in this line.
8114 */
8115 called_emsg = FALSE;
8116 for (;;)
8117 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008118#ifdef FEAT_RELTIME
8119 /* Stop searching after passing the time limit. */
8120 if (profile_passed_limit(&(shl->tm)))
8121 {
8122 shl->lnum = 0; /* no match found in time */
8123 break;
8124 }
8125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 /* Three situations:
8127 * 1. No useful previous match: search from start of line.
8128 * 2. Not Vi compatible or empty match: continue at next character.
8129 * Break the loop if this is beyond the end of the line.
8130 * 3. Vi compatible searching: continue at end of previous match.
8131 */
8132 if (shl->lnum == 0)
8133 matchcol = 0;
8134 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8135 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008136 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008138 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008139
8140 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008141 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008142 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008144 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 shl->lnum = 0;
8146 break;
8147 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008148 if (has_mbyte)
8149 matchcol += mb_ptr2len(ml);
8150 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008151 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 }
8153 else
8154 matchcol = shl->rm.endpos[0].col;
8155
8156 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008157 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008159 /* Remember whether shl->rm is using a copy of the regprog in
8160 * cur->match. */
8161 int regprog_is_copy = (shl != &search_hl && cur != NULL
8162 && shl == &cur->hl
8163 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008164 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008165
Bram Moolenaarb3414592014-06-17 17:48:32 +02008166 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8167 matchcol,
8168#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008169 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008170#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008171 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008172#endif
8173 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008174 /* Copy the regprog, in case it got freed and recompiled. */
8175 if (regprog_is_copy)
8176 cur->match.regprog = cur->hl.rm.regprog;
8177
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008178 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008179 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008180 /* Error while handling regexp: stop using this regexp. */
8181 if (shl == &search_hl)
8182 {
8183 /* don't free regprog in the match list, it's a copy */
8184 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008185 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008186 }
8187 shl->rm.regprog = NULL;
8188 shl->lnum = 0;
8189 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8190 message */
8191 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008192 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008193 }
8194 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008195 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008196 else
8197 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 if (nmatched == 0)
8199 {
8200 shl->lnum = 0; /* no match found */
8201 break;
8202 }
8203 if (shl->rm.startpos[0].lnum > 0
8204 || shl->rm.startpos[0].col >= mincol
8205 || nmatched > 1
8206 || shl->rm.endpos[0].col > mincol)
8207 {
8208 shl->lnum += shl->rm.startpos[0].lnum;
8209 break; /* useful match found */
8210 }
8211 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008212
8213 // Restore called_emsg for assert_fails().
8214 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216
Bram Moolenaar85077472016-10-16 14:35:48 +02008217/*
8218 * If there is a match fill "shl" and return one.
8219 * Return zero otherwise.
8220 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008221 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008222next_search_hl_pos(
8223 match_T *shl, /* points to a match */
8224 linenr_T lnum,
8225 posmatch_T *posmatch, /* match positions */
8226 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008227{
8228 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008229 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008230
Bram Moolenaarb3414592014-06-17 17:48:32 +02008231 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8232 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008233 llpos_T *pos = &posmatch->pos[i];
8234
8235 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008236 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008237 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008238 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008239 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008240 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008241 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008242 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008243 /* if this match comes before the one at "found" then swap
8244 * them */
8245 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008246 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008247 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008248
Bram Moolenaar85077472016-10-16 14:35:48 +02008249 *pos = posmatch->pos[found];
8250 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008251 }
8252 }
8253 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008254 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008255 }
8256 }
8257 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008258 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008259 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008260 colnr_T start = posmatch->pos[found].col == 0
8261 ? 0 : posmatch->pos[found].col - 1;
8262 colnr_T end = posmatch->pos[found].col == 0
8263 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008264
Bram Moolenaar85077472016-10-16 14:35:48 +02008265 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008266 shl->rm.startpos[0].lnum = 0;
8267 shl->rm.startpos[0].col = start;
8268 shl->rm.endpos[0].lnum = 0;
8269 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008270 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008271 posmatch->cur = found + 1;
8272 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008273 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008274 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008275}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008276#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008277
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008279screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280{
8281 attrentry_T *aep = NULL;
8282
8283 screen_attr = attr;
8284 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008285#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 && termcap_active
8287#endif
8288 )
8289 {
8290#ifdef FEAT_GUI
8291 if (gui.in_use)
8292 {
8293 char buf[20];
8294
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008295 /* The GUI handles this internally. */
8296 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 OUT_STR(buf);
8298 }
8299 else
8300#endif
8301 {
8302 if (attr > HL_ALL) /* special HL attr. */
8303 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008304 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 aep = syn_cterm_attr2entry(attr);
8306 else
8307 aep = syn_term_attr2entry(attr);
8308 if (aep == NULL) /* did ":syntax clear" */
8309 attr = 0;
8310 else
8311 attr = aep->ae_attr;
8312 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008313 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008315 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008316#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008317 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8318 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8319 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008320#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008321 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008322 /* If the Normal FG color has BOLD attribute and the new HL
8323 * has a FG color defined, clear BOLD. */
8324 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008325 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008327 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008328 out_str(T_UCS);
8329 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008330 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8331 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008333 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008335 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008337 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008338 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339
8340 /*
8341 * Output the color or start string after bold etc., in case the
8342 * bold etc. override the color setting.
8343 */
8344 if (aep != NULL)
8345 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008346#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008347 /* When 'termguicolors' is set but fg or bg is unset,
8348 * fall back to the cterm colors. This helps for SpellBad,
8349 * where the GUI uses a red undercurl. */
8350 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008352 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008353 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008354 }
8355 else
8356#endif
8357 if (t_colors > 1)
8358 {
8359 if (aep->ae_u.cterm.fg_color)
8360 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8361 }
8362#ifdef FEAT_TERMGUICOLORS
8363 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8364 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008365 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008366 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 }
8368 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008369#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008370 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008372 if (aep->ae_u.cterm.bg_color)
8373 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8374 }
8375
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008376 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008377 {
8378 if (aep->ae_u.term.start != NULL)
8379 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 }
8381 }
8382 }
8383 }
8384}
8385
8386 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008387screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388{
8389 int do_ME = FALSE; /* output T_ME code */
8390
8391 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008392#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 && termcap_active
8394#endif
8395 )
8396 {
8397#ifdef FEAT_GUI
8398 if (gui.in_use)
8399 {
8400 char buf[20];
8401
8402 /* use internal GUI code */
8403 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8404 OUT_STR(buf);
8405 }
8406 else
8407#endif
8408 {
8409 if (screen_attr > HL_ALL) /* special HL attr. */
8410 {
8411 attrentry_T *aep;
8412
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008413 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
8415 /*
8416 * Assume that t_me restores the original colors!
8417 */
8418 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008419 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008420#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008421 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8422 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8423 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008424#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008425 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008426#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008427 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8428 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8429 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008430#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008431 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 do_ME = TRUE;
8433 }
8434 else
8435 {
8436 aep = syn_term_attr2entry(screen_attr);
8437 if (aep != NULL && aep->ae_u.term.stop != NULL)
8438 {
8439 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8440 do_ME = TRUE;
8441 else
8442 out_str(aep->ae_u.term.stop);
8443 }
8444 }
8445 if (aep == NULL) /* did ":syntax clear" */
8446 screen_attr = 0;
8447 else
8448 screen_attr = aep->ae_attr;
8449 }
8450
8451 /*
8452 * Often all ending-codes are equal to T_ME. Avoid outputting the
8453 * same sequence several times.
8454 */
8455 if (screen_attr & HL_STANDOUT)
8456 {
8457 if (STRCMP(T_SE, T_ME) == 0)
8458 do_ME = TRUE;
8459 else
8460 out_str(T_SE);
8461 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008462 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008463 {
8464 if (STRCMP(T_UCE, T_ME) == 0)
8465 do_ME = TRUE;
8466 else
8467 out_str(T_UCE);
8468 }
8469 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008470 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 {
8472 if (STRCMP(T_UE, T_ME) == 0)
8473 do_ME = TRUE;
8474 else
8475 out_str(T_UE);
8476 }
8477 if (screen_attr & HL_ITALIC)
8478 {
8479 if (STRCMP(T_CZR, T_ME) == 0)
8480 do_ME = TRUE;
8481 else
8482 out_str(T_CZR);
8483 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008484 if (screen_attr & HL_STRIKETHROUGH)
8485 {
8486 if (STRCMP(T_STE, T_ME) == 0)
8487 do_ME = TRUE;
8488 else
8489 out_str(T_STE);
8490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8492 out_str(T_ME);
8493
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008494#ifdef FEAT_TERMGUICOLORS
8495 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008497 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008498 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008499 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008500 term_bg_rgb_color(cterm_normal_bg_gui_color);
8501 }
8502 else
8503#endif
8504 {
8505 if (t_colors > 1)
8506 {
8507 /* set Normal cterm colors */
8508 if (cterm_normal_fg_color != 0)
8509 term_fg_color(cterm_normal_fg_color - 1);
8510 if (cterm_normal_bg_color != 0)
8511 term_bg_color(cterm_normal_bg_color - 1);
8512 if (cterm_normal_fg_bold)
8513 out_str(T_MD);
8514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 }
8516 }
8517 }
8518 screen_attr = 0;
8519}
8520
8521/*
8522 * Reset the colors for a cterm. Used when leaving Vim.
8523 * The machine specific code may override this again.
8524 */
8525 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008526reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008528 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 {
8530 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008531#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008532 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8533 || cterm_normal_bg_gui_color != INVALCOLOR)
8534 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008535#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 {
8539 out_str(T_OP);
8540 screen_attr = -1;
8541 }
8542 if (cterm_normal_fg_bold)
8543 {
8544 out_str(T_ME);
8545 screen_attr = -1;
8546 }
8547 }
8548}
8549
8550/*
8551 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8552 * using the attributes from ScreenAttrs["off"].
8553 */
8554 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008555screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556{
8557 int attr;
8558
8559 /* Check for illegal values, just in case (could happen just after
8560 * resizing). */
8561 if (row >= screen_Rows || col >= screen_Columns)
8562 return;
8563
Bram Moolenaarae654382019-01-17 21:09:05 +01008564#ifdef FEAT_INS_EXPAND
8565 if (pum_under_menu(row, col))
8566 return;
8567#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008568 /* Outputting a character in the last cell on the screen may scroll the
8569 * screen up. Only do it when the "xn" termcap property is set, otherwise
8570 * mark the character invalid (update it when scrolled up). */
8571 if (*T_XN == NUL
8572 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573#ifdef FEAT_RIGHTLEFT
8574 /* account for first command-line character in rightleft mode */
8575 && !cmdmsg_rl
8576#endif
8577 )
8578 {
8579 ScreenAttrs[off] = (sattr_T)-1;
8580 return;
8581 }
8582
8583 /*
8584 * Stop highlighting first, so it's easier to move the cursor.
8585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 if (screen_char_attr != 0)
8587 attr = screen_char_attr;
8588 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 attr = ScreenAttrs[off];
8590 if (screen_attr != attr)
8591 screen_stop_highlight();
8592
8593 windgoto(row, col);
8594
8595 if (screen_attr != attr)
8596 screen_start_highlight(attr);
8597
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 if (enc_utf8 && ScreenLinesUC[off] != 0)
8599 {
8600 char_u buf[MB_MAXBYTES + 1];
8601
Bram Moolenaarcb070082016-04-02 22:14:51 +02008602 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008603 {
8604 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008605#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008606 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008607#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008608 )
8609 {
8610 /* Clear the two screen cells. If the character is actually
8611 * single width it won't change the second cell. */
8612 out_str((char_u *)" ");
8613 term_windgoto(row, col);
8614 }
8615 /* not sure where the cursor is after drawing the ambiguous width
8616 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008617 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008618 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008619 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008621
8622 /* Convert the UTF-8 character to bytes and write it. */
8623 buf[utfc_char2bytes(off, buf)] = NUL;
8624 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 }
8626 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 /* double-byte character in single-width cell */
8631 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8632 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 }
8634
8635 screen_cur_col++;
8636}
8637
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638/*
8639 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8640 * on the screen at position 'row' and 'col'.
8641 * The attributes of the first byte is used for all. This is required to
8642 * output the two bytes of a double-byte character with nothing in between.
8643 */
8644 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008645screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646{
8647 /* Check for illegal values (could be wrong when screen was resized). */
8648 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8649 return;
8650
8651 /* Outputting the last character on the screen may scrollup the screen.
8652 * Don't to it! Mark the character invalid (update it when scrolled up) */
8653 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8654 {
8655 ScreenAttrs[off] = (sattr_T)-1;
8656 return;
8657 }
8658
8659 /* Output the first byte normally (positions the cursor), then write the
8660 * second byte directly. */
8661 screen_char(off, row, col);
8662 out_char(ScreenLines[off + 1]);
8663 ++screen_cur_col;
8664}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666/*
8667 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8668 * This uses the contents of ScreenLines[] and doesn't change it.
8669 */
8670 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008671screen_draw_rectangle(
8672 int row,
8673 int col,
8674 int height,
8675 int width,
8676 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677{
8678 int r, c;
8679 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008680 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008682 /* Can't use ScreenLines unless initialized */
8683 if (ScreenLines == NULL)
8684 return;
8685
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 if (invert)
8687 screen_char_attr = HL_INVERSE;
8688 for (r = row; r < row + height; ++r)
8689 {
8690 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008691 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 for (c = col; c < col + width; ++c)
8693 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008694 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 {
8696 screen_char_2(off + c, r, c);
8697 ++c;
8698 }
8699 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 {
8701 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008702 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 }
8705 }
8706 }
8707 screen_char_attr = 0;
8708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710/*
8711 * Redraw the characters for a vertically split window.
8712 */
8713 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008714redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
8716 int col;
8717 int width;
8718
8719# ifdef FEAT_CLIPBOARD
8720 clip_may_clear_selection(row, end - 1);
8721# endif
8722
8723 if (wp == NULL)
8724 {
8725 col = 0;
8726 width = Columns;
8727 }
8728 else
8729 {
8730 col = wp->w_wincol;
8731 width = wp->w_width;
8732 }
8733 screen_draw_rectangle(row, col, end - row, width, FALSE);
8734}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008736 static void
8737space_to_screenline(int off, int attr)
8738{
8739 ScreenLines[off] = ' ';
8740 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008741 if (enc_utf8)
8742 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008743}
8744
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745/*
8746 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8747 * with character 'c1' in first column followed by 'c2' in the other columns.
8748 * Use attributes 'attr'.
8749 */
8750 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008751screen_fill(
8752 int start_row,
8753 int end_row,
8754 int start_col,
8755 int end_col,
8756 int c1,
8757 int c2,
8758 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759{
8760 int row;
8761 int col;
8762 int off;
8763 int end_off;
8764 int did_delete;
8765 int c;
8766 int norm_term;
8767#if defined(FEAT_GUI) || defined(UNIX)
8768 int force_next = FALSE;
8769#endif
8770
8771 if (end_row > screen_Rows) /* safety check */
8772 end_row = screen_Rows;
8773 if (end_col > screen_Columns) /* safety check */
8774 end_col = screen_Columns;
8775 if (ScreenLines == NULL
8776 || start_row >= end_row
8777 || start_col >= end_col) /* nothing to do */
8778 return;
8779
8780 /* it's a "normal" terminal when not in a GUI or cterm */
8781 norm_term = (
8782#ifdef FEAT_GUI
8783 !gui.in_use &&
8784#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008785 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 for (row = start_row; row < end_row; ++row)
8787 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008788 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008789#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008790 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008791#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008792 )
8793 {
8794 /* When drawing over the right halve of a double-wide char clear
8795 * out the left halve. When drawing over the left halve of a
8796 * double wide-char clear out the right halve. Only needed in a
8797 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008798 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008799 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008800 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008801 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 /*
8804 * Try to use delete-line termcap code, when no attributes or in a
8805 * "normal" terminal, where a bold/italic space is just a
8806 * space.
8807 */
8808 did_delete = FALSE;
8809 if (c2 == ' '
8810 && end_col == Columns
8811 && can_clear(T_CE)
8812 && (attr == 0
8813 || (norm_term
8814 && attr <= HL_ALL
8815 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8816 {
8817 /*
8818 * check if we really need to clear something
8819 */
8820 col = start_col;
8821 if (c1 != ' ') /* don't clear first char */
8822 ++col;
8823
8824 off = LineOffset[row] + col;
8825 end_off = LineOffset[row] + end_col;
8826
8827 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 if (enc_utf8)
8829 while (off < end_off && ScreenLines[off] == ' '
8830 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8831 ++off;
8832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 while (off < end_off && ScreenLines[off] == ' '
8834 && ScreenAttrs[off] == 0)
8835 ++off;
8836 if (off < end_off) /* something to be cleared */
8837 {
8838 col = off - LineOffset[row];
8839 screen_stop_highlight();
8840 term_windgoto(row, col);/* clear rest of this screen line */
8841 out_str(T_CE);
8842 screen_start(); /* don't know where cursor is now */
8843 col = end_col - col;
8844 while (col--) /* clear chars in ScreenLines */
8845 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008846 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 ++off;
8848 }
8849 }
8850 did_delete = TRUE; /* the chars are cleared now */
8851 }
8852
8853 off = LineOffset[row] + start_col;
8854 c = c1;
8855 for (col = start_col; col < end_col; ++col)
8856 {
8857 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008858 || (enc_utf8 && (int)ScreenLinesUC[off]
8859 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 || ScreenAttrs[off] != attr
8861#if defined(FEAT_GUI) || defined(UNIX)
8862 || force_next
8863#endif
8864 )
8865 {
8866#if defined(FEAT_GUI) || defined(UNIX)
8867 /* The bold trick may make a single row of pixels appear in
8868 * the next character. When a bold character is removed, the
8869 * next character should be redrawn too. This happens for our
8870 * own GUI and for some xterms. */
8871 if (
8872# ifdef FEAT_GUI
8873 gui.in_use
8874# endif
8875# if defined(FEAT_GUI) && defined(UNIX)
8876 ||
8877# endif
8878# ifdef UNIX
8879 term_is_xterm
8880# endif
8881 )
8882 {
8883 if (ScreenLines[off] != ' '
8884 && (ScreenAttrs[off] > HL_ALL
8885 || ScreenAttrs[off] & HL_BOLD))
8886 force_next = TRUE;
8887 else
8888 force_next = FALSE;
8889 }
8890#endif
8891 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 if (enc_utf8)
8893 {
8894 if (c >= 0x80)
8895 {
8896 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008897 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 }
8899 else
8900 ScreenLinesUC[off] = 0;
8901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 ScreenAttrs[off] = attr;
8903 if (!did_delete || c != ' ')
8904 screen_char(off, row, col);
8905 }
8906 ++off;
8907 if (col == start_col)
8908 {
8909 if (did_delete)
8910 break;
8911 c = c2;
8912 }
8913 }
8914 if (end_col == Columns)
8915 LineWraps[row] = FALSE;
8916 if (row == Rows - 1) /* overwritten the command line */
8917 {
8918 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008919 if (start_col == 0 && end_col == Columns
8920 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008922 if (start_col == 0)
8923 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 }
8925 }
8926}
8927
8928/*
8929 * Check if there should be a delay. Used before clearing or redrawing the
8930 * screen or the command line.
8931 */
8932 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008933check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
8935 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8936 && !did_wait_return
8937 && emsg_silent == 0)
8938 {
8939 out_flush();
8940 ui_delay(1000L, TRUE);
8941 emsg_on_display = FALSE;
8942 if (check_msg_scroll)
8943 msg_scroll = FALSE;
8944 }
8945}
8946
8947/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008948 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8949 */
8950 static void
8951clear_TabPageIdxs(void)
8952{
8953 int scol;
8954
8955 for (scol = 0; scol < Columns; ++scol)
8956 TabPageIdxs[scol] = 0;
8957}
8958
8959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008961 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 * Returns TRUE if there is a valid screen to write to.
8963 * Returns FALSE when starting up and screen not initialized yet.
8964 */
8965 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008966screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008968 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 return (ScreenLines != NULL);
8970}
8971
8972/*
8973 * Resize the shell to Rows and Columns.
8974 * Allocate ScreenLines[] and associated items.
8975 *
8976 * There may be some time between setting Rows and Columns and (re)allocating
8977 * ScreenLines[]. This happens when starting up and when (manually) changing
8978 * the shell size. Always use screen_Rows and screen_Columns to access items
8979 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8980 * final size of the shell is needed.
8981 */
8982 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008983screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984{
8985 int new_row, old_row;
8986#ifdef FEAT_GUI
8987 int old_Rows;
8988#endif
8989 win_T *wp;
8990 int outofmem = FALSE;
8991 int len;
8992 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008994 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008996 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 sattr_T *new_ScreenAttrs;
8998 unsigned *new_LineOffset;
8999 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009000 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009001 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009003 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009004 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009006retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 /*
9008 * Allocation of the screen buffers is done only when the size changes and
9009 * when Rows and Columns have been set and we have started doing full
9010 * screen stuff.
9011 */
9012 if ((ScreenLines != NULL
9013 && Rows == screen_Rows
9014 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 && enc_utf8 == (ScreenLinesUC != NULL)
9016 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01009017 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 || Rows == 0
9019 || Columns == 0
9020 || (!full_screen && ScreenLines == NULL))
9021 return;
9022
9023 /*
9024 * It's possible that we produce an out-of-memory message below, which
9025 * will cause this function to be called again. To break the loop, just
9026 * return here.
9027 */
9028 if (entered)
9029 return;
9030 entered = TRUE;
9031
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009032 /*
9033 * Note that the window sizes are updated before reallocating the arrays,
9034 * thus we must not redraw here!
9035 */
9036 ++RedrawingDisabled;
9037
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 win_new_shellsize(); /* fit the windows in the new sized shell */
9039
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 comp_col(); /* recompute columns for shown command and ruler */
9041
9042 /*
9043 * We're changing the size of the screen.
9044 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9045 * - Move lines from the old arrays into the new arrays, clear extra
9046 * lines (unless the screen is going to be cleared).
9047 * - Free the old arrays.
9048 *
9049 * If anything fails, make ScreenLines NULL, so we don't do anything!
9050 * Continuing with the old ScreenLines may result in a crash, because the
9051 * size is wrong.
9052 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009053 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009055 if (aucmd_win != NULL)
9056 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009057#ifdef FEAT_TEXT_PROP
9058 // global popup windows
9059 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9060 win_free_lsize(wp);
9061 // tab-local popup windows
9062 FOR_ALL_TABPAGES(tp)
9063 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9064 win_free_lsize(wp);
9065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009067 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01009068 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 if (enc_utf8)
9070 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009071 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009072 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009073 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
9074 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 }
9076 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009077 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
9078 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
9079 new_LineOffset = LALLOC_MULT(unsigned, Rows);
9080 new_LineWraps = LALLOC_MULT(char_u, Rows);
9081 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009083 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 {
9085 if (win_alloc_lines(wp) == FAIL)
9086 {
9087 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009088 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 }
9090 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009091 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9092 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009093 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009094#ifdef FEAT_TEXT_PROP
9095 // global popup windows
9096 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9097 if (win_alloc_lines(wp) == FAIL)
9098 {
9099 outofmem = TRUE;
9100 goto give_up;
9101 }
9102 // tab-local popup windows
9103 FOR_ALL_TABPAGES(tp)
9104 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9105 if (win_alloc_lines(wp) == FAIL)
9106 {
9107 outofmem = TRUE;
9108 goto give_up;
9109 }
9110#endif
9111
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009112give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009114 for (i = 0; i < p_mco; ++i)
9115 if (new_ScreenLinesC[i] == NULL)
9116 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009118 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 || new_ScreenAttrs == NULL
9121 || new_LineOffset == NULL
9122 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009123 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 || outofmem)
9125 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009126 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009127 {
9128 /* guess the size */
9129 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9130
9131 /* Remember we did this to avoid getting outofmem messages over
9132 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009133 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009134 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009135 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009136 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009137 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009138 VIM_CLEAR(new_ScreenLinesC[i]);
9139 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009140 VIM_CLEAR(new_ScreenAttrs);
9141 VIM_CLEAR(new_LineOffset);
9142 VIM_CLEAR(new_LineWraps);
9143 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 }
9145 else
9146 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009147 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009148
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 for (new_row = 0; new_row < Rows; ++new_row)
9150 {
9151 new_LineOffset[new_row] = new_row * Columns;
9152 new_LineWraps[new_row] = FALSE;
9153
9154 /*
9155 * If the screen is not going to be cleared, copy as much as
9156 * possible from the old screen to the new one and clear the rest
9157 * (used when resizing the window at the "--more--" prompt or when
9158 * executing an external command, for the GUI).
9159 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009160 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 {
9162 (void)vim_memset(new_ScreenLines + new_row * Columns,
9163 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 if (enc_utf8)
9165 {
9166 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9167 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009168 for (i = 0; i < p_mco; ++i)
9169 (void)vim_memset(new_ScreenLinesC[i]
9170 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 0, (size_t)Columns * sizeof(u8char_T));
9172 }
9173 if (enc_dbcs == DBCS_JPNU)
9174 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9175 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9177 0, (size_t)Columns * sizeof(sattr_T));
9178 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009179 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 {
9181 if (screen_Columns < Columns)
9182 len = screen_Columns;
9183 else
9184 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009185 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009186 * may be invalid now. Also when p_mco changes. */
9187 if (!(enc_utf8 && ScreenLinesUC == NULL)
9188 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009189 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9190 ScreenLines + LineOffset[old_row],
9191 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009192 if (enc_utf8 && ScreenLinesUC != NULL
9193 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 {
9195 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9196 ScreenLinesUC + LineOffset[old_row],
9197 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009198 for (i = 0; i < p_mco; ++i)
9199 mch_memmove(new_ScreenLinesC[i]
9200 + new_LineOffset[new_row],
9201 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 (size_t)len * sizeof(u8char_T));
9203 }
9204 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9205 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9206 ScreenLines2 + LineOffset[old_row],
9207 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9209 ScreenAttrs + LineOffset[old_row],
9210 (size_t)len * sizeof(sattr_T));
9211 }
9212 }
9213 }
9214 /* Use the last line of the screen for the current line. */
9215 current_ScreenLine = new_ScreenLines + Rows * Columns;
9216 }
9217
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009218 free_screenlines();
9219
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009222 for (i = 0; i < p_mco; ++i)
9223 ScreenLinesC[i] = new_ScreenLinesC[i];
9224 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 ScreenAttrs = new_ScreenAttrs;
9227 LineOffset = new_LineOffset;
9228 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009229 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
9231 /* It's important that screen_Rows and screen_Columns reflect the actual
9232 * size of ScreenLines[]. Set them before calling anything. */
9233#ifdef FEAT_GUI
9234 old_Rows = screen_Rows;
9235#endif
9236 screen_Rows = Rows;
9237 screen_Columns = Columns;
9238
9239 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009240 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242#ifdef FEAT_GUI
9243 else if (gui.in_use
9244 && !gui.starting
9245 && ScreenLines != NULL
9246 && old_Rows != Rows)
9247 {
9248 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9249 /*
9250 * Adjust the position of the cursor, for when executing an external
9251 * command.
9252 */
9253 if (msg_row >= Rows) /* Rows got smaller */
9254 msg_row = Rows - 1; /* put cursor at last row */
9255 else if (Rows > old_Rows) /* Rows got bigger */
9256 msg_row += Rows - old_Rows; /* put cursor in same place */
9257 if (msg_col >= Columns) /* Columns got smaller */
9258 msg_col = Columns - 1; /* put cursor at last column */
9259 }
9260#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009261 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009264 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009265
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009266 /*
9267 * Do not apply autocommands more than 3 times to avoid an endless loop
9268 * in case applying autocommands always changes Rows or Columns.
9269 */
9270 if (starting == 0 && ++retry_count <= 3)
9271 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009272 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009273 /* In rare cases, autocommands may have altered Rows or Columns,
9274 * jump back to check if we need to allocate the screen again. */
9275 goto retry;
9276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277}
9278
9279 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009280free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009281{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009282 int i;
9283
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009284 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009285 for (i = 0; i < Screen_mco; ++i)
9286 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009287 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009288 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009289 vim_free(ScreenAttrs);
9290 vim_free(LineOffset);
9291 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009292 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009293}
9294
9295 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009296screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297{
9298 check_for_delay(FALSE);
9299 screenalloc(FALSE); /* allocate screen buffers if size changed */
9300 screenclear2(); /* clear the screen */
9301}
9302
9303 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009304screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305{
9306 int i;
9307
9308 if (starting == NO_SCREEN || ScreenLines == NULL
9309#ifdef FEAT_GUI
9310 || (gui.in_use && gui.starting)
9311#endif
9312 )
9313 return;
9314
9315#ifdef FEAT_GUI
9316 if (!gui.in_use)
9317#endif
9318 screen_attr = -1; /* force setting the Normal colors */
9319 screen_stop_highlight(); /* don't want highlighting here */
9320
9321#ifdef FEAT_CLIPBOARD
9322 /* disable selection without redrawing it */
9323 clip_scroll_selection(9999);
9324#endif
9325
9326 /* blank out ScreenLines */
9327 for (i = 0; i < Rows; ++i)
9328 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009329 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 LineWraps[i] = FALSE;
9331 }
9332
9333 if (can_clear(T_CL))
9334 {
9335 out_str(T_CL); /* clear the display */
9336 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009337 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 }
9339 else
9340 {
9341 /* can't clear the screen, mark all chars with invalid attributes */
9342 for (i = 0; i < Rows; ++i)
9343 lineinvalid(LineOffset[i], (int)Columns);
9344 clear_cmdline = TRUE;
9345 }
9346
9347 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9348
9349 win_rest_invalid(firstwin);
9350 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009351 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 if (must_redraw == CLEAR) /* no need to clear again */
9353 must_redraw = NOT_VALID;
9354 compute_cmdrow();
9355 msg_row = cmdline_row; /* put cursor on last line for messages */
9356 msg_col = 0;
9357 screen_start(); /* don't know where cursor is now */
9358 msg_scrolled = 0; /* can't scroll back */
9359 msg_didany = FALSE;
9360 msg_didout = FALSE;
9361}
9362
9363/*
9364 * Clear one line in ScreenLines.
9365 */
9366 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009367lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368{
9369 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 if (enc_utf8)
9371 (void)vim_memset(ScreenLinesUC + off, 0,
9372 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009373 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374}
9375
9376/*
9377 * Mark one line in ScreenLines invalid by setting the attributes to an
9378 * invalid value.
9379 */
9380 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009381lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
9383 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9384}
9385
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386/*
9387 * Copy part of a Screenline for vertically split window "wp".
9388 */
9389 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009390linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391{
9392 unsigned off_to = LineOffset[to] + wp->w_wincol;
9393 unsigned off_from = LineOffset[from] + wp->w_wincol;
9394
9395 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9396 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 if (enc_utf8)
9398 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009399 int i;
9400
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9402 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009403 for (i = 0; i < p_mco; ++i)
9404 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9405 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 }
9407 if (enc_dbcs == DBCS_JPNU)
9408 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9409 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9411 wp->w_width * sizeof(sattr_T));
9412}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413
9414/*
9415 * Return TRUE if clearing with term string "p" would work.
9416 * It can't work when the string is empty or it won't set the right background.
9417 */
9418 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009419can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420{
9421 return (*p != NUL && (t_colors <= 1
9422#ifdef FEAT_GUI
9423 || gui.in_use
9424#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009425#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009426 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009427 || (!p_tgc && cterm_normal_bg_color == 0)
9428#else
9429 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009430#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009431 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432}
9433
9434/*
9435 * Reset cursor position. Use whenever cursor was moved because of outputting
9436 * something directly to the screen (shell commands) or a terminal control
9437 * code.
9438 */
9439 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009440screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441{
9442 screen_cur_row = screen_cur_col = 9999;
9443}
9444
9445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 * Move the cursor to position "row","col" in the screen.
9447 * This tries to find the most efficient way to move, minimizing the number of
9448 * characters sent to the terminal.
9449 */
9450 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009451windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009453 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 int i;
9455 int plan;
9456 int cost;
9457 int wouldbe_col;
9458 int noinvcurs;
9459 char_u *bs;
9460 int goto_cost;
9461 int attr;
9462
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009463#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9465
9466#define PLAN_LE 1
9467#define PLAN_CR 2
9468#define PLAN_NL 3
9469#define PLAN_WRITE 4
9470 /* Can't use ScreenLines unless initialized */
9471 if (ScreenLines == NULL)
9472 return;
9473
9474 if (col != screen_cur_col || row != screen_cur_row)
9475 {
9476 /* Check for valid position. */
9477 if (row < 0) /* window without text lines? */
9478 row = 0;
9479 if (row >= screen_Rows)
9480 row = screen_Rows - 1;
9481 if (col >= screen_Columns)
9482 col = screen_Columns - 1;
9483
9484 /* check if no cursor movement is allowed in highlight mode */
9485 if (screen_attr && *T_MS == NUL)
9486 noinvcurs = HIGHL_COST;
9487 else
9488 noinvcurs = 0;
9489 goto_cost = GOTO_COST + noinvcurs;
9490
9491 /*
9492 * Plan how to do the positioning:
9493 * 1. Use CR to move it to column 0, same row.
9494 * 2. Use T_LE to move it a few columns to the left.
9495 * 3. Use NL to move a few lines down, column 0.
9496 * 4. Move a few columns to the right with T_ND or by writing chars.
9497 *
9498 * Don't do this if the cursor went beyond the last column, the cursor
9499 * position is unknown then (some terminals wrap, some don't )
9500 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009501 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 * characters to move the cursor to the right.
9503 */
9504 if (row >= screen_cur_row && screen_cur_col < Columns)
9505 {
9506 /*
9507 * If the cursor is in the same row, bigger col, we can use CR
9508 * or T_LE.
9509 */
9510 bs = NULL; /* init for GCC */
9511 attr = screen_attr;
9512 if (row == screen_cur_row && col < screen_cur_col)
9513 {
9514 /* "le" is preferred over "bc", because "bc" is obsolete */
9515 if (*T_LE)
9516 bs = T_LE; /* "cursor left" */
9517 else
9518 bs = T_BC; /* "backspace character (old) */
9519 if (*bs)
9520 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9521 else
9522 cost = 999;
9523 if (col + 1 < cost) /* using CR is less characters */
9524 {
9525 plan = PLAN_CR;
9526 wouldbe_col = 0;
9527 cost = 1; /* CR is just one character */
9528 }
9529 else
9530 {
9531 plan = PLAN_LE;
9532 wouldbe_col = col;
9533 }
9534 if (noinvcurs) /* will stop highlighting */
9535 {
9536 cost += noinvcurs;
9537 attr = 0;
9538 }
9539 }
9540
9541 /*
9542 * If the cursor is above where we want to be, we can use CR LF.
9543 */
9544 else if (row > screen_cur_row)
9545 {
9546 plan = PLAN_NL;
9547 wouldbe_col = 0;
9548 cost = (row - screen_cur_row) * 2; /* CR LF */
9549 if (noinvcurs) /* will stop highlighting */
9550 {
9551 cost += noinvcurs;
9552 attr = 0;
9553 }
9554 }
9555
9556 /*
9557 * If the cursor is in the same row, smaller col, just use write.
9558 */
9559 else
9560 {
9561 plan = PLAN_WRITE;
9562 wouldbe_col = screen_cur_col;
9563 cost = 0;
9564 }
9565
9566 /*
9567 * Check if any characters that need to be written have the
9568 * correct attributes. Also avoid UTF-8 characters.
9569 */
9570 i = col - wouldbe_col;
9571 if (i > 0)
9572 cost += i;
9573 if (cost < goto_cost && i > 0)
9574 {
9575 /*
9576 * Check if the attributes are correct without additionally
9577 * stopping highlighting.
9578 */
9579 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9580 while (i && *p++ == attr)
9581 --i;
9582 if (i != 0)
9583 {
9584 /*
9585 * Try if it works when highlighting is stopped here.
9586 */
9587 if (*--p == 0)
9588 {
9589 cost += noinvcurs;
9590 while (i && *p++ == 0)
9591 --i;
9592 }
9593 if (i != 0)
9594 cost = 999; /* different attributes, don't do it */
9595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 if (enc_utf8)
9597 {
9598 /* Don't use an UTF-8 char for positioning, it's slow. */
9599 for (i = wouldbe_col; i < col; ++i)
9600 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9601 {
9602 cost = 999;
9603 break;
9604 }
9605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 }
9607
9608 /*
9609 * We can do it without term_windgoto()!
9610 */
9611 if (cost < goto_cost)
9612 {
9613 if (plan == PLAN_LE)
9614 {
9615 if (noinvcurs)
9616 screen_stop_highlight();
9617 while (screen_cur_col > col)
9618 {
9619 out_str(bs);
9620 --screen_cur_col;
9621 }
9622 }
9623 else if (plan == PLAN_CR)
9624 {
9625 if (noinvcurs)
9626 screen_stop_highlight();
9627 out_char('\r');
9628 screen_cur_col = 0;
9629 }
9630 else if (plan == PLAN_NL)
9631 {
9632 if (noinvcurs)
9633 screen_stop_highlight();
9634 while (screen_cur_row < row)
9635 {
9636 out_char('\n');
9637 ++screen_cur_row;
9638 }
9639 screen_cur_col = 0;
9640 }
9641
9642 i = col - screen_cur_col;
9643 if (i > 0)
9644 {
9645 /*
9646 * Use cursor-right if it's one character only. Avoids
9647 * removing a line of pixels from the last bold char, when
9648 * using the bold trick in the GUI.
9649 */
9650 if (T_ND[0] != NUL && T_ND[1] == NUL)
9651 {
9652 while (i-- > 0)
9653 out_char(*T_ND);
9654 }
9655 else
9656 {
9657 int off;
9658
9659 off = LineOffset[row] + screen_cur_col;
9660 while (i-- > 0)
9661 {
9662 if (ScreenAttrs[off] != screen_attr)
9663 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 if (enc_dbcs == DBCS_JPNU
9667 && ScreenLines[off] == 0x8e)
9668 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 ++off;
9670 }
9671 }
9672 }
9673 }
9674 }
9675 else
9676 cost = 999;
9677
9678 if (cost >= goto_cost)
9679 {
9680 if (noinvcurs)
9681 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009682 if (row == screen_cur_row && (col > screen_cur_col)
9683 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 term_cursor_right(col - screen_cur_col);
9685 else
9686 term_windgoto(row, col);
9687 }
9688 screen_cur_row = row;
9689 screen_cur_col = col;
9690 }
9691}
9692
9693/*
9694 * Set cursor to its position in the current window.
9695 */
9696 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009697setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009699 setcursor_mayforce(FALSE);
9700}
9701
9702/*
9703 * Set cursor to its position in the current window.
9704 * When "force" is TRUE also when not redrawing.
9705 */
9706 void
9707setcursor_mayforce(int force)
9708{
9709 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 {
9711 validate_cursor();
9712 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009713 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009715 /* With 'rightleft' set and the cursor on a double-wide
9716 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009717 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9718 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009719 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009720 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721#endif
9722 curwin->w_wcol));
9723 }
9724}
9725
9726
9727/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009728 * Insert 'line_count' lines at 'row' in window 'wp'.
9729 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9730 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 * scrolling.
9732 * Returns FAIL if the lines are not inserted, OK for success.
9733 */
9734 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009735win_ins_lines(
9736 win_T *wp,
9737 int row,
9738 int line_count,
9739 int invalid,
9740 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741{
9742 int did_delete;
9743 int nextrow;
9744 int lastrow;
9745 int retval;
9746
9747 if (invalid)
9748 wp->w_lines_valid = 0;
9749
9750 if (wp->w_height < 5)
9751 return FAIL;
9752
9753 if (line_count > wp->w_height - row)
9754 line_count = wp->w_height - row;
9755
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009756 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 if (retval != MAYBE)
9758 return retval;
9759
9760 /*
9761 * If there is a next window or a status line, we first try to delete the
9762 * lines at the bottom to avoid messing what is after the window.
9763 * If this fails and there are following windows, don't do anything to avoid
9764 * messing up those windows, better just redraw.
9765 */
9766 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 if (wp->w_next != NULL || wp->w_status_height)
9768 {
9769 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009770 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 did_delete = TRUE;
9772 else if (wp->w_next)
9773 return FAIL;
9774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 /*
9776 * if no lines deleted, blank the lines that will end up below the window
9777 */
9778 if (!did_delete)
9779 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009782 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 lastrow = nextrow + line_count;
9784 if (lastrow > Rows)
9785 lastrow = Rows;
9786 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009787 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 ' ', ' ', 0);
9789 }
9790
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009791 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 == FAIL)
9793 {
9794 /* deletion will have messed up other windows */
9795 if (did_delete)
9796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 win_rest_invalid(W_NEXT(wp));
9799 }
9800 return FAIL;
9801 }
9802
9803 return OK;
9804}
9805
9806/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009807 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9809 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9810 * scrolling
9811 * Return OK for success, FAIL if the lines are not deleted.
9812 */
9813 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009814win_del_lines(
9815 win_T *wp,
9816 int row,
9817 int line_count,
9818 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009819 int mayclear,
9820 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
9822 int retval;
9823
9824 if (invalid)
9825 wp->w_lines_valid = 0;
9826
9827 if (line_count > wp->w_height - row)
9828 line_count = wp->w_height - row;
9829
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009830 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 if (retval != MAYBE)
9832 return retval;
9833
9834 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009835 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 return FAIL;
9837
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 /*
9839 * If there are windows or status lines below, try to put them at the
9840 * correct place. If we can't do that, they have to be redrawn.
9841 */
9842 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9843 {
9844 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009845 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 {
9847 wp->w_redr_status = TRUE;
9848 win_rest_invalid(wp->w_next);
9849 }
9850 }
9851 /*
9852 * If this is the last window and there is no status line, redraw the
9853 * command line later.
9854 */
9855 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 redraw_cmdline = TRUE;
9857 return OK;
9858}
9859
9860/*
9861 * Common code for win_ins_lines() and win_del_lines().
9862 * Returns OK or FAIL when the work has been done.
9863 * Returns MAYBE when not finished yet.
9864 */
9865 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009866win_do_lines(
9867 win_T *wp,
9868 int row,
9869 int line_count,
9870 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009871 int del,
9872 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873{
9874 int retval;
9875
9876 if (!redrawing() || line_count <= 0)
9877 return FAIL;
9878
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009879 /* When inserting lines would result in loss of command output, just redraw
9880 * the lines. */
9881 if (no_win_do_lines_ins && !del)
9882 return FAIL;
9883
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009885 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009887 if (!no_win_do_lines_ins)
9888 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 return FAIL;
9890 }
9891
9892 /*
9893 * Delete all remaining lines
9894 */
9895 if (row + line_count >= wp->w_height)
9896 {
9897 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009898 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 ' ', ' ', 0);
9900 return OK;
9901 }
9902
9903 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009904 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009906 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009908 if (!no_win_do_lines_ins)
9909 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910
9911 /*
9912 * If the terminal can set a scroll region, use that.
9913 * Always do this in a vertically split window. This will redraw from
9914 * ScreenLines[] when t_CV isn't defined. That's faster than using
9915 * win_line().
9916 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009917 * a character in the lower right corner of the scroll region may cause a
9918 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009920 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 scroll_region_set(wp, row);
9924 if (del)
9925 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009926 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 else
9928 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009929 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 scroll_region_reset();
9932 return retval;
9933 }
9934
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9936 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937
9938 return MAYBE;
9939}
9940
9941/*
9942 * window 'wp' and everything after it is messed up, mark it for redraw
9943 */
9944 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009945win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 {
9949 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 wp->w_redr_status = TRUE;
9951 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 }
9953 redraw_cmdline = TRUE;
9954}
9955
9956/*
9957 * The rest of the routines in this file perform screen manipulations. The
9958 * given operation is performed physically on the screen. The corresponding
9959 * change is also made to the internal screen image. In this way, the editor
9960 * anticipates the effect of editing changes on the appearance of the screen.
9961 * That way, when we call screenupdate a complete redraw isn't usually
9962 * necessary. Another advantage is that we can keep adding code to anticipate
9963 * screen changes, and in the meantime, everything still works.
9964 */
9965
9966/*
9967 * types for inserting or deleting lines
9968 */
9969#define USE_T_CAL 1
9970#define USE_T_CDL 2
9971#define USE_T_AL 3
9972#define USE_T_CE 4
9973#define USE_T_DL 5
9974#define USE_T_SR 6
9975#define USE_NL 7
9976#define USE_T_CD 8
9977#define USE_REDRAW 9
9978
9979/*
9980 * insert lines on the screen and update ScreenLines[]
9981 * 'end' is the line after the scrolled part. Normally it is Rows.
9982 * When scrolling region used 'off' is the offset from the top for the region.
9983 * 'row' and 'end' are relative to the start of the region.
9984 *
9985 * return FAIL for failure, OK for success.
9986 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009987 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009988screen_ins_lines(
9989 int off,
9990 int row,
9991 int line_count,
9992 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009993 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009994 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995{
9996 int i;
9997 int j;
9998 unsigned temp;
9999 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010000 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 int type;
10002 int result_empty;
10003 int can_ce = can_clear(T_CE);
10004
10005 /*
10006 * FAIL if
10007 * - there is no valid screen
10008 * - the screen has to be redrawn completely
10009 * - the line count is less than one
10010 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010011 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010013 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
10014#ifdef FEAT_CLIPBOARD
10015 || (clip_star.state != SELECT_CLEARED
10016 && redrawing_for_callback > 0)
10017#endif
10018 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 return FAIL;
10020
10021 /*
10022 * There are seven ways to insert lines:
10023 * 0. When in a vertically split window and t_CV isn't set, redraw the
10024 * characters from ScreenLines[].
10025 * 1. Use T_CD (clear to end of display) if it exists and the result of
10026 * the insert is just empty lines
10027 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10028 * present or line_count > 1. It looks better if we do all the inserts
10029 * at once.
10030 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10031 * insert is just empty lines and T_CE is not present or line_count >
10032 * 1.
10033 * 4. Use T_AL (insert line) if it exists.
10034 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10035 * just empty lines.
10036 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10037 * just empty lines.
10038 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10039 * the 'da' flag is not set or we have clear line capability.
10040 * 8. redraw the characters from ScreenLines[].
10041 *
10042 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10043 * the scrollbar for the window. It does have insert line, use that if it
10044 * exists.
10045 */
10046 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10048 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010049 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 type = USE_T_CD;
10051 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10052 type = USE_T_CAL;
10053 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10054 type = USE_T_CDL;
10055 else if (*T_AL != NUL)
10056 type = USE_T_AL;
10057 else if (can_ce && result_empty)
10058 type = USE_T_CE;
10059 else if (*T_DL != NUL && result_empty)
10060 type = USE_T_DL;
10061 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10062 type = USE_T_SR;
10063 else
10064 return FAIL;
10065
10066 /*
10067 * For clearing the lines screen_del_lines() is used. This will also take
10068 * care of t_db if necessary.
10069 */
10070 if (type == USE_T_CD || type == USE_T_CDL ||
10071 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010072 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073
10074 /*
10075 * If text is retained below the screen, first clear or delete as many
10076 * lines at the bottom of the window as are about to be inserted so that
10077 * the deleted lines won't later surface during a screen_del_lines.
10078 */
10079 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010080 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081
10082#ifdef FEAT_CLIPBOARD
10083 /* Remove a modeless selection when inserting lines halfway the screen
10084 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010085 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010086 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 else
10088 clip_scroll_selection(-line_count);
10089#endif
10090
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091#ifdef FEAT_GUI
10092 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10093 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010094 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095#endif
10096
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010097 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10098 cursor_col = wp->w_wincol;
10099
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 if (*T_CCS != NUL) /* cursor relative to region */
10101 cursor_row = row;
10102 else
10103 cursor_row = row + off;
10104
10105 /*
10106 * Shift LineOffset[] line_count down to reflect the inserted lines.
10107 * Clear the inserted lines in ScreenLines[].
10108 */
10109 row += off;
10110 end += off;
10111 for (i = 0; i < line_count; ++i)
10112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 if (wp != NULL && wp->w_width != Columns)
10114 {
10115 /* need to copy part of a line */
10116 j = end - 1 - i;
10117 while ((j -= line_count) >= row)
10118 linecopy(j + line_count, j, wp);
10119 j += line_count;
10120 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010121 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10122 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 else
10124 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10125 LineWraps[j] = FALSE;
10126 }
10127 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 {
10129 j = end - 1 - i;
10130 temp = LineOffset[j];
10131 while ((j -= line_count) >= row)
10132 {
10133 LineOffset[j + line_count] = LineOffset[j];
10134 LineWraps[j + line_count] = LineWraps[j];
10135 }
10136 LineOffset[j + line_count] = temp;
10137 LineWraps[j + line_count] = FALSE;
10138 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010139 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 else
10141 lineinvalid(temp, (int)Columns);
10142 }
10143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144
10145 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010146 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010147 if (clear_attr != 0)
10148 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 /* redraw the characters */
10151 if (type == USE_REDRAW)
10152 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010153 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 {
10155 term_append_lines(line_count);
10156 screen_start(); /* don't know where cursor is now */
10157 }
10158 else
10159 {
10160 for (i = 0; i < line_count; i++)
10161 {
10162 if (type == USE_T_AL)
10163 {
10164 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010165 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 out_str(T_AL);
10167 }
10168 else /* type == USE_T_SR */
10169 out_str(T_SR);
10170 screen_start(); /* don't know where cursor is now */
10171 }
10172 }
10173
10174 /*
10175 * With scroll-reverse and 'da' flag set we need to clear the lines that
10176 * have been scrolled down into the region.
10177 */
10178 if (type == USE_T_SR && *T_DA)
10179 {
10180 for (i = 0; i < line_count; ++i)
10181 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010182 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 out_str(T_CE);
10184 screen_start(); /* don't know where cursor is now */
10185 }
10186 }
10187
10188#ifdef FEAT_GUI
10189 gui_can_update_cursor();
10190 if (gui.in_use)
10191 out_flush(); /* always flush after a scroll */
10192#endif
10193 return OK;
10194}
10195
10196/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010197 * Delete lines on the screen and update ScreenLines[].
10198 * "end" is the line after the scrolled part. Normally it is Rows.
10199 * When scrolling region used "off" is the offset from the top for the region.
10200 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 *
10202 * Return OK for success, FAIL if the lines are not deleted.
10203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010205screen_del_lines(
10206 int off,
10207 int row,
10208 int line_count,
10209 int end,
10210 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010211 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010212 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213{
10214 int j;
10215 int i;
10216 unsigned temp;
10217 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010218 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 int cursor_end;
10220 int result_empty; /* result is empty until end of region */
10221 int can_delete; /* deleting line codes can be used */
10222 int type;
10223
10224 /*
10225 * FAIL if
10226 * - there is no valid screen
10227 * - the screen has to be redrawn completely
10228 * - the line count is less than one
10229 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010230 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010232 if (!screen_valid(TRUE) || line_count <= 0
10233 || (!force && line_count > p_ttyscroll)
10234#ifdef FEAT_CLIPBOARD
10235 || (clip_star.state != SELECT_CLEARED
10236 && redrawing_for_callback > 0)
10237#endif
10238 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 return FAIL;
10240
10241 /*
10242 * Check if the rest of the current region will become empty.
10243 */
10244 result_empty = row + line_count >= end;
10245
10246 /*
10247 * We can delete lines only when 'db' flag not set or when 'ce' option
10248 * available.
10249 */
10250 can_delete = (*T_DB == NUL || can_clear(T_CE));
10251
10252 /*
10253 * There are six ways to delete lines:
10254 * 0. When in a vertically split window and t_CV isn't set, redraw the
10255 * characters from ScreenLines[].
10256 * 1. Use T_CD if it exists and the result is empty.
10257 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10258 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10259 * none of the other ways work.
10260 * 4. Use T_CE (erase line) if the result is empty.
10261 * 5. Use T_DL (delete line) if it exists.
10262 * 6. redraw the characters from ScreenLines[].
10263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10265 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010266 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 type = USE_T_CD;
10268#if defined(__BEOS__) && defined(BEOS_DR8)
10269 /*
10270 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10271 * its internal termcap... this works okay for tests which test *T_DB !=
10272 * NUL. It has the disadvantage that the user cannot use any :set t_*
10273 * command to get T_DB (back) to empty_option, only :set term=... will do
10274 * the trick...
10275 * Anyway, this hack will hopefully go away with the next OS release.
10276 * (Olaf Seibert)
10277 */
10278 else if (row == 0 && T_DB == empty_option
10279 && (line_count == 1 || *T_CDL == NUL))
10280#else
10281 else if (row == 0 && (
10282#ifndef AMIGA
10283 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10284 * up, so use delete-line command */
10285 line_count == 1 ||
10286#endif
10287 *T_CDL == NUL))
10288#endif
10289 type = USE_NL;
10290 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10291 type = USE_T_CDL;
10292 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010293 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 type = USE_T_CE;
10295 else if (*T_DL != NUL && can_delete)
10296 type = USE_T_DL;
10297 else if (*T_CDL != NUL && can_delete)
10298 type = USE_T_CDL;
10299 else
10300 return FAIL;
10301
10302#ifdef FEAT_CLIPBOARD
10303 /* Remove a modeless selection when deleting lines halfway the screen or
10304 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010305 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010306 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 else
10308 clip_scroll_selection(line_count);
10309#endif
10310
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311#ifdef FEAT_GUI
10312 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10313 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010314 gui_dont_update_cursor(gui.cursor_row >= row + off
10315 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316#endif
10317
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010318 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10319 cursor_col = wp->w_wincol;
10320
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 if (*T_CCS != NUL) /* cursor relative to region */
10322 {
10323 cursor_row = row;
10324 cursor_end = end;
10325 }
10326 else
10327 {
10328 cursor_row = row + off;
10329 cursor_end = end + off;
10330 }
10331
10332 /*
10333 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10334 * Clear the inserted lines in ScreenLines[].
10335 */
10336 row += off;
10337 end += off;
10338 for (i = 0; i < line_count; ++i)
10339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 if (wp != NULL && wp->w_width != Columns)
10341 {
10342 /* need to copy part of a line */
10343 j = row + i;
10344 while ((j += line_count) <= end - 1)
10345 linecopy(j - line_count, j, wp);
10346 j -= line_count;
10347 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010348 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10349 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 else
10351 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10352 LineWraps[j] = FALSE;
10353 }
10354 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 {
10356 /* whole width, moving the line pointers is faster */
10357 j = row + i;
10358 temp = LineOffset[j];
10359 while ((j += line_count) <= end - 1)
10360 {
10361 LineOffset[j - line_count] = LineOffset[j];
10362 LineWraps[j - line_count] = LineWraps[j];
10363 }
10364 LineOffset[j - line_count] = temp;
10365 LineWraps[j - line_count] = FALSE;
10366 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010367 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 else
10369 lineinvalid(temp, (int)Columns);
10370 }
10371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010373 if (screen_attr != clear_attr)
10374 screen_stop_highlight();
10375 if (clear_attr != 0)
10376 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 /* redraw the characters */
10379 if (type == USE_REDRAW)
10380 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010381 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010383 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 out_str(T_CD);
10385 screen_start(); /* don't know where cursor is now */
10386 }
10387 else if (type == USE_T_CDL)
10388 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010389 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 term_delete_lines(line_count);
10391 screen_start(); /* don't know where cursor is now */
10392 }
10393 /*
10394 * Deleting lines at top of the screen or scroll region: Just scroll
10395 * the whole screen (scroll region) up by outputting newlines on the
10396 * last line.
10397 */
10398 else if (type == USE_NL)
10399 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010400 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 for (i = line_count; --i >= 0; )
10402 out_char('\n'); /* cursor will remain on same line */
10403 }
10404 else
10405 {
10406 for (i = line_count; --i >= 0; )
10407 {
10408 if (type == USE_T_DL)
10409 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010410 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 out_str(T_DL); /* delete a line */
10412 }
10413 else /* type == USE_T_CE */
10414 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010415 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 out_str(T_CE); /* erase a line */
10417 }
10418 screen_start(); /* don't know where cursor is now */
10419 }
10420 }
10421
10422 /*
10423 * If the 'db' flag is set, we need to clear the lines that have been
10424 * scrolled up at the bottom of the region.
10425 */
10426 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10427 {
10428 for (i = line_count; i > 0; --i)
10429 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010430 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 out_str(T_CE); /* erase a line */
10432 screen_start(); /* don't know where cursor is now */
10433 }
10434 }
10435
10436#ifdef FEAT_GUI
10437 gui_can_update_cursor();
10438 if (gui.in_use)
10439 out_flush(); /* always flush after a scroll */
10440#endif
10441
10442 return OK;
10443}
10444
10445/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010446 * Return TRUE when postponing displaying the mode message: when not redrawing
10447 * or inside a mapping.
10448 */
10449 int
10450skip_showmode()
10451{
10452 // Call char_avail() only when we are going to show something, because it
10453 // takes a bit of time. redrawing() may also call char_avail_avail().
10454 if (global_busy
10455 || msg_silent != 0
10456 || !redrawing()
10457 || (char_avail() && !KeyTyped))
10458 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010459 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010460 return TRUE;
10461 }
10462 return FALSE;
10463}
10464
10465/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010466 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 *
10468 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10469 * If clear_cmdline is FALSE there may be a message there that needs to be
10470 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010471 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 * Return the length of the message (0 if no message).
10473 */
10474 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010475showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476{
10477 int need_clear;
10478 int length = 0;
10479 int do_mode;
10480 int attr;
10481 int nwr_save;
10482#ifdef FEAT_INS_EXPAND
10483 int sub_attr;
10484#endif
10485
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010486 do_mode = ((p_smd && msg_silent == 0)
10487 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010488 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010489 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010490 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010492 if (skip_showmode())
10493 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494
10495 nwr_save = need_wait_return;
10496
10497 /* wait a bit before overwriting an important message */
10498 check_for_delay(FALSE);
10499
10500 /* if the cmdline is more than one line high, erase top lines */
10501 need_clear = clear_cmdline;
10502 if (clear_cmdline && cmdline_row < Rows - 1)
10503 msg_clr_cmdline(); /* will reset clear_cmdline */
10504
10505 /* Position on the last line in the window, column 0 */
10506 msg_pos_mode();
10507 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010508 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 if (do_mode)
10510 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010511 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010513 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010514# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010515 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010516# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010517 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010518# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010519 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010520# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010521 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010523 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524# endif
10525#endif
10526#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10527 if (gui.in_use)
10528 {
10529 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010530 {
10531 /* HANGUL */
10532 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010533 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010534 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010535 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 }
10538#endif
10539#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010540 /* CTRL-X in Insert mode */
10541 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 {
10543 /* These messages can get long, avoid a wrap in a narrow
10544 * window. Prefer showing edit_submode_extra. */
10545 length = (Rows - msg_row) * Columns - 3;
10546 if (edit_submode_extra != NULL)
10547 length -= vim_strsize(edit_submode_extra);
10548 if (length > 0)
10549 {
10550 if (edit_submode_pre != NULL)
10551 length -= vim_strsize(edit_submode_pre);
10552 if (length - vim_strsize(edit_submode) > 0)
10553 {
10554 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010555 msg_puts_attr((char *)edit_submode_pre, attr);
10556 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 }
10558 if (edit_submode_extra != NULL)
10559 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010560 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010562 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 else
10564 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010565 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 }
10567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 }
10569 else
10570#endif
10571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010573 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010574 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010575 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 else if (State & INSERT)
10577 {
10578#ifdef FEAT_RIGHTLEFT
10579 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010580 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010582 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010584 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010585 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010587 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010589 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590#ifdef FEAT_RIGHTLEFT
10591 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010592 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593#endif
10594#ifdef FEAT_KEYMAP
10595 if (State & LANGMAP)
10596 {
10597# ifdef FEAT_ARABIC
10598 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010599 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 else
10601# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010602 if (get_keymap_str(curwin, (char_u *)" (%s)",
10603 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010604 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 }
10606#endif
10607 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010608 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 if (VIsual_active)
10611 {
10612 char *p;
10613
10614 /* Don't concatenate separate words to avoid translation
10615 * problems. */
10616 switch ((VIsual_select ? 4 : 0)
10617 + (VIsual_mode == Ctrl_V) * 2
10618 + (VIsual_mode == 'V'))
10619 {
10620 case 0: p = N_(" VISUAL"); break;
10621 case 1: p = N_(" VISUAL LINE"); break;
10622 case 2: p = N_(" VISUAL BLOCK"); break;
10623 case 4: p = N_(" SELECT"); break;
10624 case 5: p = N_(" SELECT LINE"); break;
10625 default: p = N_(" SELECT BLOCK"); break;
10626 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010627 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010629 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010631
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 need_clear = TRUE;
10633 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010634 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635#ifdef FEAT_INS_EXPAND
10636 && edit_submode == NULL /* otherwise it gets too long */
10637#endif
10638 )
10639 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010640 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 need_clear = TRUE;
10642 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010643
10644 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010645 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 msg_clr_eos();
10647 msg_didout = FALSE; /* overwrite this message */
10648 length = msg_col;
10649 msg_col = 0;
10650 need_wait_return = nwr_save; /* never ask for hit-return for this */
10651 }
10652 else if (clear_cmdline && msg_silent == 0)
10653 /* Clear the whole command line. Will reset "clear_cmdline". */
10654 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010655 else if (redraw_mode)
10656 {
10657 msg_pos_mode();
10658 msg_clr_eos();
10659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660
10661#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 /* In Visual mode the size of the selected area must be redrawn. */
10663 if (VIsual_active)
10664 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665
10666 /* If the last window has no status line, the ruler is after the mode
10667 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010668 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010669 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670#endif
10671 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010672 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 clear_cmdline = FALSE;
10674
10675 return length;
10676}
10677
10678/*
10679 * Position for a mode message.
10680 */
10681 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010682msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683{
10684 msg_col = 0;
10685 msg_row = Rows - 1;
10686}
10687
10688/*
10689 * Delete mode message. Used when ESC is typed which is expected to end
10690 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010691 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 */
10693 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010694unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695{
10696 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010697 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 */
10699 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10700 redraw_cmdline = TRUE; /* delete mode later */
10701 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010702 clearmode();
10703}
10704
10705/*
10706 * Clear the mode message.
10707 */
10708 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010709clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010710{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010711 int save_msg_row = msg_row;
10712 int save_msg_col = msg_col;
10713
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010714 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010715 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010716 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010717 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010718
10719 msg_col = save_msg_col;
10720 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721}
10722
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010723 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010724recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010725{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010726 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010727 if (!shortmess(SHM_RECORDING))
10728 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010729 char s[4];
10730
10731 sprintf(s, " @%c", reg_recording);
10732 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010733 }
10734}
10735
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010736/*
10737 * Draw the tab pages line at the top of the Vim window.
10738 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010739 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010740draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010741{
10742 int tabcount = 0;
10743 tabpage_T *tp;
10744 int tabwidth;
10745 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010746 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010747 int attr;
10748 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010749 win_T *cwp;
10750 int wincount;
10751 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010752 int c;
10753 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010754 int attr_sel = HL_ATTR(HLF_TPS);
10755 int attr_nosel = HL_ATTR(HLF_TP);
10756 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010757 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010758 int room;
10759 int use_sep_chars = (t_colors < 8
10760#ifdef FEAT_GUI
10761 && !gui.in_use
10762#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010763#ifdef FEAT_TERMGUICOLORS
10764 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010765#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010766 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010767
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010768 if (ScreenLines == NULL)
10769 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010770 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010771
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010772#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010773 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010774 if (gui_use_tabline())
10775 {
10776 gui_update_tabline();
10777 return;
10778 }
10779#endif
10780
10781 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010782 return;
10783
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010784#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010785 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010786
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010787 /* Use the 'tabline' option if it's set. */
10788 if (*p_tal != NUL)
10789 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010790 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010791
10792 /* Check for an error. If there is one we would loop in redrawing the
10793 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010794 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010795 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010796 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010797 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010798 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010799 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010800 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010801 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010802#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010803 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010804 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010805 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010806
Bram Moolenaar238a5642006-02-21 22:12:05 +000010807 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10808 if (tabwidth < 6)
10809 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010810
Bram Moolenaar238a5642006-02-21 22:12:05 +000010811 attr = attr_nosel;
10812 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010813 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10814 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010815 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010816 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010817
Bram Moolenaar238a5642006-02-21 22:12:05 +000010818 if (tp->tp_topframe == topframe)
10819 attr = attr_sel;
10820 if (use_sep_chars && col > 0)
10821 screen_putchar('|', 0, col++, attr);
10822
10823 if (tp->tp_topframe != topframe)
10824 attr = attr_nosel;
10825
10826 screen_putchar(' ', 0, col++, attr);
10827
10828 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010829 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010830 cwp = curwin;
10831 wp = firstwin;
10832 }
10833 else
10834 {
10835 cwp = tp->tp_curwin;
10836 wp = tp->tp_firstwin;
10837 }
10838
10839 modified = FALSE;
10840 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10841 if (bufIsChanged(wp->w_buffer))
10842 modified = TRUE;
10843 if (modified || wincount > 1)
10844 {
10845 if (wincount > 1)
10846 {
10847 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010848 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010849 if (col + len >= Columns - 3)
10850 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010851 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010852#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010853 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010854#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010855 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010856#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010857 );
10858 col += len;
10859 }
10860 if (modified)
10861 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10862 screen_putchar(' ', 0, col++, attr);
10863 }
10864
10865 room = scol - col + tabwidth - 1;
10866 if (room > 0)
10867 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010868 /* Get buffer name in NameBuff[] */
10869 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010870 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010871 len = vim_strsize(NameBuff);
10872 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010873 if (has_mbyte)
10874 while (len > room)
10875 {
10876 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010877 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010878 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010879 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010880 {
10881 p += len - room;
10882 len = room;
10883 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010884 if (len > Columns - col - 1)
10885 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010886
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010887 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010888 col += len;
10889 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010890 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010891
10892 /* Store the tab page number in TabPageIdxs[], so that
10893 * jump_to_mouse() knows where each one is. */
10894 ++tabcount;
10895 while (scol < col)
10896 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010897 }
10898
Bram Moolenaar238a5642006-02-21 22:12:05 +000010899 if (use_sep_chars)
10900 c = '_';
10901 else
10902 c = ' ';
10903 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010904
10905 /* Put an "X" for closing the current tab if there are several. */
10906 if (first_tabpage->tp_next != NULL)
10907 {
10908 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10909 TabPageIdxs[Columns - 1] = -999;
10910 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010911 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010912
10913 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10914 * set. */
10915 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010916}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010917
10918/*
10919 * Get buffer name for "buf" into NameBuff[].
10920 * Takes care of special buffer names and translates special characters.
10921 */
10922 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010923get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010924{
10925 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010926 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010927 else
10928 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10929 trans_characters(NameBuff, MAXPATHL);
10930}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010931
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932/*
10933 * Get the character to use in a status line. Get its attributes in "*attr".
10934 */
10935 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010936fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937{
10938 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010939
10940#ifdef FEAT_TERMINAL
10941 if (bt_terminal(wp->w_buffer))
10942 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010943 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010944 {
10945 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010946 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010947 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010948 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010949 {
10950 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010951 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010952 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010953 }
10954 else
10955#endif
10956 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010958 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 fill = fill_stl;
10960 }
10961 else
10962 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010963 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 fill = fill_stlnc;
10965 }
10966 /* Use fill when there is highlighting, and highlighting of current
10967 * window differs, or the fillchars differ, or this is not the
10968 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010969 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010970 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 || (fill_stl != fill_stlnc)))
10972 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010973 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 return '^';
10975 return '=';
10976}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978/*
10979 * Get the character to use in a separator between vertically split windows.
10980 * Get its attributes in "*attr".
10981 */
10982 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010983fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010985 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 if (*attr == 0 && fill_vert == ' ')
10987 return '|';
10988 else
10989 return fill_vert;
10990}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991
10992/*
10993 * Return TRUE if redrawing should currently be done.
10994 */
10995 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010996redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010998#ifdef FEAT_EVAL
10999 if (disable_redraw_for_testing)
11000 return 0;
11001 else
11002#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020011003 return ((!RedrawingDisabled
11004#ifdef FEAT_EVAL
11005 || ignore_redraw_flag_for_testing
11006#endif
11007 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008}
11009
11010/*
11011 * Return TRUE if printing messages should currently be done.
11012 */
11013 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011014messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015{
11016 return (!(p_lz && char_avail() && !KeyTyped));
11017}
11018
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011019#ifdef FEAT_MENU
11020/*
11021 * Draw the window toolbar.
11022 */
11023 static void
11024redraw_win_toolbar(win_T *wp)
11025{
11026 vimmenu_T *menu;
11027 int item_idx = 0;
11028 int item_count = 0;
11029 int col = 0;
11030 int next_col;
11031 int off = (int)(current_ScreenLine - ScreenLines);
11032 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
11033 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
11034
11035 vim_free(wp->w_winbar_items);
11036 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11037 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011038 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011039
11040 /* TODO: use fewer spaces if there is not enough room */
11041 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011042 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011043 {
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 if (col > 1)
11048 {
11049 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011050 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011051 break;
11052 }
11053
11054 wp->w_winbar_items[item_idx].wb_startcol = col;
11055 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011056 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011057 break;
11058
11059 next_col = text_to_screenline(wp, menu->name, col);
11060 while (col < next_col)
11061 {
11062 ScreenAttrs[off + col] = button_attr;
11063 ++col;
11064 }
11065 wp->w_winbar_items[item_idx].wb_endcol = col;
11066 wp->w_winbar_items[item_idx].wb_menu = menu;
11067 ++item_idx;
11068
Bram Moolenaar02631462017-09-22 15:20:32 +020011069 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011070 break;
11071 space_to_screenline(off + col, button_attr);
11072 ++col;
11073 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011074 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011075 {
11076 space_to_screenline(off + col, fill_attr);
11077 ++col;
11078 }
11079 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11080
Bram Moolenaar02631462017-09-22 15:20:32 +020011081 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011082 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011083}
11084#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011085
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086/*
11087 * Show current status info in ruler and various other places
11088 * If always is FALSE, only show ruler if position has changed.
11089 */
11090 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011091showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092{
11093 if (!always && !redrawing())
11094 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011095#ifdef FEAT_INS_EXPAND
11096 if (pum_visible())
11097 {
11098 /* Don't redraw right now, do it later. */
11099 curwin->w_redr_status = TRUE;
11100 return;
11101 }
11102#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011103#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011104 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011105 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 else
11107#endif
11108#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011109 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110#endif
11111
11112#ifdef FEAT_TITLE
11113 if (need_maketitle
11114# ifdef FEAT_STL_OPT
11115 || (p_icon && (stl_syntax & STL_IN_ICON))
11116 || (p_title && (stl_syntax & STL_IN_TITLE))
11117# endif
11118 )
11119 maketitle();
11120#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011121 /* Redraw the tab pages line if needed. */
11122 if (redraw_tabline)
11123 draw_tabline();
Bram Moolenaar988c4332019-06-02 14:12:11 +020011124
11125#ifdef FEAT_TEXT_PROP
11126 // Display popup windows on top of everything.
11127 update_popups();
11128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129}
11130
11131#ifdef FEAT_CMDL_INFO
11132 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011133win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011135#define RULER_BUF_LEN 70
11136 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 int row;
11138 int fillchar;
11139 int attr;
11140 int empty_line = FALSE;
11141 colnr_T virtcol;
11142 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011143 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 int this_ru_col;
11146 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011147 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148
11149 /* If 'ruler' off or redrawing disabled, don't do anything */
11150 if (!p_ru)
11151 return;
11152
11153 /*
11154 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11155 * after deleting lines, before cursor.lnum is corrected.
11156 */
11157 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11158 return;
11159
11160#ifdef FEAT_INS_EXPAND
11161 /* Don't draw the ruler while doing insert-completion, it might overwrite
11162 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164 if (edit_submode != NULL)
11165 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011166 // Don't draw the ruler when the popup menu is visible, it may overlap.
11167 // Except when the popup menu will be redrawn anyway.
11168 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011169 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170#endif
11171
11172#ifdef FEAT_STL_OPT
11173 if (*p_ruf)
11174 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011175 int save_called_emsg = called_emsg;
11176
11177 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011179 if (called_emsg)
11180 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011181 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011182 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 return;
11184 }
11185#endif
11186
11187 /*
11188 * Check if not in Insert mode and the line is empty (will show "0-1").
11189 */
11190 if (!(State & INSERT)
11191 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11192 empty_line = TRUE;
11193
11194 /*
11195 * Only draw the ruler when something changed.
11196 */
11197 validate_virtcol_win(wp);
11198 if ( redraw_cmdline
11199 || always
11200 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11201 || wp->w_cursor.col != wp->w_ru_cursor.col
11202 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 || wp->w_topline != wp->w_ru_topline
11205 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11206#ifdef FEAT_DIFF
11207 || wp->w_topfill != wp->w_ru_topfill
11208#endif
11209 || empty_line != wp->w_ru_empty)
11210 {
11211 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 if (wp->w_status_height)
11213 {
11214 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011215 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011216 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011217 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 }
11219 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 {
11221 row = Rows - 1;
11222 fillchar = ' ';
11223 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 width = Columns;
11225 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226 }
11227
11228 /* In list mode virtcol needs to be recomputed */
11229 virtcol = wp->w_virtcol;
11230 if (wp->w_p_list && lcs_tab1 == NUL)
11231 {
11232 wp->w_p_list = FALSE;
11233 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11234 wp->w_p_list = TRUE;
11235 }
11236
11237 /*
11238 * Some sprintfs return the length, some return a pointer.
11239 * To avoid portability problems we use strlen() here.
11240 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011241 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11243 ? 0L
11244 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011245 len = STRLEN(buffer);
11246 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11248 (int)virtcol + 1);
11249
11250 /*
11251 * Add a "50%" if there is room for it.
11252 * On the last line, don't print in the last column (scrolls the
11253 * screen up on some terminals).
11254 */
11255 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011256 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 this_ru_col = ru_col - (Columns - width);
11261 if (this_ru_col < 0)
11262 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 /* Never use more than half the window/screen width, leave the other
11264 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011265 if (this_ru_col < (width + 1) / 2)
11266 this_ru_col = (width + 1) / 2;
11267 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011269 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011270 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 if (has_mbyte)
11273 i += (*mb_char2bytes)(fillchar, buffer + i);
11274 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 buffer[i++] = fillchar;
11276 ++o;
11277 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011278 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 }
11280 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 if (has_mbyte)
11282 {
11283 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011284 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 {
11286 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011287 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288 {
11289 buffer[i] = NUL;
11290 break;
11291 }
11292 }
11293 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011294 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011295 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296
Bram Moolenaar4033c552017-09-16 20:54:51 +020011297 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 i = redraw_cmdline;
11299 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011300 this_ru_col + off + (int)STRLEN(buffer),
11301 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 fillchar, fillchar, attr);
11303 /* don't redraw the cmdline because of showing the ruler */
11304 redraw_cmdline = i;
11305 wp->w_ru_cursor = wp->w_cursor;
11306 wp->w_ru_virtcol = wp->w_virtcol;
11307 wp->w_ru_empty = empty_line;
11308 wp->w_ru_topline = wp->w_topline;
11309 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11310#ifdef FEAT_DIFF
11311 wp->w_ru_topfill = wp->w_topfill;
11312#endif
11313 }
11314}
11315#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011316
11317#if defined(FEAT_LINEBREAK) || defined(PROTO)
11318/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011319 * Return the width of the 'number' and 'relativenumber' column.
11320 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011321 * Otherwise it depends on 'numberwidth' and the line count.
11322 */
11323 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011324number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011325{
11326 int n;
11327 linenr_T lnum;
11328
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011329 if (wp->w_p_rnu && !wp->w_p_nu)
11330 /* cursor line shows "0" */
11331 lnum = wp->w_height;
11332 else
11333 /* cursor line shows absolute line number */
11334 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011335
Bram Moolenaar6b314672015-03-20 15:42:10 +010011336 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011337 return wp->w_nrwidth_width;
11338 wp->w_nrwidth_line_count = lnum;
11339
11340 n = 0;
11341 do
11342 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011343 lnum /= 10;
11344 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011345 } while (lnum > 0);
11346
11347 /* 'numberwidth' gives the minimal width plus one */
11348 if (n < wp->w_p_nuw - 1)
11349 n = wp->w_p_nuw - 1;
11350
11351 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011352 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011353 return n;
11354}
11355#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011356
Bram Moolenaar113e1072019-01-20 15:30:40 +010011357#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011358/*
11359 * Return the current cursor column. This is the actual position on the
11360 * screen. First column is 0.
11361 */
11362 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011363screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011364{
11365 return screen_cur_col;
11366}
11367
11368/*
11369 * Return the current cursor row. This is the actual position on the screen.
11370 * First row is 0.
11371 */
11372 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011373screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011374{
11375 return screen_cur_row;
11376}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011377#endif