blob: 37999bbf6bd9edc523c835ba3d731c28b6021bbb [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 {
Bram Moolenaar98fb65c2019-06-02 20:33:32 +0200648 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 {
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.
Bram Moolenaar3397f742019-06-02 18:40:06 +02001053 popup_visible = FALSE;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001054 popup_reset_handled();
1055 while ((wp = find_next_popup(TRUE)) != NULL)
1056 {
1057 // Recompute the position if the text changed.
1058 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1059 popup_adjust_position(wp);
1060
1061 // adjust w_winrow and w_wincol for border and padding, since
1062 // win_update() doesn't handle them.
1063 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1064 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1065 wp->w_winrow += top_off;
1066 wp->w_wincol += left_off;
1067
1068 // Draw the popup text.
1069 win_update(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +02001070 popup_visible = TRUE;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001071
1072 wp->w_winrow -= top_off;
1073 wp->w_wincol -= left_off;
1074
1075 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1076 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
1077 total_height = wp->w_popup_border[0] + wp->w_popup_padding[0]
1078 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1079 popup_attr = get_wcr_attr(wp);
1080
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001081 if (enc_utf8)
1082 {
Bram Moolenaar790498b2019-06-01 22:15:29 +02001083 border_char[0] = border_char[2] = 0x2550;
1084 border_char[1] = border_char[3] = 0x2551;
1085 border_char[4] = 0x2554;
1086 border_char[5] = 0x2557;
1087 border_char[6] = 0x255d;
1088 border_char[7] = 0x255a;
1089 }
1090 for (i = 0; i < 8; ++i)
1091 if (wp->w_border_char[i] != 0)
1092 border_char[i] = wp->w_border_char[i];
1093
1094 for (i = 0; i < 4; ++i)
1095 {
1096 border_attr[i] = popup_attr;
1097 if (wp->w_border_highlight[i] != NULL)
1098 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001099 }
1100
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001101 if (wp->w_popup_border[0] > 0)
1102 {
1103 // top border
1104 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1105 wp->w_wincol,
1106 wp->w_wincol + total_width,
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001107 wp->w_popup_border[3] != 0
Bram Moolenaar790498b2019-06-01 22:15:29 +02001108 ? border_char[4] : border_char[0],
1109 border_char[0], border_attr[0]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001110 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001111 {
1112 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1113 screen_puts(buf, wp->w_winrow,
1114 wp->w_wincol + total_width - 1, border_attr[1]);
1115 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001116 }
1117
1118 if (wp->w_popup_padding[0] > 0)
1119 {
1120 // top padding
1121 row = wp->w_winrow + wp->w_popup_border[0];
1122 screen_fill(row, row + wp->w_popup_padding[0],
1123 wp->w_wincol + wp->w_popup_border[3],
1124 wp->w_wincol + total_width - wp->w_popup_border[1],
1125 ' ', ' ', popup_attr);
1126 }
1127
1128 for (row = wp->w_winrow + wp->w_popup_border[0];
1129 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1130 ++row)
1131 {
1132 // left border
1133 if (wp->w_popup_border[3] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001134 {
1135 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1136 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1137 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001138 // left padding
1139 if (wp->w_popup_padding[3] > 0)
1140 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1141 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1142 // right border
1143 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001144 {
1145 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1146 screen_puts(buf, row,
1147 wp->w_wincol + total_width - 1, border_attr[1]);
1148 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001149 // right padding
1150 if (wp->w_popup_padding[1] > 0)
1151 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1152 wp->w_wincol + wp->w_popup_border[3]
1153 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1154 }
1155
1156 if (wp->w_popup_padding[2] > 0)
1157 {
1158 // bottom padding
1159 row = wp->w_winrow + wp->w_popup_border[0]
1160 + wp->w_popup_padding[0] + wp->w_height;
1161 screen_fill(row, row + wp->w_popup_padding[2],
1162 wp->w_wincol + wp->w_popup_border[3],
1163 wp->w_wincol + total_width - wp->w_popup_border[1],
1164 ' ', ' ', popup_attr);
1165 }
1166
1167 if (wp->w_popup_border[2] > 0)
1168 {
1169 // bottom border
1170 row = wp->w_winrow + total_height - 1;
1171 screen_fill(row , row + 1,
1172 wp->w_wincol,
1173 wp->w_wincol + total_width,
Bram Moolenaar790498b2019-06-01 22:15:29 +02001174 wp->w_popup_border[3] != 0
1175 ? border_char[7] : border_char[2],
1176 border_char[2], border_attr[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001177 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001178 {
1179 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1180 screen_puts(buf, row,
1181 wp->w_wincol + total_width - 1, border_attr[2]);
1182 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001183 }
1184 }
1185}
1186#endif
1187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188#if defined(FEAT_GUI) || defined(PROTO)
1189/*
1190 * Update a single window, its status line and maybe the command line msg.
1191 * Used for the GUI scrollbar.
1192 */
1193 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001194updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001196 /* return if already busy updating */
1197 if (updating_screen)
1198 return;
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 update_prepare();
1201
1202#ifdef FEAT_CLIPBOARD
1203 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001204 if (clip_star.available && clip_isautosel_star())
1205 clip_update_selection(&clip_star);
1206 if (clip_plus.available && clip_isautosel_plus())
1207 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001209
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001211
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001212 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001213 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001214 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (wp->w_redr_status
1217# ifdef FEAT_CMDL_INFO
1218 || p_ru
1219# endif
1220# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001221 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222# endif
1223 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001224 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaar988c4332019-06-02 14:12:11 +02001226#ifdef FEAT_TEXT_PROP
1227 // Display popup windows on top of everything.
1228 update_popups();
1229#endif
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 update_finish();
1232}
1233#endif
1234
1235/*
1236 * Update a single window.
1237 *
1238 * This may cause the windows below it also to be redrawn (when clearing the
1239 * screen or scrolling lines).
1240 *
1241 * How the window is redrawn depends on wp->w_redr_type. Each type also
1242 * implies the one below it.
1243 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001244 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1246 * INVERTED redraw the changed part of the Visual area
1247 * INVERTED_ALL redraw the whole Visual area
1248 * VALID 1. scroll up/down to adjust for a changed w_topline
1249 * 2. update lines at the top when scrolled down
1250 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001251 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 * b_mod_top and b_mod_bot.
1253 * - if wp->w_redraw_top non-zero, redraw lines between
1254 * wp->w_redraw_top and wp->w_redr_bot.
1255 * - continue redrawing when syntax status is invalid.
1256 * 4. if scrolled up, update lines at the bottom.
1257 * This results in three areas that may need updating:
1258 * top: from first row to top_end (when scrolled down)
1259 * mid: from mid_start to mid_end (update inversion or changed text)
1260 * bot: from bot_start to last row (when scrolled up)
1261 */
1262 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001263win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
1265 buf_T *buf = wp->w_buffer;
1266 int type;
1267 int top_end = 0; /* Below last row of the top area that needs
1268 updating. 0 when no top area updating. */
1269 int mid_start = 999;/* first row of the mid area that needs
1270 updating. 999 when no mid area updating. */
1271 int mid_end = 0; /* Below last row of the mid area that needs
1272 updating. 0 when no mid area updating. */
1273 int bot_start = 999;/* first row of the bot area that needs
1274 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 int scrolled_down = FALSE; /* TRUE when scrolled down when
1276 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001278 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 int top_to_mod = FALSE; /* redraw above mod_top */
1280#endif
1281
1282 int row; /* current window row to display */
1283 linenr_T lnum; /* current buffer lnum to display */
1284 int idx; /* current index in w_lines[] */
1285 int srow; /* starting row of the current line */
1286
1287 int eof = FALSE; /* if TRUE, we hit the end of the file */
1288 int didline = FALSE; /* if TRUE, we finished the last line */
1289 int i;
1290 long j;
1291 static int recursive = FALSE; /* being called recursively */
1292 int old_botline = wp->w_botline;
1293#ifdef FEAT_FOLDING
1294 long fold_count;
1295#endif
1296#ifdef FEAT_SYN_HL
1297 /* remember what happened to the previous line, to know if
1298 * check_visual_highlight() can be used */
1299#define DID_NONE 1 /* didn't update a line */
1300#define DID_LINE 2 /* updated a normal line */
1301#define DID_FOLD 3 /* updated a folded line */
1302 int did_update = DID_NONE;
1303 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1304#endif
1305 linenr_T mod_top = 0;
1306 linenr_T mod_bot = 0;
1307#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1308 int save_got_int;
1309#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001310#ifdef SYN_TIME_LIMIT
1311 proftime_T syntax_tm;
1312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
1314 type = wp->w_redr_type;
1315
1316 if (type == NOT_VALID)
1317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 wp->w_lines_valid = 0;
1320 }
1321
1322 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001323 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 {
1325 wp->w_redr_type = 0;
1326 return;
1327 }
1328
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 /* Window is zero-width: Only need to draw the separator. */
1330 if (wp->w_width == 0)
1331 {
1332 /* draw the vertical separator right of this window */
1333 draw_vsep_win(wp, 0);
1334 wp->w_redr_type = 0;
1335 return;
1336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001338#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001339 // If this window contains a terminal, redraw works completely differently.
1340 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001341 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001342 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001343# ifdef FEAT_MENU
1344 /* Draw the window toolbar, if there is one. */
1345 if (winbar_height(wp) > 0)
1346 redraw_win_toolbar(wp);
1347# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001348 wp->w_redr_type = 0;
1349 return;
1350 }
1351#endif
1352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001354 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355#endif
1356
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001357#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001358 /* Force redraw when width of 'number' or 'relativenumber' column
1359 * changes. */
1360 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001361 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001362 {
1363 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001364 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001365 }
1366 else
1367#endif
1368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1370 {
1371 /*
1372 * When there are both inserted/deleted lines and specific lines to be
1373 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1374 * everything (only happens when redrawing is off for while).
1375 */
1376 type = NOT_VALID;
1377 }
1378 else
1379 {
1380 /*
1381 * Set mod_top to the first line that needs displaying because of
1382 * changes. Set mod_bot to the first line after the changes.
1383 */
1384 mod_top = wp->w_redraw_top;
1385 if (wp->w_redraw_bot != 0)
1386 mod_bot = wp->w_redraw_bot + 1;
1387 else
1388 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (buf->b_mod_set)
1390 {
1391 if (mod_top == 0 || mod_top > buf->b_mod_top)
1392 {
1393 mod_top = buf->b_mod_top;
1394#ifdef FEAT_SYN_HL
1395 /* Need to redraw lines above the change that may be included
1396 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001397 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001399 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (mod_top < 1)
1401 mod_top = 1;
1402 }
1403#endif
1404 }
1405 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1406 mod_bot = buf->b_mod_bot;
1407
1408#ifdef FEAT_SEARCH_EXTRA
1409 /* When 'hlsearch' is on and using a multi-line search pattern, a
1410 * change in one line may make the Search highlighting in a
1411 * previous line invalid. Simple solution: redraw all visible
1412 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001413 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001415 if (search_hl.rm.regprog != NULL
1416 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001418 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001419 {
1420 cur = wp->w_match_head;
1421 while (cur != NULL)
1422 {
1423 if (cur->match.regprog != NULL
1424 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001425 {
1426 top_to_mod = TRUE;
1427 break;
1428 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001429 cur = cur->next;
1430 }
1431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432#endif
1433 }
1434#ifdef FEAT_FOLDING
1435 if (mod_top != 0 && hasAnyFolding(wp))
1436 {
1437 linenr_T lnumt, lnumb;
1438
1439 /*
1440 * A change in a line can cause lines above it to become folded or
1441 * unfolded. Find the top most buffer line that may be affected.
1442 * If the line was previously folded and displayed, get the first
1443 * line of that fold. If the line is folded now, get the first
1444 * folded line. Use the minimum of these two.
1445 */
1446
1447 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1448 * the line below it. If there is no valid entry, use w_topline.
1449 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1450 * to this line. If there is no valid entry, use MAXLNUM. */
1451 lnumt = wp->w_topline;
1452 lnumb = MAXLNUM;
1453 for (i = 0; i < wp->w_lines_valid; ++i)
1454 if (wp->w_lines[i].wl_valid)
1455 {
1456 if (wp->w_lines[i].wl_lastlnum < mod_top)
1457 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1458 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1459 {
1460 lnumb = wp->w_lines[i].wl_lnum;
1461 /* When there is a fold column it might need updating
1462 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001463 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 ++lnumb;
1465 }
1466 }
1467
1468 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1469 if (mod_top > lnumt)
1470 mod_top = lnumt;
1471
1472 /* Now do the same for the bottom line (one above mod_bot). */
1473 --mod_bot;
1474 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1475 ++mod_bot;
1476 if (mod_bot < lnumb)
1477 mod_bot = lnumb;
1478 }
1479#endif
1480
1481 /* When a change starts above w_topline and the end is below
1482 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001483 * If the end of the change is above w_topline: do like no change was
1484 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 if (mod_top != 0 && mod_top < wp->w_topline)
1486 {
1487 if (mod_bot > wp->w_topline)
1488 mod_top = wp->w_topline;
1489#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001490 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 top_end = 1;
1492#endif
1493 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001494
1495 /* When line numbers are displayed need to redraw all lines below
1496 * inserted/deleted lines. */
1497 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1498 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001500 wp->w_redraw_top = 0; // reset for next time
1501 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
1503 /*
1504 * When only displaying the lines at the top, set top_end. Used when
1505 * window has scrolled down for msg_scrolled.
1506 */
1507 if (type == REDRAW_TOP)
1508 {
1509 j = 0;
1510 for (i = 0; i < wp->w_lines_valid; ++i)
1511 {
1512 j += wp->w_lines[i].wl_size;
1513 if (j >= wp->w_upd_rows)
1514 {
1515 top_end = j;
1516 break;
1517 }
1518 }
1519 if (top_end == 0)
1520 /* not found (cannot happen?): redraw everything */
1521 type = NOT_VALID;
1522 else
1523 /* top area defined, the rest is VALID */
1524 type = VALID;
1525 }
1526
Bram Moolenaar367329b2007-08-30 11:53:22 +00001527 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001528 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1529 * non-zero and thus not FALSE) will indicate that screenclear() was not
1530 * called. */
1531 if (screen_cleared)
1532 screen_cleared = MAYBE;
1533
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 /*
1535 * If there are no changes on the screen that require a complete redraw,
1536 * handle three cases:
1537 * 1: we are off the top of the screen by a few lines: scroll down
1538 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1539 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1540 * w_lines[] that needs updating.
1541 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001542 if ((type == VALID || type == SOME_VALID
1543 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544#ifdef FEAT_DIFF
1545 && !wp->w_botfill && !wp->w_old_botfill
1546#endif
1547 )
1548 {
1549 if (mod_top != 0 && wp->w_topline == mod_top)
1550 {
1551 /*
1552 * w_topline is the first changed line, the scrolling will be done
1553 * further down.
1554 */
1555 }
1556 else if (wp->w_lines[0].wl_valid
1557 && (wp->w_topline < wp->w_lines[0].wl_lnum
1558#ifdef FEAT_DIFF
1559 || (wp->w_topline == wp->w_lines[0].wl_lnum
1560 && wp->w_topfill > wp->w_old_topfill)
1561#endif
1562 ))
1563 {
1564 /*
1565 * New topline is above old topline: May scroll down.
1566 */
1567#ifdef FEAT_FOLDING
1568 if (hasAnyFolding(wp))
1569 {
1570 linenr_T ln;
1571
1572 /* count the number of lines we are off, counting a sequence
1573 * of folded lines as one */
1574 j = 0;
1575 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1576 {
1577 ++j;
1578 if (j >= wp->w_height - 2)
1579 break;
1580 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1581 }
1582 }
1583 else
1584#endif
1585 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1586 if (j < wp->w_height - 2) /* not too far off */
1587 {
1588 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1589#ifdef FEAT_DIFF
1590 /* insert extra lines for previously invisible filler lines */
1591 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1592 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1593 - wp->w_old_topfill;
1594#endif
1595 if (i < wp->w_height - 2) /* less than a screen off */
1596 {
1597 /*
1598 * Try to insert the correct number of lines.
1599 * If not the last window, delete the lines at the bottom.
1600 * win_ins_lines may fail when the terminal can't do it.
1601 */
1602 if (i > 0)
1603 check_for_delay(FALSE);
1604 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1605 {
1606 if (wp->w_lines_valid != 0)
1607 {
1608 /* Need to update rows that are new, stop at the
1609 * first one that scrolled down. */
1610 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
1613 /* Move the entries that were scrolled, disable
1614 * the entries for the lines to be redrawn. */
1615 if ((wp->w_lines_valid += j) > wp->w_height)
1616 wp->w_lines_valid = wp->w_height;
1617 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1618 wp->w_lines[idx] = wp->w_lines[idx - j];
1619 while (idx >= 0)
1620 wp->w_lines[idx--].wl_valid = FALSE;
1621 }
1622 }
1623 else
1624 mid_start = 0; /* redraw all lines */
1625 }
1626 else
1627 mid_start = 0; /* redraw all lines */
1628 }
1629 else
1630 mid_start = 0; /* redraw all lines */
1631 }
1632 else
1633 {
1634 /*
1635 * New topline is at or below old topline: May scroll up.
1636 * When topline didn't change, find first entry in w_lines[] that
1637 * needs updating.
1638 */
1639
1640 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1641 j = -1;
1642 row = 0;
1643 for (i = 0; i < wp->w_lines_valid; i++)
1644 {
1645 if (wp->w_lines[i].wl_valid
1646 && wp->w_lines[i].wl_lnum == wp->w_topline)
1647 {
1648 j = i;
1649 break;
1650 }
1651 row += wp->w_lines[i].wl_size;
1652 }
1653 if (j == -1)
1654 {
1655 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1656 * lines */
1657 mid_start = 0;
1658 }
1659 else
1660 {
1661 /*
1662 * Try to delete the correct number of lines.
1663 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1664 */
1665#ifdef FEAT_DIFF
1666 /* If the topline didn't change, delete old filler lines,
1667 * otherwise delete filler lines of the new topline... */
1668 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1669 row += wp->w_old_topfill;
1670 else
1671 row += diff_check_fill(wp, wp->w_topline);
1672 /* ... but don't delete new filler lines. */
1673 row -= wp->w_topfill;
1674#endif
1675 if (row > 0)
1676 {
1677 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001678 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1679 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 bot_start = wp->w_height - row;
1681 else
1682 mid_start = 0; /* redraw all lines */
1683 }
1684 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1685 {
1686 /*
1687 * Skip the lines (below the deleted lines) that are still
1688 * valid and don't need redrawing. Copy their info
1689 * upwards, to compensate for the deleted lines. Set
1690 * bot_start to the first row that needs redrawing.
1691 */
1692 bot_start = 0;
1693 idx = 0;
1694 for (;;)
1695 {
1696 wp->w_lines[idx] = wp->w_lines[j];
1697 /* stop at line that didn't fit, unless it is still
1698 * valid (no lines deleted) */
1699 if (row > 0 && bot_start + row
1700 + (int)wp->w_lines[j].wl_size > wp->w_height)
1701 {
1702 wp->w_lines_valid = idx + 1;
1703 break;
1704 }
1705 bot_start += wp->w_lines[idx++].wl_size;
1706
1707 /* stop at the last valid entry in w_lines[].wl_size */
1708 if (++j >= wp->w_lines_valid)
1709 {
1710 wp->w_lines_valid = idx;
1711 break;
1712 }
1713 }
1714#ifdef FEAT_DIFF
1715 /* Correct the first entry for filler lines at the top
1716 * when it won't get updated below. */
1717 if (wp->w_p_diff && bot_start > 0)
1718 wp->w_lines[0].wl_size =
1719 plines_win_nofill(wp, wp->w_topline, TRUE)
1720 + wp->w_topfill;
1721#endif
1722 }
1723 }
1724 }
1725
1726 /* When starting redraw in the first line, redraw all lines. When
1727 * there is only one window it's probably faster to clear the screen
1728 * first. */
1729 if (mid_start == 0)
1730 {
1731 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001732 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001733 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001734 /* Clear the screen when it was not done by win_del_lines() or
1735 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1736 * then. */
1737 if (screen_cleared != TRUE)
1738 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001739 /* The screen was cleared, redraw the tab pages line. */
1740 if (redraw_tabline)
1741 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001744
1745 /* When win_del_lines() or win_ins_lines() caused the screen to be
1746 * cleared (only happens for the first window) or when screenclear()
1747 * was called directly above, "must_redraw" will have been set to
1748 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1749 if (screen_cleared == TRUE)
1750 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752 else
1753 {
1754 /* Not VALID or INVERTED: redraw all lines. */
1755 mid_start = 0;
1756 mid_end = wp->w_height;
1757 }
1758
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001759 if (type == SOME_VALID)
1760 {
1761 /* SOME_VALID: redraw all lines. */
1762 mid_start = 0;
1763 mid_end = wp->w_height;
1764 type = NOT_VALID;
1765 }
1766
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 /* check if we are updating or removing the inverted part */
1768 if ((VIsual_active && buf == curwin->w_buffer)
1769 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1770 {
1771 linenr_T from, to;
1772
1773 if (VIsual_active)
1774 {
1775 if (VIsual_active
1776 && (VIsual_mode != wp->w_old_visual_mode
1777 || type == INVERTED_ALL))
1778 {
1779 /*
1780 * If the type of Visual selection changed, redraw the whole
1781 * selection. Also when the ownership of the X selection is
1782 * gained or lost.
1783 */
1784 if (curwin->w_cursor.lnum < VIsual.lnum)
1785 {
1786 from = curwin->w_cursor.lnum;
1787 to = VIsual.lnum;
1788 }
1789 else
1790 {
1791 from = VIsual.lnum;
1792 to = curwin->w_cursor.lnum;
1793 }
1794 /* redraw more when the cursor moved as well */
1795 if (wp->w_old_cursor_lnum < from)
1796 from = wp->w_old_cursor_lnum;
1797 if (wp->w_old_cursor_lnum > to)
1798 to = wp->w_old_cursor_lnum;
1799 if (wp->w_old_visual_lnum < from)
1800 from = wp->w_old_visual_lnum;
1801 if (wp->w_old_visual_lnum > to)
1802 to = wp->w_old_visual_lnum;
1803 }
1804 else
1805 {
1806 /*
1807 * Find the line numbers that need to be updated: The lines
1808 * between the old cursor position and the current cursor
1809 * position. Also check if the Visual position changed.
1810 */
1811 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1812 {
1813 from = curwin->w_cursor.lnum;
1814 to = wp->w_old_cursor_lnum;
1815 }
1816 else
1817 {
1818 from = wp->w_old_cursor_lnum;
1819 to = curwin->w_cursor.lnum;
1820 if (from == 0) /* Visual mode just started */
1821 from = to;
1822 }
1823
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001824 if (VIsual.lnum != wp->w_old_visual_lnum
1825 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 {
1827 if (wp->w_old_visual_lnum < from
1828 && wp->w_old_visual_lnum != 0)
1829 from = wp->w_old_visual_lnum;
1830 if (wp->w_old_visual_lnum > to)
1831 to = wp->w_old_visual_lnum;
1832 if (VIsual.lnum < from)
1833 from = VIsual.lnum;
1834 if (VIsual.lnum > to)
1835 to = VIsual.lnum;
1836 }
1837 }
1838
1839 /*
1840 * If in block mode and changed column or curwin->w_curswant:
1841 * update all lines.
1842 * First compute the actual start and end column.
1843 */
1844 if (VIsual_mode == Ctrl_V)
1845 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001846 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001847#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001848 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
Bram Moolenaar404406a2014-10-09 13:24:43 +02001850 if (curwin->w_p_lbr)
1851 ve_flags = VE_ALL;
1852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001854#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001855 ve_flags = save_ve_flags;
1856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 ++toc;
1858 if (curwin->w_curswant == MAXCOL)
1859 toc = MAXCOL;
1860
1861 if (fromc != wp->w_old_cursor_fcol
1862 || toc != wp->w_old_cursor_lcol)
1863 {
1864 if (from > VIsual.lnum)
1865 from = VIsual.lnum;
1866 if (to < VIsual.lnum)
1867 to = VIsual.lnum;
1868 }
1869 wp->w_old_cursor_fcol = fromc;
1870 wp->w_old_cursor_lcol = toc;
1871 }
1872 }
1873 else
1874 {
1875 /* Use the line numbers of the old Visual area. */
1876 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1877 {
1878 from = wp->w_old_cursor_lnum;
1879 to = wp->w_old_visual_lnum;
1880 }
1881 else
1882 {
1883 from = wp->w_old_visual_lnum;
1884 to = wp->w_old_cursor_lnum;
1885 }
1886 }
1887
1888 /*
1889 * There is no need to update lines above the top of the window.
1890 */
1891 if (from < wp->w_topline)
1892 from = wp->w_topline;
1893
1894 /*
1895 * If we know the value of w_botline, use it to restrict the update to
1896 * the lines that are visible in the window.
1897 */
1898 if (wp->w_valid & VALID_BOTLINE)
1899 {
1900 if (from >= wp->w_botline)
1901 from = wp->w_botline - 1;
1902 if (to >= wp->w_botline)
1903 to = wp->w_botline - 1;
1904 }
1905
1906 /*
1907 * Find the minimal part to be updated.
1908 * Watch out for scrolling that made entries in w_lines[] invalid.
1909 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1910 * top_end; need to redraw from top_end to the "to" line.
1911 * A middle mouse click with a Visual selection may change the text
1912 * above the Visual area and reset wl_valid, do count these for
1913 * mid_end (in srow).
1914 */
1915 if (mid_start > 0)
1916 {
1917 lnum = wp->w_topline;
1918 idx = 0;
1919 srow = 0;
1920 if (scrolled_down)
1921 mid_start = top_end;
1922 else
1923 mid_start = 0;
1924 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1925 {
1926 if (wp->w_lines[idx].wl_valid)
1927 mid_start += wp->w_lines[idx].wl_size;
1928 else if (!scrolled_down)
1929 srow += wp->w_lines[idx].wl_size;
1930 ++idx;
1931# ifdef FEAT_FOLDING
1932 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1933 lnum = wp->w_lines[idx].wl_lnum;
1934 else
1935# endif
1936 ++lnum;
1937 }
1938 srow += mid_start;
1939 mid_end = wp->w_height;
1940 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1941 {
1942 if (wp->w_lines[idx].wl_valid
1943 && wp->w_lines[idx].wl_lnum >= to + 1)
1944 {
1945 /* Only update until first row of this line */
1946 mid_end = srow;
1947 break;
1948 }
1949 srow += wp->w_lines[idx].wl_size;
1950 }
1951 }
1952 }
1953
1954 if (VIsual_active && buf == curwin->w_buffer)
1955 {
1956 wp->w_old_visual_mode = VIsual_mode;
1957 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1958 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001959 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 wp->w_old_curswant = curwin->w_curswant;
1961 }
1962 else
1963 {
1964 wp->w_old_visual_mode = 0;
1965 wp->w_old_cursor_lnum = 0;
1966 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001967 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969
1970#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1971 /* reset got_int, otherwise regexp won't work */
1972 save_got_int = got_int;
1973 got_int = 0;
1974#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001975#ifdef SYN_TIME_LIMIT
1976 /* Set the time limit to 'redrawtime'. */
1977 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001978 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980#ifdef FEAT_FOLDING
1981 win_foldinfo.fi_level = 0;
1982#endif
1983
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001984#ifdef FEAT_MENU
1985 /*
1986 * Draw the window toolbar, if there is one.
1987 * TODO: only when needed.
1988 */
1989 if (winbar_height(wp) > 0)
1990 redraw_win_toolbar(wp);
1991#endif
1992
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 /*
1994 * Update all the window rows.
1995 */
1996 idx = 0; /* first entry in w_lines[].wl_size */
1997 row = 0;
1998 srow = 0;
1999 lnum = wp->w_topline; /* first line shown in window */
2000 for (;;)
2001 {
2002 /* stop updating when reached the end of the window (check for _past_
2003 * the end of the window is at the end of the loop) */
2004 if (row == wp->w_height)
2005 {
2006 didline = TRUE;
2007 break;
2008 }
2009
2010 /* stop updating when hit the end of the file */
2011 if (lnum > buf->b_ml.ml_line_count)
2012 {
2013 eof = TRUE;
2014 break;
2015 }
2016
2017 /* Remember the starting row of the line that is going to be dealt
2018 * with. It is used further down when the line doesn't fit. */
2019 srow = row;
2020
2021 /*
2022 * Update a line when it is in an area that needs updating, when it
2023 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002024 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 * win_del_lines(), check if the current line includes it.
2026 * When syntax folding is being used, the saved syntax states will
2027 * already have been updated, we can't see where the syntax state is
2028 * the same again, just update until the end of the window.
2029 */
2030 if (row < top_end
2031 || (row >= mid_start && row < mid_end)
2032#ifdef FEAT_SEARCH_EXTRA
2033 || top_to_mod
2034#endif
2035 || idx >= wp->w_lines_valid
2036 || (row + wp->w_lines[idx].wl_size > bot_start)
2037 || (mod_top != 0
2038 && (lnum == mod_top
2039 || (lnum >= mod_top
2040 && (lnum < mod_bot
2041#ifdef FEAT_SYN_HL
2042 || did_update == DID_FOLD
2043 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02002044 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 && (
2046# ifdef FEAT_FOLDING
2047 (foldmethodIsSyntax(wp)
2048 && hasAnyFolding(wp)) ||
2049# endif
2050 syntax_check_changed(lnum)))
2051#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002052#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02002053 /* match in fixed position might need redraw
2054 * if lines were inserted or deleted */
2055 || (wp->w_match_head != NULL
2056 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 )))))
2059 {
2060#ifdef FEAT_SEARCH_EXTRA
2061 if (lnum == mod_top)
2062 top_to_mod = FALSE;
2063#endif
2064
2065 /*
2066 * When at start of changed lines: May scroll following lines
2067 * up or down to minimize redrawing.
2068 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002069 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 */
2071 if (lnum == mod_top
2072 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002073 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
2075 int old_rows = 0;
2076 int new_rows = 0;
2077 int xtra_rows;
2078 linenr_T l;
2079
2080 /* Count the old number of window rows, using w_lines[], which
2081 * should still contain the sizes for the lines as they are
2082 * currently displayed. */
2083 for (i = idx; i < wp->w_lines_valid; ++i)
2084 {
2085 /* Only valid lines have a meaningful wl_lnum. Invalid
2086 * lines are part of the changed area. */
2087 if (wp->w_lines[i].wl_valid
2088 && wp->w_lines[i].wl_lnum == mod_bot)
2089 break;
2090 old_rows += wp->w_lines[i].wl_size;
2091#ifdef FEAT_FOLDING
2092 if (wp->w_lines[i].wl_valid
2093 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2094 {
2095 /* Must have found the last valid entry above mod_bot.
2096 * Add following invalid entries. */
2097 ++i;
2098 while (i < wp->w_lines_valid
2099 && !wp->w_lines[i].wl_valid)
2100 old_rows += wp->w_lines[i++].wl_size;
2101 break;
2102 }
2103#endif
2104 }
2105
2106 if (i >= wp->w_lines_valid)
2107 {
2108 /* We can't find a valid line below the changed lines,
2109 * need to redraw until the end of the window.
2110 * Inserting/deleting lines has no use. */
2111 bot_start = 0;
2112 }
2113 else
2114 {
2115 /* Able to count old number of rows: Count new window
2116 * rows, and may insert/delete lines */
2117 j = idx;
2118 for (l = lnum; l < mod_bot; ++l)
2119 {
2120#ifdef FEAT_FOLDING
2121 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2122 ++new_rows;
2123 else
2124#endif
2125#ifdef FEAT_DIFF
2126 if (l == wp->w_topline)
2127 new_rows += plines_win_nofill(wp, l, TRUE)
2128 + wp->w_topfill;
2129 else
2130#endif
2131 new_rows += plines_win(wp, l, TRUE);
2132 ++j;
2133 if (new_rows > wp->w_height - row - 2)
2134 {
2135 /* it's getting too much, must redraw the rest */
2136 new_rows = 9999;
2137 break;
2138 }
2139 }
2140 xtra_rows = new_rows - old_rows;
2141 if (xtra_rows < 0)
2142 {
2143 /* May scroll text up. If there is not enough
2144 * remaining text or scrolling fails, must redraw the
2145 * rest. If scrolling works, must redraw the text
2146 * below the scrolled text. */
2147 if (row - xtra_rows >= wp->w_height - 2)
2148 mod_bot = MAXLNUM;
2149 else
2150 {
2151 check_for_delay(FALSE);
2152 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002153 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 mod_bot = MAXLNUM;
2155 else
2156 bot_start = wp->w_height + xtra_rows;
2157 }
2158 }
2159 else if (xtra_rows > 0)
2160 {
2161 /* May scroll text down. If there is not enough
2162 * remaining text of scrolling fails, must redraw the
2163 * rest. */
2164 if (row + xtra_rows >= wp->w_height - 2)
2165 mod_bot = MAXLNUM;
2166 else
2167 {
2168 check_for_delay(FALSE);
2169 if (win_ins_lines(wp, row + old_rows,
2170 xtra_rows, FALSE, FALSE) == FAIL)
2171 mod_bot = MAXLNUM;
2172 else if (top_end > row + old_rows)
2173 /* Scrolled the part at the top that requires
2174 * updating down. */
2175 top_end += xtra_rows;
2176 }
2177 }
2178
2179 /* When not updating the rest, may need to move w_lines[]
2180 * entries. */
2181 if (mod_bot != MAXLNUM && i != j)
2182 {
2183 if (j < i)
2184 {
2185 int x = row + new_rows;
2186
2187 /* move entries in w_lines[] upwards */
2188 for (;;)
2189 {
2190 /* stop at last valid entry in w_lines[] */
2191 if (i >= wp->w_lines_valid)
2192 {
2193 wp->w_lines_valid = j;
2194 break;
2195 }
2196 wp->w_lines[j] = wp->w_lines[i];
2197 /* stop at a line that won't fit */
2198 if (x + (int)wp->w_lines[j].wl_size
2199 > wp->w_height)
2200 {
2201 wp->w_lines_valid = j + 1;
2202 break;
2203 }
2204 x += wp->w_lines[j++].wl_size;
2205 ++i;
2206 }
2207 if (bot_start > x)
2208 bot_start = x;
2209 }
2210 else /* j > i */
2211 {
2212 /* move entries in w_lines[] downwards */
2213 j -= i;
2214 wp->w_lines_valid += j;
2215 if (wp->w_lines_valid > wp->w_height)
2216 wp->w_lines_valid = wp->w_height;
2217 for (i = wp->w_lines_valid; i - j >= idx; --i)
2218 wp->w_lines[i] = wp->w_lines[i - j];
2219
2220 /* The w_lines[] entries for inserted lines are
2221 * now invalid, but wl_size may be used above.
2222 * Reset to zero. */
2223 while (i >= idx)
2224 {
2225 wp->w_lines[i].wl_size = 0;
2226 wp->w_lines[i--].wl_valid = FALSE;
2227 }
2228 }
2229 }
2230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232
2233#ifdef FEAT_FOLDING
2234 /*
2235 * When lines are folded, display one line for all of them.
2236 * Otherwise, display normally (can be several display lines when
2237 * 'wrap' is on).
2238 */
2239 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2240 if (fold_count != 0)
2241 {
2242 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2243 ++row;
2244 --fold_count;
2245 wp->w_lines[idx].wl_folded = TRUE;
2246 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2247# ifdef FEAT_SYN_HL
2248 did_update = DID_FOLD;
2249# endif
2250 }
2251 else
2252#endif
2253 if (idx < wp->w_lines_valid
2254 && wp->w_lines[idx].wl_valid
2255 && wp->w_lines[idx].wl_lnum == lnum
2256 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002257 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar17146962019-05-30 00:12:11 +02002258#ifdef FEAT_TEXT_PROP
2259 && !bt_popup(wp->w_buffer)
2260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 && srow + wp->w_lines[idx].wl_size > wp->w_height
2262#ifdef FEAT_DIFF
2263 && diff_check_fill(wp, lnum) == 0
2264#endif
2265 )
2266 {
2267 /* This line is not going to fit. Don't draw anything here,
2268 * will draw "@ " lines below. */
2269 row = wp->w_height + 1;
2270 }
2271 else
2272 {
2273#ifdef FEAT_SEARCH_EXTRA
2274 prepare_search_hl(wp, lnum);
2275#endif
2276#ifdef FEAT_SYN_HL
2277 /* Let the syntax stuff know we skipped a few lines. */
2278 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002279 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 syntax_end_parsing(syntax_last_parsed + 1);
2281#endif
2282
2283 /*
2284 * Display one line.
2285 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002286 row = win_line(wp, lnum, srow, wp->w_height,
2287 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
2289#ifdef FEAT_FOLDING
2290 wp->w_lines[idx].wl_folded = FALSE;
2291 wp->w_lines[idx].wl_lastlnum = lnum;
2292#endif
2293#ifdef FEAT_SYN_HL
2294 did_update = DID_LINE;
2295 syntax_last_parsed = lnum;
2296#endif
2297 }
2298
2299 wp->w_lines[idx].wl_lnum = lnum;
2300 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002301
2302 /* Past end of the window or end of the screen. Note that after
2303 * resizing wp->w_height may be end up too big. That's a problem
2304 * elsewhere, but prevent a crash here. */
2305 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
2307 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002308 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2310 ++idx;
2311 break;
2312 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002313 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 wp->w_lines[idx].wl_size = row - srow;
2315 ++idx;
2316#ifdef FEAT_FOLDING
2317 lnum += fold_count + 1;
2318#else
2319 ++lnum;
2320#endif
2321 }
2322 else
2323 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002324 if (wp->w_p_rnu)
2325 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002326#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002327 // 'relativenumber' set: The text doesn't need to be drawn, but
2328 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002329 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2330 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002331 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002332 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002333#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002334 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002335 }
2336
2337 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 row += wp->w_lines[idx++].wl_size;
2339 if (row > wp->w_height) /* past end of screen */
2340 break;
2341#ifdef FEAT_FOLDING
2342 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2343#else
2344 ++lnum;
2345#endif
2346#ifdef FEAT_SYN_HL
2347 did_update = DID_NONE;
2348#endif
2349 }
2350
2351 if (lnum > buf->b_ml.ml_line_count)
2352 {
2353 eof = TRUE;
2354 break;
2355 }
2356 }
2357 /*
2358 * End of loop over all window lines.
2359 */
2360
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002361#ifdef FEAT_VTP
2362 /* Rewrite the character at the end of the screen line. */
2363 if (use_vtp())
2364 {
2365 int i;
2366
2367 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002368 if (enc_utf8)
2369 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2370 LineOffset[i] + screen_Columns) > 1)
2371 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2372 else
2373 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2374 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002375 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2376 }
2377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379 if (idx > wp->w_lines_valid)
2380 wp->w_lines_valid = idx;
2381
2382#ifdef FEAT_SYN_HL
2383 /*
2384 * Let the syntax stuff know we stop parsing here.
2385 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002386 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 syntax_end_parsing(syntax_last_parsed + 1);
2388#endif
2389
2390 /*
2391 * If we didn't hit the end of the file, and we didn't finish the last
2392 * line we were working on, then the line didn't fit.
2393 */
2394 wp->w_empty_rows = 0;
2395#ifdef FEAT_DIFF
2396 wp->w_filler_rows = 0;
2397#endif
2398 if (!eof && !didline)
2399 {
2400 if (lnum == wp->w_topline)
2401 {
2402 /*
2403 * Single line that does not fit!
2404 * Don't overwrite it, it can be edited.
2405 */
2406 wp->w_botline = lnum + 1;
2407 }
2408#ifdef FEAT_DIFF
2409 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2410 {
2411 /* Window ends in filler lines. */
2412 wp->w_botline = lnum;
2413 wp->w_filler_rows = wp->w_height - srow;
2414 }
2415#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002416#ifdef FEAT_TEXT_PROP
2417 else if (bt_popup(wp->w_buffer))
2418 {
2419 // popup line that doesn't fit is left as-is
2420 wp->w_botline = lnum;
2421 }
2422#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002423 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2424 {
2425 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2426
2427 /*
2428 * Last line isn't finished: Display "@@@" in the last screen line.
2429 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002430 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002431 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002432 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002433 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002434 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002435 set_empty_rows(wp, srow);
2436 wp->w_botline = lnum;
2437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2439 {
2440 /*
2441 * Last line isn't finished: Display "@@@" at the end.
2442 */
2443 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2444 W_WINROW(wp) + wp->w_height,
2445 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002446 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 set_empty_rows(wp, srow);
2448 wp->w_botline = lnum;
2449 }
2450 else
2451 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002452 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 wp->w_botline = lnum;
2454 }
2455 }
2456 else
2457 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 if (eof) /* we hit the end of the file */
2460 {
2461 wp->w_botline = buf->b_ml.ml_line_count + 1;
2462#ifdef FEAT_DIFF
2463 j = diff_check_fill(wp, wp->w_botline);
2464 if (j > 0 && !wp->w_botfill)
2465 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002466 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (char2cells(fill_diff) > 1)
2468 i = '-';
2469 else
2470 i = fill_diff;
2471 if (row + j > wp->w_height)
2472 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002473 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 row += j;
2475 }
2476#endif
2477 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002478 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 wp->w_botline = lnum;
2480
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002481 // Make sure the rest of the screen is blank
2482 // put '~'s on rows that aren't part of the file.
Bram Moolenaar17146962019-05-30 00:12:11 +02002483 win_draw_end(wp,
2484#ifdef FEAT_TEXT_PROP
2485 bt_popup(wp->w_buffer) ? ' ' :
2486#endif
2487 '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
2489
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002490#ifdef SYN_TIME_LIMIT
2491 syn_set_timeout(NULL);
2492#endif
2493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 /* Reset the type of redrawing required, the window has been updated. */
2495 wp->w_redr_type = 0;
2496#ifdef FEAT_DIFF
2497 wp->w_old_topfill = wp->w_topfill;
2498 wp->w_old_botfill = wp->w_botfill;
2499#endif
2500
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002501 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
2503 /*
2504 * There is a trick with w_botline. If we invalidate it on each
2505 * change that might modify it, this will cause a lot of expensive
2506 * calls to plines() in update_topline() each time. Therefore the
2507 * value of w_botline is often approximated, and this value is used to
2508 * compute the value of w_topline. If the value of w_botline was
2509 * wrong, check that the value of w_topline is correct (cursor is on
2510 * the visible part of the text). If it's not, we need to redraw
2511 * again. Mostly this just means scrolling up a few lines, so it
2512 * doesn't look too bad. Only do this for the current window (where
2513 * changes are relevant).
2514 */
2515 wp->w_valid |= VALID_BOTLINE;
2516 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2517 {
2518 recursive = TRUE;
2519 curwin->w_valid &= ~VALID_TOPLINE;
2520 update_topline(); /* may invalidate w_botline again */
2521 if (must_redraw != 0)
2522 {
2523 /* Don't update for changes in buffer again. */
2524 i = curbuf->b_mod_set;
2525 curbuf->b_mod_set = FALSE;
2526 win_update(curwin);
2527 must_redraw = 0;
2528 curbuf->b_mod_set = i;
2529 }
2530 recursive = FALSE;
2531 }
2532 }
2533
2534#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2535 /* restore got_int, unless CTRL-C was hit while redrawing */
2536 if (!got_int)
2537 got_int = save_got_int;
2538#endif
2539}
2540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002542 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2543 * Return the new offset.
2544 */
2545 static int
2546screen_fill_end(
2547 win_T *wp,
2548 int c1,
2549 int c2,
2550 int off,
2551 int width,
2552 int row,
2553 int endrow,
2554 int attr)
2555{
2556 int nn = off + width;
2557
2558 if (nn > wp->w_width)
2559 nn = wp->w_width;
2560#ifdef FEAT_RIGHTLEFT
2561 if (wp->w_p_rl)
2562 {
2563 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2564 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2565 c1, c2, attr);
2566 }
2567 else
2568#endif
2569 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2570 wp->w_wincol + off, (int)wp->w_wincol + nn,
2571 c1, c2, attr);
2572 return nn;
2573}
2574
2575/*
2576 * Clear lines near the end the window and mark the unused lines with "c1".
2577 * use "c2" as the filler character.
2578 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 */
2580 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002581win_draw_end(
2582 win_T *wp,
2583 int c1,
2584 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002585 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002586 int row,
2587 int endrow,
2588 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002591 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002592 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002593
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002594 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002595
2596 if (draw_margin)
2597 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002598#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002599 int fdc = compute_foldcolumn(wp, 0);
2600
2601 if (fdc > 0)
2602 // draw the fold column
2603 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002604 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002605#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002606#ifdef FEAT_SIGNS
2607 if (signcolumn_on(wp))
2608 // draw the sign column
2609 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002610 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002611#endif
2612 if ((wp->w_p_nu || wp->w_p_rnu)
2613 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2614 // draw the number column
2615 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002616 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619#ifdef FEAT_RIGHTLEFT
2620 if (wp->w_p_rl)
2621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002623 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002624 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002626 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002627 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
2629 else
2630#endif
2631 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002633 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002634 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 set_empty_rows(wp, row);
2638}
2639
Bram Moolenaar1a384422010-07-14 19:53:30 +02002640#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002641/*
2642 * Advance **color_cols and return TRUE when there are columns to draw.
2643 */
2644 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002645advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002646{
2647 while (**color_cols >= 0 && vcol > **color_cols)
2648 ++*color_cols;
2649 return (**color_cols >= 0);
2650}
2651#endif
2652
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002653#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2654/*
2655 * Copy "text" to ScreenLines using "attr".
2656 * Returns the next screen column.
2657 */
2658 static int
2659text_to_screenline(win_T *wp, char_u *text, int col)
2660{
2661 int off = (int)(current_ScreenLine - ScreenLines);
2662
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002663 if (has_mbyte)
2664 {
2665 int cells;
2666 int u8c, u8cc[MAX_MCO];
2667 int i;
2668 int idx;
2669 int c_len;
2670 char_u *p;
2671# ifdef FEAT_ARABIC
2672 int prev_c = 0; /* previous Arabic character */
2673 int prev_c1 = 0; /* first composing char for prev_c */
2674# endif
2675
2676# ifdef FEAT_RIGHTLEFT
2677 if (wp->w_p_rl)
2678 idx = off;
2679 else
2680# endif
2681 idx = off + col;
2682
2683 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2684 for (p = text; *p != NUL; )
2685 {
2686 cells = (*mb_ptr2cells)(p);
2687 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002688 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002689# ifdef FEAT_RIGHTLEFT
2690 - (wp->w_p_rl ? col : 0)
2691# endif
2692 )
2693 break;
2694 ScreenLines[idx] = *p;
2695 if (enc_utf8)
2696 {
2697 u8c = utfc_ptr2char(p, u8cc);
2698 if (*p < 0x80 && u8cc[0] == 0)
2699 {
2700 ScreenLinesUC[idx] = 0;
2701#ifdef FEAT_ARABIC
2702 prev_c = u8c;
2703#endif
2704 }
2705 else
2706 {
2707#ifdef FEAT_ARABIC
2708 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2709 {
2710 /* Do Arabic shaping. */
2711 int pc, pc1, nc;
2712 int pcc[MAX_MCO];
2713 int firstbyte = *p;
2714
2715 /* The idea of what is the previous and next
2716 * character depends on 'rightleft'. */
2717 if (wp->w_p_rl)
2718 {
2719 pc = prev_c;
2720 pc1 = prev_c1;
2721 nc = utf_ptr2char(p + c_len);
2722 prev_c1 = u8cc[0];
2723 }
2724 else
2725 {
2726 pc = utfc_ptr2char(p + c_len, pcc);
2727 nc = prev_c;
2728 pc1 = pcc[0];
2729 }
2730 prev_c = u8c;
2731
2732 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2733 pc, pc1, nc);
2734 ScreenLines[idx] = firstbyte;
2735 }
2736 else
2737 prev_c = u8c;
2738#endif
2739 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002740 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002741 for (i = 0; i < Screen_mco; ++i)
2742 {
2743 ScreenLinesC[i][idx] = u8cc[i];
2744 if (u8cc[i] == 0)
2745 break;
2746 }
2747 }
2748 if (cells > 1)
2749 ScreenLines[idx + 1] = 0;
2750 }
2751 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2752 /* double-byte single width character */
2753 ScreenLines2[idx] = p[1];
2754 else if (cells > 1)
2755 /* double-width character */
2756 ScreenLines[idx + 1] = p[1];
2757 col += cells;
2758 idx += cells;
2759 p += c_len;
2760 }
2761 }
2762 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002763 {
2764 int len = (int)STRLEN(text);
2765
Bram Moolenaar02631462017-09-22 15:20:32 +02002766 if (len > wp->w_width - col)
2767 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002768 if (len > 0)
2769 {
2770#ifdef FEAT_RIGHTLEFT
2771 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002772 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002773 else
2774#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002775 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002776 col += len;
2777 }
2778 }
2779 return col;
2780}
2781#endif
2782
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783#ifdef FEAT_FOLDING
2784/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002785 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2786 * space is available for window "wp", minus "col".
2787 */
2788 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002789compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002790{
2791 int fdc = wp->w_p_fdc;
2792 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002793 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002794
2795 if (fdc > wwidth - (col + wmw))
2796 fdc = wwidth - (col + wmw);
2797 return fdc;
2798}
2799
2800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 * Display one folded line.
2802 */
2803 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002804fold_line(
2805 win_T *wp,
2806 long fold_count,
2807 foldinfo_T *foldinfo,
2808 linenr_T lnum,
2809 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002811 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 pos_T *top, *bot;
2813 linenr_T lnume = lnum + fold_count - 1;
2814 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002815 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 int col;
2818 int txtcol;
2819 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002820 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821
2822 /* Build the fold line:
2823 * 1. Add the cmdwin_type for the command-line window
2824 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002825 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 * 4. Compose the text
2827 * 5. Add the text
2828 * 6. set highlighting for the Visual area an other text
2829 */
2830 col = 0;
2831
2832 /*
2833 * 1. Add the cmdwin_type for the command-line window
2834 * Ignores 'rightleft', this window is never right-left.
2835 */
2836#ifdef FEAT_CMDWIN
2837 if (cmdwin_type != 0 && wp == curwin)
2838 {
2839 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002840 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 if (enc_utf8)
2842 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 ++col;
2844 }
2845#endif
2846
2847 /*
2848 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002849 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002851 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 if (fdc > 0)
2853 {
2854 fill_foldcolumn(buf, wp, TRUE, lnum);
2855#ifdef FEAT_RIGHTLEFT
2856 if (wp->w_p_rl)
2857 {
2858 int i;
2859
Bram Moolenaar02631462017-09-22 15:20:32 +02002860 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002861 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 /* reverse the fold column */
2863 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002864 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
2866 else
2867#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002868 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 col += fdc;
2870 }
2871
2872#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002873# define RL_MEMSET(p, v, l) \
2874 do { \
2875 if (wp->w_p_rl) \
2876 for (ri = 0; ri < l; ++ri) \
2877 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2878 else \
2879 for (ri = 0; ri < l; ++ri) \
2880 ScreenAttrs[off + (p) + ri] = v; \
2881 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002883# define RL_MEMSET(p, v, l) \
2884 do { \
2885 for (ri = 0; ri < l; ++ri) \
2886 ScreenAttrs[off + (p) + ri] = v; \
2887 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888#endif
2889
Bram Moolenaar64486672010-05-16 15:46:46 +02002890 /* Set all attributes of the 'number' or 'relativenumber' column and the
2891 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002892 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
2894#ifdef FEAT_SIGNS
2895 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002896 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002898 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 if (len > 0)
2900 {
2901 if (len > 2)
2902 len = 2;
2903# ifdef FEAT_RIGHTLEFT
2904 if (wp->w_p_rl)
2905 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002906 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002907 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 else
2909# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002910 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 col += len;
2912 }
2913 }
2914#endif
2915
2916 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002917 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002919 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002921 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 if (len > 0)
2923 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002924 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002925 long num;
2926 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002927
2928 if (len > w + 1)
2929 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002930
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002931 if (wp->w_p_nu && !wp->w_p_rnu)
2932 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002933 num = (long)lnum;
2934 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002935 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002936 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002937 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002938 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002939 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002940 /* 'number' + 'relativenumber': cursor line shows absolute
2941 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002942 num = lnum;
2943 fmt = "%-*ld ";
2944 }
2945 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002946
Bram Moolenaar700e7342013-01-30 12:31:36 +01002947 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948#ifdef FEAT_RIGHTLEFT
2949 if (wp->w_p_rl)
2950 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002951 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002952 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 else
2954#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002955 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 col += len;
2957 }
2958 }
2959
2960 /*
2961 * 4. Compose the folded-line string with 'foldtext', if set.
2962 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002963 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
2965 txtcol = col; /* remember where text starts */
2966
2967 /*
2968 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2969 * Right-left text is put in columns 0 - number-col, normal text is put
2970 * in columns number-col - window-width.
2971 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002972 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 /* Fill the rest of the line with the fold filler */
2975#ifdef FEAT_RIGHTLEFT
2976 if (wp->w_p_rl)
2977 col -= txtcol;
2978#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002979 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980#ifdef FEAT_RIGHTLEFT
2981 - (wp->w_p_rl ? txtcol : 0)
2982#endif
2983 )
2984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 if (enc_utf8)
2986 {
2987 if (fill_fold >= 0x80)
2988 {
2989 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002990 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002991 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
2993 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002996 ScreenLines[off + col] = fill_fold;
2997 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002998 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003000 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003001 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
3003
3004 if (text != buf)
3005 vim_free(text);
3006
3007 /*
3008 * 6. set highlighting for the Visual area an other text.
3009 * If all folded lines are in the Visual area, highlight the line.
3010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3012 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003013 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 {
3015 /* Visual is after curwin->w_cursor */
3016 top = &curwin->w_cursor;
3017 bot = &VIsual;
3018 }
3019 else
3020 {
3021 /* Visual is before curwin->w_cursor */
3022 top = &VIsual;
3023 bot = &curwin->w_cursor;
3024 }
3025 if (lnum >= top->lnum
3026 && lnume <= bot->lnum
3027 && (VIsual_mode != 'v'
3028 || ((lnum > top->lnum
3029 || (lnum == top->lnum
3030 && top->col == 0))
3031 && (lnume < bot->lnum
3032 || (lnume == bot->lnum
3033 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003034 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
3036 if (VIsual_mode == Ctrl_V)
3037 {
3038 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02003039 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02003041 if (wp->w_old_cursor_lcol != MAXCOL
3042 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02003043 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 len = wp->w_old_cursor_lcol;
3045 else
Bram Moolenaar02631462017-09-22 15:20:32 +02003046 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003047 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003048 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
3050 }
3051 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003052 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02003054 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 }
3057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003059#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003060 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
3061 if (wp->w_p_cc_cols)
3062 {
3063 int i = 0;
3064 int j = wp->w_p_cc_cols[i];
3065 int old_txtcol = txtcol;
3066
3067 while (j > -1)
3068 {
3069 txtcol += j;
3070 if (wp->w_p_wrap)
3071 txtcol -= wp->w_skipcol;
3072 else
3073 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003074 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003075 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003076 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003077 txtcol = old_txtcol;
3078 j = wp->w_p_cc_cols[++i];
3079 }
3080 }
3081
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003082 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00003083 if (wp->w_p_cuc)
3084 {
3085 txtcol += wp->w_virtcol;
3086 if (wp->w_p_wrap)
3087 txtcol -= wp->w_skipcol;
3088 else
3089 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003090 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00003091 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003092 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00003093 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
Bram Moolenaar02631462017-09-22 15:20:32 +02003096 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003097 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
3099 /*
3100 * Update w_cline_height and w_cline_folded if the cursor line was
3101 * updated (saves a call to plines() later).
3102 */
3103 if (wp == curwin
3104 && lnum <= curwin->w_cursor.lnum
3105 && lnume >= curwin->w_cursor.lnum)
3106 {
3107 curwin->w_cline_row = row;
3108 curwin->w_cline_height = 1;
3109 curwin->w_cline_folded = TRUE;
3110 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
3111 }
3112}
3113
3114/*
3115 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
3116 */
3117 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003118copy_text_attr(
3119 int off,
3120 char_u *buf,
3121 int len,
3122 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003124 int i;
3125
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (enc_utf8)
3128 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003129 for (i = 0; i < len; ++i)
3130 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131}
3132
3133/*
3134 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003135 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 */
3137 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003138fill_foldcolumn(
3139 char_u *p,
3140 win_T *wp,
3141 int closed, /* TRUE of FALSE */
3142 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143{
3144 int i = 0;
3145 int level;
3146 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003147 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003148 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003151 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 level = win_foldinfo.fi_level;
3154 if (level > 0)
3155 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003156 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003157 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003158
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 /* If the column is too narrow, we start at the lowest level that
3160 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003161 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 if (first_level < 1)
3163 first_level = 1;
3164
Bram Moolenaar1c934292015-01-27 16:39:29 +01003165 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 {
3167 if (win_foldinfo.fi_lnum == lnum
3168 && first_level + i >= win_foldinfo.fi_low_level)
3169 p[i] = '-';
3170 else if (first_level == 1)
3171 p[i] = '|';
3172 else if (first_level + i <= 9)
3173 p[i] = '0' + first_level + i;
3174 else
3175 p[i] = '>';
3176 if (first_level + i == level)
3177 break;
3178 }
3179 }
3180 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003181 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182}
3183#endif /* FEAT_FOLDING */
3184
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003185#ifdef FEAT_TEXT_PROP
3186static textprop_T *current_text_props = NULL;
3187static buf_T *current_buf = NULL;
3188
3189 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003190text_prop_compare(const void *s1, const void *s2)
3191{
3192 int idx1, idx2;
3193 proptype_T *pt1, *pt2;
3194 colnr_T col1, col2;
3195
3196 idx1 = *(int *)s1;
3197 idx2 = *(int *)s2;
3198 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3199 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3200 if (pt1 == pt2)
3201 return 0;
3202 if (pt1 == NULL)
3203 return -1;
3204 if (pt2 == NULL)
3205 return 1;
3206 if (pt1->pt_priority != pt2->pt_priority)
3207 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3208 col1 = current_text_props[idx1].tp_col;
3209 col2 = current_text_props[idx2].tp_col;
3210 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3211}
3212#endif
3213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214/*
3215 * Display line "lnum" of window 'wp' on the screen.
3216 * Start at row "startrow", stop when "endrow" is reached.
3217 * wp->w_virtcol needs to be valid.
3218 *
3219 * Return the number of last row the line occupies.
3220 */
3221 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003222win_line(
3223 win_T *wp,
3224 linenr_T lnum,
3225 int startrow,
3226 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003227 int nochange UNUSED, // not updating for changed text
3228 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003230 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3232 int c = 0; /* init for GCC */
3233 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003234#ifdef FEAT_LINEBREAK
3235 long vcol_sbr = -1; /* virtual column after showbreak */
3236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 long vcol_prev = -1; /* "vcol" of previous character */
3238 char_u *line; /* current line */
3239 char_u *ptr; /* current position in "line" */
3240 int row; /* row in the window, excl w_winrow */
3241 int screen_row; /* row on the screen, incl w_winrow */
3242
3243 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3244 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003245 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003246 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003248 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 int extra_attr = 0; /* attributes when n_extra != 0 */
3250 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3251 displaying lcs_eol at end-of-line */
3252 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3253 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3254
3255 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3256 int saved_n_extra = 0;
3257 char_u *saved_p_extra = NULL;
3258 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003259 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 int saved_char_attr = 0;
3261
3262 int n_attr = 0; /* chars with special attr */
3263 int saved_attr2 = 0; /* char_attr saved for n_attr */
3264 int n_attr3 = 0; /* chars with overruling special attr */
3265 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3266
3267 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3268
3269 int fromcol, tocol; /* start/end of inverting */
3270 int fromcol_prev = -2; /* start of inverting after cursor */
3271 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003273 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 pos_T pos;
3275 long v;
3276
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003277 int char_attr = 0; // attributes for next character
3278 int attr_pri = FALSE; // char_attr has priority
3279 int area_highlighting = FALSE; // Visual or incsearch highlighting
3280 // in this line
3281 int vi_attr = 0; // attributes for Visual and incsearch
3282 // highlighting
3283 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003284 int win_attr = 0; // background for whole window, except
3285 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003286 int area_attr = 0; // attributes desired by highlighting
3287 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003289 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 int syntax_attr = 0; /* attributes desired by syntax */
3291 int has_syntax = FALSE; /* this buffer has syntax highl. */
3292 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003293 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003294 int draw_color_col = FALSE; /* highlight colorcolumn */
3295 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003296#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003297#ifdef FEAT_TEXT_PROP
3298 int text_prop_count;
3299 int text_prop_next = 0; // next text property to use
3300 textprop_T *text_props = NULL;
3301 int *text_prop_idxs = NULL;
3302 int text_props_active = 0;
3303 proptype_T *text_prop_type = NULL;
3304 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003305 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003306#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003307#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003308 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003309# define SPWORDLEN 150
3310 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003311 int nextlinecol = 0; /* column where nextline[] starts */
3312 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003313 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003314 int spell_attr = 0; /* attributes desired by spelling */
3315 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003316 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3317 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003318 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003319 static int cap_col = -1; /* column to check for Cap word */
3320 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003321 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003323 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 int multi_attr = 0; /* attributes desired by multibyte */
3325 int mb_l = 1; /* multi-byte byte length */
3326 int mb_c = 0; /* decoded multi-byte character */
3327 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003328 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#ifdef FEAT_DIFF
3330 int filler_lines; /* nr of filler lines to be drawn */
3331 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003332 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 int change_start = MAXCOL; /* first col of changed area */
3334 int change_end = -1; /* last col of changed area */
3335#endif
3336 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3337#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003338 int need_showbreak = FALSE; /* overlong line, skipping first x
3339 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003341#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003342 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003344 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345#endif
3346#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003347 matchitem_T *cur; /* points to the match list */
3348 match_T *shl; /* points to search_hl or a match */
3349 int shl_flag; /* flag to indicate whether search_hl
3350 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003351 int pos_inprogress; /* marks that position match search is
3352 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003353 int prevcol_hl_flag; /* flag to indicate whether prevcol
3354 equals startcol of search_hl or one
3355 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356#endif
3357#ifdef FEAT_ARABIC
3358 int prev_c = 0; /* previous Arabic character */
3359 int prev_c1 = 0; /* first composing char for prev_c */
3360#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003361#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003362 int did_line_attr = 0;
3363#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003364#ifdef FEAT_TERMINAL
3365 int get_term_attr = FALSE;
3366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367
3368 /* draw_state: items that are drawn in sequence: */
3369#define WL_START 0 /* nothing done yet */
3370#ifdef FEAT_CMDWIN
3371# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3372#else
3373# define WL_CMDLINE WL_START
3374#endif
3375#ifdef FEAT_FOLDING
3376# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3377#else
3378# define WL_FOLD WL_CMDLINE
3379#endif
3380#ifdef FEAT_SIGNS
3381# define WL_SIGN WL_FOLD + 1 /* column for signs */
3382#else
3383# define WL_SIGN WL_FOLD /* column for signs */
3384#endif
3385#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003386#ifdef FEAT_LINEBREAK
3387# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003389# define WL_BRI WL_NR
3390#endif
3391#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3392# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3393#else
3394# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395#endif
3396#define WL_LINE WL_SBR + 1 /* text in the line */
3397 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003398#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 int feedback_col = 0;
3400 int feedback_old_attr = -1;
3401#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003402 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403
Bram Moolenaar860cae12010-06-05 23:22:07 +02003404#ifdef FEAT_CONCEAL
3405 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003406 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003407 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003408 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003409 int is_concealing = FALSE;
3410 int boguscols = 0; /* nonexistent columns added to force
3411 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003412 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003413 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003414 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003415 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003416# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003417# define FIX_FOR_BOGUSCOLS \
3418 { \
3419 n_extra += vcol_off; \
3420 vcol -= vcol_off; \
3421 vcol_off = 0; \
3422 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003423 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003424 boguscols = 0; \
3425 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003426#else
3427# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 if (startrow > endrow) /* past the end already! */
3431 return startrow;
3432
3433 row = startrow;
3434 screen_row = row + W_WINROW(wp);
3435
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003436 if (!number_only)
3437 {
3438 /*
3439 * To speed up the loop below, set extra_check when there is linebreak,
3440 * trailing white space and/or syntax processing to be done.
3441 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003443 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444#endif
3445#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003446 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003447# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003448 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003449# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003450 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003452 /* Prepare for syntax highlighting in this line. When there is an
3453 * error, stop syntax highlighting. */
3454 save_did_emsg = did_emsg;
3455 did_emsg = FALSE;
3456 syntax_start(wp, lnum);
3457 if (did_emsg)
3458 wp->w_s->b_syn_error = TRUE;
3459 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003460 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003461 did_emsg = save_did_emsg;
3462#ifdef SYN_TIME_LIMIT
3463 if (!wp->w_s->b_syn_slow)
3464#endif
3465 {
3466 has_syntax = TRUE;
3467 extra_check = TRUE;
3468 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003471
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003472 /* Check for columns to display for 'colorcolumn'. */
3473 color_cols = wp->w_p_cc_cols;
3474 if (color_cols != NULL)
3475 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003476#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003477
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003478#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003479 if (term_show_buffer(wp->w_buffer))
3480 {
3481 extra_check = TRUE;
3482 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003483 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003484 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003485#endif
3486
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003487#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003488 if (wp->w_p_spell
3489 && *wp->w_s->b_p_spl != NUL
3490 && wp->w_s->b_langp.ga_len > 0
3491 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003492 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003493 /* Prepare for spell checking. */
3494 has_spell = TRUE;
3495 extra_check = TRUE;
3496
Bram Moolenaar7701f302018-10-02 21:20:32 +02003497 /* Get the start of the next line, so that words that wrap to the
3498 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003499 * Trick: skip a few chars for C/shell/Vim comments */
3500 nextline[SPWORDLEN] = NUL;
3501 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3502 {
3503 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3504 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3505 }
3506
Bram Moolenaar7701f302018-10-02 21:20:32 +02003507 /* When a word wrapped from the previous line the start of the
3508 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003509 if (lnum == checked_lnum)
3510 cur_checked_col = checked_col;
3511 checked_lnum = 0;
3512
3513 /* When there was a sentence end in the previous line may require a
3514 * word starting with capital in this line. In line 1 always check
3515 * the first word. */
3516 if (lnum != capcol_lnum)
3517 cap_col = -1;
3518 if (lnum == 1)
3519 cap_col = 0;
3520 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#endif
3523
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003524 /*
3525 * handle visual active in this window
3526 */
3527 fromcol = -10;
3528 tocol = MAXCOL;
3529 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003531 /* Visual is after curwin->w_cursor */
3532 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003534 top = &curwin->w_cursor;
3535 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003537 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003539 top = &VIsual;
3540 bot = &curwin->w_cursor;
3541 }
3542 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3543 if (VIsual_mode == Ctrl_V) /* block mode */
3544 {
3545 if (lnum_in_visual_area)
3546 {
3547 fromcol = wp->w_old_cursor_fcol;
3548 tocol = wp->w_old_cursor_lcol;
3549 }
3550 }
3551 else /* non-block mode */
3552 {
3553 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003555 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003557 if (VIsual_mode == 'V') /* linewise */
3558 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 else
3560 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003561 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3562 if (gchar_pos(top) == NUL)
3563 tocol = fromcol + 1;
3564 }
3565 }
3566 if (VIsual_mode != 'V' && lnum == bot->lnum)
3567 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003568 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003569 {
3570 fromcol = -10;
3571 tocol = MAXCOL;
3572 }
3573 else if (bot->col == MAXCOL)
3574 tocol = MAXCOL;
3575 else
3576 {
3577 pos = *bot;
3578 if (*p_sel == 'e')
3579 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3580 else
3581 {
3582 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3583 ++tocol;
3584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
3586 }
3587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003589 /* Check if the character under the cursor should not be inverted */
3590 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003591#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003592 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003593#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003594 )
3595 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003597 /* if inverting in this line set area_highlighting */
3598 if (fromcol >= 0)
3599 {
3600 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003601 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003603 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003604 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003605 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003606 && clip_isautosel_plus()))
3607 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003612 /*
3613 * handle 'incsearch' and ":s///c" highlighting
3614 */
3615 else if (highlight_match
3616 && wp == curwin
3617 && lnum >= curwin->w_cursor.lnum
3618 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003620 if (lnum == curwin->w_cursor.lnum)
3621 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003622 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003623 else
3624 fromcol = 0;
3625 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3626 {
3627 pos.lnum = lnum;
3628 pos.col = search_match_endcol;
3629 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3630 }
3631 else
3632 tocol = MAXCOL;
3633 /* do at least one character; happens when past end of line */
3634 if (fromcol == tocol)
3635 tocol = fromcol + 1;
3636 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003637 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640
3641#ifdef FEAT_DIFF
3642 filler_lines = diff_check(wp, lnum);
3643 if (filler_lines < 0)
3644 {
3645 if (filler_lines == -1)
3646 {
3647 if (diff_find_change(wp, lnum, &change_start, &change_end))
3648 diff_hlf = HLF_ADD; /* added line */
3649 else if (change_start == 0)
3650 diff_hlf = HLF_TXD; /* changed text */
3651 else
3652 diff_hlf = HLF_CHD; /* changed line */
3653 }
3654 else
3655 diff_hlf = HLF_ADD; /* added line */
3656 filler_lines = 0;
3657 area_highlighting = TRUE;
3658 }
3659 if (lnum == wp->w_topline)
3660 filler_lines = wp->w_topfill;
3661 filler_todo = filler_lines;
3662#endif
3663
3664#ifdef LINE_ATTR
3665# ifdef FEAT_SIGNS
3666 /* If this line has a sign with line highlighting set line_attr. */
3667 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3668 if (v != 0)
3669 line_attr = sign_get_attr((int)v, TRUE);
3670# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003671# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003673 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003674 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675# endif
3676 if (line_attr != 0)
3677 area_highlighting = TRUE;
3678#endif
3679
3680 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3681 ptr = line;
3682
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003683#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003684 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003685 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003686 /* For checking first word with a capital skip white space. */
3687 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003688 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003689
Bram Moolenaar30abd282005-06-22 22:35:10 +00003690 /* To be able to spell-check over line boundaries copy the end of the
3691 * current line into nextline[]. Above the start of the next line was
3692 * copied to nextline[SPWORDLEN]. */
3693 if (nextline[SPWORDLEN] == NUL)
3694 {
3695 /* No next line or it is empty. */
3696 nextlinecol = MAXCOL;
3697 nextline_idx = 0;
3698 }
3699 else
3700 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003701 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003702 if (v < SPWORDLEN)
3703 {
3704 /* Short line, use it completely and append the start of the
3705 * next line. */
3706 nextlinecol = 0;
3707 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003708 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003709 nextline_idx = v + 1;
3710 }
3711 else
3712 {
3713 /* Long line, use only the last SPWORDLEN bytes. */
3714 nextlinecol = v - SPWORDLEN;
3715 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3716 nextline_idx = SPWORDLEN + 1;
3717 }
3718 }
3719 }
3720#endif
3721
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003722 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003724 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003725 extra_check = TRUE;
3726 /* find start of trailing whitespace */
3727 if (lcs_trail)
3728 {
3729 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003730 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003731 --trailcol;
3732 trailcol += (colnr_T) (ptr - line);
3733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 }
3735
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003736 wcr_attr = get_wcr_attr(wp);
3737 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003738 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003739 win_attr = wcr_attr;
3740 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003741 }
3742#ifdef FEAT_TEXT_PROP
3743 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003744 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003745#endif
3746
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 /*
3748 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3749 * first character to be displayed.
3750 */
3751 if (wp->w_p_wrap)
3752 v = wp->w_skipcol;
3753 else
3754 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003755 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003758
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 while (vcol < v && *ptr != NUL)
3760 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003761 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003764 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003767 /* When:
3768 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003769 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003770 * - 'virtualedit' is set, or
3771 * - the visual mode is active,
3772 * the end of the line may be before the start of the displayed part.
3773 */
3774 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003775#ifdef FEAT_SYN_HL
3776 wp->w_p_cuc || draw_color_col ||
3777#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003778 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003779 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
3782 /* Handle a character that's not completely on the screen: Put ptr at
3783 * that character but skip the first few screen characters. */
3784 if (vcol > v)
3785 {
3786 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003788 /* If the character fits on the screen, don't need to skip it.
3789 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003790 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003791 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793
3794 /*
3795 * Adjust for when the inverted text is before the screen,
3796 * and when the start of the inverted text is before the screen.
3797 */
3798 if (tocol <= vcol)
3799 fromcol = 0;
3800 else if (fromcol >= 0 && fromcol < vcol)
3801 fromcol = vcol;
3802
3803#ifdef FEAT_LINEBREAK
3804 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3805 if (wp->w_p_wrap)
3806 need_showbreak = TRUE;
3807#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003808#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003809 /* When spell checking a word we need to figure out the start of the
3810 * word and if it's badly spelled or not. */
3811 if (has_spell)
3812 {
3813 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003814 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003815 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003816
3817 pos = wp->w_cursor;
3818 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003819 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003820 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003821
3822 /* spell_move_to() may call ml_get() and make "line" invalid */
3823 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3824 ptr = line + linecol;
3825
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003826 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003827 {
3828 /* no bad word found at line start, don't check until end of a
3829 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003830 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003831 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003832 }
3833 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003834 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003835 /* bad word found, use attributes until end of word */
3836 word_end = wp->w_cursor.col + len + 1;
3837
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003838 /* Turn index into actual attributes. */
3839 if (spell_hlf != HLF_COUNT)
3840 spell_attr = highlight_attr[spell_hlf];
3841 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003842 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003843
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003844# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003845 /* Need to restart syntax highlighting for this line. */
3846 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003847 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003848# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003849 }
3850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852
3853 /*
3854 * Correct highlighting for cursor that can't be disabled.
3855 * Avoids having to check this for each character.
3856 */
3857 if (fromcol >= 0)
3858 {
3859 if (noinvcur)
3860 {
3861 if ((colnr_T)fromcol == wp->w_virtcol)
3862 {
3863 /* highlighting starts at cursor, let it start just after the
3864 * cursor */
3865 fromcol_prev = fromcol;
3866 fromcol = -1;
3867 }
3868 else if ((colnr_T)fromcol < wp->w_virtcol)
3869 /* restart highlighting after the cursor */
3870 fromcol_prev = wp->w_virtcol;
3871 }
3872 if (fromcol >= tocol)
3873 fromcol = -1;
3874 }
3875
3876#ifdef FEAT_SEARCH_EXTRA
3877 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003878 * Handle highlighting the last used search pattern and matches.
3879 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003880 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003882 cur = wp->w_match_head;
3883 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003884 while ((cur != NULL || shl_flag == FALSE) && !number_only
3885 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003887 if (shl_flag == FALSE)
3888 {
3889 shl = &search_hl;
3890 shl_flag = TRUE;
3891 }
3892 else
3893 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003894 shl->startcol = MAXCOL;
3895 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003897 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003898 v = (long)(ptr - line);
3899 if (cur != NULL)
3900 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003901 next_search_hl(wp, shl, lnum, (colnr_T)v,
3902 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003903
3904 /* Need to get the line again, a multi-line regexp may have made it
3905 * invalid. */
3906 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3907 ptr = line + v;
3908
3909 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003911 if (shl->lnum == lnum)
3912 shl->startcol = shl->rm.startpos[0].col;
3913 else
3914 shl->startcol = 0;
3915 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3916 - shl->rm.startpos[0].lnum)
3917 shl->endcol = shl->rm.endpos[0].col;
3918 else
3919 shl->endcol = MAXCOL;
3920 /* Highlight one character for an empty match. */
3921 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003923 if (has_mbyte && line[shl->endcol] != NUL)
3924 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3925 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003926 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003928 if ((long)shl->startcol < v) /* match at leftcol */
3929 {
3930 shl->attr_cur = shl->attr;
3931 search_attr = shl->attr;
3932 }
3933 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003935 if (shl != &search_hl && cur != NULL)
3936 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938#endif
3939
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003940#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003941 // Cursor line highlighting for 'cursorline' in the current window.
3942 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003943 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003944 // Do not show the cursor line when Visual mode is active, because it's
3945 // not clear what is selected then. Do update w_last_cursorline.
3946 if (!(wp == curwin && VIsual_active))
3947 {
3948 line_attr = HL_ATTR(HLF_CUL);
3949 area_highlighting = TRUE;
3950 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003951 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003952 }
3953#endif
3954
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003955#ifdef FEAT_TEXT_PROP
3956 {
3957 char_u *prop_start;
3958
3959 text_prop_count = get_text_props(wp->w_buffer, lnum,
3960 &prop_start, FALSE);
3961 if (text_prop_count > 0)
3962 {
3963 // Make a copy of the properties, so that they are properly
3964 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003965 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003966 if (text_props != NULL)
3967 mch_memmove(text_props, prop_start,
3968 text_prop_count * sizeof(textprop_T));
3969
3970 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003971 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003972 area_highlighting = TRUE;
3973 extra_check = TRUE;
3974 }
3975 }
3976#endif
3977
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003978 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981#ifdef FEAT_RIGHTLEFT
3982 if (wp->w_p_rl)
3983 {
3984 /* Rightleft window: process the text in the normal direction, but put
3985 * it in current_ScreenLine[] from right to left. Start at the
3986 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003987 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003989 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
3991#endif
3992
3993 /*
3994 * Repeat for the whole displayed line.
3995 */
3996 for (;;)
3997 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003998#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003999 int has_match_conc = 0; // match wants to conceal
4000 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 /* Skip this quickly when working on the text. */
4003 if (draw_state != WL_LINE)
4004 {
4005#ifdef FEAT_CMDWIN
4006 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
4007 {
4008 draw_state = WL_CMDLINE;
4009 if (cmdwin_type != 0 && wp == curwin)
4010 {
4011 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004013 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004014 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004015 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 }
4018#endif
4019
4020#ifdef FEAT_FOLDING
4021 if (draw_state == WL_FOLD - 1 && n_extra == 0)
4022 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01004023 int fdc = compute_foldcolumn(wp, 0);
4024
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01004026 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
Bram Moolenaar62706602016-12-09 19:28:48 +01004028 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004029 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004030 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01004031 p_extra_free = alloc(12 + 1);
4032
4033 if (p_extra_free != NULL)
4034 {
4035 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
4036 n_extra = fdc;
4037 p_extra_free[n_extra] = NUL;
4038 p_extra = p_extra_free;
4039 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004040 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004041 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01004042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
4044 }
4045#endif
4046
4047#ifdef FEAT_SIGNS
4048 if (draw_state == WL_SIGN - 1 && n_extra == 0)
4049 {
4050 draw_state = WL_SIGN;
4051 /* Show the sign column when there are any signs in this
4052 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02004053 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004055 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004057 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058# endif
4059
4060 /* Draw two cells with the sign value or blank. */
4061 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004062 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004063 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 n_extra = 2;
4065
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02004066 if (row == startrow
4067#ifdef FEAT_DIFF
4068 + filler_lines && filler_todo <= 0
4069#endif
4070 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
4072 text_sign = buf_getsigntype(wp->w_buffer, lnum,
4073 SIGN_TEXT);
4074# ifdef FEAT_SIGN_ICONS
4075 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
4076 SIGN_ICON);
4077 if (gui.in_use && icon_sign != 0)
4078 {
4079 /* Use the image in this position. */
4080 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004081 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082# ifdef FEAT_NETBEANS_INTG
4083 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004086 c_final = NUL;
4087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088# endif
4089 char_attr = icon_sign;
4090 }
4091 else
4092# endif
4093 if (text_sign != 0)
4094 {
4095 p_extra = sign_get_text(text_sign);
4096 if (p_extra != NULL)
4097 {
4098 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004099 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004100 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102 char_attr = sign_get_attr(text_sign, FALSE);
4103 }
4104 }
4105 }
4106 }
4107#endif
4108
4109 if (draw_state == WL_NR - 1 && n_extra == 0)
4110 {
4111 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02004112 /* Display the absolute or relative line number. After the
4113 * first fill with blanks when the 'n' flag isn't in 'cpo' */
4114 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 && (row == startrow
4116#ifdef FEAT_DIFF
4117 + filler_lines
4118#endif
4119 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
4120 {
4121 /* Draw the line number (empty space after wrapping). */
4122 if (row == startrow
4123#ifdef FEAT_DIFF
4124 + filler_lines
4125#endif
4126 )
4127 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004128 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004129 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004130
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004131 if (wp->w_p_nu && !wp->w_p_rnu)
4132 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004133 num = (long)lnum;
4134 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004135 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004136 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004137 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004138 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004139 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004140 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004141 num = lnum;
4142 fmt = "%-*ld ";
4143 }
4144 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004145
Bram Moolenaar700e7342013-01-30 12:31:36 +01004146 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004147 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 if (wp->w_skipcol > 0)
4149 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4150 *p_extra = '-';
4151#ifdef FEAT_RIGHTLEFT
4152 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004153 {
4154 char_u *p1, *p2;
4155 int t;
4156
4157 // like rl_mirror(), but keep the space at the end
4158 p2 = skiptowhite(extra) - 1;
4159 for (p1 = extra; p1 < p2; ++p1, --p2)
4160 {
4161 t = *p1;
4162 *p1 = *p2;
4163 *p2 = t;
4164 }
4165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166#endif
4167 p_extra = extra;
4168 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004169 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 }
4171 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004174 c_final = NUL;
4175 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004176 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004177 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004178#ifdef FEAT_SYN_HL
4179 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004180 * the current line differently.
4181 * TODO: Can we use CursorLine instead of CursorLineNr
4182 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004183 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004184 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004185 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 }
4188 }
4189
Bram Moolenaar597a4222014-06-25 14:39:50 +02004190#ifdef FEAT_LINEBREAK
4191 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4192 && n_extra == 0 && *p_sbr != NUL)
4193 /* draw indent after showbreak value */
4194 draw_state = WL_BRI;
4195 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4196 /* After the showbreak, draw the breakindent */
4197 draw_state = WL_BRI - 1;
4198
4199 /* draw 'breakindent': indent wrapped text accordingly */
4200 if (draw_state == WL_BRI - 1 && n_extra == 0)
4201 {
4202 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004203 /* if need_showbreak is set, breakindent also applies */
4204 if (wp->w_p_bri && n_extra == 0
4205 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004206# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004207 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004208# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004209 )
4210 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004211 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004212# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004213 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004214 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004215 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004216# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004217 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4218 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004219 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004220# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004221 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004222# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004223 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004224 c_extra = ' ';
4225 n_extra = get_breakindent_win(wp,
4226 ml_get_buf(wp->w_buffer, lnum, FALSE));
4227 /* Correct end of highlighted area for 'breakindent',
4228 * required when 'linebreak' is also set. */
4229 if (tocol == vcol)
4230 tocol += n_extra;
4231 }
4232 }
4233#endif
4234
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4236 if (draw_state == WL_SBR - 1 && n_extra == 0)
4237 {
4238 draw_state = WL_SBR;
4239# ifdef FEAT_DIFF
4240 if (filler_todo > 0)
4241 {
4242 /* Draw "deleted" diff line(s). */
4243 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004244 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004246 c_final = NUL;
4247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004249 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004251 c_final = NUL;
4252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253# ifdef FEAT_RIGHTLEFT
4254 if (wp->w_p_rl)
4255 n_extra = col + 1;
4256 else
4257# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004258 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004259 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261# endif
4262# ifdef FEAT_LINEBREAK
4263 if (*p_sbr != NUL && need_showbreak)
4264 {
4265 /* Draw 'showbreak' at the start of each broken line. */
4266 p_extra = p_sbr;
4267 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004268 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004270 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004272 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 /* Correct end of highlighted area for 'showbreak',
4274 * required when 'linebreak' is also set. */
4275 if (tocol == vcol)
4276 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004277#ifdef FEAT_SYN_HL
4278 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004279 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004280 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004281 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284# endif
4285 }
4286#endif
4287
4288 if (draw_state == WL_LINE - 1 && n_extra == 0)
4289 {
4290 draw_state = WL_LINE;
4291 if (saved_n_extra)
4292 {
4293 /* Continue item from end of wrapped line. */
4294 n_extra = saved_n_extra;
4295 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004296 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 p_extra = saved_p_extra;
4298 char_attr = saved_char_attr;
4299 }
4300 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004301 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 }
4304
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004305 // When still displaying '$' of change command, stop at cursor.
4306 // When only displaying the (relative) line number and that's done,
4307 // stop here.
4308 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004309 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310#ifdef FEAT_DIFF
4311 && filler_todo <= 0
4312#endif
4313 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004314 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004316 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004317 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004318 /* Pretend we have finished updating the window. Except when
4319 * 'cursorcolumn' is set. */
4320#ifdef FEAT_SYN_HL
4321 if (wp->w_p_cuc)
4322 row = wp->w_cline_row + wp->w_cline_height;
4323 else
4324#endif
4325 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 break;
4327 }
4328
Bram Moolenaar637532b2019-01-03 21:44:40 +01004329 if (draw_state == WL_LINE && (area_highlighting
4330#ifdef FEAT_SPELL
4331 || has_spell
4332#endif
4333 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 {
4335 /* handle Visual or match highlighting in this line */
4336 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4338 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004340 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004342 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 else if (area_attr != 0
4344 && (vcol == tocol
4345 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
4348#ifdef FEAT_SEARCH_EXTRA
4349 if (!n_extra)
4350 {
4351 /*
4352 * Check for start/end of search pattern match.
4353 * After end, check for start/end of next match.
4354 * When another match, have to check for start again.
4355 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004356 * Do this for 'search_hl' and the match list (ordered by
4357 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004359 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004360 cur = wp->w_match_head;
4361 shl_flag = FALSE;
4362 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004364 if (shl_flag == FALSE
4365 && ((cur != NULL
4366 && cur->priority > SEARCH_HL_PRIORITY)
4367 || cur == NULL))
4368 {
4369 shl = &search_hl;
4370 shl_flag = TRUE;
4371 }
4372 else
4373 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004374 if (cur != NULL)
4375 cur->pos.cur = 0;
4376 pos_inprogress = TRUE;
4377 while (shl->rm.regprog != NULL
4378 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004380 if (shl->startcol != MAXCOL
4381 && v >= (long)shl->startcol
4382 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004384 int tmp_col = v + MB_PTR2LEN(ptr);
4385
4386 if (shl->endcol < tmp_col)
4387 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004389#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004390 // Match with the "Conceal" group results in hiding
4391 // the match.
4392 if (cur != NULL
4393 && shl != &search_hl
4394 && syn_name2id((char_u *)"Conceal")
4395 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004396 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004397 has_match_conc =
4398 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004399 match_conc = cur->conceal_char;
4400 }
4401 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004402 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004405 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 {
4407 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004408 next_search_hl(wp, shl, lnum, (colnr_T)v,
4409 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004410 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004411 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
4413 /* Need to get the line again, a multi-line regexp
4414 * may have made it invalid. */
4415 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4416 ptr = line + v;
4417
4418 if (shl->lnum == lnum)
4419 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004420 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004422 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004424 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004426 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 {
4428 /* highlight empty match, try again after
4429 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004431 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004432 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004434 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
4436
4437 /* Loop to check if the match starts at the
4438 * current position */
4439 continue;
4440 }
4441 }
4442 break;
4443 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004444 if (shl != &search_hl && cur != NULL)
4445 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004447
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004448 /* Use attributes from match with highest priority among
4449 * 'search_hl' and the match list. */
4450 search_attr = search_hl.attr_cur;
4451 cur = wp->w_match_head;
4452 shl_flag = FALSE;
4453 while (cur != NULL || shl_flag == FALSE)
4454 {
4455 if (shl_flag == FALSE
4456 && ((cur != NULL
4457 && cur->priority > SEARCH_HL_PRIORITY)
4458 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004459 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004460 shl = &search_hl;
4461 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004462 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004463 else
4464 shl = &cur->hl;
4465 if (shl->attr_cur != 0)
4466 search_attr = shl->attr_cur;
4467 if (shl != &search_hl && cur != NULL)
4468 cur = cur->next;
4469 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004470 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004471 if (*ptr == NUL && (did_line_attr >= 1
4472 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004473 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475#endif
4476
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004478 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004480 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4481 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004483 if (diff_hlf == HLF_TXD && ptr - line > change_end
4484 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004486 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004487 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004488 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004491
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004492#ifdef FEAT_TEXT_PROP
4493 if (text_props != NULL)
4494 {
4495 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004496 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004497
4498 // Check if any active property ends.
4499 for (pi = 0; pi < text_props_active; ++pi)
4500 {
4501 int tpi = text_prop_idxs[pi];
4502
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004503 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004504 + text_props[tpi].tp_len)
4505 {
4506 if (pi + 1 < text_props_active)
4507 mch_memmove(text_prop_idxs + pi,
4508 text_prop_idxs + pi + 1,
4509 sizeof(int)
4510 * (text_props_active - (pi + 1)));
4511 --text_props_active;
4512 --pi;
4513 }
4514 }
4515
4516 // Add any text property that starts in this column.
4517 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004518 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004519 text_prop_idxs[text_props_active++] = text_prop_next++;
4520
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004521 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004522 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004523 if (text_props_active > 0)
4524 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004525 // Sort the properties on priority and/or starting last.
4526 // Then combine the attributes, highest priority last.
4527 current_text_props = text_props;
4528 current_buf = wp->w_buffer;
4529 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4530 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004531
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004532 for (pi = 0; pi < text_props_active; ++pi)
4533 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004534 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004535 proptype_T *pt = text_prop_type_by_id(
4536 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004537
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004538 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004539 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004540 int pt_attr = syn_id2attr(pt->pt_hl_id);
4541
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004542 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004543 text_prop_attr =
4544 hl_combine_attr(text_prop_attr, pt_attr);
4545 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004546 }
4547 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004548 }
4549 }
4550#endif
4551
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004552 /* Decide which of the highlight attributes to use. */
4553 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004554#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004555 if (area_attr != 0)
4556 char_attr = hl_combine_attr(line_attr, area_attr);
4557 else if (search_attr != 0)
4558 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004559# ifdef FEAT_TEXT_PROP
4560 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004561 {
4562 char_attr = hl_combine_attr(
4563 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4564 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004565# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004566 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004567 || vcol < fromcol || vcol_prev < fromcol_prev
4568 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004569 // Use line_attr when not in the Visual or 'incsearch' area
4570 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004571 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004572#else
4573 if (area_attr != 0)
4574 char_attr = area_attr;
4575 else if (search_attr != 0)
4576 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004577#endif
4578 else
4579 {
4580 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004581#ifdef FEAT_TEXT_PROP
4582 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004583 {
4584 if (text_prop_combine)
4585 char_attr = hl_combine_attr(
4586 syntax_attr, text_prop_attr);
4587 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004588 char_attr = hl_combine_attr(
4589 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004590 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004591 else
4592#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004593#ifdef FEAT_SYN_HL
4594 if (has_syntax)
4595 char_attr = syntax_attr;
4596 else
4597#endif
4598 char_attr = 0;
4599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004601 if (char_attr == 0)
4602 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
4604 /*
4605 * Get the next character to put on the screen.
4606 */
4607 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004608 * The "p_extra" points to the extra stuff that is inserted to
4609 * represent special characters (non-printable stuff) and other
4610 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004611 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004612 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4613 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4615 */
4616 if (n_extra > 0)
4617 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004618 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004620 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004622 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
4624 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004625 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004626 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
4628 else
4629 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 }
4631 else
4632 {
4633 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (has_mbyte)
4635 {
4636 mb_c = c;
4637 if (enc_utf8)
4638 {
4639 /* If the UTF-8 character is more than one byte:
4640 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004641 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 mb_utf8 = FALSE;
4643 if (mb_l > n_extra)
4644 mb_l = 1;
4645 else if (mb_l > 1)
4646 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004647 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004649 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 }
4651 }
4652 else
4653 {
4654 /* if this is a DBCS character, put it in "mb_c" */
4655 mb_l = MB_BYTE2LEN(c);
4656 if (mb_l >= n_extra)
4657 mb_l = 1;
4658 else if (mb_l > 1)
4659 mb_c = (c << 8) + p_extra[1];
4660 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004661 if (mb_l == 0) /* at the NUL at end-of-line */
4662 mb_l = 1;
4663
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 /* If a double-width char doesn't fit display a '>' in the
4665 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004666 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667# ifdef FEAT_RIGHTLEFT
4668 wp->w_p_rl ? (col <= 0) :
4669# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004670 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 && (*mb_char2cells)(mb_c) == 2)
4672 {
4673 c = '>';
4674 mb_c = c;
4675 mb_l = 1;
4676 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004677 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 /* put the pointer back to output the double-width
4679 * character at the start of the next line. */
4680 ++n_extra;
4681 --p_extra;
4682 }
4683 else
4684 {
4685 n_extra -= mb_l - 1;
4686 p_extra += mb_l - 1;
4687 }
4688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 ++p_extra;
4690 }
4691 --n_extra;
4692 }
4693 else
4694 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004695#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004696 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004697#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004698
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004699 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004700 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 /*
4702 * Get a character from the line itself.
4703 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004704 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004705#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004706 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 if (has_mbyte)
4709 {
4710 mb_c = c;
4711 if (enc_utf8)
4712 {
4713 /* If the UTF-8 character is more than one byte: Decode it
4714 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004715 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 mb_utf8 = FALSE;
4717 if (mb_l > 1)
4718 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004719 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 /* Overlong encoded ASCII or ASCII with composing char
4721 * is displayed normally, except a NUL. */
4722 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004723 {
4724 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004725#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004726 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004727#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004730
4731 /* At start of the line we can have a composing char.
4732 * Draw it as a space with a composing char. */
4733 if (utf_iscomposing(mb_c))
4734 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004735 int i;
4736
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004737 for (i = Screen_mco - 1; i > 0; --i)
4738 u8cc[i] = u8cc[i - 1];
4739 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004740 mb_c = ' ';
4741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
4743
4744 if ((mb_l == 1 && c >= 0x80)
4745 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004746 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
4748 /*
4749 * Illegal UTF-8 byte: display as <xx>.
4750 * Non-BMP character : display as ? or fullwidth ?.
4751 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004752 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004753# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004754 if (wp->w_p_rl) /* reverse */
4755 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 p_extra = extra;
4758 c = *p_extra;
4759 mb_c = mb_ptr2char_adv(&p_extra);
4760 mb_utf8 = (c >= 0x80);
4761 n_extra = (int)STRLEN(p_extra);
4762 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004763 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 if (area_attr == 0 && search_attr == 0)
4765 {
4766 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004767 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 saved_attr2 = char_attr; /* save current attr */
4769 }
4770 }
4771 else if (mb_l == 0) /* at the NUL at end-of-line */
4772 mb_l = 1;
4773#ifdef FEAT_ARABIC
4774 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4775 {
4776 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004777 int pc, pc1, nc;
4778 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
4780 /* The idea of what is the previous and next
4781 * character depends on 'rightleft'. */
4782 if (wp->w_p_rl)
4783 {
4784 pc = prev_c;
4785 pc1 = prev_c1;
4786 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004787 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
4789 else
4790 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004791 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004793 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795 prev_c = mb_c;
4796
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004797 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 }
4799 else
4800 prev_c = mb_c;
4801#endif
4802 }
4803 else /* enc_dbcs */
4804 {
4805 mb_l = MB_BYTE2LEN(c);
4806 if (mb_l == 0) /* at the NUL at end-of-line */
4807 mb_l = 1;
4808 else if (mb_l > 1)
4809 {
4810 /* We assume a second byte below 32 is illegal.
4811 * Hopefully this is OK for all double-byte encodings!
4812 */
4813 if (ptr[1] >= 32)
4814 mb_c = (c << 8) + ptr[1];
4815 else
4816 {
4817 if (ptr[1] == NUL)
4818 {
4819 /* head byte at end of line */
4820 mb_l = 1;
4821 transchar_nonprint(extra, c);
4822 }
4823 else
4824 {
4825 /* illegal tail byte */
4826 mb_l = 2;
4827 STRCPY(extra, "XX");
4828 }
4829 p_extra = extra;
4830 n_extra = (int)STRLEN(extra) - 1;
4831 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004832 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 c = *p_extra++;
4834 if (area_attr == 0 && search_attr == 0)
4835 {
4836 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004837 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 saved_attr2 = char_attr; /* save current attr */
4839 }
4840 mb_c = c;
4841 }
4842 }
4843 }
4844 /* If a double-width char doesn't fit display a '>' in the
4845 * last column; the character is displayed at the start of the
4846 * next line. */
4847 if ((
4848# ifdef FEAT_RIGHTLEFT
4849 wp->w_p_rl ? (col <= 0) :
4850# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004851 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 && (*mb_char2cells)(mb_c) == 2)
4853 {
4854 c = '>';
4855 mb_c = c;
4856 mb_utf8 = FALSE;
4857 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004858 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004859 // Put pointer back so that the character will be
4860 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004862#ifdef FEAT_CONCEAL
4863 did_decrement_ptr = TRUE;
4864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
4866 else if (*ptr != NUL)
4867 ptr += mb_l - 1;
4868
4869 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004870 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004871 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004872 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004875 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004876 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 c = ' ';
4878 if (area_attr == 0 && search_attr == 0)
4879 {
4880 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004881 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 saved_attr2 = char_attr; /* save current attr */
4883 }
4884 mb_c = c;
4885 mb_utf8 = FALSE;
4886 mb_l = 1;
4887 }
4888
4889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 ++ptr;
4891
4892 if (extra_check)
4893 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004894#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004895 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004896#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004897
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004898#ifdef FEAT_TERMINAL
4899 if (get_term_attr)
4900 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004901 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004902
4903 if (!attr_pri)
4904 char_attr = syntax_attr;
4905 else
4906 char_attr = hl_combine_attr(syntax_attr, char_attr);
4907 }
4908#endif
4909
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004910#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004911 // Get syntax attribute, unless still at the start of the line
4912 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004913 v = (long)(ptr - line);
4914 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
4916 /* Get the syntax attribute for the character. If there
4917 * is an error, disable syntax highlighting. */
4918 save_did_emsg = did_emsg;
4919 did_emsg = FALSE;
4920
Bram Moolenaar217ad922005-03-20 22:37:15 +00004921 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004922# ifdef FEAT_SPELL
4923 has_spell ? &can_spell :
4924# endif
4925 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
4927 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004928 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004929 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004930 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004931 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 else
4934 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004935
4936 // combine syntax attribute with 'wincolor'
4937 if (win_attr != 0)
4938 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4939
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004940#ifdef SYN_TIME_LIMIT
4941 if (wp->w_s->b_syn_slow)
4942 has_syntax = FALSE;
4943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945 /* Need to get the line again, a multi-line regexp may
4946 * have made it invalid. */
4947 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4948 ptr = line + v;
4949
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004950# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004951 // Text properties overrule syntax highlighting or combine.
4952 if (text_prop_attr == 0 || text_prop_combine)
4953# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004954 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004955 int comb_attr = syntax_attr;
4956# ifdef FEAT_TEXT_PROP
4957 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4958# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004959 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004960 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004961 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004962 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004963 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004964# ifdef FEAT_CONCEAL
4965 /* no concealing past the end of the line, it interferes
4966 * with line highlighting */
4967 if (c == NUL)
4968 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004969 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004970 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004973#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004974
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004975#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004976 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004977 * Only do this when there is no syntax highlighting, the
4978 * @Spell cluster is not used or the current syntax item
4979 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004980 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004981 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004982 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004983 if (c != 0 && (
4984# ifdef FEAT_SYN_HL
4985 !has_syntax ||
4986# endif
4987 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004988 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004989 char_u *prev_ptr, *p;
4990 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004991 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004992 if (has_mbyte)
4993 {
4994 prev_ptr = ptr - mb_l;
4995 v -= mb_l - 1;
4996 }
4997 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004998 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004999
5000 /* Use nextline[] if possible, it has the start of the
5001 * next line concatenated. */
5002 if ((prev_ptr - line) - nextlinecol >= 0)
5003 p = nextline + (prev_ptr - line) - nextlinecol;
5004 else
5005 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005006 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005007 len = spell_check(wp, p, &spell_hlf, &cap_col,
5008 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005009 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005010
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005011 /* In Insert mode only highlight a word that
5012 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005013 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005014 && (State & INSERT) != 0
5015 && wp->w_cursor.lnum == lnum
5016 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00005017 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005018 && wp->w_cursor.col < (colnr_T)word_end)
5019 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005020 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005021 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005022 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00005023
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005024 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00005025 && (p - nextline) + len > nextline_idx)
5026 {
5027 /* Remember that the good word continues at the
5028 * start of the next line. */
5029 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005030 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005031 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005032
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005033 /* Turn index into actual attributes. */
5034 if (spell_hlf != HLF_COUNT)
5035 spell_attr = highlight_attr[spell_hlf];
5036
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005037 if (cap_col > 0)
5038 {
5039 if (p != prev_ptr
5040 && (p - nextline) + cap_col >= nextline_idx)
5041 {
5042 /* Remember that the word in the next line
5043 * must start with a capital. */
5044 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005045 cap_col = (int)((p - nextline) + cap_col
5046 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005047 }
5048 else
5049 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005050 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005051 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005052 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005053 }
5054 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005055 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005056 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005057 char_attr = hl_combine_attr(char_attr, spell_attr);
5058 else
5059 char_attr = hl_combine_attr(spell_attr, char_attr);
5060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061#endif
5062#ifdef FEAT_LINEBREAK
5063 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00005064 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01005066 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01005067 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01005069 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005070 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01005071
Bram Moolenaar597a4222014-06-25 14:39:50 +02005072 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005073 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02005074 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02005075 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005076# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02005077 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005078 wp->w_buffer->b_p_vts_array) - 1;
5079# else
Bram Moolenaara3650912014-11-19 13:21:57 +01005080 n_extra = (int)wp->w_buffer->b_p_ts
5081 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005082# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01005083
Bram Moolenaar4df70292015-03-21 14:20:16 +01005084 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01005085 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005086 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005087 {
5088#ifdef FEAT_CONCEAL
5089 if (c == TAB)
5090 /* See "Tab alignment" below. */
5091 FIX_FOR_BOGUSCOLS;
5092#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005093 if (!wp->w_p_list)
5094 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097#endif
5098
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005099 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
5100 // But not when the character is followed by a composing
5101 // character (use mb_l to check that).
5102 if (wp->w_p_list
5103 && ((((c == 160 && mb_l == 1)
5104 || (mb_utf8
5105 && ((mb_c == 160 && mb_l == 2)
5106 || (mb_c == 0x202f && mb_l == 3))))
5107 && lcs_nbsp)
5108 || (c == ' '
5109 && mb_l == 1
5110 && lcs_space
5111 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005112 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005113 c = (c == ' ') ? lcs_space : lcs_nbsp;
5114 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005115 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005116 n_attr = 1;
5117 extra_attr = HL_ATTR(HLF_8);
5118 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005119 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005120 mb_c = c;
5121 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005122 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005123 mb_utf8 = TRUE;
5124 u8cc[0] = 0;
5125 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005126 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005127 else
5128 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005129 }
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5132 {
5133 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005134 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
5136 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005137 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 saved_attr2 = char_attr; /* save current attr */
5139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005141 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
5143 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005144 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005145 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147 else
5148 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150 }
5151
5152 /*
5153 * Handling of non-printable characters.
5154 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005155 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
5157 /*
5158 * when getting a character from the file, we may have to
5159 * turn it into something else on the way to putting it
5160 * into "ScreenLines".
5161 */
5162 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5163 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005164 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005165 long vcol_adjusted = vcol; /* removed showbreak length */
5166#ifdef FEAT_LINEBREAK
5167 /* only adjust the tab_len, when at the first column
5168 * after the showbreak value was drawn */
5169 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5170 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005173#ifdef FEAT_VARTABS
5174 tab_len = tabstop_padding(vcol_adjusted,
5175 wp->w_buffer->b_p_ts,
5176 wp->w_buffer->b_p_vts_array) - 1;
5177#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005178 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005179 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5180#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005181
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005182#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005183 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005184#endif
5185 /* tab amount depends on current column */
5186 n_extra = tab_len;
5187#ifdef FEAT_LINEBREAK
5188 else
5189 {
5190 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005191 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005192 int i;
5193 int saved_nextra = n_extra;
5194
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005195#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005196 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005197 /* there are characters to conceal */
5198 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005199 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5200 */
5201 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5202 && n_extra > tab_len)
5203 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005204#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005205
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005206 /* if n_extra > 0, it gives the number of chars, to
5207 * use for a tab, else we need to calculate the width
5208 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005209 len = (tab_len * mb_char2len(lcs_tab2));
5210 if (n_extra > 0)
5211 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005212 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005213 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005214 vim_memset(p, ' ', len);
5215 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005216 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005217 p_extra_free = p;
5218 for (i = 0; i < tab_len; i++)
5219 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005220 if (*p == NUL)
5221 {
5222 tab_len = i;
5223 break;
5224 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005225 mb_char2bytes(lcs_tab2, p);
5226 p += mb_char2len(lcs_tab2);
5227 n_extra += mb_char2len(lcs_tab2)
5228 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005229 }
5230 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005231#ifdef FEAT_CONCEAL
5232 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5233 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005234 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005235 n_extra -= vcol_off;
5236#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005237 }
5238#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005239#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005240 {
5241 int vc_saved = vcol_off;
5242
5243 /* Tab alignment should be identical regardless of
5244 * 'conceallevel' value. So tab compensates of all
5245 * previous concealed characters, and thus resets
5246 * vcol_off and boguscols accumulated so far in the
5247 * line. Note that the tab can be longer than
5248 * 'tabstop' when there are concealed characters. */
5249 FIX_FOR_BOGUSCOLS;
5250
5251 /* Make sure, the highlighting for the tab char will be
5252 * correctly set further below (effectively reverts the
5253 * FIX_FOR_BOGSUCOLS macro */
5254 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005255 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005256 tab_len += vc_saved;
5257 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 if (wp->w_p_list)
5261 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005262 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005263#ifdef FEAT_LINEBREAK
5264 if (wp->w_p_lbr)
5265 c_extra = NUL; /* using p_extra from above */
5266 else
5267#endif
5268 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005269 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005270 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005271 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005274 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {
5276 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005277 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005278 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
5281 else
5282 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005283 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 c_extra = ' ';
5285 c = ' ';
5286 }
5287 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005288 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005289 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005290 || ((fromcol >= 0 || fromcol_prev >= 0)
5291 && tocol > vcol
5292 && VIsual_mode != Ctrl_V
5293 && (
5294# ifdef FEAT_RIGHTLEFT
5295 wp->w_p_rl ? (col >= 0) :
5296# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005297 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005298 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005299 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005300 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005301 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005303 /* Display a '$' after the line or highlight an extra
5304 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5306 /* For a diff line the highlighting continues after the
5307 * "$". */
5308 if (
5309# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005310 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311# ifdef LINE_ATTR
5312 &&
5313# endif
5314# endif
5315# ifdef LINE_ATTR
5316 line_attr == 0
5317# endif
5318 )
5319#endif
5320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 /* In virtualedit, visual selections may extend
5322 * beyond end of line. */
5323 if (area_highlighting && virtual_active()
5324 && tocol != MAXCOL && vcol < tocol)
5325 n_extra = 0;
5326 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
5328 p_extra = at_end_str;
5329 n_extra = 1;
5330 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005331 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005334 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005335 c = lcs_eol;
5336 else
5337 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 lcs_eol_one = -1;
5339 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005340 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005342 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 n_attr = 1;
5344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005346 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
5348 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005349 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005350 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
5352 else
5353 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 }
5355 else if (c != NUL)
5356 {
5357 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005358 if (n_extra == 0)
5359 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360#ifdef FEAT_RIGHTLEFT
5361 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5362 rl_mirror(p_extra); /* reverse "<12>" */
5363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005365 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005366#ifdef FEAT_LINEBREAK
5367 if (wp->w_p_lbr)
5368 {
5369 char_u *p;
5370
5371 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005372 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005373 vim_memset(p, ' ', n_extra);
5374 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5375 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005376 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005377 p_extra_free = p_extra = p;
5378 }
5379 else
5380#endif
5381 {
5382 n_extra = byte2cells(c) - 1;
5383 c = *p_extra++;
5384 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005385 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
5387 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005388 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 saved_attr2 = char_attr; /* save current attr */
5390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 else if (VIsual_active
5394 && (VIsual_mode == Ctrl_V
5395 || VIsual_mode == 'v')
5396 && virtual_active()
5397 && tocol != MAXCOL
5398 && vcol < tocol
5399 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005400#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005402#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005403 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 {
5405 c = ' ';
5406 --ptr; /* put it back at the NUL */
5407 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005408#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 else if ((
5410# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005411 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005413# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005414 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 ) && (
5418# ifdef FEAT_RIGHTLEFT
5419 wp->w_p_rl ? (col >= 0) :
5420# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005421 (col
5422# ifdef FEAT_CONCEAL
5423 - boguscols
5424# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005425 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 {
5427 /* Highlight until the right side of the window */
5428 c = ' ';
5429 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005430
5431 /* Remember we do the char for line highlighting. */
5432 ++did_line_attr;
5433
5434 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005435 if (line_attr != 0 && char_attr == search_attr
5436 && (did_line_attr > 1
5437 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005438 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439# ifdef FEAT_DIFF
5440 if (diff_hlf == HLF_TXD)
5441 {
5442 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005443 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005444 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005445 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005446 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5447 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005448 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 }
5451# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005452# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005453 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005454 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005455 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005456 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5457 char_attr = hl_combine_attr(char_attr,
5458 HL_ATTR(HLF_CUL));
5459 }
5460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 }
5462#endif
5463 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005464
5465#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005466 if ( wp->w_p_cole > 0
5467 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005468 conceal_cursor_line(wp))
5469 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005470 && !(lnum_in_visual_area
5471 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005472 {
5473 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005474 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005475 && (syn_get_sub_char() != NUL || match_conc
5476 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005477 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005478 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005479 /* First time at this concealed item: display one
5480 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005481 if (match_conc)
5482 c = match_conc;
5483 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005484 c = syn_get_sub_char();
5485 else if (lcs_conceal != NUL)
5486 c = lcs_conceal;
5487 else
5488 c = ' ';
5489
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005490 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005491
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005492 if (n_extra > 0)
5493 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005494 vcol += n_extra;
5495 if (wp->w_p_wrap && n_extra > 0)
5496 {
5497# ifdef FEAT_RIGHTLEFT
5498 if (wp->w_p_rl)
5499 {
5500 col -= n_extra;
5501 boguscols -= n_extra;
5502 }
5503 else
5504# endif
5505 {
5506 boguscols += n_extra;
5507 col += n_extra;
5508 }
5509 }
5510 n_extra = 0;
5511 n_attr = 0;
5512 }
5513 else if (n_skip == 0)
5514 {
5515 is_concealing = TRUE;
5516 n_skip = 1;
5517 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005518 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005519 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005520 {
5521 mb_utf8 = TRUE;
5522 u8cc[0] = 0;
5523 c = 0xc0;
5524 }
5525 else
5526 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005527 }
5528 else
5529 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005530 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005531 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005532 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005533
5534 if (n_skip > 0 && did_decrement_ptr)
5535 // not showing the '>', put pointer back to avoid getting stuck
5536 ++ptr;
5537
5538#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 }
5540
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005541#ifdef FEAT_CONCEAL
5542 /* In the cursor line and we may be concealing characters: correct
5543 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005544 if (!did_wcol && draw_state == WL_LINE
5545 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005546 && conceal_cursor_line(wp)
5547 && (int)wp->w_virtcol <= vcol + n_skip)
5548 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005549# ifdef FEAT_RIGHTLEFT
5550 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005551 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005552 else
5553# endif
5554 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005555 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005556 did_wcol = TRUE;
5557 }
5558#endif
5559
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 /* Don't override visual selection highlighting. */
5561 if (n_attr > 0
5562 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005563 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 char_attr = extra_attr;
5565
Bram Moolenaar81695252004-12-29 20:58:21 +00005566#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 /* XIM don't send preedit_start and preedit_end, but they send
5568 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5569 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005570 if (p_imst == IM_ON_THE_SPOT
5571 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005572 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 && (State & INSERT)
5574 && !p_imdisable
5575 && im_is_preediting()
5576 && draw_state == WL_LINE)
5577 {
5578 colnr_T tcol;
5579
5580 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005581 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 else
5583 tcol = preedit_end_col;
5584 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5585 {
5586 if (feedback_old_attr < 0)
5587 {
5588 feedback_col = 0;
5589 feedback_old_attr = char_attr;
5590 }
5591 char_attr = im_get_feedback_attr(feedback_col);
5592 if (char_attr < 0)
5593 char_attr = feedback_old_attr;
5594 feedback_col++;
5595 }
5596 else if (feedback_old_attr >= 0)
5597 {
5598 char_attr = feedback_old_attr;
5599 feedback_old_attr = -1;
5600 feedback_col = 0;
5601 }
5602 }
5603#endif
5604 /*
5605 * Handle the case where we are in column 0 but not on the first
5606 * character of the line and the user wants us to show us a
5607 * special character (via 'listchars' option "precedes:<char>".
5608 */
5609 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005610 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5612#ifdef FEAT_DIFF
5613 && filler_todo <= 0
5614#endif
5615 && draw_state > WL_NR
5616 && c != NUL)
5617 {
5618 c = lcs_prec;
5619 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005620 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5621 {
5622 /* Double-width character being overwritten by the "precedes"
5623 * character, need to fill up half the character. */
5624 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005625 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005626 n_extra = 1;
5627 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005628 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005631 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 {
5633 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005634 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005635 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
5637 else
5638 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005639 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 {
5641 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005642 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 n_attr3 = 1;
5644 }
5645 }
5646
5647 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005648 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005650 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005651#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005652 || did_line_attr == 1
5653#endif
5654 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005656#ifdef FEAT_SEARCH_EXTRA
5657 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005658
5659 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005660 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005661 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005662#endif
5663
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005664 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 * highlight match at end of line. If it's beyond the last
5666 * char on the screen, just overwrite that one (tricky!) Not
5667 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005668#ifdef FEAT_SEARCH_EXTRA
5669 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005670 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005671 prevcol_hl_flag = TRUE;
5672 else
5673 {
5674 cur = wp->w_match_head;
5675 while (cur != NULL)
5676 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005677 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005678 {
5679 prevcol_hl_flag = TRUE;
5680 break;
5681 }
5682 cur = cur->next;
5683 }
5684 }
5685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005687 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005688 && (VIsual_mode != Ctrl_V
5689 || lnum == VIsual.lnum
5690 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005691 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692#ifdef FEAT_SEARCH_EXTRA
5693 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005694 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005695# ifdef FEAT_SYN_HL
5696 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5697 && !(wp == curwin && VIsual_active))
5698# endif
5699# ifdef FEAT_DIFF
5700 && diff_hlf == (hlf_T)0
5701# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005702# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005703 && did_line_attr <= 1
5704# endif
5705 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706#endif
5707 ))
5708 {
5709 int n = 0;
5710
5711#ifdef FEAT_RIGHTLEFT
5712 if (wp->w_p_rl)
5713 {
5714 if (col < 0)
5715 n = 1;
5716 }
5717 else
5718#endif
5719 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005720 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 n = -1;
5722 }
5723 if (n != 0)
5724 {
5725 /* At the window boundary, highlight the last character
5726 * instead (better than nothing). */
5727 off += n;
5728 col += n;
5729 }
5730 else
5731 {
5732 /* Add a blank character to highlight. */
5733 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 if (enc_utf8)
5735 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 }
5737#ifdef FEAT_SEARCH_EXTRA
5738 if (area_attr == 0)
5739 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005740 /* Use attributes from match with highest priority among
5741 * 'search_hl' and the match list. */
5742 char_attr = search_hl.attr;
5743 cur = wp->w_match_head;
5744 shl_flag = FALSE;
5745 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005746 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005747 if (shl_flag == FALSE
5748 && ((cur != NULL
5749 && cur->priority > SEARCH_HL_PRIORITY)
5750 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005751 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005752 shl = &search_hl;
5753 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005754 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005755 else
5756 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005757 if ((ptr - line) - 1 == (long)shl->startcol
5758 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005759 char_attr = shl->attr;
5760 if (shl != &search_hl && cur != NULL)
5761 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 }
5764#endif
5765 ScreenAttrs[off] = char_attr;
5766#ifdef FEAT_RIGHTLEFT
5767 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005768 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005770 --off;
5771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 else
5773#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005774 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005776 ++off;
5777 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005778 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005779#ifdef FEAT_SYN_HL
5780 eol_hl_off = 1;
5781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
Bram Moolenaar91170f82006-05-05 21:15:17 +00005785 /*
5786 * At end of the text line.
5787 */
5788 if (c == NUL)
5789 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005790#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005791 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005792 if (wp->w_p_wrap)
5793 v = wp->w_skipcol;
5794 else
5795 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005796
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005797 /* check if line ends before left margin */
5798 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005799 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005800#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005801 // Get rid of the boguscols now, we want to draw until the right
5802 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005803 col -= boguscols;
5804 boguscols = 0;
5805#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005806
5807 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005808 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005809
5810 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005811 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5812 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005813 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005814 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005815 || draw_color_col
5816 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005817# ifdef FEAT_RIGHTLEFT
5818 && !wp->w_p_rl
5819# endif
5820 )
5821 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005822 int rightmost_vcol = 0;
5823 int i;
5824
5825 if (wp->w_p_cuc)
5826 rightmost_vcol = wp->w_virtcol;
5827 if (draw_color_col)
5828 /* determine rightmost colorcolumn to possibly draw */
5829 for (i = 0; color_cols[i] >= 0; ++i)
5830 if (rightmost_vcol < color_cols[i])
5831 rightmost_vcol = color_cols[i];
5832
Bram Moolenaar02631462017-09-22 15:20:32 +02005833 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005834 {
5835 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005836 if (enc_utf8)
5837 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005838 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005839 if (draw_color_col)
5840 draw_color_col = advance_color_col(VCOL_HLC,
5841 &color_cols);
5842
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005843 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005844 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005845 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005846 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005847 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005848 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005849
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005850 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005851 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005852
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005853 ++vcol;
5854 }
5855 }
5856#endif
5857
Bram Moolenaar53f81742017-09-22 14:35:51 +02005858 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005859 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 row++;
5861
5862 /*
5863 * Update w_cline_height and w_cline_folded if the cursor line was
5864 * updated (saves a call to plines() later).
5865 */
5866 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5867 {
5868 curwin->w_cline_row = startrow;
5869 curwin->w_cline_height = row - startrow;
5870#ifdef FEAT_FOLDING
5871 curwin->w_cline_folded = FALSE;
5872#endif
5873 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5874 }
5875
5876 break;
5877 }
5878
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005879 // Show "extends" character from 'listchars' if beyond the line end and
5880 // 'list' is set.
5881 if (lcs_ext != NUL
5882 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 && !wp->w_p_wrap
5884#ifdef FEAT_DIFF
5885 && filler_todo <= 0
5886#endif
5887 && (
5888#ifdef FEAT_RIGHTLEFT
5889 wp->w_p_rl ? col == 0 :
5890#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005891 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005893 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5895 {
5896 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005897 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005899 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 {
5901 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005902 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005903 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 }
5905 else
5906 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
5908
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005909#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005910 /* advance to the next 'colorcolumn' */
5911 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005912 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005913
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005914 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005915 * highlight the cursor position itself.
5916 * Also highlight the 'colorcolumn' if it is different than
5917 * 'cursorcolumn' */
5918 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005919 if (draw_state == WL_LINE && !lnum_in_visual_area
5920 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005921 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005922 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005923 && lnum != wp->w_cursor.lnum)
5924 {
5925 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005926 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005927 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005928 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005929 {
5930 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005931 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005932 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005933 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005934#endif
5935
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 /*
5937 * Store character to be displayed.
5938 * Skip characters that are left of the screen for 'nowrap'.
5939 */
5940 vcol_prev = vcol;
5941 if (draw_state < WL_LINE || n_skip <= 0)
5942 {
5943 /*
5944 * Store the character.
5945 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005946#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5948 {
5949 /* A double-wide character is: put first halve in left cell. */
5950 --off;
5951 --col;
5952 }
5953#endif
5954 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005956 {
5957 if ((mb_c & 0xff00) == 0x8e00)
5958 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 else if (enc_utf8)
5962 {
5963 if (mb_utf8)
5964 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005965 int i;
5966
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005968 if ((c & 0xff) == 0)
5969 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005970 for (i = 0; i < Screen_mco; ++i)
5971 {
5972 ScreenLinesC[i][off] = u8cc[i];
5973 if (u8cc[i] == 0)
5974 break;
5975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 }
5977 else
5978 ScreenLinesUC[off] = 0;
5979 }
5980 if (multi_attr)
5981 {
5982 ScreenAttrs[off] = multi_attr;
5983 multi_attr = 0;
5984 }
5985 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 ScreenAttrs[off] = char_attr;
5987
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5989 {
5990 /* Need to fill two screen columns. */
5991 ++off;
5992 ++col;
5993 if (enc_utf8)
5994 /* UTF-8: Put a 0 in the second screen char. */
5995 ScreenLines[off] = 0;
5996 else
5997 /* DBCS: Put second byte in the second screen char. */
5998 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005999 if (draw_state > WL_NR
6000#ifdef FEAT_DIFF
6001 && filler_todo <= 0
6002#endif
6003 )
6004 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 /* When "tocol" is halfway a character, set it to the end of
6006 * the character, otherwise highlighting won't stop. */
6007 if (tocol == vcol)
6008 ++tocol;
6009#ifdef FEAT_RIGHTLEFT
6010 if (wp->w_p_rl)
6011 {
6012 /* now it's time to backup one cell */
6013 --off;
6014 --col;
6015 }
6016#endif
6017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018#ifdef FEAT_RIGHTLEFT
6019 if (wp->w_p_rl)
6020 {
6021 --off;
6022 --col;
6023 }
6024 else
6025#endif
6026 {
6027 ++off;
6028 ++col;
6029 }
6030 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006031#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006032 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006033 {
6034 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006035 ++vcol_off;
6036 if (n_extra > 0)
6037 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02006038 if (wp->w_p_wrap)
6039 {
6040 /*
6041 * Special voodoo required if 'wrap' is on.
6042 *
6043 * Advance the column indicator to force the line
6044 * drawing to wrap early. This will make the line
6045 * take up the same screen space when parts are concealed,
6046 * so that cursor line computations aren't messed up.
6047 *
6048 * To avoid the fictitious advance of 'col' causing
6049 * trailing junk to be written out of the screen line
6050 * we are building, 'boguscols' keeps track of the number
6051 * of bad columns we have advanced.
6052 */
6053 if (n_extra > 0)
6054 {
6055 vcol += n_extra;
6056# ifdef FEAT_RIGHTLEFT
6057 if (wp->w_p_rl)
6058 {
6059 col -= n_extra;
6060 boguscols -= n_extra;
6061 }
6062 else
6063# endif
6064 {
6065 col += n_extra;
6066 boguscols += n_extra;
6067 }
6068 n_extra = 0;
6069 n_attr = 0;
6070 }
6071
6072
Bram Moolenaar860cae12010-06-05 23:22:07 +02006073 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6074 {
6075 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006076# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02006077 if (wp->w_p_rl)
6078 {
6079 --boguscols;
6080 --col;
6081 }
6082 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01006083# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006084 {
6085 ++boguscols;
6086 ++col;
6087 }
6088 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006089
6090# ifdef FEAT_RIGHTLEFT
6091 if (wp->w_p_rl)
6092 {
6093 --boguscols;
6094 --col;
6095 }
6096 else
6097# endif
6098 {
6099 ++boguscols;
6100 ++col;
6101 }
6102 }
6103 else
6104 {
6105 if (n_extra > 0)
6106 {
6107 vcol += n_extra;
6108 n_extra = 0;
6109 n_attr = 0;
6110 }
6111 }
6112
6113 }
6114#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 else
6116 --n_skip;
6117
Bram Moolenaar64486672010-05-16 15:46:46 +02006118 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
6119 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00006120 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121#ifdef FEAT_DIFF
6122 && filler_todo <= 0
6123#endif
6124 )
6125 ++vcol;
6126
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006127#ifdef FEAT_SYN_HL
6128 if (vcol_save_attr >= 0)
6129 char_attr = vcol_save_attr;
6130#endif
6131
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 /* restore attributes after "predeces" in 'listchars' */
6133 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6134 char_attr = saved_attr3;
6135
6136 /* restore attributes after last 'listchars' or 'number' char */
6137 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6138 char_attr = saved_attr2;
6139
6140 /*
6141 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006142 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 */
6144 if ((
6145#ifdef FEAT_RIGHTLEFT
6146 wp->w_p_rl ? (col < 0) :
6147#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006148 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 && (*ptr != NUL
6150#ifdef FEAT_DIFF
6151 || filler_todo > 0
6152#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006153 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6155 )
6156 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006157#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006158 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006159 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006160 boguscols = 0;
6161#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006162 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006163 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 ++row;
6166 ++screen_row;
6167
6168 /* When not wrapping and finished diff lines, or when displayed
6169 * '$' and highlighting until last column, break here. */
6170 if ((!wp->w_p_wrap
6171#ifdef FEAT_DIFF
6172 && filler_todo <= 0
6173#endif
6174 ) || lcs_eol_one == -1)
6175 break;
6176
6177 /* When the window is too narrow draw all "@" lines. */
6178 if (draw_state != WL_LINE
6179#ifdef FEAT_DIFF
6180 && filler_todo <= 0
6181#endif
6182 )
6183 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006184 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 row = endrow;
6187 }
6188
6189 /* When line got too long for screen break here. */
6190 if (row == endrow)
6191 {
6192 ++row;
6193 break;
6194 }
6195
6196 if (screen_cur_row == screen_row - 1
6197#ifdef FEAT_DIFF
6198 && filler_todo <= 0
6199#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006200 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 {
6202 /* Remember that the line wraps, used for modeless copy. */
6203 LineWraps[screen_row - 1] = TRUE;
6204
6205 /*
6206 * Special trick to make copy/paste of wrapped lines work with
6207 * xterm/screen: write an extra character beyond the end of
6208 * the line. This will work with all terminal types
6209 * (regardless of the xn,am settings).
6210 * Only do this on a fast tty.
6211 * Only do this if the cursor is on the current line
6212 * (something has been written in it).
6213 * Don't do this for the GUI.
6214 * Don't do this for double-width characters.
6215 * Don't do this for a window not at the right screen border.
6216 */
6217 if (p_tf
6218#ifdef FEAT_GUI
6219 && !gui.in_use
6220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006222 && ((*mb_off2cells)(LineOffset[screen_row],
6223 LineOffset[screen_row] + screen_Columns)
6224 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006226 + (int)Columns - 2,
6227 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006228 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 {
6230 /* First make sure we are at the end of the screen line,
6231 * then output the same character again to let the
6232 * terminal know about the wrap. If the terminal doesn't
6233 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006234 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 screen_char(LineOffset[screen_row - 1]
6236 + (unsigned)Columns - 1,
6237 screen_row - 1, (int)(Columns - 1));
6238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 /* When there is a multi-byte character, just output a
6240 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006241 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6242 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 out_char(' ');
6244 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 out_char(ScreenLines[LineOffset[screen_row - 1]
6246 + (Columns - 1)]);
6247 /* force a redraw of the first char on the next line */
6248 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6249 screen_start(); /* don't know where cursor is now */
6250 }
6251 }
6252
6253 col = 0;
6254 off = (unsigned)(current_ScreenLine - ScreenLines);
6255#ifdef FEAT_RIGHTLEFT
6256 if (wp->w_p_rl)
6257 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006258 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 off += col;
6260 }
6261#endif
6262
6263 /* reset the drawing state for the start of a wrapped line */
6264 draw_state = WL_START;
6265 saved_n_extra = n_extra;
6266 saved_p_extra = p_extra;
6267 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006268 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 saved_char_attr = char_attr;
6270 n_extra = 0;
6271 lcs_prec_todo = lcs_prec;
6272#ifdef FEAT_LINEBREAK
6273# ifdef FEAT_DIFF
6274 if (filler_todo <= 0)
6275# endif
6276 need_showbreak = TRUE;
6277#endif
6278#ifdef FEAT_DIFF
6279 --filler_todo;
6280 /* When the filler lines are actually below the last line of the
6281 * file, don't draw the line itself, break here. */
6282 if (filler_todo == 0 && wp->w_botfill)
6283 break;
6284#endif
6285 }
6286
6287 } /* for every character in the line */
6288
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006289#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006290 /* After an empty line check first word for capital. */
6291 if (*skipwhite(line) == NUL)
6292 {
6293 capcol_lnum = lnum + 1;
6294 cap_col = 0;
6295 }
6296#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006297#ifdef FEAT_TEXT_PROP
6298 vim_free(text_props);
6299 vim_free(text_prop_idxs);
6300#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006301
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006302 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 return row;
6304}
6305
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006306/*
6307 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006308 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006309 */
6310 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006311comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006312{
6313 int i;
6314
6315 for (i = 0; i < Screen_mco; ++i)
6316 {
6317 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6318 return TRUE;
6319 if (ScreenLinesC[i][off_from] == 0)
6320 break;
6321 }
6322 return FALSE;
6323}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006324
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325/*
6326 * Check whether the given character needs redrawing:
6327 * - the (first byte of the) character is different
6328 * - the attributes are different
6329 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006330 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 */
6332 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006333char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334{
6335 if (cols > 0
6336 && ((ScreenLines[off_from] != ScreenLines[off_to]
6337 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 || (enc_dbcs != 0
6339 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6340 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6341 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6342 : (cols > 1 && ScreenLines[off_from + 1]
6343 != ScreenLines[off_to + 1])))
6344 || (enc_utf8
6345 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6346 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006347 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006348 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6349 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006350 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 return TRUE;
6352 return FALSE;
6353}
6354
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006355#if defined(FEAT_TERMINAL) || defined(PROTO)
6356/*
6357 * Return the index in ScreenLines[] for the current screen line.
6358 */
6359 int
6360screen_get_current_line_off()
6361{
6362 return (int)(current_ScreenLine - ScreenLines);
6363}
6364#endif
6365
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366/*
6367 * Move one "cooked" screen line to the screen, but only the characters that
6368 * have actually changed. Handle insert/delete character.
6369 * "coloff" gives the first column on the screen for this line.
6370 * "endcol" gives the columns where valid characters are.
6371 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6372 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006373 * "flags" can have bits:
6374 * SLF_POPUP popup window
6375 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6377 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6378 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006379 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006380screen_line(
6381 int row,
6382 int coloff,
6383 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006384 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006385 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 unsigned off_from;
6388 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006389 unsigned max_off_from;
6390 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 int force = FALSE; /* force update rest of the line */
6394 int redraw_this /* bool: does character need redraw? */
6395#ifdef FEAT_GUI
6396 = TRUE /* For GUI when while-loop empty */
6397#endif
6398 ;
6399 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 int clear_next = FALSE;
6401 int char_cells; /* 1: normal char */
6402 /* 2: occupies two display cells */
6403# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006405 /* Check for illegal row and col, just in case. */
6406 if (row >= Rows)
6407 row = Rows - 1;
6408 if (endcol > Columns)
6409 endcol = Columns;
6410
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411# ifdef FEAT_CLIPBOARD
6412 clip_may_clear_selection(row, row);
6413# endif
6414
6415 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6416 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006417 max_off_from = off_from + screen_Columns;
6418 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
6420#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006421 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 {
6423 /* Clear rest first, because it's left of the text. */
6424 if (clear_width > 0)
6425 {
6426 while (col <= endcol && ScreenLines[off_to] == ' '
6427 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006428 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 {
6430 ++off_to;
6431 ++col;
6432 }
6433 if (col <= endcol)
6434 screen_fill(row, row + 1, col + coloff,
6435 endcol + coloff + 1, ' ', ' ', 0);
6436 }
6437 col = endcol + 1;
6438 off_to = LineOffset[row] + col + coloff;
6439 off_from += col;
6440 endcol = (clear_width > 0 ? clear_width : -clear_width);
6441 }
6442#endif /* FEAT_RIGHTLEFT */
6443
6444 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6445
6446 while (col < endcol)
6447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006449 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 else
6451 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
6453 redraw_this = redraw_next;
6454 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6455 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6456
6457#ifdef FEAT_GUI
6458 /* If the next character was bold, then redraw the current character to
6459 * remove any pixels that might have spilt over into us. This only
6460 * happens in the GUI.
6461 */
6462 if (redraw_next && gui.in_use)
6463 {
6464 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006465 if (hl > HL_ALL)
6466 hl = syn_attr2attr(hl);
6467 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 redraw_this = TRUE;
6469 }
6470#endif
6471
6472 if (redraw_this)
6473 {
6474 /*
6475 * Special handling when 'xs' termcap flag set (hpterm):
6476 * Attributes for characters are stored at the position where the
6477 * cursor is when writing the highlighting code. The
6478 * start-highlighting code must be written with the cursor on the
6479 * first highlighted character. The stop-highlighting code must
6480 * be written with the cursor just after the last highlighted
6481 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006482 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 * to clear the rest of the line, and force redrawing it
6484 * completely.
6485 */
6486 if ( p_wiv
6487 && !force
6488#ifdef FEAT_GUI
6489 && !gui.in_use
6490#endif
6491 && ScreenAttrs[off_to] != 0
6492 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6493 {
6494 /*
6495 * Need to remove highlighting attributes here.
6496 */
6497 windgoto(row, col + coloff);
6498 out_str(T_CE); /* clear rest of this screen line */
6499 screen_start(); /* don't know where cursor is now */
6500 force = TRUE; /* force redraw of rest of the line */
6501 redraw_next = TRUE; /* or else next char would miss out */
6502
6503 /*
6504 * If the previous character was highlighted, need to stop
6505 * highlighting at this character.
6506 */
6507 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6508 {
6509 screen_attr = ScreenAttrs[off_to - 1];
6510 term_windgoto(row, col + coloff);
6511 screen_stop_highlight();
6512 }
6513 else
6514 screen_attr = 0; /* highlighting has stopped */
6515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 if (enc_dbcs != 0)
6517 {
6518 /* Check if overwriting a double-byte with a single-byte or
6519 * the other way around requires another character to be
6520 * redrawn. For UTF-8 this isn't needed, because comparing
6521 * ScreenLinesUC[] is sufficient. */
6522 if (char_cells == 1
6523 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006524 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 {
6526 /* Writing a single-cell character over a double-cell
6527 * character: need to redraw the next cell. */
6528 ScreenLines[off_to + 1] = 0;
6529 redraw_next = TRUE;
6530 }
6531 else if (char_cells == 2
6532 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006533 && (*mb_off2cells)(off_to, max_off_to) == 1
6534 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
6536 /* Writing the second half of a double-cell character over
6537 * a double-cell character: need to redraw the second
6538 * cell. */
6539 ScreenLines[off_to + 2] = 0;
6540 redraw_next = TRUE;
6541 }
6542
6543 if (enc_dbcs == DBCS_JPNU)
6544 ScreenLines2[off_to] = ScreenLines2[off_from];
6545 }
6546 /* When writing a single-width character over a double-width
6547 * character and at the end of the redrawn text, need to clear out
6548 * the right halve of the old character.
6549 * Also required when writing the right halve of a double-width
6550 * char over the left halve of an existing one. */
6551 if (has_mbyte && col + char_cells == endcol
6552 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006553 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006555 && (*mb_off2cells)(off_to, max_off_to) == 1
6556 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558
6559 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 if (enc_utf8)
6561 {
6562 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6563 if (ScreenLinesUC[off_from] != 0)
6564 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006565 int i;
6566
6567 for (i = 0; i < Screen_mco; ++i)
6568 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 }
6570 }
6571 if (char_cells == 2)
6572 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573
6574#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006575 /* The bold trick makes a single column of pixels appear in the
6576 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 * character should be redrawn too. This happens for our own GUI
6578 * and for some xterms. */
6579 if (
6580# ifdef FEAT_GUI
6581 gui.in_use
6582# endif
6583# if defined(FEAT_GUI) && defined(UNIX)
6584 ||
6585# endif
6586# ifdef UNIX
6587 term_is_xterm
6588# endif
6589 )
6590 {
6591 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006592 if (hl > HL_ALL)
6593 hl = syn_attr2attr(hl);
6594 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 redraw_next = TRUE;
6596 }
6597#endif
6598 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006599
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006600 /* For simplicity set the attributes of second half of a
6601 * double-wide character equal to the first half. */
6602 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006604
6605 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 screen_char(off_to, row, col + coloff);
6609 }
6610 else if ( p_wiv
6611#ifdef FEAT_GUI
6612 && !gui.in_use
6613#endif
6614 && col + coloff > 0)
6615 {
6616 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6617 {
6618 /*
6619 * Don't output stop-highlight when moving the cursor, it will
6620 * stop the highlighting when it should continue.
6621 */
6622 screen_attr = 0;
6623 }
6624 else if (screen_attr != 0)
6625 screen_stop_highlight();
6626 }
6627
6628 off_to += CHAR_CELLS;
6629 off_from += CHAR_CELLS;
6630 col += CHAR_CELLS;
6631 }
6632
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 if (clear_next)
6634 {
6635 /* Clear the second half of a double-wide character of which the left
6636 * half was overwritten with a single-wide character. */
6637 ScreenLines[off_to] = ' ';
6638 if (enc_utf8)
6639 ScreenLinesUC[off_to] = 0;
6640 screen_char(off_to, row, col + coloff);
6641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642
6643 if (clear_width > 0
6644#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006645 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646#endif
6647 )
6648 {
6649#ifdef FEAT_GUI
6650 int startCol = col;
6651#endif
6652
6653 /* blank out the rest of the line */
6654 while (col < clear_width && ScreenLines[off_to] == ' '
6655 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006656 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 {
6658 ++off_to;
6659 ++col;
6660 }
6661 if (col < clear_width)
6662 {
6663#ifdef FEAT_GUI
6664 /*
6665 * In the GUI, clearing the rest of the line may leave pixels
6666 * behind if the first character cleared was bold. Some bold
6667 * fonts spill over the left. In this case we redraw the previous
6668 * character too. If we didn't skip any blanks above, then we
6669 * only redraw if the character wasn't already redrawn anyway.
6670 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006671 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 {
6673 hl = ScreenAttrs[off_to];
6674 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006675 {
6676 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006677
Bram Moolenaar9c697322006-10-09 20:11:17 +00006678 if (enc_utf8)
6679 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6680 * that its width is 2. */
6681 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6682 else if (enc_dbcs != 0)
6683 {
6684 /* find previous character by counting from first
6685 * column and get its width. */
6686 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006687 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006688
6689 while (off < off_to)
6690 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006691 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006692 off += prev_cells;
6693 }
6694 }
6695
6696 if (enc_dbcs != 0 && prev_cells > 1)
6697 screen_char_2(off_to - prev_cells, row,
6698 col + coloff - prev_cells);
6699 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006700 screen_char(off_to - prev_cells, row,
6701 col + coloff - prev_cells);
6702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 }
6704#endif
6705 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6706 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 off_to += clear_width - col;
6708 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 }
6710 }
6711
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006712 if (clear_width > 0
6713#ifdef FEAT_TEXT_PROP
6714 && !(flags & SLF_POPUP) // no separator for popup window
6715#endif
6716 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006718 // For a window that has a right neighbor, draw the separator char
6719 // right of the window contents.
6720 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 {
6722 int c;
6723
6724 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006725 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006726 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6727 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 || ScreenAttrs[off_to] != hl)
6729 {
6730 ScreenLines[off_to] = c;
6731 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 if (enc_utf8)
6733 {
6734 if (c >= 0x80)
6735 {
6736 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006737 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 }
6739 else
6740 ScreenLinesUC[off_to] = 0;
6741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 screen_char(off_to, row, col + coloff);
6743 }
6744 }
6745 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 LineWraps[row] = FALSE;
6747 }
6748}
6749
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006750#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006752 * Mirror text "str" for right-left displaying.
6753 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006755 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006756rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757{
6758 char_u *p1, *p2;
6759 int t;
6760
6761 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6762 {
6763 t = *p1;
6764 *p1 = *p2;
6765 *p2 = t;
6766 }
6767}
6768#endif
6769
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770/*
6771 * mark all status lines for redraw; used after first :cd
6772 */
6773 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006774status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775{
6776 win_T *wp;
6777
Bram Moolenaar29323592016-07-24 22:04:11 +02006778 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 if (wp->w_status_height)
6780 {
6781 wp->w_redr_status = TRUE;
6782 redraw_later(VALID);
6783 }
6784}
6785
6786/*
6787 * mark all status lines of the current buffer for redraw
6788 */
6789 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006790status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791{
6792 win_T *wp;
6793
Bram Moolenaar29323592016-07-24 22:04:11 +02006794 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6796 {
6797 wp->w_redr_status = TRUE;
6798 redraw_later(VALID);
6799 }
6800}
6801
6802/*
6803 * Redraw all status lines that need to be redrawn.
6804 */
6805 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006806redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807{
6808 win_T *wp;
6809
Bram Moolenaar29323592016-07-24 22:04:11 +02006810 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006812 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006813 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006814 draw_tabline();
Bram Moolenaar988c4332019-06-02 14:12:11 +02006815
6816#ifdef FEAT_TEXT_PROP
6817 // Display popup windows on top of the status lines.
6818 update_popups();
6819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821
Bram Moolenaar4033c552017-09-16 20:54:51 +02006822#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823/*
6824 * Redraw all status lines at the bottom of frame "frp".
6825 */
6826 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006827win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828{
6829 if (frp->fr_layout == FR_LEAF)
6830 frp->fr_win->w_redr_status = TRUE;
6831 else if (frp->fr_layout == FR_ROW)
6832 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006833 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 win_redraw_last_status(frp);
6835 }
6836 else /* frp->fr_layout == FR_COL */
6837 {
6838 frp = frp->fr_child;
6839 while (frp->fr_next != NULL)
6840 frp = frp->fr_next;
6841 win_redraw_last_status(frp);
6842 }
6843}
6844#endif
6845
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846/*
6847 * Draw the verticap separator right of window "wp" starting with line "row".
6848 */
6849 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006850draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851{
6852 int hl;
6853 int c;
6854
6855 if (wp->w_vsep_width)
6856 {
6857 /* draw the vertical separator right of this window */
6858 c = fillchar_vsep(&hl);
6859 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6860 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6861 c, ' ', hl);
6862 }
6863}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864
6865#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006866static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867
6868/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006869 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 */
6871 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006872status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873{
6874 int len = 0;
6875
6876#ifdef FEAT_MENU
6877 int emenu = (xp->xp_context == EXPAND_MENUS
6878 || xp->xp_context == EXPAND_MENUNAMES);
6879
6880 /* Check for menu separators - replace with '|'. */
6881 if (emenu && menu_is_separator(s))
6882 return 1;
6883#endif
6884
6885 while (*s != NUL)
6886 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006887 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006888 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006889 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 }
6891
6892 return len;
6893}
6894
6895/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006896 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006897 * These are backslashes used for escaping. Do show backslashes in help tags.
6898 */
6899 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006900skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006901{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006902 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006903#ifdef FEAT_MENU
6904 || ((xp->xp_context == EXPAND_MENUS
6905 || xp->xp_context == EXPAND_MENUNAMES)
6906 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6907#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006908 )
6909 {
6910#ifndef BACKSLASH_IN_FILENAME
6911 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6912 return 2;
6913#endif
6914 return 1;
6915 }
6916 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006917}
6918
6919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 * Show wildchar matches in the status line.
6921 * Show at least the "match" item.
6922 * We start at item 'first_match' in the list and show all matches that fit.
6923 *
6924 * If inversion is possible we use it. Else '=' characters are used.
6925 */
6926 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006927win_redr_status_matches(
6928 expand_T *xp,
6929 int num_matches,
6930 char_u **matches, /* list of matches */
6931 int match,
6932 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933{
6934#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6935 int row;
6936 char_u *buf;
6937 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006938 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 int fillchar;
6940 int attr;
6941 int i;
6942 int highlight = TRUE;
6943 char_u *selstart = NULL;
6944 int selstart_col = 0;
6945 char_u *selend = NULL;
6946 static int first_match = 0;
6947 int add_left = FALSE;
6948 char_u *s;
6949#ifdef FEAT_MENU
6950 int emenu;
6951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
6954 if (matches == NULL) /* interrupted completion? */
6955 return;
6956
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006957 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006958 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006959 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006960 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if (buf == NULL)
6962 return;
6963
6964 if (match == -1) /* don't show match but original text */
6965 {
6966 match = 0;
6967 highlight = FALSE;
6968 }
6969 /* count 1 for the ending ">" */
6970 clen = status_match_len(xp, L_MATCH(match)) + 3;
6971 if (match == 0)
6972 first_match = 0;
6973 else if (match < first_match)
6974 {
6975 /* jumping left, as far as we can go */
6976 first_match = match;
6977 add_left = TRUE;
6978 }
6979 else
6980 {
6981 /* check if match fits on the screen */
6982 for (i = first_match; i < match; ++i)
6983 clen += status_match_len(xp, L_MATCH(i)) + 2;
6984 if (first_match > 0)
6985 clen += 2;
6986 /* jumping right, put match at the left */
6987 if ((long)clen > Columns)
6988 {
6989 first_match = match;
6990 /* if showing the last match, we can add some on the left */
6991 clen = 2;
6992 for (i = match; i < num_matches; ++i)
6993 {
6994 clen += status_match_len(xp, L_MATCH(i)) + 2;
6995 if ((long)clen >= Columns)
6996 break;
6997 }
6998 if (i == num_matches)
6999 add_left = TRUE;
7000 }
7001 }
7002 if (add_left)
7003 while (first_match > 0)
7004 {
7005 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
7006 if ((long)clen >= Columns)
7007 break;
7008 --first_match;
7009 }
7010
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007011 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012
7013 if (first_match == 0)
7014 {
7015 *buf = NUL;
7016 len = 0;
7017 }
7018 else
7019 {
7020 STRCPY(buf, "< ");
7021 len = 2;
7022 }
7023 clen = len;
7024
7025 i = first_match;
7026 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
7027 {
7028 if (i == match)
7029 {
7030 selstart = buf + len;
7031 selstart_col = clen;
7032 }
7033
7034 s = L_MATCH(i);
7035 /* Check for menu separators - replace with '|' */
7036#ifdef FEAT_MENU
7037 emenu = (xp->xp_context == EXPAND_MENUS
7038 || xp->xp_context == EXPAND_MENUNAMES);
7039 if (emenu && menu_is_separator(s))
7040 {
7041 STRCPY(buf + len, transchar('|'));
7042 l = (int)STRLEN(buf + len);
7043 len += l;
7044 clen += l;
7045 }
7046 else
7047#endif
7048 for ( ; *s != NUL; ++s)
7049 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007050 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007052 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 {
7054 STRNCPY(buf + len, s, l);
7055 s += l - 1;
7056 len += l;
7057 }
7058 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {
7060 STRCPY(buf + len, transchar_byte(*s));
7061 len += (int)STRLEN(buf + len);
7062 }
7063 }
7064 if (i == match)
7065 selend = buf + len;
7066
7067 *(buf + len++) = ' ';
7068 *(buf + len++) = ' ';
7069 clen += 2;
7070 if (++i == num_matches)
7071 break;
7072 }
7073
7074 if (i != num_matches)
7075 {
7076 *(buf + len++) = '>';
7077 ++clen;
7078 }
7079
7080 buf[len] = NUL;
7081
7082 row = cmdline_row - 1;
7083 if (row >= 0)
7084 {
7085 if (wild_menu_showing == 0)
7086 {
7087 if (msg_scrolled > 0)
7088 {
7089 /* Put the wildmenu just above the command line. If there is
7090 * no room, scroll the screen one line up. */
7091 if (cmdline_row == Rows - 1)
7092 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02007093 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 ++msg_scrolled;
7095 }
7096 else
7097 {
7098 ++cmdline_row;
7099 ++row;
7100 }
7101 wild_menu_showing = WM_SCROLLED;
7102 }
7103 else
7104 {
7105 /* Create status line if needed by setting 'laststatus' to 2.
7106 * Set 'winminheight' to zero to avoid that the window is
7107 * resized. */
7108 if (lastwin->w_status_height == 0)
7109 {
7110 save_p_ls = p_ls;
7111 save_p_wmh = p_wmh;
7112 p_ls = 2;
7113 p_wmh = 0;
7114 last_status(FALSE);
7115 }
7116 wild_menu_showing = WM_SHOWN;
7117 }
7118 }
7119
7120 screen_puts(buf, row, 0, attr);
7121 if (selstart != NULL && highlight)
7122 {
7123 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007124 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 }
7126
7127 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7128 }
7129
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 vim_free(buf);
7132}
7133#endif
7134
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135/*
7136 * Redraw the status line of window wp.
7137 *
7138 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007139 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7140 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007142 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007143win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144{
7145 int row;
7146 char_u *p;
7147 int len;
7148 int fillchar;
7149 int attr;
7150 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007151 static int busy = FALSE;
7152
7153 /* It's possible to get here recursively when 'statusline' (indirectly)
7154 * invokes ":redrawstatus". Simply ignore the call then. */
7155 if (busy)
7156 return;
7157 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
7159 wp->w_redr_status = FALSE;
7160 if (wp->w_status_height == 0)
7161 {
7162 /* no status line, can only be last window */
7163 redraw_cmdline = TRUE;
7164 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007165 else if (!redrawing()
7166#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007167 // don't update status line when popup menu is visible and may be
7168 // drawn over it, unless it will be redrawn later
7169 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007170#endif
7171 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {
7173 /* Don't redraw right now, do it later. */
7174 wp->w_redr_status = TRUE;
7175 }
7176#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007177 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {
7179 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007180 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 }
7182#endif
7183 else
7184 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007185 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007187 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 p = NameBuff;
7189 len = (int)STRLEN(p);
7190
Bram Moolenaard85f2712017-07-28 21:51:57 +02007191 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192#ifdef FEAT_QUICKFIX
7193 || wp->w_p_pvw
7194#endif
7195 || bufIsChanged(wp->w_buffer)
7196 || wp->w_buffer->b_p_ro)
7197 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007198 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007200 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 len += (int)STRLEN(p + len);
7202 }
7203#ifdef FEAT_QUICKFIX
7204 if (wp->w_p_pvw)
7205 {
7206 STRCPY(p + len, _("[Preview]"));
7207 len += (int)STRLEN(p + len);
7208 }
7209#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007210 if (bufIsChanged(wp->w_buffer)
7211#ifdef FEAT_TERMINAL
7212 && !bt_terminal(wp->w_buffer)
7213#endif
7214 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 {
7216 STRCPY(p + len, "[+]");
7217 len += 3;
7218 }
7219 if (wp->w_buffer->b_p_ro)
7220 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007221 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007222 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 }
7224
Bram Moolenaar02631462017-09-22 15:20:32 +02007225 this_ru_col = ru_col - (Columns - wp->w_width);
7226 if (this_ru_col < (wp->w_width + 1) / 2)
7227 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 if (this_ru_col <= 1)
7229 {
7230 p = (char_u *)"<"; /* No room for file name! */
7231 len = 1;
7232 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007233 else if (has_mbyte)
7234 {
7235 int clen = 0, i;
7236
7237 /* Count total number of display cells. */
7238 clen = mb_string2cells(p, -1);
7239
7240 /* Find first character that will fit.
7241 * Going from start to end is much faster for DBCS. */
7242 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7243 i += (*mb_ptr2len)(p + i))
7244 clen -= (*mb_ptr2cells)(p + i);
7245 len = clen;
7246 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007248 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007250 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 }
7252
Bram Moolenaara12a1612019-01-24 16:39:02 +01007253 }
7254 else if (len > this_ru_col - 1)
7255 {
7256 p += len - (this_ru_col - 1);
7257 *p = '<';
7258 len = this_ru_col - 1;
7259 }
7260
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007262 screen_puts(p, row, wp->w_wincol, attr);
7263 screen_fill(row, row + 1, len + wp->w_wincol,
7264 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007266 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7268 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007269 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270
7271#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007272 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273#endif
7274 }
7275
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 /*
7277 * May need to draw the character below the vertical separator.
7278 */
7279 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7280 {
7281 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007282 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 else
7284 fillchar = fillchar_vsep(&attr);
7285 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7286 attr);
7287 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007288 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289}
7290
Bram Moolenaar238a5642006-02-21 22:12:05 +00007291#ifdef FEAT_STL_OPT
7292/*
7293 * Redraw the status line according to 'statusline' and take care of any
7294 * errors encountered.
7295 */
7296 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007297redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007298{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007299 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007300 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007301
7302 /* When called recursively return. This can happen when the statusline
7303 * contains an expression that triggers a redraw. */
7304 if (entered)
7305 return;
7306 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007307
Bram Moolenaara742e082016-04-05 21:10:38 +02007308 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007309 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007310 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007311 {
7312 /* When there is an error disable the statusline, otherwise the
7313 * display is messed up with errors and a redraw triggers the problem
7314 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007315 set_string_option_direct((char_u *)"statusline", -1,
7316 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007317 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007318 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007319 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007320 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007321}
7322#endif
7323
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324/*
7325 * Return TRUE if the status line of window "wp" is connected to the status
7326 * line of the window right of it. If not, then it's a vertical separator.
7327 * Only call if (wp->w_vsep_width != 0).
7328 */
7329 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007330stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332 frame_T *fr;
7333
7334 fr = wp->w_frame;
7335 while (fr->fr_parent != NULL)
7336 {
7337 if (fr->fr_parent->fr_layout == FR_COL)
7338 {
7339 if (fr->fr_next != NULL)
7340 break;
7341 }
7342 else
7343 {
7344 if (fr->fr_next != NULL)
7345 return TRUE;
7346 }
7347 fr = fr->fr_parent;
7348 }
7349 return FALSE;
7350}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353/*
7354 * Get the value to show for the language mappings, active 'keymap'.
7355 */
7356 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007357get_keymap_str(
7358 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007359 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007360 char_u *buf, /* buffer for the result */
7361 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362{
7363 char_u *p;
7364
7365 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7366 return FALSE;
7367
7368 {
7369#ifdef FEAT_EVAL
7370 buf_T *old_curbuf = curbuf;
7371 win_T *old_curwin = curwin;
7372 char_u *s;
7373
7374 curbuf = wp->w_buffer;
7375 curwin = wp;
7376 STRCPY(buf, "b:keymap_name"); /* must be writable */
7377 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007378 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 --emsg_skip;
7380 curbuf = old_curbuf;
7381 curwin = old_curwin;
7382 if (p == NULL || *p == NUL)
7383#endif
7384 {
7385#ifdef FEAT_KEYMAP
7386 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7387 p = wp->w_buffer->b_p_keymap;
7388 else
7389#endif
7390 p = (char_u *)"lang";
7391 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007392 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 buf[0] = NUL;
7394#ifdef FEAT_EVAL
7395 vim_free(s);
7396#endif
7397 }
7398 return buf[0] != NUL;
7399}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
7401#if defined(FEAT_STL_OPT) || defined(PROTO)
7402/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007403 * Redraw the status line or ruler of window "wp".
7404 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 */
7406 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007407win_redr_custom(
7408 win_T *wp,
7409 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007411 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 int attr;
7413 int curattr;
7414 int row;
7415 int col = 0;
7416 int maxwidth;
7417 int width;
7418 int n;
7419 int len;
7420 int fillchar;
7421 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007422 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007424 struct stl_hlrec hltab[STL_MAX_ITEM];
7425 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007426 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007427 win_T *ewp;
7428 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429
Bram Moolenaar1d633412013-12-11 15:52:01 +01007430 /* There is a tiny chance that this gets called recursively: When
7431 * redrawing a status line triggers redrawing the ruler or tabline.
7432 * Avoid trouble by not allowing recursion. */
7433 if (entered)
7434 return;
7435 entered = TRUE;
7436
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007438 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007440 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007441 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007442 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007443 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007444 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007445 maxwidth = Columns;
7446# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007447 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007450 else
7451 {
7452 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007453 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007454 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007455
7456 if (draw_ruler)
7457 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007458 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007459 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007460 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007461 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007462 if (*++stl == '-')
7463 stl++;
7464 if (atoi((char *)stl))
7465 while (VIM_ISDIGIT(*stl))
7466 stl++;
7467 if (*stl++ != '(')
7468 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007469 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007470 col = ru_col - (Columns - wp->w_width);
7471 if (col < (wp->w_width + 1) / 2)
7472 col = (wp->w_width + 1) / 2;
7473 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007474 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007475 {
7476 row = Rows - 1;
7477 --maxwidth; /* writing in last column may cause scrolling */
7478 fillchar = ' ';
7479 attr = 0;
7480 }
7481
7482# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007483 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007484# endif
7485 }
7486 else
7487 {
7488 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007489 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007490 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007491 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007492# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007493 use_sandbox = was_set_insecurely((char_u *)"statusline",
7494 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007495# endif
7496 }
7497
Bram Moolenaar53f81742017-09-22 14:35:51 +02007498 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007499 }
7500
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007502 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
Bram Moolenaar61452852011-02-01 18:01:11 +01007504 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7505 * the cursor away and back. */
7506 ewp = wp == NULL ? curwin : wp;
7507 p_crb_save = ewp->w_p_crb;
7508 ewp->w_p_crb = FALSE;
7509
Bram Moolenaar362f3562009-11-03 16:20:34 +00007510 /* Make a copy, because the statusline may include a function call that
7511 * might change the option value and free the memory. */
7512 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007513 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007514 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007515 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007516 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007517 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007519 /* Make all characters printable. */
7520 p = transstr(buf);
7521 if (p != NULL)
7522 {
7523 vim_strncpy(buf, p, sizeof(buf) - 1);
7524 vim_free(p);
7525 }
7526
7527 /* fill up with "fillchar" */
7528 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007529 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 ++width;
7533 }
7534 buf[len] = NUL;
7535
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007536 /*
7537 * Draw each snippet with the specified highlighting.
7538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 curattr = attr;
7540 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007541 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007543 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 screen_puts_len(p, len, row, col, curattr);
7545 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007546 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007548 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007550 else if (hltab[n].userhl < 0)
7551 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007552#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007553 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7554 && wp->w_status_height != 0)
7555 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007556 else if (wp != NULL && bt_terminal(wp->w_buffer)
7557 && wp->w_status_height != 0)
7558 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007559#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007560 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007561 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007563 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 }
7565 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007566
7567 if (wp == NULL)
7568 {
7569 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7570 col = 0;
7571 len = 0;
7572 p = buf;
7573 fillchar = 0;
7574 for (n = 0; tabtab[n].start != NULL; n++)
7575 {
7576 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7577 while (col < len)
7578 TabPageIdxs[col++] = fillchar;
7579 p = tabtab[n].start;
7580 fillchar = tabtab[n].userhl;
7581 }
7582 while (col < Columns)
7583 TabPageIdxs[col++] = fillchar;
7584 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007585
7586theend:
7587 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588}
7589
7590#endif /* FEAT_STL_OPT */
7591
7592/*
7593 * Output a single character directly to the screen and update ScreenLines.
7594 */
7595 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007596screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 char_u buf[MB_MAXBYTES + 1];
7599
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007600 if (has_mbyte)
7601 buf[(*mb_char2bytes)(c, buf)] = NUL;
7602 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007603 {
7604 buf[0] = c;
7605 buf[1] = NUL;
7606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 screen_puts(buf, row, col, attr);
7608}
7609
7610/*
7611 * Get a single character directly from ScreenLines into "bytes[]".
7612 * Also return its attribute in *attrp;
7613 */
7614 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007615screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616{
7617 unsigned off;
7618
7619 /* safety check */
7620 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7621 {
7622 off = LineOffset[row] + col;
7623 *attrp = ScreenAttrs[off];
7624 bytes[0] = ScreenLines[off];
7625 bytes[1] = NUL;
7626
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 if (enc_utf8 && ScreenLinesUC[off] != 0)
7628 bytes[utfc_char2bytes(off, bytes)] = NUL;
7629 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7630 {
7631 bytes[0] = ScreenLines[off];
7632 bytes[1] = ScreenLines2[off];
7633 bytes[2] = NUL;
7634 }
7635 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7636 {
7637 bytes[1] = ScreenLines[off + 1];
7638 bytes[2] = NUL;
7639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 }
7641}
7642
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007643/*
7644 * Return TRUE if composing characters for screen posn "off" differs from
7645 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007646 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007647 */
7648 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007649screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007650{
7651 int i;
7652
7653 for (i = 0; i < Screen_mco; ++i)
7654 {
7655 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7656 return TRUE;
7657 if (u8cc[i] == 0)
7658 break;
7659 }
7660 return FALSE;
7661}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007662
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663/*
7664 * Put string '*text' on the screen at position 'row' and 'col', with
7665 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7666 * Note: only outputs within one row, message is truncated at screen boundary!
7667 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7668 */
7669 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007670screen_puts(
7671 char_u *text,
7672 int row,
7673 int col,
7674 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675{
7676 screen_puts_len(text, -1, row, col, attr);
7677}
7678
7679/*
7680 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7681 * a NUL.
7682 */
7683 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007684screen_puts_len(
7685 char_u *text,
7686 int textlen,
7687 int row,
7688 int col,
7689 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690{
7691 unsigned off;
7692 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007693 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007695 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 int mbyte_blen = 1;
7697 int mbyte_cells = 1;
7698 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007699 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007701#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007703 int pc, nc, nc1;
7704 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007706 int force_redraw_this;
7707 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007708 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709
7710 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7711 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007712 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713
Bram Moolenaarc236c162008-07-13 17:41:49 +00007714 /* When drawing over the right halve of a double-wide char clear out the
7715 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007716 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007717#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007718 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007719#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007720 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007721 {
7722 ScreenLines[off - 1] = ' ';
7723 ScreenAttrs[off - 1] = 0;
7724 if (enc_utf8)
7725 {
7726 ScreenLinesUC[off - 1] = 0;
7727 ScreenLinesC[0][off - 1] = 0;
7728 }
7729 /* redraw the previous cell, make it empty */
7730 screen_char(off - 1, row, col - 1);
7731 /* force the cell at "col" to be redrawn */
7732 force_redraw_next = TRUE;
7733 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007734
Bram Moolenaar367329b2007-08-30 11:53:22 +00007735 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007736 while (col < screen_Columns
7737 && (len < 0 || (int)(ptr - text) < len)
7738 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {
7740 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 /* check if this is the first byte of a multibyte */
7742 if (has_mbyte)
7743 {
7744 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007745 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007747 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7749 mbyte_cells = 1;
7750 else if (enc_dbcs != 0)
7751 mbyte_cells = mbyte_blen;
7752 else /* enc_utf8 */
7753 {
7754 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007755 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 (int)((text + len) - ptr));
7757 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007758 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007760#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7762 {
7763 /* Do Arabic shaping. */
7764 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7765 {
7766 /* Past end of string to be displayed. */
7767 nc = NUL;
7768 nc1 = NUL;
7769 }
7770 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007771 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007772 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7773 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007774 nc1 = pcc[0];
7775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 pc = prev_c;
7777 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007778 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 }
7780 else
7781 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007782#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007783 if (col + mbyte_cells > screen_Columns)
7784 {
7785 /* Only 1 cell left, but character requires 2 cells:
7786 * display a '>' in the last column to avoid wrapping. */
7787 c = '>';
7788 mbyte_cells = 1;
7789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 }
7791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007793 force_redraw_this = force_redraw_next;
7794 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007795
7796 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 || (mbyte_cells == 2
7798 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7799 || (enc_dbcs == DBCS_JPNU
7800 && c == 0x8e
7801 && ScreenLines2[off] != ptr[1])
7802 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007803 && (ScreenLinesUC[off] !=
7804 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7805 || (ScreenLinesUC[off] != 0
7806 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007808 || exmode_active;
7809
Bram Moolenaara12a1612019-01-24 16:39:02 +01007810 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 {
7812#if defined(FEAT_GUI) || defined(UNIX)
7813 /* The bold trick makes a single row of pixels appear in the next
7814 * character. When a bold character is removed, the next
7815 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007816 * and for some xterms. */
7817 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818# ifdef FEAT_GUI
7819 gui.in_use
7820# endif
7821# if defined(FEAT_GUI) && defined(UNIX)
7822 ||
7823# endif
7824# ifdef UNIX
7825 term_is_xterm
7826# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007827 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007829 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007831 if (n > HL_ALL)
7832 n = syn_attr2attr(n);
7833 if (n & HL_BOLD)
7834 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 }
7836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 /* When at the end of the text and overwriting a two-cell
7838 * character with a one-cell character, need to clear the next
7839 * cell. Also when overwriting the left halve of a two-cell char
7840 * with the right halve of a two-cell char. Do this only once
7841 * (mb_off2cells() may return 2 on the right halve). */
7842 if (clear_next_cell)
7843 clear_next_cell = FALSE;
7844 else if (has_mbyte
7845 && (len < 0 ? ptr[mbyte_blen] == NUL
7846 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007847 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007849 && (*mb_off2cells)(off, max_off) == 1
7850 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 clear_next_cell = TRUE;
7852
7853 /* Make sure we never leave a second byte of a double-byte behind,
7854 * it confuses mb_off2cells(). */
7855 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007856 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007858 && (*mb_off2cells)(off, max_off) == 1
7859 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 ScreenLines[off] = c;
7862 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 if (enc_utf8)
7864 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007865 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 ScreenLinesUC[off] = 0;
7867 else
7868 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007869 int i;
7870
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007872 for (i = 0; i < Screen_mco; ++i)
7873 {
7874 ScreenLinesC[i][off] = u8cc[i];
7875 if (u8cc[i] == 0)
7876 break;
7877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 }
7879 if (mbyte_cells == 2)
7880 {
7881 ScreenLines[off + 1] = 0;
7882 ScreenAttrs[off + 1] = attr;
7883 }
7884 screen_char(off, row, col);
7885 }
7886 else if (mbyte_cells == 2)
7887 {
7888 ScreenLines[off + 1] = ptr[1];
7889 ScreenAttrs[off + 1] = attr;
7890 screen_char_2(off, row, col);
7891 }
7892 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7893 {
7894 ScreenLines2[off] = ptr[1];
7895 screen_char(off, row, col);
7896 }
7897 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 screen_char(off, row, col);
7899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 if (has_mbyte)
7901 {
7902 off += mbyte_cells;
7903 col += mbyte_cells;
7904 ptr += mbyte_blen;
7905 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007906 {
7907 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007909 len = -1;
7910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 }
7912 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {
7914 ++off;
7915 ++col;
7916 ++ptr;
7917 }
7918 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007919
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007920 /* If we detected the next character needs to be redrawn, but the text
7921 * doesn't extend up to there, update the character here. */
7922 if (force_redraw_next && col < screen_Columns)
7923 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007924 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7925 screen_char_2(off, row, col);
7926 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007927 screen_char(off, row, col);
7928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929}
7930
7931#ifdef FEAT_SEARCH_EXTRA
7932/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007933 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 */
7935 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007936start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938 if (p_hls && !no_hlsearch)
7939 {
7940 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007941 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007942# ifdef FEAT_RELTIME
7943 /* Set the time limit to 'redrawtime'. */
7944 profile_setlimit(p_rdt, &search_hl.tm);
7945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 }
7947}
7948
7949/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007950 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 */
7952 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007953end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954{
7955 if (search_hl.rm.regprog != NULL)
7956 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007957 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 search_hl.rm.regprog = NULL;
7959 }
7960}
7961
7962/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007963 * Init for calling prepare_search_hl().
7964 */
7965 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007966init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007967{
7968 matchitem_T *cur;
7969
7970 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7971 * match */
7972 cur = wp->w_match_head;
7973 while (cur != NULL)
7974 {
7975 cur->hl.rm = cur->match;
7976 if (cur->hlg_id == 0)
7977 cur->hl.attr = 0;
7978 else
7979 cur->hl.attr = syn_id2attr(cur->hlg_id);
7980 cur->hl.buf = wp->w_buffer;
7981 cur->hl.lnum = 0;
7982 cur->hl.first_lnum = 0;
7983# ifdef FEAT_RELTIME
7984 /* Set the time limit to 'redrawtime'. */
7985 profile_setlimit(p_rdt, &(cur->hl.tm));
7986# endif
7987 cur = cur->next;
7988 }
7989 search_hl.buf = wp->w_buffer;
7990 search_hl.lnum = 0;
7991 search_hl.first_lnum = 0;
7992 /* time limit is set at the toplevel, for all windows */
7993}
7994
7995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 * Advance to the match in window "wp" line "lnum" or past it.
7997 */
7998 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007999prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008001 matchitem_T *cur; /* points to the match list */
8002 match_T *shl; /* points to search_hl or a match */
8003 int shl_flag; /* flag to indicate whether search_hl
8004 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008005 int pos_inprogress; /* marks that position match search is
8006 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 int n;
8008
8009 /*
8010 * When using a multi-line pattern, start searching at the top
8011 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008012 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008014 cur = wp->w_match_head;
8015 shl_flag = FALSE;
8016 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008018 if (shl_flag == FALSE)
8019 {
8020 shl = &search_hl;
8021 shl_flag = TRUE;
8022 }
8023 else
8024 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 if (shl->rm.regprog != NULL
8026 && shl->lnum == 0
8027 && re_multiline(shl->rm.regprog))
8028 {
8029 if (shl->first_lnum == 0)
8030 {
8031# ifdef FEAT_FOLDING
8032 for (shl->first_lnum = lnum;
8033 shl->first_lnum > wp->w_topline; --shl->first_lnum)
8034 if (hasFoldingWin(wp, shl->first_lnum - 1,
8035 NULL, NULL, TRUE, NULL))
8036 break;
8037# else
8038 shl->first_lnum = wp->w_topline;
8039# endif
8040 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008041 if (cur != NULL)
8042 cur->pos.cur = 0;
8043 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008045 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
8046 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02008048 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
8049 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008050 pos_inprogress = cur == NULL || cur->pos.cur == 0
8051 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 if (shl->lnum != 0)
8053 {
8054 shl->first_lnum = shl->lnum
8055 + shl->rm.endpos[0].lnum
8056 - shl->rm.startpos[0].lnum;
8057 n = shl->rm.endpos[0].col;
8058 }
8059 else
8060 {
8061 ++shl->first_lnum;
8062 n = 0;
8063 }
8064 }
8065 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008066 if (shl != &search_hl && cur != NULL)
8067 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 }
8069}
8070
8071/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008072 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 * Uses shl->buf.
8074 * Sets shl->lnum and shl->rm contents.
8075 * Note: Assumes a previous match is always before "lnum", unless
8076 * shl->lnum is zero.
8077 * Careful: Any pointers for buffer lines will become invalid.
8078 */
8079 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008080next_search_hl(
8081 win_T *win,
8082 match_T *shl, /* points to search_hl or a match */
8083 linenr_T lnum,
8084 colnr_T mincol, /* minimal column for a match */
8085 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086{
8087 linenr_T l;
8088 colnr_T matchcol;
8089 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008090 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008092 // for :{range}s/pat only highlight inside the range
8093 if (lnum < search_first_line || lnum > search_last_line)
8094 {
8095 shl->lnum = 0;
8096 return;
8097 }
8098
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 if (shl->lnum != 0)
8100 {
8101 /* Check for three situations:
8102 * 1. If the "lnum" is below a previous match, start a new search.
8103 * 2. If the previous match includes "mincol", use it.
8104 * 3. Continue after the previous match.
8105 */
8106 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8107 if (lnum > l)
8108 shl->lnum = 0;
8109 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8110 return;
8111 }
8112
8113 /*
8114 * Repeat searching for a match until one is found that includes "mincol"
8115 * or none is found in this line.
8116 */
8117 called_emsg = FALSE;
8118 for (;;)
8119 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008120#ifdef FEAT_RELTIME
8121 /* Stop searching after passing the time limit. */
8122 if (profile_passed_limit(&(shl->tm)))
8123 {
8124 shl->lnum = 0; /* no match found in time */
8125 break;
8126 }
8127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 /* Three situations:
8129 * 1. No useful previous match: search from start of line.
8130 * 2. Not Vi compatible or empty match: continue at next character.
8131 * Break the loop if this is beyond the end of the line.
8132 * 3. Vi compatible searching: continue at end of previous match.
8133 */
8134 if (shl->lnum == 0)
8135 matchcol = 0;
8136 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8137 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008138 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008140 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008141
8142 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008143 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008144 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008146 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 shl->lnum = 0;
8148 break;
8149 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008150 if (has_mbyte)
8151 matchcol += mb_ptr2len(ml);
8152 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008153 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 }
8155 else
8156 matchcol = shl->rm.endpos[0].col;
8157
8158 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008159 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008161 /* Remember whether shl->rm is using a copy of the regprog in
8162 * cur->match. */
8163 int regprog_is_copy = (shl != &search_hl && cur != NULL
8164 && shl == &cur->hl
8165 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008166 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008167
Bram Moolenaarb3414592014-06-17 17:48:32 +02008168 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8169 matchcol,
8170#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008171 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008172#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008173 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008174#endif
8175 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008176 /* Copy the regprog, in case it got freed and recompiled. */
8177 if (regprog_is_copy)
8178 cur->match.regprog = cur->hl.rm.regprog;
8179
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008180 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008181 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008182 /* Error while handling regexp: stop using this regexp. */
8183 if (shl == &search_hl)
8184 {
8185 /* don't free regprog in the match list, it's a copy */
8186 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008187 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008188 }
8189 shl->rm.regprog = NULL;
8190 shl->lnum = 0;
8191 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8192 message */
8193 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008194 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008195 }
8196 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008197 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008198 else
8199 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 if (nmatched == 0)
8201 {
8202 shl->lnum = 0; /* no match found */
8203 break;
8204 }
8205 if (shl->rm.startpos[0].lnum > 0
8206 || shl->rm.startpos[0].col >= mincol
8207 || nmatched > 1
8208 || shl->rm.endpos[0].col > mincol)
8209 {
8210 shl->lnum += shl->rm.startpos[0].lnum;
8211 break; /* useful match found */
8212 }
8213 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008214
8215 // Restore called_emsg for assert_fails().
8216 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218
Bram Moolenaar85077472016-10-16 14:35:48 +02008219/*
8220 * If there is a match fill "shl" and return one.
8221 * Return zero otherwise.
8222 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008223 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008224next_search_hl_pos(
8225 match_T *shl, /* points to a match */
8226 linenr_T lnum,
8227 posmatch_T *posmatch, /* match positions */
8228 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008229{
8230 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008231 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008232
Bram Moolenaarb3414592014-06-17 17:48:32 +02008233 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8234 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008235 llpos_T *pos = &posmatch->pos[i];
8236
8237 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008238 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008239 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008240 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008241 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008242 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008243 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008244 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008245 /* if this match comes before the one at "found" then swap
8246 * them */
8247 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008248 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008249 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008250
Bram Moolenaar85077472016-10-16 14:35:48 +02008251 *pos = posmatch->pos[found];
8252 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008253 }
8254 }
8255 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008256 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008257 }
8258 }
8259 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008260 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008261 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008262 colnr_T start = posmatch->pos[found].col == 0
8263 ? 0 : posmatch->pos[found].col - 1;
8264 colnr_T end = posmatch->pos[found].col == 0
8265 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008266
Bram Moolenaar85077472016-10-16 14:35:48 +02008267 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008268 shl->rm.startpos[0].lnum = 0;
8269 shl->rm.startpos[0].col = start;
8270 shl->rm.endpos[0].lnum = 0;
8271 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008272 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008273 posmatch->cur = found + 1;
8274 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008275 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008276 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008277}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008278#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008279
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008281screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282{
8283 attrentry_T *aep = NULL;
8284
8285 screen_attr = attr;
8286 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008287#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 && termcap_active
8289#endif
8290 )
8291 {
8292#ifdef FEAT_GUI
8293 if (gui.in_use)
8294 {
8295 char buf[20];
8296
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008297 /* The GUI handles this internally. */
8298 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 OUT_STR(buf);
8300 }
8301 else
8302#endif
8303 {
8304 if (attr > HL_ALL) /* special HL attr. */
8305 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008306 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 aep = syn_cterm_attr2entry(attr);
8308 else
8309 aep = syn_term_attr2entry(attr);
8310 if (aep == NULL) /* did ":syntax clear" */
8311 attr = 0;
8312 else
8313 attr = aep->ae_attr;
8314 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008315 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008317 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008318#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008319 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8320 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8321 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008322#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008323 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008324 /* If the Normal FG color has BOLD attribute and the new HL
8325 * has a FG color defined, clear BOLD. */
8326 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008327 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008329 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008330 out_str(T_UCS);
8331 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008332 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8333 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008335 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008337 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008339 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008340 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341
8342 /*
8343 * Output the color or start string after bold etc., in case the
8344 * bold etc. override the color setting.
8345 */
8346 if (aep != NULL)
8347 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008348#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008349 /* When 'termguicolors' is set but fg or bg is unset,
8350 * fall back to the cterm colors. This helps for SpellBad,
8351 * where the GUI uses a red undercurl. */
8352 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008354 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008355 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008356 }
8357 else
8358#endif
8359 if (t_colors > 1)
8360 {
8361 if (aep->ae_u.cterm.fg_color)
8362 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8363 }
8364#ifdef FEAT_TERMGUICOLORS
8365 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8366 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008367 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008368 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 }
8370 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008371#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008372 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008374 if (aep->ae_u.cterm.bg_color)
8375 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8376 }
8377
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008378 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008379 {
8380 if (aep->ae_u.term.start != NULL)
8381 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383 }
8384 }
8385 }
8386}
8387
8388 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008389screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390{
8391 int do_ME = FALSE; /* output T_ME code */
8392
8393 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008394#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 && termcap_active
8396#endif
8397 )
8398 {
8399#ifdef FEAT_GUI
8400 if (gui.in_use)
8401 {
8402 char buf[20];
8403
8404 /* use internal GUI code */
8405 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8406 OUT_STR(buf);
8407 }
8408 else
8409#endif
8410 {
8411 if (screen_attr > HL_ALL) /* special HL attr. */
8412 {
8413 attrentry_T *aep;
8414
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008415 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {
8417 /*
8418 * Assume that t_me restores the original colors!
8419 */
8420 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008421 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008422#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008423 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8424 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8425 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008426#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008427 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008428#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008429 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8430 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8431 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008432#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008433 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 do_ME = TRUE;
8435 }
8436 else
8437 {
8438 aep = syn_term_attr2entry(screen_attr);
8439 if (aep != NULL && aep->ae_u.term.stop != NULL)
8440 {
8441 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8442 do_ME = TRUE;
8443 else
8444 out_str(aep->ae_u.term.stop);
8445 }
8446 }
8447 if (aep == NULL) /* did ":syntax clear" */
8448 screen_attr = 0;
8449 else
8450 screen_attr = aep->ae_attr;
8451 }
8452
8453 /*
8454 * Often all ending-codes are equal to T_ME. Avoid outputting the
8455 * same sequence several times.
8456 */
8457 if (screen_attr & HL_STANDOUT)
8458 {
8459 if (STRCMP(T_SE, T_ME) == 0)
8460 do_ME = TRUE;
8461 else
8462 out_str(T_SE);
8463 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008464 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008465 {
8466 if (STRCMP(T_UCE, T_ME) == 0)
8467 do_ME = TRUE;
8468 else
8469 out_str(T_UCE);
8470 }
8471 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008472 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {
8474 if (STRCMP(T_UE, T_ME) == 0)
8475 do_ME = TRUE;
8476 else
8477 out_str(T_UE);
8478 }
8479 if (screen_attr & HL_ITALIC)
8480 {
8481 if (STRCMP(T_CZR, T_ME) == 0)
8482 do_ME = TRUE;
8483 else
8484 out_str(T_CZR);
8485 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008486 if (screen_attr & HL_STRIKETHROUGH)
8487 {
8488 if (STRCMP(T_STE, T_ME) == 0)
8489 do_ME = TRUE;
8490 else
8491 out_str(T_STE);
8492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8494 out_str(T_ME);
8495
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008496#ifdef FEAT_TERMGUICOLORS
8497 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008499 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008500 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008501 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008502 term_bg_rgb_color(cterm_normal_bg_gui_color);
8503 }
8504 else
8505#endif
8506 {
8507 if (t_colors > 1)
8508 {
8509 /* set Normal cterm colors */
8510 if (cterm_normal_fg_color != 0)
8511 term_fg_color(cterm_normal_fg_color - 1);
8512 if (cterm_normal_bg_color != 0)
8513 term_bg_color(cterm_normal_bg_color - 1);
8514 if (cterm_normal_fg_bold)
8515 out_str(T_MD);
8516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 }
8518 }
8519 }
8520 screen_attr = 0;
8521}
8522
8523/*
8524 * Reset the colors for a cterm. Used when leaving Vim.
8525 * The machine specific code may override this again.
8526 */
8527 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008528reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008530 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 {
8532 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008533#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008534 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8535 || cterm_normal_bg_gui_color != INVALCOLOR)
8536 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008537#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {
8541 out_str(T_OP);
8542 screen_attr = -1;
8543 }
8544 if (cterm_normal_fg_bold)
8545 {
8546 out_str(T_ME);
8547 screen_attr = -1;
8548 }
8549 }
8550}
8551
8552/*
8553 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8554 * using the attributes from ScreenAttrs["off"].
8555 */
8556 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008557screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558{
8559 int attr;
8560
8561 /* Check for illegal values, just in case (could happen just after
8562 * resizing). */
8563 if (row >= screen_Rows || col >= screen_Columns)
8564 return;
8565
Bram Moolenaarae654382019-01-17 21:09:05 +01008566#ifdef FEAT_INS_EXPAND
8567 if (pum_under_menu(row, col))
8568 return;
8569#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008570 /* Outputting a character in the last cell on the screen may scroll the
8571 * screen up. Only do it when the "xn" termcap property is set, otherwise
8572 * mark the character invalid (update it when scrolled up). */
8573 if (*T_XN == NUL
8574 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575#ifdef FEAT_RIGHTLEFT
8576 /* account for first command-line character in rightleft mode */
8577 && !cmdmsg_rl
8578#endif
8579 )
8580 {
8581 ScreenAttrs[off] = (sattr_T)-1;
8582 return;
8583 }
8584
8585 /*
8586 * Stop highlighting first, so it's easier to move the cursor.
8587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 if (screen_char_attr != 0)
8589 attr = screen_char_attr;
8590 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 attr = ScreenAttrs[off];
8592 if (screen_attr != attr)
8593 screen_stop_highlight();
8594
8595 windgoto(row, col);
8596
8597 if (screen_attr != attr)
8598 screen_start_highlight(attr);
8599
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 if (enc_utf8 && ScreenLinesUC[off] != 0)
8601 {
8602 char_u buf[MB_MAXBYTES + 1];
8603
Bram Moolenaarcb070082016-04-02 22:14:51 +02008604 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008605 {
8606 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008607#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008608 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008609#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008610 )
8611 {
8612 /* Clear the two screen cells. If the character is actually
8613 * single width it won't change the second cell. */
8614 out_str((char_u *)" ");
8615 term_windgoto(row, col);
8616 }
8617 /* not sure where the cursor is after drawing the ambiguous width
8618 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008619 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008620 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008621 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008623
8624 /* Convert the UTF-8 character to bytes and write it. */
8625 buf[utfc_char2bytes(off, buf)] = NUL;
8626 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 }
8628 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 /* double-byte character in single-width cell */
8633 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8634 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 }
8636
8637 screen_cur_col++;
8638}
8639
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640/*
8641 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8642 * on the screen at position 'row' and 'col'.
8643 * The attributes of the first byte is used for all. This is required to
8644 * output the two bytes of a double-byte character with nothing in between.
8645 */
8646 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008647screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648{
8649 /* Check for illegal values (could be wrong when screen was resized). */
8650 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8651 return;
8652
8653 /* Outputting the last character on the screen may scrollup the screen.
8654 * Don't to it! Mark the character invalid (update it when scrolled up) */
8655 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8656 {
8657 ScreenAttrs[off] = (sattr_T)-1;
8658 return;
8659 }
8660
8661 /* Output the first byte normally (positions the cursor), then write the
8662 * second byte directly. */
8663 screen_char(off, row, col);
8664 out_char(ScreenLines[off + 1]);
8665 ++screen_cur_col;
8666}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668/*
8669 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8670 * This uses the contents of ScreenLines[] and doesn't change it.
8671 */
8672 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008673screen_draw_rectangle(
8674 int row,
8675 int col,
8676 int height,
8677 int width,
8678 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679{
8680 int r, c;
8681 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008682 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008684 /* Can't use ScreenLines unless initialized */
8685 if (ScreenLines == NULL)
8686 return;
8687
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 if (invert)
8689 screen_char_attr = HL_INVERSE;
8690 for (r = row; r < row + height; ++r)
8691 {
8692 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008693 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 for (c = col; c < col + width; ++c)
8695 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008696 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 {
8698 screen_char_2(off + c, r, c);
8699 ++c;
8700 }
8701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 {
8703 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008704 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 }
8707 }
8708 }
8709 screen_char_attr = 0;
8710}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712/*
8713 * Redraw the characters for a vertically split window.
8714 */
8715 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008716redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717{
8718 int col;
8719 int width;
8720
8721# ifdef FEAT_CLIPBOARD
8722 clip_may_clear_selection(row, end - 1);
8723# endif
8724
8725 if (wp == NULL)
8726 {
8727 col = 0;
8728 width = Columns;
8729 }
8730 else
8731 {
8732 col = wp->w_wincol;
8733 width = wp->w_width;
8734 }
8735 screen_draw_rectangle(row, col, end - row, width, FALSE);
8736}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008738 static void
8739space_to_screenline(int off, int attr)
8740{
8741 ScreenLines[off] = ' ';
8742 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008743 if (enc_utf8)
8744 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008745}
8746
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747/*
8748 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8749 * with character 'c1' in first column followed by 'c2' in the other columns.
8750 * Use attributes 'attr'.
8751 */
8752 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008753screen_fill(
8754 int start_row,
8755 int end_row,
8756 int start_col,
8757 int end_col,
8758 int c1,
8759 int c2,
8760 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 int row;
8763 int col;
8764 int off;
8765 int end_off;
8766 int did_delete;
8767 int c;
8768 int norm_term;
8769#if defined(FEAT_GUI) || defined(UNIX)
8770 int force_next = FALSE;
8771#endif
8772
8773 if (end_row > screen_Rows) /* safety check */
8774 end_row = screen_Rows;
8775 if (end_col > screen_Columns) /* safety check */
8776 end_col = screen_Columns;
8777 if (ScreenLines == NULL
8778 || start_row >= end_row
8779 || start_col >= end_col) /* nothing to do */
8780 return;
8781
8782 /* it's a "normal" terminal when not in a GUI or cterm */
8783 norm_term = (
8784#ifdef FEAT_GUI
8785 !gui.in_use &&
8786#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008787 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 for (row = start_row; row < end_row; ++row)
8789 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008790 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008791#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008792 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008793#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008794 )
8795 {
8796 /* When drawing over the right halve of a double-wide char clear
8797 * out the left halve. When drawing over the left halve of a
8798 * double wide-char clear out the right halve. Only needed in a
8799 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008800 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008801 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008802 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008803 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 /*
8806 * Try to use delete-line termcap code, when no attributes or in a
8807 * "normal" terminal, where a bold/italic space is just a
8808 * space.
8809 */
8810 did_delete = FALSE;
8811 if (c2 == ' '
8812 && end_col == Columns
8813 && can_clear(T_CE)
8814 && (attr == 0
8815 || (norm_term
8816 && attr <= HL_ALL
8817 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8818 {
8819 /*
8820 * check if we really need to clear something
8821 */
8822 col = start_col;
8823 if (c1 != ' ') /* don't clear first char */
8824 ++col;
8825
8826 off = LineOffset[row] + col;
8827 end_off = LineOffset[row] + end_col;
8828
8829 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 if (enc_utf8)
8831 while (off < end_off && ScreenLines[off] == ' '
8832 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8833 ++off;
8834 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 while (off < end_off && ScreenLines[off] == ' '
8836 && ScreenAttrs[off] == 0)
8837 ++off;
8838 if (off < end_off) /* something to be cleared */
8839 {
8840 col = off - LineOffset[row];
8841 screen_stop_highlight();
8842 term_windgoto(row, col);/* clear rest of this screen line */
8843 out_str(T_CE);
8844 screen_start(); /* don't know where cursor is now */
8845 col = end_col - col;
8846 while (col--) /* clear chars in ScreenLines */
8847 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008848 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 ++off;
8850 }
8851 }
8852 did_delete = TRUE; /* the chars are cleared now */
8853 }
8854
8855 off = LineOffset[row] + start_col;
8856 c = c1;
8857 for (col = start_col; col < end_col; ++col)
8858 {
8859 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008860 || (enc_utf8 && (int)ScreenLinesUC[off]
8861 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 || ScreenAttrs[off] != attr
8863#if defined(FEAT_GUI) || defined(UNIX)
8864 || force_next
8865#endif
8866 )
8867 {
8868#if defined(FEAT_GUI) || defined(UNIX)
8869 /* The bold trick may make a single row of pixels appear in
8870 * the next character. When a bold character is removed, the
8871 * next character should be redrawn too. This happens for our
8872 * own GUI and for some xterms. */
8873 if (
8874# ifdef FEAT_GUI
8875 gui.in_use
8876# endif
8877# if defined(FEAT_GUI) && defined(UNIX)
8878 ||
8879# endif
8880# ifdef UNIX
8881 term_is_xterm
8882# endif
8883 )
8884 {
8885 if (ScreenLines[off] != ' '
8886 && (ScreenAttrs[off] > HL_ALL
8887 || ScreenAttrs[off] & HL_BOLD))
8888 force_next = TRUE;
8889 else
8890 force_next = FALSE;
8891 }
8892#endif
8893 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 if (enc_utf8)
8895 {
8896 if (c >= 0x80)
8897 {
8898 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008899 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 }
8901 else
8902 ScreenLinesUC[off] = 0;
8903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 ScreenAttrs[off] = attr;
8905 if (!did_delete || c != ' ')
8906 screen_char(off, row, col);
8907 }
8908 ++off;
8909 if (col == start_col)
8910 {
8911 if (did_delete)
8912 break;
8913 c = c2;
8914 }
8915 }
8916 if (end_col == Columns)
8917 LineWraps[row] = FALSE;
8918 if (row == Rows - 1) /* overwritten the command line */
8919 {
8920 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008921 if (start_col == 0 && end_col == Columns
8922 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008924 if (start_col == 0)
8925 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 }
8927 }
8928}
8929
8930/*
8931 * Check if there should be a delay. Used before clearing or redrawing the
8932 * screen or the command line.
8933 */
8934 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008935check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
8937 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8938 && !did_wait_return
8939 && emsg_silent == 0)
8940 {
8941 out_flush();
8942 ui_delay(1000L, TRUE);
8943 emsg_on_display = FALSE;
8944 if (check_msg_scroll)
8945 msg_scroll = FALSE;
8946 }
8947}
8948
8949/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008950 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8951 */
8952 static void
8953clear_TabPageIdxs(void)
8954{
8955 int scol;
8956
8957 for (scol = 0; scol < Columns; ++scol)
8958 TabPageIdxs[scol] = 0;
8959}
8960
8961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008963 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 * Returns TRUE if there is a valid screen to write to.
8965 * Returns FALSE when starting up and screen not initialized yet.
8966 */
8967 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008968screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008970 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 return (ScreenLines != NULL);
8972}
8973
8974/*
8975 * Resize the shell to Rows and Columns.
8976 * Allocate ScreenLines[] and associated items.
8977 *
8978 * There may be some time between setting Rows and Columns and (re)allocating
8979 * ScreenLines[]. This happens when starting up and when (manually) changing
8980 * the shell size. Always use screen_Rows and screen_Columns to access items
8981 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8982 * final size of the shell is needed.
8983 */
8984 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008985screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986{
8987 int new_row, old_row;
8988#ifdef FEAT_GUI
8989 int old_Rows;
8990#endif
8991 win_T *wp;
8992 int outofmem = FALSE;
8993 int len;
8994 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008996 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008998 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 sattr_T *new_ScreenAttrs;
9000 unsigned *new_LineOffset;
9001 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009002 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009003 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009005 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009006 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009008retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 /*
9010 * Allocation of the screen buffers is done only when the size changes and
9011 * when Rows and Columns have been set and we have started doing full
9012 * screen stuff.
9013 */
9014 if ((ScreenLines != NULL
9015 && Rows == screen_Rows
9016 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 && enc_utf8 == (ScreenLinesUC != NULL)
9018 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01009019 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 || Rows == 0
9021 || Columns == 0
9022 || (!full_screen && ScreenLines == NULL))
9023 return;
9024
9025 /*
9026 * It's possible that we produce an out-of-memory message below, which
9027 * will cause this function to be called again. To break the loop, just
9028 * return here.
9029 */
9030 if (entered)
9031 return;
9032 entered = TRUE;
9033
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009034 /*
9035 * Note that the window sizes are updated before reallocating the arrays,
9036 * thus we must not redraw here!
9037 */
9038 ++RedrawingDisabled;
9039
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 win_new_shellsize(); /* fit the windows in the new sized shell */
9041
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 comp_col(); /* recompute columns for shown command and ruler */
9043
9044 /*
9045 * We're changing the size of the screen.
9046 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9047 * - Move lines from the old arrays into the new arrays, clear extra
9048 * lines (unless the screen is going to be cleared).
9049 * - Free the old arrays.
9050 *
9051 * If anything fails, make ScreenLines NULL, so we don't do anything!
9052 * Continuing with the old ScreenLines may result in a crash, because the
9053 * size is wrong.
9054 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009055 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009057 if (aucmd_win != NULL)
9058 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009059#ifdef FEAT_TEXT_PROP
9060 // global popup windows
9061 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9062 win_free_lsize(wp);
9063 // tab-local popup windows
9064 FOR_ALL_TABPAGES(tp)
9065 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9066 win_free_lsize(wp);
9067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009069 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01009070 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 if (enc_utf8)
9072 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009073 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009074 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009075 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
9076 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 }
9078 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009079 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
9080 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
9081 new_LineOffset = LALLOC_MULT(unsigned, Rows);
9082 new_LineWraps = LALLOC_MULT(char_u, Rows);
9083 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009085 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 {
9087 if (win_alloc_lines(wp) == FAIL)
9088 {
9089 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009090 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 }
9092 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009093 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9094 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009095 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009096#ifdef FEAT_TEXT_PROP
9097 // global popup windows
9098 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9099 if (win_alloc_lines(wp) == FAIL)
9100 {
9101 outofmem = TRUE;
9102 goto give_up;
9103 }
9104 // tab-local popup windows
9105 FOR_ALL_TABPAGES(tp)
9106 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9107 if (win_alloc_lines(wp) == FAIL)
9108 {
9109 outofmem = TRUE;
9110 goto give_up;
9111 }
9112#endif
9113
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009114give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009116 for (i = 0; i < p_mco; ++i)
9117 if (new_ScreenLinesC[i] == NULL)
9118 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009120 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 || new_ScreenAttrs == NULL
9123 || new_LineOffset == NULL
9124 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009125 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 || outofmem)
9127 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009128 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009129 {
9130 /* guess the size */
9131 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9132
9133 /* Remember we did this to avoid getting outofmem messages over
9134 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009135 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009136 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009137 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009138 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009139 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009140 VIM_CLEAR(new_ScreenLinesC[i]);
9141 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009142 VIM_CLEAR(new_ScreenAttrs);
9143 VIM_CLEAR(new_LineOffset);
9144 VIM_CLEAR(new_LineWraps);
9145 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 }
9147 else
9148 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009149 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009150
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 for (new_row = 0; new_row < Rows; ++new_row)
9152 {
9153 new_LineOffset[new_row] = new_row * Columns;
9154 new_LineWraps[new_row] = FALSE;
9155
9156 /*
9157 * If the screen is not going to be cleared, copy as much as
9158 * possible from the old screen to the new one and clear the rest
9159 * (used when resizing the window at the "--more--" prompt or when
9160 * executing an external command, for the GUI).
9161 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009162 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 {
9164 (void)vim_memset(new_ScreenLines + new_row * Columns,
9165 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 if (enc_utf8)
9167 {
9168 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9169 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009170 for (i = 0; i < p_mco; ++i)
9171 (void)vim_memset(new_ScreenLinesC[i]
9172 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 0, (size_t)Columns * sizeof(u8char_T));
9174 }
9175 if (enc_dbcs == DBCS_JPNU)
9176 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9177 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9179 0, (size_t)Columns * sizeof(sattr_T));
9180 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009181 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 {
9183 if (screen_Columns < Columns)
9184 len = screen_Columns;
9185 else
9186 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009187 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009188 * may be invalid now. Also when p_mco changes. */
9189 if (!(enc_utf8 && ScreenLinesUC == NULL)
9190 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009191 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9192 ScreenLines + LineOffset[old_row],
9193 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009194 if (enc_utf8 && ScreenLinesUC != NULL
9195 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 {
9197 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9198 ScreenLinesUC + LineOffset[old_row],
9199 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009200 for (i = 0; i < p_mco; ++i)
9201 mch_memmove(new_ScreenLinesC[i]
9202 + new_LineOffset[new_row],
9203 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 (size_t)len * sizeof(u8char_T));
9205 }
9206 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9207 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9208 ScreenLines2 + LineOffset[old_row],
9209 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9211 ScreenAttrs + LineOffset[old_row],
9212 (size_t)len * sizeof(sattr_T));
9213 }
9214 }
9215 }
9216 /* Use the last line of the screen for the current line. */
9217 current_ScreenLine = new_ScreenLines + Rows * Columns;
9218 }
9219
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009220 free_screenlines();
9221
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009224 for (i = 0; i < p_mco; ++i)
9225 ScreenLinesC[i] = new_ScreenLinesC[i];
9226 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 ScreenAttrs = new_ScreenAttrs;
9229 LineOffset = new_LineOffset;
9230 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009231 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
9233 /* It's important that screen_Rows and screen_Columns reflect the actual
9234 * size of ScreenLines[]. Set them before calling anything. */
9235#ifdef FEAT_GUI
9236 old_Rows = screen_Rows;
9237#endif
9238 screen_Rows = Rows;
9239 screen_Columns = Columns;
9240
9241 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009242 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244#ifdef FEAT_GUI
9245 else if (gui.in_use
9246 && !gui.starting
9247 && ScreenLines != NULL
9248 && old_Rows != Rows)
9249 {
9250 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9251 /*
9252 * Adjust the position of the cursor, for when executing an external
9253 * command.
9254 */
9255 if (msg_row >= Rows) /* Rows got smaller */
9256 msg_row = Rows - 1; /* put cursor at last row */
9257 else if (Rows > old_Rows) /* Rows got bigger */
9258 msg_row += Rows - old_Rows; /* put cursor in same place */
9259 if (msg_col >= Columns) /* Columns got smaller */
9260 msg_col = Columns - 1; /* put cursor at last column */
9261 }
9262#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009263 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009266 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009267
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009268 /*
9269 * Do not apply autocommands more than 3 times to avoid an endless loop
9270 * in case applying autocommands always changes Rows or Columns.
9271 */
9272 if (starting == 0 && ++retry_count <= 3)
9273 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009274 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009275 /* In rare cases, autocommands may have altered Rows or Columns,
9276 * jump back to check if we need to allocate the screen again. */
9277 goto retry;
9278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279}
9280
9281 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009282free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009283{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009284 int i;
9285
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009286 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009287 for (i = 0; i < Screen_mco; ++i)
9288 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009289 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009290 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009291 vim_free(ScreenAttrs);
9292 vim_free(LineOffset);
9293 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009294 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009295}
9296
9297 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009298screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299{
9300 check_for_delay(FALSE);
9301 screenalloc(FALSE); /* allocate screen buffers if size changed */
9302 screenclear2(); /* clear the screen */
9303}
9304
9305 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009306screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307{
9308 int i;
9309
9310 if (starting == NO_SCREEN || ScreenLines == NULL
9311#ifdef FEAT_GUI
9312 || (gui.in_use && gui.starting)
9313#endif
9314 )
9315 return;
9316
9317#ifdef FEAT_GUI
9318 if (!gui.in_use)
9319#endif
9320 screen_attr = -1; /* force setting the Normal colors */
9321 screen_stop_highlight(); /* don't want highlighting here */
9322
9323#ifdef FEAT_CLIPBOARD
9324 /* disable selection without redrawing it */
9325 clip_scroll_selection(9999);
9326#endif
9327
9328 /* blank out ScreenLines */
9329 for (i = 0; i < Rows; ++i)
9330 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009331 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 LineWraps[i] = FALSE;
9333 }
9334
9335 if (can_clear(T_CL))
9336 {
9337 out_str(T_CL); /* clear the display */
9338 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009339 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 }
9341 else
9342 {
9343 /* can't clear the screen, mark all chars with invalid attributes */
9344 for (i = 0; i < Rows; ++i)
9345 lineinvalid(LineOffset[i], (int)Columns);
9346 clear_cmdline = TRUE;
9347 }
9348
9349 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9350
9351 win_rest_invalid(firstwin);
9352 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009353 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 if (must_redraw == CLEAR) /* no need to clear again */
9355 must_redraw = NOT_VALID;
9356 compute_cmdrow();
9357 msg_row = cmdline_row; /* put cursor on last line for messages */
9358 msg_col = 0;
9359 screen_start(); /* don't know where cursor is now */
9360 msg_scrolled = 0; /* can't scroll back */
9361 msg_didany = FALSE;
9362 msg_didout = FALSE;
9363}
9364
9365/*
9366 * Clear one line in ScreenLines.
9367 */
9368 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009369lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370{
9371 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 if (enc_utf8)
9373 (void)vim_memset(ScreenLinesUC + off, 0,
9374 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009375 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376}
9377
9378/*
9379 * Mark one line in ScreenLines invalid by setting the attributes to an
9380 * invalid value.
9381 */
9382 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009383lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9386}
9387
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388/*
9389 * Copy part of a Screenline for vertically split window "wp".
9390 */
9391 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009392linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
9394 unsigned off_to = LineOffset[to] + wp->w_wincol;
9395 unsigned off_from = LineOffset[from] + wp->w_wincol;
9396
9397 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9398 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 if (enc_utf8)
9400 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009401 int i;
9402
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9404 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009405 for (i = 0; i < p_mco; ++i)
9406 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9407 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 }
9409 if (enc_dbcs == DBCS_JPNU)
9410 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9411 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9413 wp->w_width * sizeof(sattr_T));
9414}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415
9416/*
9417 * Return TRUE if clearing with term string "p" would work.
9418 * It can't work when the string is empty or it won't set the right background.
9419 */
9420 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009421can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
9423 return (*p != NUL && (t_colors <= 1
9424#ifdef FEAT_GUI
9425 || gui.in_use
9426#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009427#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009428 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009429 || (!p_tgc && cterm_normal_bg_color == 0)
9430#else
9431 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009432#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009433 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434}
9435
9436/*
9437 * Reset cursor position. Use whenever cursor was moved because of outputting
9438 * something directly to the screen (shell commands) or a terminal control
9439 * code.
9440 */
9441 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009442screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443{
9444 screen_cur_row = screen_cur_col = 9999;
9445}
9446
9447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 * Move the cursor to position "row","col" in the screen.
9449 * This tries to find the most efficient way to move, minimizing the number of
9450 * characters sent to the terminal.
9451 */
9452 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009453windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009455 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 int i;
9457 int plan;
9458 int cost;
9459 int wouldbe_col;
9460 int noinvcurs;
9461 char_u *bs;
9462 int goto_cost;
9463 int attr;
9464
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009465#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9467
9468#define PLAN_LE 1
9469#define PLAN_CR 2
9470#define PLAN_NL 3
9471#define PLAN_WRITE 4
9472 /* Can't use ScreenLines unless initialized */
9473 if (ScreenLines == NULL)
9474 return;
9475
9476 if (col != screen_cur_col || row != screen_cur_row)
9477 {
9478 /* Check for valid position. */
9479 if (row < 0) /* window without text lines? */
9480 row = 0;
9481 if (row >= screen_Rows)
9482 row = screen_Rows - 1;
9483 if (col >= screen_Columns)
9484 col = screen_Columns - 1;
9485
9486 /* check if no cursor movement is allowed in highlight mode */
9487 if (screen_attr && *T_MS == NUL)
9488 noinvcurs = HIGHL_COST;
9489 else
9490 noinvcurs = 0;
9491 goto_cost = GOTO_COST + noinvcurs;
9492
9493 /*
9494 * Plan how to do the positioning:
9495 * 1. Use CR to move it to column 0, same row.
9496 * 2. Use T_LE to move it a few columns to the left.
9497 * 3. Use NL to move a few lines down, column 0.
9498 * 4. Move a few columns to the right with T_ND or by writing chars.
9499 *
9500 * Don't do this if the cursor went beyond the last column, the cursor
9501 * position is unknown then (some terminals wrap, some don't )
9502 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009503 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 * characters to move the cursor to the right.
9505 */
9506 if (row >= screen_cur_row && screen_cur_col < Columns)
9507 {
9508 /*
9509 * If the cursor is in the same row, bigger col, we can use CR
9510 * or T_LE.
9511 */
9512 bs = NULL; /* init for GCC */
9513 attr = screen_attr;
9514 if (row == screen_cur_row && col < screen_cur_col)
9515 {
9516 /* "le" is preferred over "bc", because "bc" is obsolete */
9517 if (*T_LE)
9518 bs = T_LE; /* "cursor left" */
9519 else
9520 bs = T_BC; /* "backspace character (old) */
9521 if (*bs)
9522 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9523 else
9524 cost = 999;
9525 if (col + 1 < cost) /* using CR is less characters */
9526 {
9527 plan = PLAN_CR;
9528 wouldbe_col = 0;
9529 cost = 1; /* CR is just one character */
9530 }
9531 else
9532 {
9533 plan = PLAN_LE;
9534 wouldbe_col = col;
9535 }
9536 if (noinvcurs) /* will stop highlighting */
9537 {
9538 cost += noinvcurs;
9539 attr = 0;
9540 }
9541 }
9542
9543 /*
9544 * If the cursor is above where we want to be, we can use CR LF.
9545 */
9546 else if (row > screen_cur_row)
9547 {
9548 plan = PLAN_NL;
9549 wouldbe_col = 0;
9550 cost = (row - screen_cur_row) * 2; /* CR LF */
9551 if (noinvcurs) /* will stop highlighting */
9552 {
9553 cost += noinvcurs;
9554 attr = 0;
9555 }
9556 }
9557
9558 /*
9559 * If the cursor is in the same row, smaller col, just use write.
9560 */
9561 else
9562 {
9563 plan = PLAN_WRITE;
9564 wouldbe_col = screen_cur_col;
9565 cost = 0;
9566 }
9567
9568 /*
9569 * Check if any characters that need to be written have the
9570 * correct attributes. Also avoid UTF-8 characters.
9571 */
9572 i = col - wouldbe_col;
9573 if (i > 0)
9574 cost += i;
9575 if (cost < goto_cost && i > 0)
9576 {
9577 /*
9578 * Check if the attributes are correct without additionally
9579 * stopping highlighting.
9580 */
9581 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9582 while (i && *p++ == attr)
9583 --i;
9584 if (i != 0)
9585 {
9586 /*
9587 * Try if it works when highlighting is stopped here.
9588 */
9589 if (*--p == 0)
9590 {
9591 cost += noinvcurs;
9592 while (i && *p++ == 0)
9593 --i;
9594 }
9595 if (i != 0)
9596 cost = 999; /* different attributes, don't do it */
9597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 if (enc_utf8)
9599 {
9600 /* Don't use an UTF-8 char for positioning, it's slow. */
9601 for (i = wouldbe_col; i < col; ++i)
9602 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9603 {
9604 cost = 999;
9605 break;
9606 }
9607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 }
9609
9610 /*
9611 * We can do it without term_windgoto()!
9612 */
9613 if (cost < goto_cost)
9614 {
9615 if (plan == PLAN_LE)
9616 {
9617 if (noinvcurs)
9618 screen_stop_highlight();
9619 while (screen_cur_col > col)
9620 {
9621 out_str(bs);
9622 --screen_cur_col;
9623 }
9624 }
9625 else if (plan == PLAN_CR)
9626 {
9627 if (noinvcurs)
9628 screen_stop_highlight();
9629 out_char('\r');
9630 screen_cur_col = 0;
9631 }
9632 else if (plan == PLAN_NL)
9633 {
9634 if (noinvcurs)
9635 screen_stop_highlight();
9636 while (screen_cur_row < row)
9637 {
9638 out_char('\n');
9639 ++screen_cur_row;
9640 }
9641 screen_cur_col = 0;
9642 }
9643
9644 i = col - screen_cur_col;
9645 if (i > 0)
9646 {
9647 /*
9648 * Use cursor-right if it's one character only. Avoids
9649 * removing a line of pixels from the last bold char, when
9650 * using the bold trick in the GUI.
9651 */
9652 if (T_ND[0] != NUL && T_ND[1] == NUL)
9653 {
9654 while (i-- > 0)
9655 out_char(*T_ND);
9656 }
9657 else
9658 {
9659 int off;
9660
9661 off = LineOffset[row] + screen_cur_col;
9662 while (i-- > 0)
9663 {
9664 if (ScreenAttrs[off] != screen_attr)
9665 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 if (enc_dbcs == DBCS_JPNU
9669 && ScreenLines[off] == 0x8e)
9670 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 ++off;
9672 }
9673 }
9674 }
9675 }
9676 }
9677 else
9678 cost = 999;
9679
9680 if (cost >= goto_cost)
9681 {
9682 if (noinvcurs)
9683 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009684 if (row == screen_cur_row && (col > screen_cur_col)
9685 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 term_cursor_right(col - screen_cur_col);
9687 else
9688 term_windgoto(row, col);
9689 }
9690 screen_cur_row = row;
9691 screen_cur_col = col;
9692 }
9693}
9694
9695/*
9696 * Set cursor to its position in the current window.
9697 */
9698 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009699setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009701 setcursor_mayforce(FALSE);
9702}
9703
9704/*
9705 * Set cursor to its position in the current window.
9706 * When "force" is TRUE also when not redrawing.
9707 */
9708 void
9709setcursor_mayforce(int force)
9710{
9711 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 {
9713 validate_cursor();
9714 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009715 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009717 /* With 'rightleft' set and the cursor on a double-wide
9718 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009719 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9720 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009721 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009722 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723#endif
9724 curwin->w_wcol));
9725 }
9726}
9727
9728
9729/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009730 * Insert 'line_count' lines at 'row' in window 'wp'.
9731 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9732 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 * scrolling.
9734 * Returns FAIL if the lines are not inserted, OK for success.
9735 */
9736 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009737win_ins_lines(
9738 win_T *wp,
9739 int row,
9740 int line_count,
9741 int invalid,
9742 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743{
9744 int did_delete;
9745 int nextrow;
9746 int lastrow;
9747 int retval;
9748
9749 if (invalid)
9750 wp->w_lines_valid = 0;
9751
9752 if (wp->w_height < 5)
9753 return FAIL;
9754
9755 if (line_count > wp->w_height - row)
9756 line_count = wp->w_height - row;
9757
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009758 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 if (retval != MAYBE)
9760 return retval;
9761
9762 /*
9763 * If there is a next window or a status line, we first try to delete the
9764 * lines at the bottom to avoid messing what is after the window.
9765 * If this fails and there are following windows, don't do anything to avoid
9766 * messing up those windows, better just redraw.
9767 */
9768 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 if (wp->w_next != NULL || wp->w_status_height)
9770 {
9771 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009772 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 did_delete = TRUE;
9774 else if (wp->w_next)
9775 return FAIL;
9776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 /*
9778 * if no lines deleted, blank the lines that will end up below the window
9779 */
9780 if (!did_delete)
9781 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009784 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 lastrow = nextrow + line_count;
9786 if (lastrow > Rows)
9787 lastrow = Rows;
9788 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009789 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 ' ', ' ', 0);
9791 }
9792
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009793 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 == FAIL)
9795 {
9796 /* deletion will have messed up other windows */
9797 if (did_delete)
9798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 win_rest_invalid(W_NEXT(wp));
9801 }
9802 return FAIL;
9803 }
9804
9805 return OK;
9806}
9807
9808/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009809 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9811 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9812 * scrolling
9813 * Return OK for success, FAIL if the lines are not deleted.
9814 */
9815 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009816win_del_lines(
9817 win_T *wp,
9818 int row,
9819 int line_count,
9820 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009821 int mayclear,
9822 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823{
9824 int retval;
9825
9826 if (invalid)
9827 wp->w_lines_valid = 0;
9828
9829 if (line_count > wp->w_height - row)
9830 line_count = wp->w_height - row;
9831
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009832 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 if (retval != MAYBE)
9834 return retval;
9835
9836 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009837 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 return FAIL;
9839
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 /*
9841 * If there are windows or status lines below, try to put them at the
9842 * correct place. If we can't do that, they have to be redrawn.
9843 */
9844 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9845 {
9846 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009847 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 {
9849 wp->w_redr_status = TRUE;
9850 win_rest_invalid(wp->w_next);
9851 }
9852 }
9853 /*
9854 * If this is the last window and there is no status line, redraw the
9855 * command line later.
9856 */
9857 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 redraw_cmdline = TRUE;
9859 return OK;
9860}
9861
9862/*
9863 * Common code for win_ins_lines() and win_del_lines().
9864 * Returns OK or FAIL when the work has been done.
9865 * Returns MAYBE when not finished yet.
9866 */
9867 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009868win_do_lines(
9869 win_T *wp,
9870 int row,
9871 int line_count,
9872 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009873 int del,
9874 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875{
9876 int retval;
9877
9878 if (!redrawing() || line_count <= 0)
9879 return FAIL;
9880
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009881 /* When inserting lines would result in loss of command output, just redraw
9882 * the lines. */
9883 if (no_win_do_lines_ins && !del)
9884 return FAIL;
9885
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009887 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009889 if (!no_win_do_lines_ins)
9890 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 return FAIL;
9892 }
9893
9894 /*
9895 * Delete all remaining lines
9896 */
9897 if (row + line_count >= wp->w_height)
9898 {
9899 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009900 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 ' ', ' ', 0);
9902 return OK;
9903 }
9904
9905 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009906 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009908 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009910 if (!no_win_do_lines_ins)
9911 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912
9913 /*
9914 * If the terminal can set a scroll region, use that.
9915 * Always do this in a vertically split window. This will redraw from
9916 * ScreenLines[] when t_CV isn't defined. That's faster than using
9917 * win_line().
9918 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009919 * a character in the lower right corner of the scroll region may cause a
9920 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009922 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 scroll_region_set(wp, row);
9926 if (del)
9927 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009928 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 else
9930 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009931 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 scroll_region_reset();
9934 return retval;
9935 }
9936
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9938 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939
9940 return MAYBE;
9941}
9942
9943/*
9944 * window 'wp' and everything after it is messed up, mark it for redraw
9945 */
9946 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009947win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 {
9951 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 wp->w_redr_status = TRUE;
9953 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 }
9955 redraw_cmdline = TRUE;
9956}
9957
9958/*
9959 * The rest of the routines in this file perform screen manipulations. The
9960 * given operation is performed physically on the screen. The corresponding
9961 * change is also made to the internal screen image. In this way, the editor
9962 * anticipates the effect of editing changes on the appearance of the screen.
9963 * That way, when we call screenupdate a complete redraw isn't usually
9964 * necessary. Another advantage is that we can keep adding code to anticipate
9965 * screen changes, and in the meantime, everything still works.
9966 */
9967
9968/*
9969 * types for inserting or deleting lines
9970 */
9971#define USE_T_CAL 1
9972#define USE_T_CDL 2
9973#define USE_T_AL 3
9974#define USE_T_CE 4
9975#define USE_T_DL 5
9976#define USE_T_SR 6
9977#define USE_NL 7
9978#define USE_T_CD 8
9979#define USE_REDRAW 9
9980
9981/*
9982 * insert lines on the screen and update ScreenLines[]
9983 * 'end' is the line after the scrolled part. Normally it is Rows.
9984 * When scrolling region used 'off' is the offset from the top for the region.
9985 * 'row' and 'end' are relative to the start of the region.
9986 *
9987 * return FAIL for failure, OK for success.
9988 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009989 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009990screen_ins_lines(
9991 int off,
9992 int row,
9993 int line_count,
9994 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009995 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009996 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997{
9998 int i;
9999 int j;
10000 unsigned temp;
10001 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010002 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 int type;
10004 int result_empty;
10005 int can_ce = can_clear(T_CE);
10006
10007 /*
10008 * FAIL if
10009 * - there is no valid screen
10010 * - the screen has to be redrawn completely
10011 * - the line count is less than one
10012 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010013 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010015 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
10016#ifdef FEAT_CLIPBOARD
10017 || (clip_star.state != SELECT_CLEARED
10018 && redrawing_for_callback > 0)
10019#endif
10020 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 return FAIL;
10022
10023 /*
10024 * There are seven ways to insert lines:
10025 * 0. When in a vertically split window and t_CV isn't set, redraw the
10026 * characters from ScreenLines[].
10027 * 1. Use T_CD (clear to end of display) if it exists and the result of
10028 * the insert is just empty lines
10029 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10030 * present or line_count > 1. It looks better if we do all the inserts
10031 * at once.
10032 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10033 * insert is just empty lines and T_CE is not present or line_count >
10034 * 1.
10035 * 4. Use T_AL (insert line) if it exists.
10036 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10037 * just empty lines.
10038 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10039 * just empty lines.
10040 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10041 * the 'da' flag is not set or we have clear line capability.
10042 * 8. redraw the characters from ScreenLines[].
10043 *
10044 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10045 * the scrollbar for the window. It does have insert line, use that if it
10046 * exists.
10047 */
10048 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10050 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010051 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 type = USE_T_CD;
10053 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10054 type = USE_T_CAL;
10055 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10056 type = USE_T_CDL;
10057 else if (*T_AL != NUL)
10058 type = USE_T_AL;
10059 else if (can_ce && result_empty)
10060 type = USE_T_CE;
10061 else if (*T_DL != NUL && result_empty)
10062 type = USE_T_DL;
10063 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10064 type = USE_T_SR;
10065 else
10066 return FAIL;
10067
10068 /*
10069 * For clearing the lines screen_del_lines() is used. This will also take
10070 * care of t_db if necessary.
10071 */
10072 if (type == USE_T_CD || type == USE_T_CDL ||
10073 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010074 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075
10076 /*
10077 * If text is retained below the screen, first clear or delete as many
10078 * lines at the bottom of the window as are about to be inserted so that
10079 * the deleted lines won't later surface during a screen_del_lines.
10080 */
10081 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010082 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083
10084#ifdef FEAT_CLIPBOARD
10085 /* Remove a modeless selection when inserting lines halfway the screen
10086 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010087 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010088 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 else
10090 clip_scroll_selection(-line_count);
10091#endif
10092
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093#ifdef FEAT_GUI
10094 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10095 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010096 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097#endif
10098
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010099 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10100 cursor_col = wp->w_wincol;
10101
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 if (*T_CCS != NUL) /* cursor relative to region */
10103 cursor_row = row;
10104 else
10105 cursor_row = row + off;
10106
10107 /*
10108 * Shift LineOffset[] line_count down to reflect the inserted lines.
10109 * Clear the inserted lines in ScreenLines[].
10110 */
10111 row += off;
10112 end += off;
10113 for (i = 0; i < line_count; ++i)
10114 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 if (wp != NULL && wp->w_width != Columns)
10116 {
10117 /* need to copy part of a line */
10118 j = end - 1 - i;
10119 while ((j -= line_count) >= row)
10120 linecopy(j + line_count, j, wp);
10121 j += line_count;
10122 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010123 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10124 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 else
10126 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10127 LineWraps[j] = FALSE;
10128 }
10129 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 {
10131 j = end - 1 - i;
10132 temp = LineOffset[j];
10133 while ((j -= line_count) >= row)
10134 {
10135 LineOffset[j + line_count] = LineOffset[j];
10136 LineWraps[j + line_count] = LineWraps[j];
10137 }
10138 LineOffset[j + line_count] = temp;
10139 LineWraps[j + line_count] = FALSE;
10140 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010141 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 else
10143 lineinvalid(temp, (int)Columns);
10144 }
10145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146
10147 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010148 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010149 if (clear_attr != 0)
10150 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 /* redraw the characters */
10153 if (type == USE_REDRAW)
10154 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010155 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 {
10157 term_append_lines(line_count);
10158 screen_start(); /* don't know where cursor is now */
10159 }
10160 else
10161 {
10162 for (i = 0; i < line_count; i++)
10163 {
10164 if (type == USE_T_AL)
10165 {
10166 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010167 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 out_str(T_AL);
10169 }
10170 else /* type == USE_T_SR */
10171 out_str(T_SR);
10172 screen_start(); /* don't know where cursor is now */
10173 }
10174 }
10175
10176 /*
10177 * With scroll-reverse and 'da' flag set we need to clear the lines that
10178 * have been scrolled down into the region.
10179 */
10180 if (type == USE_T_SR && *T_DA)
10181 {
10182 for (i = 0; i < line_count; ++i)
10183 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010184 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 out_str(T_CE);
10186 screen_start(); /* don't know where cursor is now */
10187 }
10188 }
10189
10190#ifdef FEAT_GUI
10191 gui_can_update_cursor();
10192 if (gui.in_use)
10193 out_flush(); /* always flush after a scroll */
10194#endif
10195 return OK;
10196}
10197
10198/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010199 * Delete lines on the screen and update ScreenLines[].
10200 * "end" is the line after the scrolled part. Normally it is Rows.
10201 * When scrolling region used "off" is the offset from the top for the region.
10202 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 *
10204 * Return OK for success, FAIL if the lines are not deleted.
10205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010207screen_del_lines(
10208 int off,
10209 int row,
10210 int line_count,
10211 int end,
10212 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010213 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010214 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215{
10216 int j;
10217 int i;
10218 unsigned temp;
10219 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010220 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 int cursor_end;
10222 int result_empty; /* result is empty until end of region */
10223 int can_delete; /* deleting line codes can be used */
10224 int type;
10225
10226 /*
10227 * FAIL if
10228 * - there is no valid screen
10229 * - the screen has to be redrawn completely
10230 * - the line count is less than one
10231 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010232 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010234 if (!screen_valid(TRUE) || line_count <= 0
10235 || (!force && line_count > p_ttyscroll)
10236#ifdef FEAT_CLIPBOARD
10237 || (clip_star.state != SELECT_CLEARED
10238 && redrawing_for_callback > 0)
10239#endif
10240 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 return FAIL;
10242
10243 /*
10244 * Check if the rest of the current region will become empty.
10245 */
10246 result_empty = row + line_count >= end;
10247
10248 /*
10249 * We can delete lines only when 'db' flag not set or when 'ce' option
10250 * available.
10251 */
10252 can_delete = (*T_DB == NUL || can_clear(T_CE));
10253
10254 /*
10255 * There are six ways to delete lines:
10256 * 0. When in a vertically split window and t_CV isn't set, redraw the
10257 * characters from ScreenLines[].
10258 * 1. Use T_CD if it exists and the result is empty.
10259 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10260 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10261 * none of the other ways work.
10262 * 4. Use T_CE (erase line) if the result is empty.
10263 * 5. Use T_DL (delete line) if it exists.
10264 * 6. redraw the characters from ScreenLines[].
10265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10267 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010268 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 type = USE_T_CD;
10270#if defined(__BEOS__) && defined(BEOS_DR8)
10271 /*
10272 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10273 * its internal termcap... this works okay for tests which test *T_DB !=
10274 * NUL. It has the disadvantage that the user cannot use any :set t_*
10275 * command to get T_DB (back) to empty_option, only :set term=... will do
10276 * the trick...
10277 * Anyway, this hack will hopefully go away with the next OS release.
10278 * (Olaf Seibert)
10279 */
10280 else if (row == 0 && T_DB == empty_option
10281 && (line_count == 1 || *T_CDL == NUL))
10282#else
10283 else if (row == 0 && (
10284#ifndef AMIGA
10285 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10286 * up, so use delete-line command */
10287 line_count == 1 ||
10288#endif
10289 *T_CDL == NUL))
10290#endif
10291 type = USE_NL;
10292 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10293 type = USE_T_CDL;
10294 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010295 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 type = USE_T_CE;
10297 else if (*T_DL != NUL && can_delete)
10298 type = USE_T_DL;
10299 else if (*T_CDL != NUL && can_delete)
10300 type = USE_T_CDL;
10301 else
10302 return FAIL;
10303
10304#ifdef FEAT_CLIPBOARD
10305 /* Remove a modeless selection when deleting lines halfway the screen or
10306 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010307 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010308 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 else
10310 clip_scroll_selection(line_count);
10311#endif
10312
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313#ifdef FEAT_GUI
10314 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10315 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010316 gui_dont_update_cursor(gui.cursor_row >= row + off
10317 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318#endif
10319
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010320 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10321 cursor_col = wp->w_wincol;
10322
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 if (*T_CCS != NUL) /* cursor relative to region */
10324 {
10325 cursor_row = row;
10326 cursor_end = end;
10327 }
10328 else
10329 {
10330 cursor_row = row + off;
10331 cursor_end = end + off;
10332 }
10333
10334 /*
10335 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10336 * Clear the inserted lines in ScreenLines[].
10337 */
10338 row += off;
10339 end += off;
10340 for (i = 0; i < line_count; ++i)
10341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 if (wp != NULL && wp->w_width != Columns)
10343 {
10344 /* need to copy part of a line */
10345 j = row + i;
10346 while ((j += line_count) <= end - 1)
10347 linecopy(j - line_count, j, wp);
10348 j -= line_count;
10349 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010350 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10351 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 else
10353 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10354 LineWraps[j] = FALSE;
10355 }
10356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 {
10358 /* whole width, moving the line pointers is faster */
10359 j = row + i;
10360 temp = LineOffset[j];
10361 while ((j += line_count) <= end - 1)
10362 {
10363 LineOffset[j - line_count] = LineOffset[j];
10364 LineWraps[j - line_count] = LineWraps[j];
10365 }
10366 LineOffset[j - line_count] = temp;
10367 LineWraps[j - line_count] = FALSE;
10368 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010369 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 else
10371 lineinvalid(temp, (int)Columns);
10372 }
10373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010375 if (screen_attr != clear_attr)
10376 screen_stop_highlight();
10377 if (clear_attr != 0)
10378 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 /* redraw the characters */
10381 if (type == USE_REDRAW)
10382 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010383 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010385 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 out_str(T_CD);
10387 screen_start(); /* don't know where cursor is now */
10388 }
10389 else if (type == USE_T_CDL)
10390 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010391 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 term_delete_lines(line_count);
10393 screen_start(); /* don't know where cursor is now */
10394 }
10395 /*
10396 * Deleting lines at top of the screen or scroll region: Just scroll
10397 * the whole screen (scroll region) up by outputting newlines on the
10398 * last line.
10399 */
10400 else if (type == USE_NL)
10401 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010402 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 for (i = line_count; --i >= 0; )
10404 out_char('\n'); /* cursor will remain on same line */
10405 }
10406 else
10407 {
10408 for (i = line_count; --i >= 0; )
10409 {
10410 if (type == USE_T_DL)
10411 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010412 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 out_str(T_DL); /* delete a line */
10414 }
10415 else /* type == USE_T_CE */
10416 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010417 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 out_str(T_CE); /* erase a line */
10419 }
10420 screen_start(); /* don't know where cursor is now */
10421 }
10422 }
10423
10424 /*
10425 * If the 'db' flag is set, we need to clear the lines that have been
10426 * scrolled up at the bottom of the region.
10427 */
10428 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10429 {
10430 for (i = line_count; i > 0; --i)
10431 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010432 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 out_str(T_CE); /* erase a line */
10434 screen_start(); /* don't know where cursor is now */
10435 }
10436 }
10437
10438#ifdef FEAT_GUI
10439 gui_can_update_cursor();
10440 if (gui.in_use)
10441 out_flush(); /* always flush after a scroll */
10442#endif
10443
10444 return OK;
10445}
10446
10447/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010448 * Return TRUE when postponing displaying the mode message: when not redrawing
10449 * or inside a mapping.
10450 */
10451 int
10452skip_showmode()
10453{
10454 // Call char_avail() only when we are going to show something, because it
10455 // takes a bit of time. redrawing() may also call char_avail_avail().
10456 if (global_busy
10457 || msg_silent != 0
10458 || !redrawing()
10459 || (char_avail() && !KeyTyped))
10460 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010461 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010462 return TRUE;
10463 }
10464 return FALSE;
10465}
10466
10467/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010468 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 *
10470 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10471 * If clear_cmdline is FALSE there may be a message there that needs to be
10472 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010473 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 * Return the length of the message (0 if no message).
10475 */
10476 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010477showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478{
10479 int need_clear;
10480 int length = 0;
10481 int do_mode;
10482 int attr;
10483 int nwr_save;
10484#ifdef FEAT_INS_EXPAND
10485 int sub_attr;
10486#endif
10487
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010488 do_mode = ((p_smd && msg_silent == 0)
10489 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010490 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010491 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010492 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010494 if (skip_showmode())
10495 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496
10497 nwr_save = need_wait_return;
10498
10499 /* wait a bit before overwriting an important message */
10500 check_for_delay(FALSE);
10501
10502 /* if the cmdline is more than one line high, erase top lines */
10503 need_clear = clear_cmdline;
10504 if (clear_cmdline && cmdline_row < Rows - 1)
10505 msg_clr_cmdline(); /* will reset clear_cmdline */
10506
10507 /* Position on the last line in the window, column 0 */
10508 msg_pos_mode();
10509 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010510 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 if (do_mode)
10512 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010513 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010515 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010516# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010517 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010518# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010519 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010520# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010521 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010522# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010523 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010525 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526# endif
10527#endif
10528#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10529 if (gui.in_use)
10530 {
10531 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010532 {
10533 /* HANGUL */
10534 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010535 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010536 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010537 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 }
10540#endif
10541#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010542 /* CTRL-X in Insert mode */
10543 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 {
10545 /* These messages can get long, avoid a wrap in a narrow
10546 * window. Prefer showing edit_submode_extra. */
10547 length = (Rows - msg_row) * Columns - 3;
10548 if (edit_submode_extra != NULL)
10549 length -= vim_strsize(edit_submode_extra);
10550 if (length > 0)
10551 {
10552 if (edit_submode_pre != NULL)
10553 length -= vim_strsize(edit_submode_pre);
10554 if (length - vim_strsize(edit_submode) > 0)
10555 {
10556 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010557 msg_puts_attr((char *)edit_submode_pre, attr);
10558 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 }
10560 if (edit_submode_extra != NULL)
10561 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010562 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010564 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 else
10566 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010567 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 }
10569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 }
10571 else
10572#endif
10573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010575 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010576 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010577 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 else if (State & INSERT)
10579 {
10580#ifdef FEAT_RIGHTLEFT
10581 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010582 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010584 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010586 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010587 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010589 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010591 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592#ifdef FEAT_RIGHTLEFT
10593 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010594 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595#endif
10596#ifdef FEAT_KEYMAP
10597 if (State & LANGMAP)
10598 {
10599# ifdef FEAT_ARABIC
10600 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010601 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 else
10603# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010604 if (get_keymap_str(curwin, (char_u *)" (%s)",
10605 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010606 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 }
10608#endif
10609 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010610 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 if (VIsual_active)
10613 {
10614 char *p;
10615
10616 /* Don't concatenate separate words to avoid translation
10617 * problems. */
10618 switch ((VIsual_select ? 4 : 0)
10619 + (VIsual_mode == Ctrl_V) * 2
10620 + (VIsual_mode == 'V'))
10621 {
10622 case 0: p = N_(" VISUAL"); break;
10623 case 1: p = N_(" VISUAL LINE"); break;
10624 case 2: p = N_(" VISUAL BLOCK"); break;
10625 case 4: p = N_(" SELECT"); break;
10626 case 5: p = N_(" SELECT LINE"); break;
10627 default: p = N_(" SELECT BLOCK"); break;
10628 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010629 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010631 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010633
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 need_clear = TRUE;
10635 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010636 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637#ifdef FEAT_INS_EXPAND
10638 && edit_submode == NULL /* otherwise it gets too long */
10639#endif
10640 )
10641 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010642 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 need_clear = TRUE;
10644 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010645
10646 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010647 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 msg_clr_eos();
10649 msg_didout = FALSE; /* overwrite this message */
10650 length = msg_col;
10651 msg_col = 0;
10652 need_wait_return = nwr_save; /* never ask for hit-return for this */
10653 }
10654 else if (clear_cmdline && msg_silent == 0)
10655 /* Clear the whole command line. Will reset "clear_cmdline". */
10656 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010657 else if (redraw_mode)
10658 {
10659 msg_pos_mode();
10660 msg_clr_eos();
10661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662
10663#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 /* In Visual mode the size of the selected area must be redrawn. */
10665 if (VIsual_active)
10666 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667
10668 /* If the last window has no status line, the ruler is after the mode
10669 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010670 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010671 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672#endif
10673 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010674 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 clear_cmdline = FALSE;
10676
10677 return length;
10678}
10679
10680/*
10681 * Position for a mode message.
10682 */
10683 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010684msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685{
10686 msg_col = 0;
10687 msg_row = Rows - 1;
10688}
10689
10690/*
10691 * Delete mode message. Used when ESC is typed which is expected to end
10692 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010693 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 */
10695 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010696unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697{
10698 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010699 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 */
10701 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10702 redraw_cmdline = TRUE; /* delete mode later */
10703 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010704 clearmode();
10705}
10706
10707/*
10708 * Clear the mode message.
10709 */
10710 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010711clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010712{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010713 int save_msg_row = msg_row;
10714 int save_msg_col = msg_col;
10715
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010716 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010717 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010718 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010719 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010720
10721 msg_col = save_msg_col;
10722 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723}
10724
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010725 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010726recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010727{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010728 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010729 if (!shortmess(SHM_RECORDING))
10730 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010731 char s[4];
10732
10733 sprintf(s, " @%c", reg_recording);
10734 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010735 }
10736}
10737
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010738/*
10739 * Draw the tab pages line at the top of the Vim window.
10740 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010741 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010742draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010743{
10744 int tabcount = 0;
10745 tabpage_T *tp;
10746 int tabwidth;
10747 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010748 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010749 int attr;
10750 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010751 win_T *cwp;
10752 int wincount;
10753 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010754 int c;
10755 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010756 int attr_sel = HL_ATTR(HLF_TPS);
10757 int attr_nosel = HL_ATTR(HLF_TP);
10758 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010759 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010760 int room;
10761 int use_sep_chars = (t_colors < 8
10762#ifdef FEAT_GUI
10763 && !gui.in_use
10764#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010765#ifdef FEAT_TERMGUICOLORS
10766 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010767#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010768 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010769
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010770 if (ScreenLines == NULL)
10771 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010772 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010773
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010774#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010775 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010776 if (gui_use_tabline())
10777 {
10778 gui_update_tabline();
10779 return;
10780 }
10781#endif
10782
10783 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010784 return;
10785
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010786#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010787 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010788
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010789 /* Use the 'tabline' option if it's set. */
10790 if (*p_tal != NUL)
10791 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010792 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010793
10794 /* Check for an error. If there is one we would loop in redrawing the
10795 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010796 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010797 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010798 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010799 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010800 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010801 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010802 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010803 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010804#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010805 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010806 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010807 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010808
Bram Moolenaar238a5642006-02-21 22:12:05 +000010809 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10810 if (tabwidth < 6)
10811 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010812
Bram Moolenaar238a5642006-02-21 22:12:05 +000010813 attr = attr_nosel;
10814 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010815 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10816 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010817 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010818 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010819
Bram Moolenaar238a5642006-02-21 22:12:05 +000010820 if (tp->tp_topframe == topframe)
10821 attr = attr_sel;
10822 if (use_sep_chars && col > 0)
10823 screen_putchar('|', 0, col++, attr);
10824
10825 if (tp->tp_topframe != topframe)
10826 attr = attr_nosel;
10827
10828 screen_putchar(' ', 0, col++, attr);
10829
10830 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010831 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010832 cwp = curwin;
10833 wp = firstwin;
10834 }
10835 else
10836 {
10837 cwp = tp->tp_curwin;
10838 wp = tp->tp_firstwin;
10839 }
10840
10841 modified = FALSE;
10842 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10843 if (bufIsChanged(wp->w_buffer))
10844 modified = TRUE;
10845 if (modified || wincount > 1)
10846 {
10847 if (wincount > 1)
10848 {
10849 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010850 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010851 if (col + len >= Columns - 3)
10852 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010853 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010854#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010855 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010856#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010857 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010858#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010859 );
10860 col += len;
10861 }
10862 if (modified)
10863 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10864 screen_putchar(' ', 0, col++, attr);
10865 }
10866
10867 room = scol - col + tabwidth - 1;
10868 if (room > 0)
10869 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010870 /* Get buffer name in NameBuff[] */
10871 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010872 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010873 len = vim_strsize(NameBuff);
10874 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010875 if (has_mbyte)
10876 while (len > room)
10877 {
10878 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010879 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010880 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010881 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010882 {
10883 p += len - room;
10884 len = room;
10885 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010886 if (len > Columns - col - 1)
10887 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010888
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010889 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010890 col += len;
10891 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010892 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010893
10894 /* Store the tab page number in TabPageIdxs[], so that
10895 * jump_to_mouse() knows where each one is. */
10896 ++tabcount;
10897 while (scol < col)
10898 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010899 }
10900
Bram Moolenaar238a5642006-02-21 22:12:05 +000010901 if (use_sep_chars)
10902 c = '_';
10903 else
10904 c = ' ';
10905 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010906
10907 /* Put an "X" for closing the current tab if there are several. */
10908 if (first_tabpage->tp_next != NULL)
10909 {
10910 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10911 TabPageIdxs[Columns - 1] = -999;
10912 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010913 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010914
10915 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10916 * set. */
10917 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010918}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010919
10920/*
10921 * Get buffer name for "buf" into NameBuff[].
10922 * Takes care of special buffer names and translates special characters.
10923 */
10924 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010925get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010926{
10927 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010928 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010929 else
10930 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10931 trans_characters(NameBuff, MAXPATHL);
10932}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010933
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934/*
10935 * Get the character to use in a status line. Get its attributes in "*attr".
10936 */
10937 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010938fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939{
10940 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010941
10942#ifdef FEAT_TERMINAL
10943 if (bt_terminal(wp->w_buffer))
10944 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010945 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010946 {
10947 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010948 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010949 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010950 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010951 {
10952 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010953 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010954 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010955 }
10956 else
10957#endif
10958 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010960 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 fill = fill_stl;
10962 }
10963 else
10964 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010965 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 fill = fill_stlnc;
10967 }
10968 /* Use fill when there is highlighting, and highlighting of current
10969 * window differs, or the fillchars differ, or this is not the
10970 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010971 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010972 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 || (fill_stl != fill_stlnc)))
10974 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010975 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 return '^';
10977 return '=';
10978}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980/*
10981 * Get the character to use in a separator between vertically split windows.
10982 * Get its attributes in "*attr".
10983 */
10984 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010985fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010987 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 if (*attr == 0 && fill_vert == ' ')
10989 return '|';
10990 else
10991 return fill_vert;
10992}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993
10994/*
10995 * Return TRUE if redrawing should currently be done.
10996 */
10997 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010998redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010011000#ifdef FEAT_EVAL
11001 if (disable_redraw_for_testing)
11002 return 0;
11003 else
11004#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020011005 return ((!RedrawingDisabled
11006#ifdef FEAT_EVAL
11007 || ignore_redraw_flag_for_testing
11008#endif
11009 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010}
11011
11012/*
11013 * Return TRUE if printing messages should currently be done.
11014 */
11015 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011016messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017{
11018 return (!(p_lz && char_avail() && !KeyTyped));
11019}
11020
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011021#ifdef FEAT_MENU
11022/*
11023 * Draw the window toolbar.
11024 */
11025 static void
11026redraw_win_toolbar(win_T *wp)
11027{
11028 vimmenu_T *menu;
11029 int item_idx = 0;
11030 int item_count = 0;
11031 int col = 0;
11032 int next_col;
11033 int off = (int)(current_ScreenLine - ScreenLines);
11034 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
11035 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
11036
11037 vim_free(wp->w_winbar_items);
11038 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11039 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011040 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011041
11042 /* TODO: use fewer spaces if there is not enough room */
11043 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011044 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011045 {
11046 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011047 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011048 break;
11049 if (col > 1)
11050 {
11051 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011052 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011053 break;
11054 }
11055
11056 wp->w_winbar_items[item_idx].wb_startcol = col;
11057 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011058 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011059 break;
11060
11061 next_col = text_to_screenline(wp, menu->name, col);
11062 while (col < next_col)
11063 {
11064 ScreenAttrs[off + col] = button_attr;
11065 ++col;
11066 }
11067 wp->w_winbar_items[item_idx].wb_endcol = col;
11068 wp->w_winbar_items[item_idx].wb_menu = menu;
11069 ++item_idx;
11070
Bram Moolenaar02631462017-09-22 15:20:32 +020011071 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011072 break;
11073 space_to_screenline(off + col, button_attr);
11074 ++col;
11075 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011076 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011077 {
11078 space_to_screenline(off + col, fill_attr);
11079 ++col;
11080 }
11081 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11082
Bram Moolenaar02631462017-09-22 15:20:32 +020011083 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011084 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011085}
11086#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011087
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088/*
11089 * Show current status info in ruler and various other places
11090 * If always is FALSE, only show ruler if position has changed.
11091 */
11092 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011093showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094{
11095 if (!always && !redrawing())
11096 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011097#ifdef FEAT_INS_EXPAND
11098 if (pum_visible())
11099 {
11100 /* Don't redraw right now, do it later. */
11101 curwin->w_redr_status = TRUE;
11102 return;
11103 }
11104#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011105#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011106 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011107 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 else
11109#endif
11110#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011111 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112#endif
11113
11114#ifdef FEAT_TITLE
11115 if (need_maketitle
11116# ifdef FEAT_STL_OPT
11117 || (p_icon && (stl_syntax & STL_IN_ICON))
11118 || (p_title && (stl_syntax & STL_IN_TITLE))
11119# endif
11120 )
11121 maketitle();
11122#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011123 /* Redraw the tab pages line if needed. */
11124 if (redraw_tabline)
11125 draw_tabline();
Bram Moolenaar988c4332019-06-02 14:12:11 +020011126
11127#ifdef FEAT_TEXT_PROP
11128 // Display popup windows on top of everything.
11129 update_popups();
11130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131}
11132
11133#ifdef FEAT_CMDL_INFO
11134 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011135win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011137#define RULER_BUF_LEN 70
11138 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 int row;
11140 int fillchar;
11141 int attr;
11142 int empty_line = FALSE;
11143 colnr_T virtcol;
11144 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011145 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 int this_ru_col;
11148 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011149 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150
11151 /* If 'ruler' off or redrawing disabled, don't do anything */
11152 if (!p_ru)
11153 return;
11154
11155 /*
11156 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11157 * after deleting lines, before cursor.lnum is corrected.
11158 */
11159 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11160 return;
11161
11162#ifdef FEAT_INS_EXPAND
11163 /* Don't draw the ruler while doing insert-completion, it might overwrite
11164 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 if (edit_submode != NULL)
11167 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011168 // Don't draw the ruler when the popup menu is visible, it may overlap.
11169 // Except when the popup menu will be redrawn anyway.
11170 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011171 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172#endif
11173
11174#ifdef FEAT_STL_OPT
11175 if (*p_ruf)
11176 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011177 int save_called_emsg = called_emsg;
11178
11179 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011181 if (called_emsg)
11182 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011183 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011184 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 return;
11186 }
11187#endif
11188
11189 /*
11190 * Check if not in Insert mode and the line is empty (will show "0-1").
11191 */
11192 if (!(State & INSERT)
11193 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11194 empty_line = TRUE;
11195
11196 /*
11197 * Only draw the ruler when something changed.
11198 */
11199 validate_virtcol_win(wp);
11200 if ( redraw_cmdline
11201 || always
11202 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11203 || wp->w_cursor.col != wp->w_ru_cursor.col
11204 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 || wp->w_topline != wp->w_ru_topline
11207 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11208#ifdef FEAT_DIFF
11209 || wp->w_topfill != wp->w_ru_topfill
11210#endif
11211 || empty_line != wp->w_ru_empty)
11212 {
11213 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 if (wp->w_status_height)
11215 {
11216 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011217 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011218 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011219 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 }
11221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 {
11223 row = Rows - 1;
11224 fillchar = ' ';
11225 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226 width = Columns;
11227 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 }
11229
11230 /* In list mode virtcol needs to be recomputed */
11231 virtcol = wp->w_virtcol;
11232 if (wp->w_p_list && lcs_tab1 == NUL)
11233 {
11234 wp->w_p_list = FALSE;
11235 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11236 wp->w_p_list = TRUE;
11237 }
11238
11239 /*
11240 * Some sprintfs return the length, some return a pointer.
11241 * To avoid portability problems we use strlen() here.
11242 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011243 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11245 ? 0L
11246 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011247 len = STRLEN(buffer);
11248 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11250 (int)virtcol + 1);
11251
11252 /*
11253 * Add a "50%" if there is room for it.
11254 * On the last line, don't print in the last column (scrolls the
11255 * screen up on some terminals).
11256 */
11257 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011258 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 this_ru_col = ru_col - (Columns - width);
11263 if (this_ru_col < 0)
11264 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 /* Never use more than half the window/screen width, leave the other
11266 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011267 if (this_ru_col < (width + 1) / 2)
11268 this_ru_col = (width + 1) / 2;
11269 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011271 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011272 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 if (has_mbyte)
11275 i += (*mb_char2bytes)(fillchar, buffer + i);
11276 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 buffer[i++] = fillchar;
11278 ++o;
11279 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011280 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 }
11282 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 if (has_mbyte)
11284 {
11285 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011286 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 {
11288 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011289 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 {
11291 buffer[i] = NUL;
11292 break;
11293 }
11294 }
11295 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011296 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011297 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298
Bram Moolenaar4033c552017-09-16 20:54:51 +020011299 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 i = redraw_cmdline;
11301 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011302 this_ru_col + off + (int)STRLEN(buffer),
11303 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 fillchar, fillchar, attr);
11305 /* don't redraw the cmdline because of showing the ruler */
11306 redraw_cmdline = i;
11307 wp->w_ru_cursor = wp->w_cursor;
11308 wp->w_ru_virtcol = wp->w_virtcol;
11309 wp->w_ru_empty = empty_line;
11310 wp->w_ru_topline = wp->w_topline;
11311 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11312#ifdef FEAT_DIFF
11313 wp->w_ru_topfill = wp->w_topfill;
11314#endif
11315 }
11316}
11317#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011318
11319#if defined(FEAT_LINEBREAK) || defined(PROTO)
11320/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011321 * Return the width of the 'number' and 'relativenumber' column.
11322 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011323 * Otherwise it depends on 'numberwidth' and the line count.
11324 */
11325 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011326number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011327{
11328 int n;
11329 linenr_T lnum;
11330
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011331 if (wp->w_p_rnu && !wp->w_p_nu)
11332 /* cursor line shows "0" */
11333 lnum = wp->w_height;
11334 else
11335 /* cursor line shows absolute line number */
11336 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011337
Bram Moolenaar6b314672015-03-20 15:42:10 +010011338 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011339 return wp->w_nrwidth_width;
11340 wp->w_nrwidth_line_count = lnum;
11341
11342 n = 0;
11343 do
11344 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011345 lnum /= 10;
11346 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011347 } while (lnum > 0);
11348
11349 /* 'numberwidth' gives the minimal width plus one */
11350 if (n < wp->w_p_nuw - 1)
11351 n = wp->w_p_nuw - 1;
11352
11353 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011354 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011355 return n;
11356}
11357#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011358
Bram Moolenaar113e1072019-01-20 15:30:40 +010011359#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011360/*
11361 * Return the current cursor column. This is the actual position on the
11362 * screen. First column is 0.
11363 */
11364 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011365screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011366{
11367 return screen_cur_col;
11368}
11369
11370/*
11371 * Return the current cursor row. This is the actual position on the screen.
11372 * First row is 0.
11373 */
11374 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011375screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011376{
11377 return screen_cur_row;
11378}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011379#endif