blob: a4eef32052f5ad22ec266b9d387f5f4c463314ef [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
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200110#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
111static int text_to_screenline(win_T *wp, char_u *text, int col);
112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_FOLDING
114static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
117
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200118/* Flag that is set when drawing for a callback, not from the main command
119 * loop. */
120static int redrawing_for_callback = 0;
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122/*
123 * Buffer for one screen line (characters and attributes).
124 */
125static schar_T *current_ScreenLine;
126
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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100129static void win_draw_end(win_T *wp, int c1, int c2, 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 Moolenaarf3d769a2017-09-22 13:44:56 +0200135static int win_line(win_T *, linenr_T, int, int, int nochange);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000138#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200142# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void start_search_hl(void);
144static void end_search_hl(void);
145static void init_search_hl(win_T *wp);
146static void prepare_search_hl(win_T *wp, linenr_T lnum);
147static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
148static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screen_start_highlight(int attr);
151static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200156static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void lineinvalid(unsigned off, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100158static void linecopy(int to, int from, win_T *wp);
159static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200160static 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 +0100161static void win_rest_invalid(win_T *wp);
162static void msg_pos_mode(void);
163static void recording_mode(int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void draw_tabline(void);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200165static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100166static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200167#ifdef FEAT_MENU
168static void redraw_win_toolbar(win_T *wp);
169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200174static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177/* Ugly global: overrule attribute used by screen_char() */
178static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200180#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
181/* Can limit syntax highlight time to 'redrawtime'. */
182# define SYN_TIME_LIMIT 1
183#endif
184
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200185#ifdef FEAT_RIGHTLEFT
186# define HAS_RIGHTLEFT(x) x
187#else
188# define HAS_RIGHTLEFT(x) FALSE
189#endif
190
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191/*
192 * Redraw the current window later, with update_screen(type).
193 * Set must_redraw only if not already set to a higher value.
194 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
195 */
196 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100197redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
199 redraw_win_later(curwin, type);
200}
201
202 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100203redraw_win_later(
204 win_T *wp,
205 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200207 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 {
209 wp->w_redr_type = type;
210 if (type >= NOT_VALID)
211 wp->w_lines_valid = 0;
212 if (must_redraw < type) /* must_redraw is the maximum of all windows */
213 must_redraw = type;
214 }
215}
216
217/*
218 * Force a complete redraw later. Also resets the highlighting. To be used
219 * after executing a shell command that messes up the screen.
220 */
221 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100222redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223{
224 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000225#ifdef FEAT_GUI
226 if (gui.in_use)
227 /* Use a code that will reset gui.highlight_mask in
228 * gui_stop_highlight(). */
229 screen_attr = HL_ALL + 1;
230 else
231#endif
232 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200233 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234}
235
236/*
237 * Mark all windows to be redrawn later.
238 */
239 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100240redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241{
242 win_T *wp;
243
244 FOR_ALL_WINDOWS(wp)
245 {
246 redraw_win_later(wp, type);
247 }
248}
249
250/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000251 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 */
253 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100254redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255{
256 redraw_buf_later(curbuf, type);
257}
258
259 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100260redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261{
262 win_T *wp;
263
264 FOR_ALL_WINDOWS(wp)
265 {
266 if (wp->w_buffer == buf)
267 redraw_win_later(wp, type);
268 }
269}
270
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200271 void
272redraw_buf_and_status_later(buf_T *buf, int type)
273{
274 win_T *wp;
275
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200276#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200277 if (wild_menu_showing != 0)
278 /* Don't redraw while the command line completion is displayed, it
279 * would disappear. */
280 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200281#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200282 FOR_ALL_WINDOWS(wp)
283 {
284 if (wp->w_buffer == buf)
285 {
286 redraw_win_later(wp, type);
287 wp->w_redr_status = TRUE;
288 }
289 }
290}
291
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200293 * Redraw as soon as possible. When the command line is not scrolled redraw
294 * right away and restore what was on the command line.
295 * Return a code indicating what happened.
296 */
297 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100298redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299{
300 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200301 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200302 int r;
303 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200304 schar_T *screenline; /* copy from ScreenLines[] */
305 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306#ifdef FEAT_MBYTE
307 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200308 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200309 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200310 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200311#endif
312
313 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200314 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200315 return ret;
316
317 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200318 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200319 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200320 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200321 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200322 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200323 if (screenline == NULL || screenattr == NULL)
324 ret = 2;
325#ifdef FEAT_MBYTE
326 if (enc_utf8)
327 {
328 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200329 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenlineUC == NULL)
331 ret = 2;
332 for (i = 0; i < p_mco; ++i)
333 {
334 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200335 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336 if (screenlineC[i] == NULL)
337 ret = 2;
338 }
339 }
340 if (enc_dbcs == DBCS_JPNU)
341 {
342 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200343 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200344 if (screenline2 == NULL)
345 ret = 2;
346 }
347#endif
348
349 if (ret != 2)
350 {
351 /* Save the text displayed in the command line area. */
352 for (r = 0; r < rows; ++r)
353 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200354 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 (size_t)cols * sizeof(schar_T));
357 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200358 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360#ifdef FEAT_MBYTE
361 if (enc_utf8)
362 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200364 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 mch_memmove(screenlineC[i] + r * cols,
368 ScreenLinesC[i] + LineOffset[cmdline_row + r],
369 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 }
371 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200372 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200373 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200375#endif
376 }
377
378 update_screen(0);
379 ret = 3;
380
381 if (must_redraw == 0)
382 {
383 int off = (int)(current_ScreenLine - ScreenLines);
384
385 /* Restore the text displayed in the command line area. */
386 for (r = 0; r < rows; ++r)
387 {
388 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200389 screenline + r * cols,
390 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200391 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 screenattr + r * cols,
393 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394#ifdef FEAT_MBYTE
395 if (enc_utf8)
396 {
397 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200398 screenlineUC + r * cols,
399 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200400 for (i = 0; i < p_mco; ++i)
401 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200402 screenlineC[i] + r * cols,
403 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 }
405 if (enc_dbcs == DBCS_JPNU)
406 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200407 screenline2 + r * cols,
408 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200409#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200410 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200411 }
412 ret = 4;
413 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200414 }
415
416 vim_free(screenline);
417 vim_free(screenattr);
418#ifdef FEAT_MBYTE
419 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);
427#endif
428
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200429 /* Show the intro message when appropriate. */
430 maybe_intro_message();
431
432 setcursor();
433
Bram Moolenaar2951b772013-07-03 12:45:31 +0200434 return ret;
435}
436
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)
450 ; /* do nothing */
451 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200452 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200453 /* Redrawing only works when the screen didn't scroll. Don't clear
454 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200455 if (msg_scrolled == 0
456#ifdef FEAT_WILDMENU
457 && wild_menu_showing == 0
458#endif
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200459 && call_update_screen)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200460 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200461 /* Redraw in the same position, so that the user can continue
462 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200463 redrawcmdline_ex(FALSE);
464 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200465 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100466 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200467 /* keep the command line if possible */
468 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100469 setcursor();
470 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100471 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100472#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100473 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200474 /* Don't update the cursor when it is blinking and off to avoid
475 * flicker. */
Bram Moolenaara338adc2018-01-31 20:51:47 +0100476 out_flush_cursor(FALSE, FALSE);
477 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100478#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100479 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200480
481 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100482}
483
484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 * Changed something in the current window, at buffer line "lnum", that
486 * requires that line and possibly other lines to be redrawn.
487 * Used when entering/leaving Insert mode with the cursor on a folded line.
488 * Used to remove the "$" from a change command.
489 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
490 * may become invalid and the whole window will have to be redrawn.
491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100493redrawWinline(
494 linenr_T lnum,
495 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496{
497#ifdef FEAT_FOLDING
498 int i;
499#endif
500
501 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
502 curwin->w_redraw_top = lnum;
503 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
504 curwin->w_redraw_bot = lnum;
505 redraw_later(VALID);
506
507#ifdef FEAT_FOLDING
508 if (invalid)
509 {
510 /* A w_lines[] entry for this lnum has become invalid. */
511 i = find_wl_entry(curwin, lnum);
512 if (i >= 0)
513 curwin->w_lines[i].wl_valid = FALSE;
514 }
515#endif
516}
517
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200518 void
519reset_updating_screen(int may_resize_shell UNUSED)
520{
521 updating_screen = FALSE;
522#ifdef FEAT_GUI
523 if (may_resize_shell)
524 gui_may_resize_shell();
525#endif
526#ifdef FEAT_TERMINAL
527 term_check_channel_closed_recently();
528#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200529
530#ifdef HAVE_DROP_FILE
531 // If handle_drop() was called while updating_screen was TRUE need to
532 // handle the drop now.
533 handle_any_postponed_drop();
534#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200535}
536
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200538 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 */
540 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100541update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
543 redraw_curbuf_later(type);
544 update_screen(type);
545}
546
547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 * Based on the current value of curwin->w_topline, transfer a screenfull
549 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200550 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200552 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200553update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200555 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 win_T *wp;
557 static int did_intro = FALSE;
558#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
559 int did_one;
560#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200561#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200562 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200563 int gui_cursor_col;
564 int gui_cursor_row;
565#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200566 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000568 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200570 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200572 if (type == VALID_NO_UPDATE)
573 {
574 no_update = TRUE;
575 type = 0;
576 }
577
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 if (must_redraw)
579 {
580 if (type < must_redraw) /* use maximal type */
581 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000582
583 /* must_redraw is reset here, so that when we run into some weird
584 * reason to redraw while busy redrawing (e.g., asynchronous
585 * scrolling), or update_topline() in win_update() will cause a
586 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 must_redraw = 0;
588 }
589
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200590 /* May need to update w_lines[]. */
591 if (curwin->w_lines_valid == 0 && type < NOT_VALID
592#ifdef FEAT_TERMINAL
593 && !term_do_update_window(curwin)
594#endif
595 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 type = NOT_VALID;
597
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000598 /* Postpone the redrawing when it's not needed and when being called
599 * recursively. */
600 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 {
602 redraw_later(type); /* remember type for next time */
603 must_redraw = type;
604 if (type > INVERTED_ALL)
605 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200606 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
608
609 updating_screen = TRUE;
610#ifdef FEAT_SYN_HL
611 ++display_tick; /* let syntax code know we're in a next round of
612 * display updating */
613#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200614 if (no_update)
615 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617 /*
618 * if the screen was scrolled up when displaying a message, scroll it down
619 */
620 if (msg_scrolled)
621 {
622 clear_cmdline = TRUE;
623 if (msg_scrolled > Rows - 5) /* clearing is faster */
624 type = CLEAR;
625 else if (type != CLEAR)
626 {
627 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200628 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
629 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 type = CLEAR;
631 FOR_ALL_WINDOWS(wp)
632 {
633 if (W_WINROW(wp) < msg_scrolled)
634 {
635 if (W_WINROW(wp) + wp->w_height > msg_scrolled
636 && wp->w_redr_type < REDRAW_TOP
637 && wp->w_lines_valid > 0
638 && wp->w_topline == wp->w_lines[0].wl_lnum)
639 {
640 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
641 wp->w_redr_type = REDRAW_TOP;
642 }
643 else
644 {
645 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200646 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
647 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 }
650 }
651 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200652 if (!no_update)
653 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000654 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 }
656 msg_scrolled = 0;
657 need_wait_return = FALSE;
658 }
659
660 /* reset cmdline_row now (may have been changed temporarily) */
661 compute_cmdrow();
662
663 /* Check for changed highlighting */
664 if (need_highlight_changed)
665 highlight_changed();
666
667 if (type == CLEAR) /* first clear screen */
668 {
669 screenclear(); /* will reset clear_cmdline */
670 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200671 /* must_redraw may be set indirectly, avoid another redraw later */
672 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 }
674
675 if (clear_cmdline) /* going to clear cmdline (done below) */
676 check_for_delay(FALSE);
677
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000678#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200679 /* Force redraw when width of 'number' or 'relativenumber' column
680 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000681 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200682 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
683 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000684 curwin->w_redr_type = NOT_VALID;
685#endif
686
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 /*
688 * Only start redrawing if there is really something to do.
689 */
690 if (type == INVERTED)
691 update_curswant();
692 if (curwin->w_redr_type < type
693 && !((type == VALID
694 && curwin->w_lines[0].wl_valid
695#ifdef FEAT_DIFF
696 && curwin->w_topfill == curwin->w_old_topfill
697 && curwin->w_botfill == curwin->w_old_botfill
698#endif
699 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000701 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
703 && curwin->w_old_visual_mode == VIsual_mode
704 && (curwin->w_valid & VALID_VIRTCOL)
705 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 ))
707 curwin->w_redr_type = type;
708
Bram Moolenaar5a305422006-04-28 22:38:25 +0000709 /* Redraw the tab pages line if needed. */
710 if (redraw_tabline || type >= NOT_VALID)
711 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000712
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713#ifdef FEAT_SYN_HL
714 /*
715 * Correct stored syntax highlighting info for changes in each displayed
716 * buffer. Each buffer must only be done once.
717 */
718 FOR_ALL_WINDOWS(wp)
719 {
720 if (wp->w_buffer->b_mod_set)
721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 win_T *wwp;
723
724 /* Check if we already did this buffer. */
725 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
726 if (wwp->w_buffer == wp->w_buffer)
727 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200728 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 syn_stack_apply_changes(wp->w_buffer);
730 }
731 }
732#endif
733
734 /*
735 * Go from top to bottom through the windows, redrawing the ones that need
736 * it.
737 */
738#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
739 did_one = FALSE;
740#endif
741#ifdef FEAT_SEARCH_EXTRA
742 search_hl.rm.regprog = NULL;
743#endif
744 FOR_ALL_WINDOWS(wp)
745 {
746 if (wp->w_redr_type != 0)
747 {
748 cursor_off();
749#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
750 if (!did_one)
751 {
752 did_one = TRUE;
753# ifdef FEAT_SEARCH_EXTRA
754 start_search_hl();
755# endif
756# ifdef FEAT_CLIPBOARD
757 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200758 if (clip_star.available && clip_isautosel_star())
759 clip_update_selection(&clip_star);
760 if (clip_plus.available && clip_isautosel_plus())
761 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762# endif
763#ifdef FEAT_GUI
764 /* Remove the cursor before starting to do anything, because
765 * scrolling may make it difficult to redraw the text under
766 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200767 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200768 {
769 gui_cursor_col = gui.cursor_col;
770 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200772 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#endif
775 }
776#endif
777 win_update(wp);
778 }
779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 /* redraw status line after the window to minimize cursor movement */
781 if (wp->w_redr_status)
782 {
783 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200784 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
787#if defined(FEAT_SEARCH_EXTRA)
788 end_search_hl();
789#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100790#ifdef FEAT_INS_EXPAND
791 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200792 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 /* Reset b_mod_set flags. Going through all windows is probably faster
796 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200797 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200800 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
802 /* Clear or redraw the command line. Done last, because scrolling may
803 * mess up the command line. */
804 if (clear_cmdline || redraw_cmdline)
805 showmode();
806
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200807 if (no_update)
808 --no_win_do_lines_ins;
809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200811 if (!did_intro)
812 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 did_intro = TRUE;
814
815#ifdef FEAT_GUI
816 /* Redraw the cursor and update the scrollbars when all screen updating is
817 * done. */
818 if (gui.in_use)
819 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200820 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200821 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100822 mch_disable_flush();
823 out_flush(); /* required before updating the cursor */
824 mch_enable_flush();
825
Bram Moolenaar144445d2016-07-08 21:41:54 +0200826 /* Put the GUI position where the cursor was, gui_update_cursor()
827 * uses that. */
828 gui.col = gui_cursor_col;
829 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200830# ifdef FEAT_MBYTE
831 gui.col = mb_fix_col(gui.col, gui.row);
832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100834 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200835 screen_cur_col = gui.col;
836 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200837 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100838 else
839 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 gui_update_scrollbars(FALSE);
841 }
842#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200843 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844}
845
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100846#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
847/*
848 * Prepare for updating one or more windows.
849 * Caller must check for "updating_screen" already set to avoid recursiveness.
850 */
851 static void
852update_prepare(void)
853{
854 cursor_off();
855 updating_screen = TRUE;
856#ifdef FEAT_GUI
857 /* Remove the cursor before starting to do anything, because scrolling may
858 * make it difficult to redraw the text under it. */
859 if (gui.in_use)
860 gui_undraw_cursor();
861#endif
862#ifdef FEAT_SEARCH_EXTRA
863 start_search_hl();
864#endif
865}
866
867/*
868 * Finish updating one or more windows.
869 */
870 static void
871update_finish(void)
872{
873 if (redraw_cmdline)
874 showmode();
875
876# ifdef FEAT_SEARCH_EXTRA
877 end_search_hl();
878# endif
879
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200880 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100881
882# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100883 /* Redraw the cursor and update the scrollbars when all screen updating is
884 * done. */
885 if (gui.in_use)
886 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100887 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100888 gui_update_scrollbars(FALSE);
889 }
890# endif
891}
892#endif
893
Bram Moolenaar860cae12010-06-05 23:22:07 +0200894#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200895/*
896 * Return TRUE if the cursor line in window "wp" may be concealed, according
897 * to the 'concealcursor' option.
898 */
899 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100900conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200901{
902 int c;
903
904 if (*wp->w_p_cocu == NUL)
905 return FALSE;
906 if (get_real_state() & VISUAL)
907 c = 'v';
908 else if (State & INSERT)
909 c = 'i';
910 else if (State & NORMAL)
911 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200912 else if (State & CMDLINE)
913 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200914 else
915 return FALSE;
916 return vim_strchr(wp->w_p_cocu, c) != NULL;
917}
918
919/*
920 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
921 */
922 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200923conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200924{
925 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
926 {
927 need_cursor_line_redraw = TRUE;
928 /* Need to recompute cursor column, e.g., when starting Visual mode
929 * without concealing. */
930 curs_columns(TRUE);
931 }
932}
933
Bram Moolenaar860cae12010-06-05 23:22:07 +0200934 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100935update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200936{
937 int row;
938 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200939#ifdef SYN_TIME_LIMIT
940 proftime_T syntax_tm;
941#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200942
Bram Moolenaar908be432016-05-24 10:51:30 +0200943 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100944 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200945 return;
946
Bram Moolenaar860cae12010-06-05 23:22:07 +0200947 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200948 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200949 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200950#ifdef SYN_TIME_LIMIT
951 /* Set the time limit to 'redrawtime'. */
952 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200953 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200954#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100955 update_prepare();
956
Bram Moolenaar860cae12010-06-05 23:22:07 +0200957 row = 0;
958 for (j = 0; j < wp->w_lines_valid; ++j)
959 {
960 if (lnum == wp->w_lines[j].wl_lnum)
961 {
962 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200963# ifdef FEAT_SEARCH_EXTRA
964 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200965 start_search_hl();
966 prepare_search_hl(wp, lnum);
967# endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200968 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200969# if defined(FEAT_SEARCH_EXTRA)
970 end_search_hl();
971# endif
972 break;
973 }
974 row += wp->w_lines[j].wl_size;
975 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100976
977 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200978
979#ifdef SYN_TIME_LIMIT
980 syn_set_timeout(NULL);
981#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200982 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200983 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984}
985#endif
986
987#if defined(FEAT_SIGNS) || defined(PROTO)
988 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100989update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
991 win_T *wp;
992 int doit = FALSE;
993
994# ifdef FEAT_FOLDING
995 win_foldinfo.fi_level = 0;
996# endif
997
998 /* update/delete a specific mark */
999 FOR_ALL_WINDOWS(wp)
1000 {
1001 if (buf != NULL && lnum > 0)
1002 {
1003 if (wp->w_buffer == buf && lnum >= wp->w_topline
1004 && lnum < wp->w_botline)
1005 {
1006 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1007 wp->w_redraw_top = lnum;
1008 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1009 wp->w_redraw_bot = lnum;
1010 redraw_win_later(wp, VALID);
1011 }
1012 }
1013 else
1014 redraw_win_later(wp, VALID);
1015 if (wp->w_redr_type != 0)
1016 doit = TRUE;
1017 }
1018
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001019 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001020 * happening (recursive call), messages on the screen or still starting up.
1021 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001022 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001023 || State == ASKMORE || State == HITRETURN
1024 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001025#ifdef FEAT_GUI
1026 || gui.starting
1027#endif
1028 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 return;
1030
1031 /* update all windows that need updating */
1032 update_prepare();
1033
Bram Moolenaar29323592016-07-24 22:04:11 +02001034 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 if (wp->w_redr_type != 0)
1037 win_update(wp);
1038 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001039 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
1042 update_finish();
1043}
1044#endif
1045
1046
1047#if defined(FEAT_GUI) || defined(PROTO)
1048/*
1049 * Update a single window, its status line and maybe the command line msg.
1050 * Used for the GUI scrollbar.
1051 */
1052 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001053updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001055 /* return if already busy updating */
1056 if (updating_screen)
1057 return;
1058
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 update_prepare();
1060
1061#ifdef FEAT_CLIPBOARD
1062 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001063 if (clip_star.available && clip_isautosel_star())
1064 clip_update_selection(&clip_star);
1065 if (clip_plus.available && clip_isautosel_plus())
1066 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001070
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001071 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001072 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001073 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (wp->w_redr_status
1076# ifdef FEAT_CMDL_INFO
1077 || p_ru
1078# endif
1079# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001080 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081# endif
1082 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001083 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
1085 update_finish();
1086}
1087#endif
1088
1089/*
1090 * Update a single window.
1091 *
1092 * This may cause the windows below it also to be redrawn (when clearing the
1093 * screen or scrolling lines).
1094 *
1095 * How the window is redrawn depends on wp->w_redr_type. Each type also
1096 * implies the one below it.
1097 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001098 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1100 * INVERTED redraw the changed part of the Visual area
1101 * INVERTED_ALL redraw the whole Visual area
1102 * VALID 1. scroll up/down to adjust for a changed w_topline
1103 * 2. update lines at the top when scrolled down
1104 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001105 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 * b_mod_top and b_mod_bot.
1107 * - if wp->w_redraw_top non-zero, redraw lines between
1108 * wp->w_redraw_top and wp->w_redr_bot.
1109 * - continue redrawing when syntax status is invalid.
1110 * 4. if scrolled up, update lines at the bottom.
1111 * This results in three areas that may need updating:
1112 * top: from first row to top_end (when scrolled down)
1113 * mid: from mid_start to mid_end (update inversion or changed text)
1114 * bot: from bot_start to last row (when scrolled up)
1115 */
1116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001117win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
1119 buf_T *buf = wp->w_buffer;
1120 int type;
1121 int top_end = 0; /* Below last row of the top area that needs
1122 updating. 0 when no top area updating. */
1123 int mid_start = 999;/* first row of the mid area that needs
1124 updating. 999 when no mid area updating. */
1125 int mid_end = 0; /* Below last row of the mid area that needs
1126 updating. 0 when no mid area updating. */
1127 int bot_start = 999;/* first row of the bot area that needs
1128 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 int scrolled_down = FALSE; /* TRUE when scrolled down when
1130 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001132 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 int top_to_mod = FALSE; /* redraw above mod_top */
1134#endif
1135
1136 int row; /* current window row to display */
1137 linenr_T lnum; /* current buffer lnum to display */
1138 int idx; /* current index in w_lines[] */
1139 int srow; /* starting row of the current line */
1140
1141 int eof = FALSE; /* if TRUE, we hit the end of the file */
1142 int didline = FALSE; /* if TRUE, we finished the last line */
1143 int i;
1144 long j;
1145 static int recursive = FALSE; /* being called recursively */
1146 int old_botline = wp->w_botline;
1147#ifdef FEAT_FOLDING
1148 long fold_count;
1149#endif
1150#ifdef FEAT_SYN_HL
1151 /* remember what happened to the previous line, to know if
1152 * check_visual_highlight() can be used */
1153#define DID_NONE 1 /* didn't update a line */
1154#define DID_LINE 2 /* updated a normal line */
1155#define DID_FOLD 3 /* updated a folded line */
1156 int did_update = DID_NONE;
1157 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1158#endif
1159 linenr_T mod_top = 0;
1160 linenr_T mod_bot = 0;
1161#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1162 int save_got_int;
1163#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001164#ifdef SYN_TIME_LIMIT
1165 proftime_T syntax_tm;
1166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
1168 type = wp->w_redr_type;
1169
1170 if (type == NOT_VALID)
1171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 wp->w_lines_valid = 0;
1174 }
1175
1176 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001177 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
1179 wp->w_redr_type = 0;
1180 return;
1181 }
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 /* Window is zero-width: Only need to draw the separator. */
1184 if (wp->w_width == 0)
1185 {
1186 /* draw the vertical separator right of this window */
1187 draw_vsep_win(wp, 0);
1188 wp->w_redr_type = 0;
1189 return;
1190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001192#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001193 // If this window contains a terminal, redraw works completely differently.
1194 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001195 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001196 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001197# ifdef FEAT_MENU
1198 /* Draw the window toolbar, if there is one. */
1199 if (winbar_height(wp) > 0)
1200 redraw_win_toolbar(wp);
1201# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001202 wp->w_redr_type = 0;
1203 return;
1204 }
1205#endif
1206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001208 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209#endif
1210
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001211#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001212 /* Force redraw when width of 'number' or 'relativenumber' column
1213 * changes. */
1214 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001215 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001216 {
1217 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001218 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001219 }
1220 else
1221#endif
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1224 {
1225 /*
1226 * When there are both inserted/deleted lines and specific lines to be
1227 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1228 * everything (only happens when redrawing is off for while).
1229 */
1230 type = NOT_VALID;
1231 }
1232 else
1233 {
1234 /*
1235 * Set mod_top to the first line that needs displaying because of
1236 * changes. Set mod_bot to the first line after the changes.
1237 */
1238 mod_top = wp->w_redraw_top;
1239 if (wp->w_redraw_bot != 0)
1240 mod_bot = wp->w_redraw_bot + 1;
1241 else
1242 mod_bot = 0;
1243 wp->w_redraw_top = 0; /* reset for next time */
1244 wp->w_redraw_bot = 0;
1245 if (buf->b_mod_set)
1246 {
1247 if (mod_top == 0 || mod_top > buf->b_mod_top)
1248 {
1249 mod_top = buf->b_mod_top;
1250#ifdef FEAT_SYN_HL
1251 /* Need to redraw lines above the change that may be included
1252 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001253 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001255 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (mod_top < 1)
1257 mod_top = 1;
1258 }
1259#endif
1260 }
1261 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1262 mod_bot = buf->b_mod_bot;
1263
1264#ifdef FEAT_SEARCH_EXTRA
1265 /* When 'hlsearch' is on and using a multi-line search pattern, a
1266 * change in one line may make the Search highlighting in a
1267 * previous line invalid. Simple solution: redraw all visible
1268 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001269 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001271 if (search_hl.rm.regprog != NULL
1272 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001274 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001275 {
1276 cur = wp->w_match_head;
1277 while (cur != NULL)
1278 {
1279 if (cur->match.regprog != NULL
1280 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001281 {
1282 top_to_mod = TRUE;
1283 break;
1284 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001285 cur = cur->next;
1286 }
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#endif
1289 }
1290#ifdef FEAT_FOLDING
1291 if (mod_top != 0 && hasAnyFolding(wp))
1292 {
1293 linenr_T lnumt, lnumb;
1294
1295 /*
1296 * A change in a line can cause lines above it to become folded or
1297 * unfolded. Find the top most buffer line that may be affected.
1298 * If the line was previously folded and displayed, get the first
1299 * line of that fold. If the line is folded now, get the first
1300 * folded line. Use the minimum of these two.
1301 */
1302
1303 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1304 * the line below it. If there is no valid entry, use w_topline.
1305 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1306 * to this line. If there is no valid entry, use MAXLNUM. */
1307 lnumt = wp->w_topline;
1308 lnumb = MAXLNUM;
1309 for (i = 0; i < wp->w_lines_valid; ++i)
1310 if (wp->w_lines[i].wl_valid)
1311 {
1312 if (wp->w_lines[i].wl_lastlnum < mod_top)
1313 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1314 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1315 {
1316 lnumb = wp->w_lines[i].wl_lnum;
1317 /* When there is a fold column it might need updating
1318 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001319 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 ++lnumb;
1321 }
1322 }
1323
1324 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1325 if (mod_top > lnumt)
1326 mod_top = lnumt;
1327
1328 /* Now do the same for the bottom line (one above mod_bot). */
1329 --mod_bot;
1330 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1331 ++mod_bot;
1332 if (mod_bot < lnumb)
1333 mod_bot = lnumb;
1334 }
1335#endif
1336
1337 /* When a change starts above w_topline and the end is below
1338 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001339 * If the end of the change is above w_topline: do like no change was
1340 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (mod_top != 0 && mod_top < wp->w_topline)
1342 {
1343 if (mod_bot > wp->w_topline)
1344 mod_top = wp->w_topline;
1345#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001346 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 top_end = 1;
1348#endif
1349 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001350
1351 /* When line numbers are displayed need to redraw all lines below
1352 * inserted/deleted lines. */
1353 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1354 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356
1357 /*
1358 * When only displaying the lines at the top, set top_end. Used when
1359 * window has scrolled down for msg_scrolled.
1360 */
1361 if (type == REDRAW_TOP)
1362 {
1363 j = 0;
1364 for (i = 0; i < wp->w_lines_valid; ++i)
1365 {
1366 j += wp->w_lines[i].wl_size;
1367 if (j >= wp->w_upd_rows)
1368 {
1369 top_end = j;
1370 break;
1371 }
1372 }
1373 if (top_end == 0)
1374 /* not found (cannot happen?): redraw everything */
1375 type = NOT_VALID;
1376 else
1377 /* top area defined, the rest is VALID */
1378 type = VALID;
1379 }
1380
Bram Moolenaar367329b2007-08-30 11:53:22 +00001381 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001382 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1383 * non-zero and thus not FALSE) will indicate that screenclear() was not
1384 * called. */
1385 if (screen_cleared)
1386 screen_cleared = MAYBE;
1387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 /*
1389 * If there are no changes on the screen that require a complete redraw,
1390 * handle three cases:
1391 * 1: we are off the top of the screen by a few lines: scroll down
1392 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1393 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1394 * w_lines[] that needs updating.
1395 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001396 if ((type == VALID || type == SOME_VALID
1397 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#ifdef FEAT_DIFF
1399 && !wp->w_botfill && !wp->w_old_botfill
1400#endif
1401 )
1402 {
1403 if (mod_top != 0 && wp->w_topline == mod_top)
1404 {
1405 /*
1406 * w_topline is the first changed line, the scrolling will be done
1407 * further down.
1408 */
1409 }
1410 else if (wp->w_lines[0].wl_valid
1411 && (wp->w_topline < wp->w_lines[0].wl_lnum
1412#ifdef FEAT_DIFF
1413 || (wp->w_topline == wp->w_lines[0].wl_lnum
1414 && wp->w_topfill > wp->w_old_topfill)
1415#endif
1416 ))
1417 {
1418 /*
1419 * New topline is above old topline: May scroll down.
1420 */
1421#ifdef FEAT_FOLDING
1422 if (hasAnyFolding(wp))
1423 {
1424 linenr_T ln;
1425
1426 /* count the number of lines we are off, counting a sequence
1427 * of folded lines as one */
1428 j = 0;
1429 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1430 {
1431 ++j;
1432 if (j >= wp->w_height - 2)
1433 break;
1434 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1435 }
1436 }
1437 else
1438#endif
1439 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1440 if (j < wp->w_height - 2) /* not too far off */
1441 {
1442 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1443#ifdef FEAT_DIFF
1444 /* insert extra lines for previously invisible filler lines */
1445 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1446 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1447 - wp->w_old_topfill;
1448#endif
1449 if (i < wp->w_height - 2) /* less than a screen off */
1450 {
1451 /*
1452 * Try to insert the correct number of lines.
1453 * If not the last window, delete the lines at the bottom.
1454 * win_ins_lines may fail when the terminal can't do it.
1455 */
1456 if (i > 0)
1457 check_for_delay(FALSE);
1458 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1459 {
1460 if (wp->w_lines_valid != 0)
1461 {
1462 /* Need to update rows that are new, stop at the
1463 * first one that scrolled down. */
1464 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
1467 /* Move the entries that were scrolled, disable
1468 * the entries for the lines to be redrawn. */
1469 if ((wp->w_lines_valid += j) > wp->w_height)
1470 wp->w_lines_valid = wp->w_height;
1471 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1472 wp->w_lines[idx] = wp->w_lines[idx - j];
1473 while (idx >= 0)
1474 wp->w_lines[idx--].wl_valid = FALSE;
1475 }
1476 }
1477 else
1478 mid_start = 0; /* redraw all lines */
1479 }
1480 else
1481 mid_start = 0; /* redraw all lines */
1482 }
1483 else
1484 mid_start = 0; /* redraw all lines */
1485 }
1486 else
1487 {
1488 /*
1489 * New topline is at or below old topline: May scroll up.
1490 * When topline didn't change, find first entry in w_lines[] that
1491 * needs updating.
1492 */
1493
1494 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1495 j = -1;
1496 row = 0;
1497 for (i = 0; i < wp->w_lines_valid; i++)
1498 {
1499 if (wp->w_lines[i].wl_valid
1500 && wp->w_lines[i].wl_lnum == wp->w_topline)
1501 {
1502 j = i;
1503 break;
1504 }
1505 row += wp->w_lines[i].wl_size;
1506 }
1507 if (j == -1)
1508 {
1509 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1510 * lines */
1511 mid_start = 0;
1512 }
1513 else
1514 {
1515 /*
1516 * Try to delete the correct number of lines.
1517 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1518 */
1519#ifdef FEAT_DIFF
1520 /* If the topline didn't change, delete old filler lines,
1521 * otherwise delete filler lines of the new topline... */
1522 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1523 row += wp->w_old_topfill;
1524 else
1525 row += diff_check_fill(wp, wp->w_topline);
1526 /* ... but don't delete new filler lines. */
1527 row -= wp->w_topfill;
1528#endif
1529 if (row > 0)
1530 {
1531 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001532 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1533 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 bot_start = wp->w_height - row;
1535 else
1536 mid_start = 0; /* redraw all lines */
1537 }
1538 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1539 {
1540 /*
1541 * Skip the lines (below the deleted lines) that are still
1542 * valid and don't need redrawing. Copy their info
1543 * upwards, to compensate for the deleted lines. Set
1544 * bot_start to the first row that needs redrawing.
1545 */
1546 bot_start = 0;
1547 idx = 0;
1548 for (;;)
1549 {
1550 wp->w_lines[idx] = wp->w_lines[j];
1551 /* stop at line that didn't fit, unless it is still
1552 * valid (no lines deleted) */
1553 if (row > 0 && bot_start + row
1554 + (int)wp->w_lines[j].wl_size > wp->w_height)
1555 {
1556 wp->w_lines_valid = idx + 1;
1557 break;
1558 }
1559 bot_start += wp->w_lines[idx++].wl_size;
1560
1561 /* stop at the last valid entry in w_lines[].wl_size */
1562 if (++j >= wp->w_lines_valid)
1563 {
1564 wp->w_lines_valid = idx;
1565 break;
1566 }
1567 }
1568#ifdef FEAT_DIFF
1569 /* Correct the first entry for filler lines at the top
1570 * when it won't get updated below. */
1571 if (wp->w_p_diff && bot_start > 0)
1572 wp->w_lines[0].wl_size =
1573 plines_win_nofill(wp, wp->w_topline, TRUE)
1574 + wp->w_topfill;
1575#endif
1576 }
1577 }
1578 }
1579
1580 /* When starting redraw in the first line, redraw all lines. When
1581 * there is only one window it's probably faster to clear the screen
1582 * first. */
1583 if (mid_start == 0)
1584 {
1585 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001586 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001587 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001588 /* Clear the screen when it was not done by win_del_lines() or
1589 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1590 * then. */
1591 if (screen_cleared != TRUE)
1592 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001593 /* The screen was cleared, redraw the tab pages line. */
1594 if (redraw_tabline)
1595 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001598
1599 /* When win_del_lines() or win_ins_lines() caused the screen to be
1600 * cleared (only happens for the first window) or when screenclear()
1601 * was called directly above, "must_redraw" will have been set to
1602 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1603 if (screen_cleared == TRUE)
1604 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 else
1607 {
1608 /* Not VALID or INVERTED: redraw all lines. */
1609 mid_start = 0;
1610 mid_end = wp->w_height;
1611 }
1612
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001613 if (type == SOME_VALID)
1614 {
1615 /* SOME_VALID: redraw all lines. */
1616 mid_start = 0;
1617 mid_end = wp->w_height;
1618 type = NOT_VALID;
1619 }
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 /* check if we are updating or removing the inverted part */
1622 if ((VIsual_active && buf == curwin->w_buffer)
1623 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1624 {
1625 linenr_T from, to;
1626
1627 if (VIsual_active)
1628 {
1629 if (VIsual_active
1630 && (VIsual_mode != wp->w_old_visual_mode
1631 || type == INVERTED_ALL))
1632 {
1633 /*
1634 * If the type of Visual selection changed, redraw the whole
1635 * selection. Also when the ownership of the X selection is
1636 * gained or lost.
1637 */
1638 if (curwin->w_cursor.lnum < VIsual.lnum)
1639 {
1640 from = curwin->w_cursor.lnum;
1641 to = VIsual.lnum;
1642 }
1643 else
1644 {
1645 from = VIsual.lnum;
1646 to = curwin->w_cursor.lnum;
1647 }
1648 /* redraw more when the cursor moved as well */
1649 if (wp->w_old_cursor_lnum < from)
1650 from = wp->w_old_cursor_lnum;
1651 if (wp->w_old_cursor_lnum > to)
1652 to = wp->w_old_cursor_lnum;
1653 if (wp->w_old_visual_lnum < from)
1654 from = wp->w_old_visual_lnum;
1655 if (wp->w_old_visual_lnum > to)
1656 to = wp->w_old_visual_lnum;
1657 }
1658 else
1659 {
1660 /*
1661 * Find the line numbers that need to be updated: The lines
1662 * between the old cursor position and the current cursor
1663 * position. Also check if the Visual position changed.
1664 */
1665 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1666 {
1667 from = curwin->w_cursor.lnum;
1668 to = wp->w_old_cursor_lnum;
1669 }
1670 else
1671 {
1672 from = wp->w_old_cursor_lnum;
1673 to = curwin->w_cursor.lnum;
1674 if (from == 0) /* Visual mode just started */
1675 from = to;
1676 }
1677
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001678 if (VIsual.lnum != wp->w_old_visual_lnum
1679 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 {
1681 if (wp->w_old_visual_lnum < from
1682 && wp->w_old_visual_lnum != 0)
1683 from = wp->w_old_visual_lnum;
1684 if (wp->w_old_visual_lnum > to)
1685 to = wp->w_old_visual_lnum;
1686 if (VIsual.lnum < from)
1687 from = VIsual.lnum;
1688 if (VIsual.lnum > to)
1689 to = VIsual.lnum;
1690 }
1691 }
1692
1693 /*
1694 * If in block mode and changed column or curwin->w_curswant:
1695 * update all lines.
1696 * First compute the actual start and end column.
1697 */
1698 if (VIsual_mode == Ctrl_V)
1699 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001700 colnr_T fromc, toc;
1701#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1702 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaar404406a2014-10-09 13:24:43 +02001704 if (curwin->w_p_lbr)
1705 ve_flags = VE_ALL;
1706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001708#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1709 ve_flags = save_ve_flags;
1710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 ++toc;
1712 if (curwin->w_curswant == MAXCOL)
1713 toc = MAXCOL;
1714
1715 if (fromc != wp->w_old_cursor_fcol
1716 || toc != wp->w_old_cursor_lcol)
1717 {
1718 if (from > VIsual.lnum)
1719 from = VIsual.lnum;
1720 if (to < VIsual.lnum)
1721 to = VIsual.lnum;
1722 }
1723 wp->w_old_cursor_fcol = fromc;
1724 wp->w_old_cursor_lcol = toc;
1725 }
1726 }
1727 else
1728 {
1729 /* Use the line numbers of the old Visual area. */
1730 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1731 {
1732 from = wp->w_old_cursor_lnum;
1733 to = wp->w_old_visual_lnum;
1734 }
1735 else
1736 {
1737 from = wp->w_old_visual_lnum;
1738 to = wp->w_old_cursor_lnum;
1739 }
1740 }
1741
1742 /*
1743 * There is no need to update lines above the top of the window.
1744 */
1745 if (from < wp->w_topline)
1746 from = wp->w_topline;
1747
1748 /*
1749 * If we know the value of w_botline, use it to restrict the update to
1750 * the lines that are visible in the window.
1751 */
1752 if (wp->w_valid & VALID_BOTLINE)
1753 {
1754 if (from >= wp->w_botline)
1755 from = wp->w_botline - 1;
1756 if (to >= wp->w_botline)
1757 to = wp->w_botline - 1;
1758 }
1759
1760 /*
1761 * Find the minimal part to be updated.
1762 * Watch out for scrolling that made entries in w_lines[] invalid.
1763 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1764 * top_end; need to redraw from top_end to the "to" line.
1765 * A middle mouse click with a Visual selection may change the text
1766 * above the Visual area and reset wl_valid, do count these for
1767 * mid_end (in srow).
1768 */
1769 if (mid_start > 0)
1770 {
1771 lnum = wp->w_topline;
1772 idx = 0;
1773 srow = 0;
1774 if (scrolled_down)
1775 mid_start = top_end;
1776 else
1777 mid_start = 0;
1778 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1779 {
1780 if (wp->w_lines[idx].wl_valid)
1781 mid_start += wp->w_lines[idx].wl_size;
1782 else if (!scrolled_down)
1783 srow += wp->w_lines[idx].wl_size;
1784 ++idx;
1785# ifdef FEAT_FOLDING
1786 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1787 lnum = wp->w_lines[idx].wl_lnum;
1788 else
1789# endif
1790 ++lnum;
1791 }
1792 srow += mid_start;
1793 mid_end = wp->w_height;
1794 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1795 {
1796 if (wp->w_lines[idx].wl_valid
1797 && wp->w_lines[idx].wl_lnum >= to + 1)
1798 {
1799 /* Only update until first row of this line */
1800 mid_end = srow;
1801 break;
1802 }
1803 srow += wp->w_lines[idx].wl_size;
1804 }
1805 }
1806 }
1807
1808 if (VIsual_active && buf == curwin->w_buffer)
1809 {
1810 wp->w_old_visual_mode = VIsual_mode;
1811 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1812 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001813 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 wp->w_old_curswant = curwin->w_curswant;
1815 }
1816 else
1817 {
1818 wp->w_old_visual_mode = 0;
1819 wp->w_old_cursor_lnum = 0;
1820 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001821 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1825 /* reset got_int, otherwise regexp won't work */
1826 save_got_int = got_int;
1827 got_int = 0;
1828#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001829#ifdef SYN_TIME_LIMIT
1830 /* Set the time limit to 'redrawtime'. */
1831 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001832 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#ifdef FEAT_FOLDING
1835 win_foldinfo.fi_level = 0;
1836#endif
1837
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001838#ifdef FEAT_MENU
1839 /*
1840 * Draw the window toolbar, if there is one.
1841 * TODO: only when needed.
1842 */
1843 if (winbar_height(wp) > 0)
1844 redraw_win_toolbar(wp);
1845#endif
1846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 /*
1848 * Update all the window rows.
1849 */
1850 idx = 0; /* first entry in w_lines[].wl_size */
1851 row = 0;
1852 srow = 0;
1853 lnum = wp->w_topline; /* first line shown in window */
1854 for (;;)
1855 {
1856 /* stop updating when reached the end of the window (check for _past_
1857 * the end of the window is at the end of the loop) */
1858 if (row == wp->w_height)
1859 {
1860 didline = TRUE;
1861 break;
1862 }
1863
1864 /* stop updating when hit the end of the file */
1865 if (lnum > buf->b_ml.ml_line_count)
1866 {
1867 eof = TRUE;
1868 break;
1869 }
1870
1871 /* Remember the starting row of the line that is going to be dealt
1872 * with. It is used further down when the line doesn't fit. */
1873 srow = row;
1874
1875 /*
1876 * Update a line when it is in an area that needs updating, when it
1877 * has changes or w_lines[idx] is invalid.
1878 * bot_start may be halfway a wrapped line after using
1879 * win_del_lines(), check if the current line includes it.
1880 * When syntax folding is being used, the saved syntax states will
1881 * already have been updated, we can't see where the syntax state is
1882 * the same again, just update until the end of the window.
1883 */
1884 if (row < top_end
1885 || (row >= mid_start && row < mid_end)
1886#ifdef FEAT_SEARCH_EXTRA
1887 || top_to_mod
1888#endif
1889 || idx >= wp->w_lines_valid
1890 || (row + wp->w_lines[idx].wl_size > bot_start)
1891 || (mod_top != 0
1892 && (lnum == mod_top
1893 || (lnum >= mod_top
1894 && (lnum < mod_bot
1895#ifdef FEAT_SYN_HL
1896 || did_update == DID_FOLD
1897 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001898 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 && (
1900# ifdef FEAT_FOLDING
1901 (foldmethodIsSyntax(wp)
1902 && hasAnyFolding(wp)) ||
1903# endif
1904 syntax_check_changed(lnum)))
1905#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001906#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001907 /* match in fixed position might need redraw
1908 * if lines were inserted or deleted */
1909 || (wp->w_match_head != NULL
1910 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 )))))
1913 {
1914#ifdef FEAT_SEARCH_EXTRA
1915 if (lnum == mod_top)
1916 top_to_mod = FALSE;
1917#endif
1918
1919 /*
1920 * When at start of changed lines: May scroll following lines
1921 * up or down to minimize redrawing.
1922 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001923 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 */
1925 if (lnum == mod_top
1926 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001927 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
1929 int old_rows = 0;
1930 int new_rows = 0;
1931 int xtra_rows;
1932 linenr_T l;
1933
1934 /* Count the old number of window rows, using w_lines[], which
1935 * should still contain the sizes for the lines as they are
1936 * currently displayed. */
1937 for (i = idx; i < wp->w_lines_valid; ++i)
1938 {
1939 /* Only valid lines have a meaningful wl_lnum. Invalid
1940 * lines are part of the changed area. */
1941 if (wp->w_lines[i].wl_valid
1942 && wp->w_lines[i].wl_lnum == mod_bot)
1943 break;
1944 old_rows += wp->w_lines[i].wl_size;
1945#ifdef FEAT_FOLDING
1946 if (wp->w_lines[i].wl_valid
1947 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1948 {
1949 /* Must have found the last valid entry above mod_bot.
1950 * Add following invalid entries. */
1951 ++i;
1952 while (i < wp->w_lines_valid
1953 && !wp->w_lines[i].wl_valid)
1954 old_rows += wp->w_lines[i++].wl_size;
1955 break;
1956 }
1957#endif
1958 }
1959
1960 if (i >= wp->w_lines_valid)
1961 {
1962 /* We can't find a valid line below the changed lines,
1963 * need to redraw until the end of the window.
1964 * Inserting/deleting lines has no use. */
1965 bot_start = 0;
1966 }
1967 else
1968 {
1969 /* Able to count old number of rows: Count new window
1970 * rows, and may insert/delete lines */
1971 j = idx;
1972 for (l = lnum; l < mod_bot; ++l)
1973 {
1974#ifdef FEAT_FOLDING
1975 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1976 ++new_rows;
1977 else
1978#endif
1979#ifdef FEAT_DIFF
1980 if (l == wp->w_topline)
1981 new_rows += plines_win_nofill(wp, l, TRUE)
1982 + wp->w_topfill;
1983 else
1984#endif
1985 new_rows += plines_win(wp, l, TRUE);
1986 ++j;
1987 if (new_rows > wp->w_height - row - 2)
1988 {
1989 /* it's getting too much, must redraw the rest */
1990 new_rows = 9999;
1991 break;
1992 }
1993 }
1994 xtra_rows = new_rows - old_rows;
1995 if (xtra_rows < 0)
1996 {
1997 /* May scroll text up. If there is not enough
1998 * remaining text or scrolling fails, must redraw the
1999 * rest. If scrolling works, must redraw the text
2000 * below the scrolled text. */
2001 if (row - xtra_rows >= wp->w_height - 2)
2002 mod_bot = MAXLNUM;
2003 else
2004 {
2005 check_for_delay(FALSE);
2006 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002007 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 mod_bot = MAXLNUM;
2009 else
2010 bot_start = wp->w_height + xtra_rows;
2011 }
2012 }
2013 else if (xtra_rows > 0)
2014 {
2015 /* May scroll text down. If there is not enough
2016 * remaining text of scrolling fails, must redraw the
2017 * rest. */
2018 if (row + xtra_rows >= wp->w_height - 2)
2019 mod_bot = MAXLNUM;
2020 else
2021 {
2022 check_for_delay(FALSE);
2023 if (win_ins_lines(wp, row + old_rows,
2024 xtra_rows, FALSE, FALSE) == FAIL)
2025 mod_bot = MAXLNUM;
2026 else if (top_end > row + old_rows)
2027 /* Scrolled the part at the top that requires
2028 * updating down. */
2029 top_end += xtra_rows;
2030 }
2031 }
2032
2033 /* When not updating the rest, may need to move w_lines[]
2034 * entries. */
2035 if (mod_bot != MAXLNUM && i != j)
2036 {
2037 if (j < i)
2038 {
2039 int x = row + new_rows;
2040
2041 /* move entries in w_lines[] upwards */
2042 for (;;)
2043 {
2044 /* stop at last valid entry in w_lines[] */
2045 if (i >= wp->w_lines_valid)
2046 {
2047 wp->w_lines_valid = j;
2048 break;
2049 }
2050 wp->w_lines[j] = wp->w_lines[i];
2051 /* stop at a line that won't fit */
2052 if (x + (int)wp->w_lines[j].wl_size
2053 > wp->w_height)
2054 {
2055 wp->w_lines_valid = j + 1;
2056 break;
2057 }
2058 x += wp->w_lines[j++].wl_size;
2059 ++i;
2060 }
2061 if (bot_start > x)
2062 bot_start = x;
2063 }
2064 else /* j > i */
2065 {
2066 /* move entries in w_lines[] downwards */
2067 j -= i;
2068 wp->w_lines_valid += j;
2069 if (wp->w_lines_valid > wp->w_height)
2070 wp->w_lines_valid = wp->w_height;
2071 for (i = wp->w_lines_valid; i - j >= idx; --i)
2072 wp->w_lines[i] = wp->w_lines[i - j];
2073
2074 /* The w_lines[] entries for inserted lines are
2075 * now invalid, but wl_size may be used above.
2076 * Reset to zero. */
2077 while (i >= idx)
2078 {
2079 wp->w_lines[i].wl_size = 0;
2080 wp->w_lines[i--].wl_valid = FALSE;
2081 }
2082 }
2083 }
2084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
2086
2087#ifdef FEAT_FOLDING
2088 /*
2089 * When lines are folded, display one line for all of them.
2090 * Otherwise, display normally (can be several display lines when
2091 * 'wrap' is on).
2092 */
2093 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2094 if (fold_count != 0)
2095 {
2096 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2097 ++row;
2098 --fold_count;
2099 wp->w_lines[idx].wl_folded = TRUE;
2100 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2101# ifdef FEAT_SYN_HL
2102 did_update = DID_FOLD;
2103# endif
2104 }
2105 else
2106#endif
2107 if (idx < wp->w_lines_valid
2108 && wp->w_lines[idx].wl_valid
2109 && wp->w_lines[idx].wl_lnum == lnum
2110 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002111 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 && srow + wp->w_lines[idx].wl_size > wp->w_height
2113#ifdef FEAT_DIFF
2114 && diff_check_fill(wp, lnum) == 0
2115#endif
2116 )
2117 {
2118 /* This line is not going to fit. Don't draw anything here,
2119 * will draw "@ " lines below. */
2120 row = wp->w_height + 1;
2121 }
2122 else
2123 {
2124#ifdef FEAT_SEARCH_EXTRA
2125 prepare_search_hl(wp, lnum);
2126#endif
2127#ifdef FEAT_SYN_HL
2128 /* Let the syntax stuff know we skipped a few lines. */
2129 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002130 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 syntax_end_parsing(syntax_last_parsed + 1);
2132#endif
2133
2134 /*
2135 * Display one line.
2136 */
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002137 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
2139#ifdef FEAT_FOLDING
2140 wp->w_lines[idx].wl_folded = FALSE;
2141 wp->w_lines[idx].wl_lastlnum = lnum;
2142#endif
2143#ifdef FEAT_SYN_HL
2144 did_update = DID_LINE;
2145 syntax_last_parsed = lnum;
2146#endif
2147 }
2148
2149 wp->w_lines[idx].wl_lnum = lnum;
2150 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002151
2152 /* Past end of the window or end of the screen. Note that after
2153 * resizing wp->w_height may be end up too big. That's a problem
2154 * elsewhere, but prevent a crash here. */
2155 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
2157 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002158 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2160 ++idx;
2161 break;
2162 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002163 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 wp->w_lines[idx].wl_size = row - srow;
2165 ++idx;
2166#ifdef FEAT_FOLDING
2167 lnum += fold_count + 1;
2168#else
2169 ++lnum;
2170#endif
2171 }
2172 else
2173 {
2174 /* This line does not need updating, advance to the next one */
2175 row += wp->w_lines[idx++].wl_size;
2176 if (row > wp->w_height) /* past end of screen */
2177 break;
2178#ifdef FEAT_FOLDING
2179 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2180#else
2181 ++lnum;
2182#endif
2183#ifdef FEAT_SYN_HL
2184 did_update = DID_NONE;
2185#endif
2186 }
2187
2188 if (lnum > buf->b_ml.ml_line_count)
2189 {
2190 eof = TRUE;
2191 break;
2192 }
2193 }
2194 /*
2195 * End of loop over all window lines.
2196 */
2197
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002198#ifdef FEAT_VTP
2199 /* Rewrite the character at the end of the screen line. */
2200 if (use_vtp())
2201 {
2202 int i;
2203
2204 for (i = 0; i < Rows; ++i)
2205# ifdef FEAT_MBYTE
2206 if (enc_utf8)
2207 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2208 LineOffset[i] + screen_Columns) > 1)
2209 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2210 else
2211 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2212 else
2213# endif
2214 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2215 }
2216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
2218 if (idx > wp->w_lines_valid)
2219 wp->w_lines_valid = idx;
2220
2221#ifdef FEAT_SYN_HL
2222 /*
2223 * Let the syntax stuff know we stop parsing here.
2224 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002225 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 syntax_end_parsing(syntax_last_parsed + 1);
2227#endif
2228
2229 /*
2230 * If we didn't hit the end of the file, and we didn't finish the last
2231 * line we were working on, then the line didn't fit.
2232 */
2233 wp->w_empty_rows = 0;
2234#ifdef FEAT_DIFF
2235 wp->w_filler_rows = 0;
2236#endif
2237 if (!eof && !didline)
2238 {
2239 if (lnum == wp->w_topline)
2240 {
2241 /*
2242 * Single line that does not fit!
2243 * Don't overwrite it, it can be edited.
2244 */
2245 wp->w_botline = lnum + 1;
2246 }
2247#ifdef FEAT_DIFF
2248 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2249 {
2250 /* Window ends in filler lines. */
2251 wp->w_botline = lnum;
2252 wp->w_filler_rows = wp->w_height - srow;
2253 }
2254#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002255 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2256 {
2257 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2258
2259 /*
2260 * Last line isn't finished: Display "@@@" in the last screen line.
2261 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002262 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002263 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002264 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002265 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002266 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002267 set_empty_rows(wp, srow);
2268 wp->w_botline = lnum;
2269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2271 {
2272 /*
2273 * Last line isn't finished: Display "@@@" at the end.
2274 */
2275 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2276 W_WINROW(wp) + wp->w_height,
2277 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002278 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 set_empty_rows(wp, srow);
2280 wp->w_botline = lnum;
2281 }
2282 else
2283 {
2284 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2285 wp->w_botline = lnum;
2286 }
2287 }
2288 else
2289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (eof) /* we hit the end of the file */
2292 {
2293 wp->w_botline = buf->b_ml.ml_line_count + 1;
2294#ifdef FEAT_DIFF
2295 j = diff_check_fill(wp, wp->w_botline);
2296 if (j > 0 && !wp->w_botfill)
2297 {
2298 /*
2299 * Display filler lines at the end of the file
2300 */
2301 if (char2cells(fill_diff) > 1)
2302 i = '-';
2303 else
2304 i = fill_diff;
2305 if (row + j > wp->w_height)
2306 j = wp->w_height - row;
2307 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2308 row += j;
2309 }
2310#endif
2311 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002312 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 wp->w_botline = lnum;
2314
2315 /* make sure the rest of the screen is blank */
2316 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002317 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002320#ifdef SYN_TIME_LIMIT
2321 syn_set_timeout(NULL);
2322#endif
2323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 /* Reset the type of redrawing required, the window has been updated. */
2325 wp->w_redr_type = 0;
2326#ifdef FEAT_DIFF
2327 wp->w_old_topfill = wp->w_topfill;
2328 wp->w_old_botfill = wp->w_botfill;
2329#endif
2330
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002331 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
2333 /*
2334 * There is a trick with w_botline. If we invalidate it on each
2335 * change that might modify it, this will cause a lot of expensive
2336 * calls to plines() in update_topline() each time. Therefore the
2337 * value of w_botline is often approximated, and this value is used to
2338 * compute the value of w_topline. If the value of w_botline was
2339 * wrong, check that the value of w_topline is correct (cursor is on
2340 * the visible part of the text). If it's not, we need to redraw
2341 * again. Mostly this just means scrolling up a few lines, so it
2342 * doesn't look too bad. Only do this for the current window (where
2343 * changes are relevant).
2344 */
2345 wp->w_valid |= VALID_BOTLINE;
2346 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2347 {
2348 recursive = TRUE;
2349 curwin->w_valid &= ~VALID_TOPLINE;
2350 update_topline(); /* may invalidate w_botline again */
2351 if (must_redraw != 0)
2352 {
2353 /* Don't update for changes in buffer again. */
2354 i = curbuf->b_mod_set;
2355 curbuf->b_mod_set = FALSE;
2356 win_update(curwin);
2357 must_redraw = 0;
2358 curbuf->b_mod_set = i;
2359 }
2360 recursive = FALSE;
2361 }
2362 }
2363
2364#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2365 /* restore got_int, unless CTRL-C was hit while redrawing */
2366 if (!got_int)
2367 got_int = save_got_int;
2368#endif
2369}
2370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371/*
2372 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2373 * as the filler character.
2374 */
2375 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002376win_draw_end(
2377 win_T *wp,
2378 int c1,
2379 int c2,
2380 int row,
2381 int endrow,
2382 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383{
2384#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2385 int n = 0;
2386# define FDC_OFF n
2387#else
2388# define FDC_OFF 0
2389#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002390#ifdef FEAT_FOLDING
2391 int fdc = compute_foldcolumn(wp, 0);
2392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
2394#ifdef FEAT_RIGHTLEFT
2395 if (wp->w_p_rl)
2396 {
2397 /* No check for cmdline window: should never be right-left. */
2398# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002399 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
2401 if (n > 0)
2402 {
2403 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002404 if (n > wp->w_width)
2405 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2407 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002408 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410# endif
2411# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002412 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {
2414 int nn = n + 2;
2415
2416 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002417 if (nn > wp->w_width)
2418 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2420 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002421 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 n = nn;
2423 }
2424# endif
2425 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002426 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002427 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2429 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002430 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 }
2432 else
2433#endif
2434 {
2435#ifdef FEAT_CMDWIN
2436 if (cmdwin_type != 0 && wp == curwin)
2437 {
2438 /* draw the cmdline character in the leftmost column */
2439 n = 1;
2440 if (n > wp->w_width)
2441 n = wp->w_width;
2442 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002443 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002444 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
2446#endif
2447#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002448 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002450 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
2452 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002453 if (nn > wp->w_width)
2454 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002456 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002457 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 n = nn;
2459 }
2460#endif
2461#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002462 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
2464 int nn = n + 2;
2465
2466 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002467 if (nn > wp->w_width)
2468 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002470 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002471 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 n = nn;
2473 }
2474#endif
2475 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002476 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002477 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 }
2479 set_empty_rows(wp, row);
2480}
2481
Bram Moolenaar1a384422010-07-14 19:53:30 +02002482#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002483static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002484
2485/*
2486 * Advance **color_cols and return TRUE when there are columns to draw.
2487 */
2488 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002489advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002490{
2491 while (**color_cols >= 0 && vcol > **color_cols)
2492 ++*color_cols;
2493 return (**color_cols >= 0);
2494}
2495#endif
2496
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002497#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2498/*
2499 * Copy "text" to ScreenLines using "attr".
2500 * Returns the next screen column.
2501 */
2502 static int
2503text_to_screenline(win_T *wp, char_u *text, int col)
2504{
2505 int off = (int)(current_ScreenLine - ScreenLines);
2506
2507#ifdef FEAT_MBYTE
2508 if (has_mbyte)
2509 {
2510 int cells;
2511 int u8c, u8cc[MAX_MCO];
2512 int i;
2513 int idx;
2514 int c_len;
2515 char_u *p;
2516# ifdef FEAT_ARABIC
2517 int prev_c = 0; /* previous Arabic character */
2518 int prev_c1 = 0; /* first composing char for prev_c */
2519# endif
2520
2521# ifdef FEAT_RIGHTLEFT
2522 if (wp->w_p_rl)
2523 idx = off;
2524 else
2525# endif
2526 idx = off + col;
2527
2528 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2529 for (p = text; *p != NUL; )
2530 {
2531 cells = (*mb_ptr2cells)(p);
2532 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002533 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002534# ifdef FEAT_RIGHTLEFT
2535 - (wp->w_p_rl ? col : 0)
2536# endif
2537 )
2538 break;
2539 ScreenLines[idx] = *p;
2540 if (enc_utf8)
2541 {
2542 u8c = utfc_ptr2char(p, u8cc);
2543 if (*p < 0x80 && u8cc[0] == 0)
2544 {
2545 ScreenLinesUC[idx] = 0;
2546#ifdef FEAT_ARABIC
2547 prev_c = u8c;
2548#endif
2549 }
2550 else
2551 {
2552#ifdef FEAT_ARABIC
2553 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2554 {
2555 /* Do Arabic shaping. */
2556 int pc, pc1, nc;
2557 int pcc[MAX_MCO];
2558 int firstbyte = *p;
2559
2560 /* The idea of what is the previous and next
2561 * character depends on 'rightleft'. */
2562 if (wp->w_p_rl)
2563 {
2564 pc = prev_c;
2565 pc1 = prev_c1;
2566 nc = utf_ptr2char(p + c_len);
2567 prev_c1 = u8cc[0];
2568 }
2569 else
2570 {
2571 pc = utfc_ptr2char(p + c_len, pcc);
2572 nc = prev_c;
2573 pc1 = pcc[0];
2574 }
2575 prev_c = u8c;
2576
2577 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2578 pc, pc1, nc);
2579 ScreenLines[idx] = firstbyte;
2580 }
2581 else
2582 prev_c = u8c;
2583#endif
2584 /* Non-BMP character: display as ? or fullwidth ?. */
2585#ifdef UNICODE16
2586 if (u8c >= 0x10000)
2587 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2588 else
2589#endif
2590 ScreenLinesUC[idx] = u8c;
2591 for (i = 0; i < Screen_mco; ++i)
2592 {
2593 ScreenLinesC[i][idx] = u8cc[i];
2594 if (u8cc[i] == 0)
2595 break;
2596 }
2597 }
2598 if (cells > 1)
2599 ScreenLines[idx + 1] = 0;
2600 }
2601 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2602 /* double-byte single width character */
2603 ScreenLines2[idx] = p[1];
2604 else if (cells > 1)
2605 /* double-width character */
2606 ScreenLines[idx + 1] = p[1];
2607 col += cells;
2608 idx += cells;
2609 p += c_len;
2610 }
2611 }
2612 else
2613#endif
2614 {
2615 int len = (int)STRLEN(text);
2616
Bram Moolenaar02631462017-09-22 15:20:32 +02002617 if (len > wp->w_width - col)
2618 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002619 if (len > 0)
2620 {
2621#ifdef FEAT_RIGHTLEFT
2622 if (wp->w_p_rl)
2623 STRNCPY(current_ScreenLine, text, len);
2624 else
2625#endif
2626 STRNCPY(current_ScreenLine + col, text, len);
2627 col += len;
2628 }
2629 }
2630 return col;
2631}
2632#endif
2633
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634#ifdef FEAT_FOLDING
2635/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002636 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2637 * space is available for window "wp", minus "col".
2638 */
2639 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002640compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002641{
2642 int fdc = wp->w_p_fdc;
2643 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002644 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002645
2646 if (fdc > wwidth - (col + wmw))
2647 fdc = wwidth - (col + wmw);
2648 return fdc;
2649}
2650
2651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 * Display one folded line.
2653 */
2654 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002655fold_line(
2656 win_T *wp,
2657 long fold_count,
2658 foldinfo_T *foldinfo,
2659 linenr_T lnum,
2660 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002662 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 pos_T *top, *bot;
2664 linenr_T lnume = lnum + fold_count - 1;
2665 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002666 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 int col;
2669 int txtcol;
2670 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002671 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672
2673 /* Build the fold line:
2674 * 1. Add the cmdwin_type for the command-line window
2675 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002676 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 * 4. Compose the text
2678 * 5. Add the text
2679 * 6. set highlighting for the Visual area an other text
2680 */
2681 col = 0;
2682
2683 /*
2684 * 1. Add the cmdwin_type for the command-line window
2685 * Ignores 'rightleft', this window is never right-left.
2686 */
2687#ifdef FEAT_CMDWIN
2688 if (cmdwin_type != 0 && wp == curwin)
2689 {
2690 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002691 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692#ifdef FEAT_MBYTE
2693 if (enc_utf8)
2694 ScreenLinesUC[off] = 0;
2695#endif
2696 ++col;
2697 }
2698#endif
2699
2700 /*
2701 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002702 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002704 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 if (fdc > 0)
2706 {
2707 fill_foldcolumn(buf, wp, TRUE, lnum);
2708#ifdef FEAT_RIGHTLEFT
2709 if (wp->w_p_rl)
2710 {
2711 int i;
2712
Bram Moolenaar02631462017-09-22 15:20:32 +02002713 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002714 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 /* reverse the fold column */
2716 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002717 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 }
2719 else
2720#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002721 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 col += fdc;
2723 }
2724
2725#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002726# define RL_MEMSET(p, v, l) \
2727 do { \
2728 if (wp->w_p_rl) \
2729 for (ri = 0; ri < l; ++ri) \
2730 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2731 else \
2732 for (ri = 0; ri < l; ++ri) \
2733 ScreenAttrs[off + (p) + ri] = v; \
2734 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002736# define RL_MEMSET(p, v, l) \
2737 do { \
2738 for (ri = 0; ri < l; ++ri) \
2739 ScreenAttrs[off + (p) + ri] = v; \
2740 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741#endif
2742
Bram Moolenaar64486672010-05-16 15:46:46 +02002743 /* Set all attributes of the 'number' or 'relativenumber' column and the
2744 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002745 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747#ifdef FEAT_SIGNS
2748 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002749 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002751 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 if (len > 0)
2753 {
2754 if (len > 2)
2755 len = 2;
2756# ifdef FEAT_RIGHTLEFT
2757 if (wp->w_p_rl)
2758 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002759 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002760 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 else
2762# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002763 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 col += len;
2765 }
2766 }
2767#endif
2768
2769 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002770 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002772 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002774 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 if (len > 0)
2776 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002777 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002778 long num;
2779 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002780
2781 if (len > w + 1)
2782 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002783
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002784 if (wp->w_p_nu && !wp->w_p_rnu)
2785 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002786 num = (long)lnum;
2787 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002788 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002789 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002790 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002791 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002792 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002793 /* 'number' + 'relativenumber': cursor line shows absolute
2794 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002795 num = lnum;
2796 fmt = "%-*ld ";
2797 }
2798 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002799
Bram Moolenaar700e7342013-01-30 12:31:36 +01002800 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801#ifdef FEAT_RIGHTLEFT
2802 if (wp->w_p_rl)
2803 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002804 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002805 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 else
2807#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002808 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 col += len;
2810 }
2811 }
2812
2813 /*
2814 * 4. Compose the folded-line string with 'foldtext', if set.
2815 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002816 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817
2818 txtcol = col; /* remember where text starts */
2819
2820 /*
2821 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2822 * Right-left text is put in columns 0 - number-col, normal text is put
2823 * in columns number-col - window-width.
2824 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002825 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826
2827 /* Fill the rest of the line with the fold filler */
2828#ifdef FEAT_RIGHTLEFT
2829 if (wp->w_p_rl)
2830 col -= txtcol;
2831#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002832 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833#ifdef FEAT_RIGHTLEFT
2834 - (wp->w_p_rl ? txtcol : 0)
2835#endif
2836 )
2837 {
2838#ifdef FEAT_MBYTE
2839 if (enc_utf8)
2840 {
2841 if (fill_fold >= 0x80)
2842 {
2843 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002844 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002845 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 }
2847 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002850 ScreenLines[off + col] = fill_fold;
2851 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002852 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002854 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002856 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
2858
2859 if (text != buf)
2860 vim_free(text);
2861
2862 /*
2863 * 6. set highlighting for the Visual area an other text.
2864 * If all folded lines are in the Visual area, highlight the line.
2865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2867 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002868 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
2870 /* Visual is after curwin->w_cursor */
2871 top = &curwin->w_cursor;
2872 bot = &VIsual;
2873 }
2874 else
2875 {
2876 /* Visual is before curwin->w_cursor */
2877 top = &VIsual;
2878 bot = &curwin->w_cursor;
2879 }
2880 if (lnum >= top->lnum
2881 && lnume <= bot->lnum
2882 && (VIsual_mode != 'v'
2883 || ((lnum > top->lnum
2884 || (lnum == top->lnum
2885 && top->col == 0))
2886 && (lnume < bot->lnum
2887 || (lnume == bot->lnum
2888 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002889 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 {
2891 if (VIsual_mode == Ctrl_V)
2892 {
2893 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002894 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002896 if (wp->w_old_cursor_lcol != MAXCOL
2897 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002898 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 len = wp->w_old_cursor_lcol;
2900 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002901 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002902 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002903 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 }
2905 }
2906 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002909 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 }
2912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002914#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002915 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2916 if (wp->w_p_cc_cols)
2917 {
2918 int i = 0;
2919 int j = wp->w_p_cc_cols[i];
2920 int old_txtcol = txtcol;
2921
2922 while (j > -1)
2923 {
2924 txtcol += j;
2925 if (wp->w_p_wrap)
2926 txtcol -= wp->w_skipcol;
2927 else
2928 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002929 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002930 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002931 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002932 txtcol = old_txtcol;
2933 j = wp->w_p_cc_cols[++i];
2934 }
2935 }
2936
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002937 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002938 if (wp->w_p_cuc)
2939 {
2940 txtcol += wp->w_virtcol;
2941 if (wp->w_p_wrap)
2942 txtcol -= wp->w_skipcol;
2943 else
2944 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002945 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002946 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002947 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002948 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
Bram Moolenaar02631462017-09-22 15:20:32 +02002951 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2952 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
2954 /*
2955 * Update w_cline_height and w_cline_folded if the cursor line was
2956 * updated (saves a call to plines() later).
2957 */
2958 if (wp == curwin
2959 && lnum <= curwin->w_cursor.lnum
2960 && lnume >= curwin->w_cursor.lnum)
2961 {
2962 curwin->w_cline_row = row;
2963 curwin->w_cline_height = 1;
2964 curwin->w_cline_folded = TRUE;
2965 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2966 }
2967}
2968
2969/*
2970 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2971 */
2972 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002973copy_text_attr(
2974 int off,
2975 char_u *buf,
2976 int len,
2977 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002979 int i;
2980
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 mch_memmove(ScreenLines + off, buf, (size_t)len);
2982# ifdef FEAT_MBYTE
2983 if (enc_utf8)
2984 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2985# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002986 for (i = 0; i < len; ++i)
2987 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988}
2989
2990/*
2991 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002992 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 */
2994 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002995fill_foldcolumn(
2996 char_u *p,
2997 win_T *wp,
2998 int closed, /* TRUE of FALSE */
2999 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 int i = 0;
3002 int level;
3003 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003004 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003005 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
3007 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003008 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009
3010 level = win_foldinfo.fi_level;
3011 if (level > 0)
3012 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003013 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003014 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003015
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 /* If the column is too narrow, we start at the lowest level that
3017 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003018 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 if (first_level < 1)
3020 first_level = 1;
3021
Bram Moolenaar1c934292015-01-27 16:39:29 +01003022 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 {
3024 if (win_foldinfo.fi_lnum == lnum
3025 && first_level + i >= win_foldinfo.fi_low_level)
3026 p[i] = '-';
3027 else if (first_level == 1)
3028 p[i] = '|';
3029 else if (first_level + i <= 9)
3030 p[i] = '0' + first_level + i;
3031 else
3032 p[i] = '>';
3033 if (first_level + i == level)
3034 break;
3035 }
3036 }
3037 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003038 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039}
3040#endif /* FEAT_FOLDING */
3041
3042/*
3043 * Display line "lnum" of window 'wp' on the screen.
3044 * Start at row "startrow", stop when "endrow" is reached.
3045 * wp->w_virtcol needs to be valid.
3046 *
3047 * Return the number of last row the line occupies.
3048 */
3049 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003050win_line(
3051 win_T *wp,
3052 linenr_T lnum,
3053 int startrow,
3054 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003055 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003057 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3059 int c = 0; /* init for GCC */
3060 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003061#ifdef FEAT_LINEBREAK
3062 long vcol_sbr = -1; /* virtual column after showbreak */
3063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 long vcol_prev = -1; /* "vcol" of previous character */
3065 char_u *line; /* current line */
3066 char_u *ptr; /* current position in "line" */
3067 int row; /* row in the window, excl w_winrow */
3068 int screen_row; /* row on the screen, incl w_winrow */
3069
3070 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3071 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003072 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003073 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 int c_extra = NUL; /* extra chars, all the same */
3075 int extra_attr = 0; /* attributes when n_extra != 0 */
3076 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3077 displaying lcs_eol at end-of-line */
3078 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3079 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3080
3081 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3082 int saved_n_extra = 0;
3083 char_u *saved_p_extra = NULL;
3084 int saved_c_extra = 0;
3085 int saved_char_attr = 0;
3086
3087 int n_attr = 0; /* chars with special attr */
3088 int saved_attr2 = 0; /* char_attr saved for n_attr */
3089 int n_attr3 = 0; /* chars with overruling special attr */
3090 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3091
3092 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3093
3094 int fromcol, tocol; /* start/end of inverting */
3095 int fromcol_prev = -2; /* start of inverting after cursor */
3096 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003098 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 pos_T pos;
3100 long v;
3101
3102 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003103 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3105 in this line */
3106 int attr = 0; /* attributes for area highlighting */
3107 int area_attr = 0; /* attributes desired by highlighting */
3108 int search_attr = 0; /* attributes desired by 'hlsearch' */
3109#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003110 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 int syntax_attr = 0; /* attributes desired by syntax */
3112 int has_syntax = FALSE; /* this buffer has syntax highl. */
3113 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003114 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003115 int draw_color_col = FALSE; /* highlight colorcolumn */
3116 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003117#endif
3118#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003119 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003120# define SPWORDLEN 150
3121 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003122 int nextlinecol = 0; /* column where nextline[] starts */
3123 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003124 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003125 int spell_attr = 0; /* attributes desired by spelling */
3126 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003127 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3128 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003129 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003130 static int cap_col = -1; /* column to check for Cap word */
3131 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003132 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133#endif
3134 int extra_check; /* has syntax or linebreak */
3135#ifdef FEAT_MBYTE
3136 int multi_attr = 0; /* attributes desired by multibyte */
3137 int mb_l = 1; /* multi-byte byte length */
3138 int mb_c = 0; /* decoded multi-byte character */
3139 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003140 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141#endif
3142#ifdef FEAT_DIFF
3143 int filler_lines; /* nr of filler lines to be drawn */
3144 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003145 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 int change_start = MAXCOL; /* first col of changed area */
3147 int change_end = -1; /* last col of changed area */
3148#endif
3149 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3150#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003151 int need_showbreak = FALSE; /* overlong line, skipping first x
3152 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003154#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003155 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003157 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158#endif
3159#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003160 matchitem_T *cur; /* points to the match list */
3161 match_T *shl; /* points to search_hl or a match */
3162 int shl_flag; /* flag to indicate whether search_hl
3163 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003164 int pos_inprogress; /* marks that position match search is
3165 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003166 int prevcol_hl_flag; /* flag to indicate whether prevcol
3167 equals startcol of search_hl or one
3168 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169#endif
3170#ifdef FEAT_ARABIC
3171 int prev_c = 0; /* previous Arabic character */
3172 int prev_c1 = 0; /* first composing char for prev_c */
3173#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003174#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003175 int did_line_attr = 0;
3176#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003177#ifdef FEAT_TERMINAL
3178 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003179 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181
3182 /* draw_state: items that are drawn in sequence: */
3183#define WL_START 0 /* nothing done yet */
3184#ifdef FEAT_CMDWIN
3185# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3186#else
3187# define WL_CMDLINE WL_START
3188#endif
3189#ifdef FEAT_FOLDING
3190# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3191#else
3192# define WL_FOLD WL_CMDLINE
3193#endif
3194#ifdef FEAT_SIGNS
3195# define WL_SIGN WL_FOLD + 1 /* column for signs */
3196#else
3197# define WL_SIGN WL_FOLD /* column for signs */
3198#endif
3199#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003200#ifdef FEAT_LINEBREAK
3201# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003203# define WL_BRI WL_NR
3204#endif
3205#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3206# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3207#else
3208# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209#endif
3210#define WL_LINE WL_SBR + 1 /* text in the line */
3211 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003212#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 int feedback_col = 0;
3214 int feedback_old_attr = -1;
3215#endif
3216
Bram Moolenaar860cae12010-06-05 23:22:07 +02003217#ifdef FEAT_CONCEAL
3218 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003219 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003220 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003221 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003222 int is_concealing = FALSE;
3223 int boguscols = 0; /* nonexistent columns added to force
3224 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003225 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003226 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003227 int match_conc = 0; /* cchar for match functions */
3228 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003229 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003230# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003231# define FIX_FOR_BOGUSCOLS \
3232 { \
3233 n_extra += vcol_off; \
3234 vcol -= vcol_off; \
3235 vcol_off = 0; \
3236 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003237 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003238 boguscols = 0; \
3239 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003240#else
3241# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
3244 if (startrow > endrow) /* past the end already! */
3245 return startrow;
3246
3247 row = startrow;
3248 screen_row = row + W_WINROW(wp);
3249
3250 /*
3251 * To speed up the loop below, set extra_check when there is linebreak,
3252 * trailing white space and/or syntax processing to be done.
3253 */
3254#ifdef FEAT_LINEBREAK
3255 extra_check = wp->w_p_lbr;
3256#else
3257 extra_check = 0;
3258#endif
3259#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003260 if (syntax_present(wp) && !wp->w_s->b_syn_error
3261# ifdef SYN_TIME_LIMIT
3262 && !wp->w_s->b_syn_slow
3263# endif
3264 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
3266 /* Prepare for syntax highlighting in this line. When there is an
3267 * error, stop syntax highlighting. */
3268 save_did_emsg = did_emsg;
3269 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003270 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003272 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 else
3274 {
3275 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003276#ifdef SYN_TIME_LIMIT
3277 if (!wp->w_s->b_syn_slow)
3278#endif
3279 {
3280 has_syntax = TRUE;
3281 extra_check = TRUE;
3282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 }
3284 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003285
3286 /* Check for columns to display for 'colorcolumn'. */
3287 color_cols = wp->w_p_cc_cols;
3288 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003289 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003290#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003291
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003292#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003293 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003294 {
3295 extra_check = TRUE;
3296 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003297 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003298 }
3299#endif
3300
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003301#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003302 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003303 && *wp->w_s->b_p_spl != NUL
3304 && wp->w_s->b_langp.ga_len > 0
3305 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003306 {
3307 /* Prepare for spell checking. */
3308 has_spell = TRUE;
3309 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003310
3311 /* Get the start of the next line, so that words that wrap to the next
3312 * line are found too: "et<line-break>al.".
3313 * Trick: skip a few chars for C/shell/Vim comments */
3314 nextline[SPWORDLEN] = NUL;
3315 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3316 {
3317 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3318 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3319 }
3320
3321 /* When a word wrapped from the previous line the start of the current
3322 * line is valid. */
3323 if (lnum == checked_lnum)
3324 cur_checked_col = checked_col;
3325 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003326
3327 /* When there was a sentence end in the previous line may require a
3328 * word starting with capital in this line. In line 1 always check
3329 * the first word. */
3330 if (lnum != capcol_lnum)
3331 cap_col = -1;
3332 if (lnum == 1)
3333 cap_col = 0;
3334 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336#endif
3337
3338 /*
3339 * handle visual active in this window
3340 */
3341 fromcol = -10;
3342 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3344 {
3345 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003346 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
3348 top = &curwin->w_cursor;
3349 bot = &VIsual;
3350 }
3351 else /* Visual is before curwin->w_cursor */
3352 {
3353 top = &VIsual;
3354 bot = &curwin->w_cursor;
3355 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003356 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 if (VIsual_mode == Ctrl_V) /* block mode */
3358 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003359 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
3361 fromcol = wp->w_old_cursor_fcol;
3362 tocol = wp->w_old_cursor_lcol;
3363 }
3364 }
3365 else /* non-block mode */
3366 {
3367 if (lnum > top->lnum && lnum <= bot->lnum)
3368 fromcol = 0;
3369 else if (lnum == top->lnum)
3370 {
3371 if (VIsual_mode == 'V') /* linewise */
3372 fromcol = 0;
3373 else
3374 {
3375 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3376 if (gchar_pos(top) == NUL)
3377 tocol = fromcol + 1;
3378 }
3379 }
3380 if (VIsual_mode != 'V' && lnum == bot->lnum)
3381 {
3382 if (*p_sel == 'e' && bot->col == 0
3383#ifdef FEAT_VIRTUALEDIT
3384 && bot->coladd == 0
3385#endif
3386 )
3387 {
3388 fromcol = -10;
3389 tocol = MAXCOL;
3390 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003391 else if (bot->col == MAXCOL)
3392 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 else
3394 {
3395 pos = *bot;
3396 if (*p_sel == 'e')
3397 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3398 else
3399 {
3400 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3401 ++tocol;
3402 }
3403 }
3404 }
3405 }
3406
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 /* Check if the character under the cursor should not be inverted */
3408 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003409#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 )
3413 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
3415 /* if inverting in this line set area_highlighting */
3416 if (fromcol >= 0)
3417 {
3418 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003419 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003421 if ((clip_star.available && !clip_star.owned
3422 && clip_isautosel_star())
3423 || (clip_plus.available && !clip_plus.owned
3424 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003425 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426#endif
3427 }
3428 }
3429
3430 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003431 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003433 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 && wp == curwin
3435 && lnum >= curwin->w_cursor.lnum
3436 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3437 {
3438 if (lnum == curwin->w_cursor.lnum)
3439 getvcol(curwin, &(curwin->w_cursor),
3440 (colnr_T *)&fromcol, NULL, NULL);
3441 else
3442 fromcol = 0;
3443 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3444 {
3445 pos.lnum = lnum;
3446 pos.col = search_match_endcol;
3447 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3448 }
3449 else
3450 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003451 /* do at least one character; happens when past end of line */
3452 if (fromcol == tocol)
3453 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003455 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 }
3457
3458#ifdef FEAT_DIFF
3459 filler_lines = diff_check(wp, lnum);
3460 if (filler_lines < 0)
3461 {
3462 if (filler_lines == -1)
3463 {
3464 if (diff_find_change(wp, lnum, &change_start, &change_end))
3465 diff_hlf = HLF_ADD; /* added line */
3466 else if (change_start == 0)
3467 diff_hlf = HLF_TXD; /* changed text */
3468 else
3469 diff_hlf = HLF_CHD; /* changed line */
3470 }
3471 else
3472 diff_hlf = HLF_ADD; /* added line */
3473 filler_lines = 0;
3474 area_highlighting = TRUE;
3475 }
3476 if (lnum == wp->w_topline)
3477 filler_lines = wp->w_topfill;
3478 filler_todo = filler_lines;
3479#endif
3480
3481#ifdef LINE_ATTR
3482# ifdef FEAT_SIGNS
3483 /* If this line has a sign with line highlighting set line_attr. */
3484 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3485 if (v != 0)
3486 line_attr = sign_get_attr((int)v, TRUE);
3487# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003488# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003490 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003491 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492# endif
3493 if (line_attr != 0)
3494 area_highlighting = TRUE;
3495#endif
3496
3497 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3498 ptr = line;
3499
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003500#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003501 if (has_spell)
3502 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003503 /* For checking first word with a capital skip white space. */
3504 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003505 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003506
Bram Moolenaar30abd282005-06-22 22:35:10 +00003507 /* To be able to spell-check over line boundaries copy the end of the
3508 * current line into nextline[]. Above the start of the next line was
3509 * copied to nextline[SPWORDLEN]. */
3510 if (nextline[SPWORDLEN] == NUL)
3511 {
3512 /* No next line or it is empty. */
3513 nextlinecol = MAXCOL;
3514 nextline_idx = 0;
3515 }
3516 else
3517 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003518 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003519 if (v < SPWORDLEN)
3520 {
3521 /* Short line, use it completely and append the start of the
3522 * next line. */
3523 nextlinecol = 0;
3524 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003525 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003526 nextline_idx = v + 1;
3527 }
3528 else
3529 {
3530 /* Long line, use only the last SPWORDLEN bytes. */
3531 nextlinecol = v - SPWORDLEN;
3532 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3533 nextline_idx = SPWORDLEN + 1;
3534 }
3535 }
3536 }
3537#endif
3538
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003539 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003541 if (lcs_space || lcs_trail)
3542 extra_check = TRUE;
3543 /* find start of trailing whitespace */
3544 if (lcs_trail)
3545 {
3546 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003547 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003548 --trailcol;
3549 trailcol += (colnr_T) (ptr - line);
3550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552
3553 /*
3554 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3555 * first character to be displayed.
3556 */
3557 if (wp->w_p_wrap)
3558 v = wp->w_skipcol;
3559 else
3560 v = wp->w_leftcol;
3561 if (v > 0)
3562 {
3563#ifdef FEAT_MBYTE
3564 char_u *prev_ptr = ptr;
3565#endif
3566 while (vcol < v && *ptr != NUL)
3567 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003568 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 vcol += c;
3570#ifdef FEAT_MBYTE
3571 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003573 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003576 /* When:
3577 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003578 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003579 * - 'virtualedit' is set, or
3580 * - the visual mode is active,
3581 * the end of the line may be before the start of the displayed part.
3582 */
3583 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003584#ifdef FEAT_SYN_HL
3585 wp->w_p_cuc || draw_color_col ||
3586#endif
3587#ifdef FEAT_VIRTUALEDIT
3588 virtual_active() ||
3589#endif
3590 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
3595 /* Handle a character that's not completely on the screen: Put ptr at
3596 * that character but skip the first few screen characters. */
3597 if (vcol > v)
3598 {
3599 vcol -= c;
3600#ifdef FEAT_MBYTE
3601 ptr = prev_ptr;
3602#else
3603 --ptr;
3604#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003605 /* If the character fits on the screen, don't need to skip it.
3606 * Except for a TAB. */
3607 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003608#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003609 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003610#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003611 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003612 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 }
3614
3615 /*
3616 * Adjust for when the inverted text is before the screen,
3617 * and when the start of the inverted text is before the screen.
3618 */
3619 if (tocol <= vcol)
3620 fromcol = 0;
3621 else if (fromcol >= 0 && fromcol < vcol)
3622 fromcol = vcol;
3623
3624#ifdef FEAT_LINEBREAK
3625 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3626 if (wp->w_p_wrap)
3627 need_showbreak = TRUE;
3628#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003629#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003630 /* When spell checking a word we need to figure out the start of the
3631 * word and if it's badly spelled or not. */
3632 if (has_spell)
3633 {
3634 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003635 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003636 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003637
3638 pos = wp->w_cursor;
3639 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003640 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003641 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003642
3643 /* spell_move_to() may call ml_get() and make "line" invalid */
3644 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3645 ptr = line + linecol;
3646
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003647 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003648 {
3649 /* no bad word found at line start, don't check until end of a
3650 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003651 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003652 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003653 }
3654 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003655 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003656 /* bad word found, use attributes until end of word */
3657 word_end = wp->w_cursor.col + len + 1;
3658
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003659 /* Turn index into actual attributes. */
3660 if (spell_hlf != HLF_COUNT)
3661 spell_attr = highlight_attr[spell_hlf];
3662 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003663 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003664
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003665# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003666 /* Need to restart syntax highlighting for this line. */
3667 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003668 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003669# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003670 }
3671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673
3674 /*
3675 * Correct highlighting for cursor that can't be disabled.
3676 * Avoids having to check this for each character.
3677 */
3678 if (fromcol >= 0)
3679 {
3680 if (noinvcur)
3681 {
3682 if ((colnr_T)fromcol == wp->w_virtcol)
3683 {
3684 /* highlighting starts at cursor, let it start just after the
3685 * cursor */
3686 fromcol_prev = fromcol;
3687 fromcol = -1;
3688 }
3689 else if ((colnr_T)fromcol < wp->w_virtcol)
3690 /* restart highlighting after the cursor */
3691 fromcol_prev = wp->w_virtcol;
3692 }
3693 if (fromcol >= tocol)
3694 fromcol = -1;
3695 }
3696
3697#ifdef FEAT_SEARCH_EXTRA
3698 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003699 * Handle highlighting the last used search pattern and matches.
3700 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003702 cur = wp->w_match_head;
3703 shl_flag = FALSE;
3704 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003706 if (shl_flag == FALSE)
3707 {
3708 shl = &search_hl;
3709 shl_flag = TRUE;
3710 }
3711 else
3712 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003713 shl->startcol = MAXCOL;
3714 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003716 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003717 v = (long)(ptr - line);
3718 if (cur != NULL)
3719 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003720 next_search_hl(wp, shl, lnum, (colnr_T)v,
3721 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003722
3723 /* Need to get the line again, a multi-line regexp may have made it
3724 * invalid. */
3725 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3726 ptr = line + v;
3727
3728 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003730 if (shl->lnum == lnum)
3731 shl->startcol = shl->rm.startpos[0].col;
3732 else
3733 shl->startcol = 0;
3734 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3735 - shl->rm.startpos[0].lnum)
3736 shl->endcol = shl->rm.endpos[0].col;
3737 else
3738 shl->endcol = MAXCOL;
3739 /* Highlight one character for an empty match. */
3740 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003743 if (has_mbyte && line[shl->endcol] != NUL)
3744 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3745 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003747 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003749 if ((long)shl->startcol < v) /* match at leftcol */
3750 {
3751 shl->attr_cur = shl->attr;
3752 search_attr = shl->attr;
3753 }
3754 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003756 if (shl != &search_hl && cur != NULL)
3757 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 }
3759#endif
3760
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003761#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003762 /* Cursor line highlighting for 'cursorline' in the current window. Not
3763 * when Visual mode is active, because it's not clear what is selected
3764 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003765 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003766 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003767 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003768 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003769 area_highlighting = TRUE;
3770 }
3771#endif
3772
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003773 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 col = 0;
3775#ifdef FEAT_RIGHTLEFT
3776 if (wp->w_p_rl)
3777 {
3778 /* Rightleft window: process the text in the normal direction, but put
3779 * it in current_ScreenLine[] from right to left. Start at the
3780 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003781 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 off += col;
3783 }
3784#endif
3785
3786 /*
3787 * Repeat for the whole displayed line.
3788 */
3789 for (;;)
3790 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003791#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003792 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 /* Skip this quickly when working on the text. */
3795 if (draw_state != WL_LINE)
3796 {
3797#ifdef FEAT_CMDWIN
3798 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3799 {
3800 draw_state = WL_CMDLINE;
3801 if (cmdwin_type != 0 && wp == curwin)
3802 {
3803 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003805 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003806 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
3808 }
3809#endif
3810
3811#ifdef FEAT_FOLDING
3812 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3813 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003814 int fdc = compute_foldcolumn(wp, 0);
3815
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003817 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003819 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003820 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003821 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003822 p_extra_free = alloc(12 + 1);
3823
3824 if (p_extra_free != NULL)
3825 {
3826 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3827 n_extra = fdc;
3828 p_extra_free[n_extra] = NUL;
3829 p_extra = p_extra_free;
3830 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003831 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834 }
3835#endif
3836
3837#ifdef FEAT_SIGNS
3838 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3839 {
3840 draw_state = WL_SIGN;
3841 /* Show the sign column when there are any signs in this
3842 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003843 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003845 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003847 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848# endif
3849
3850 /* Draw two cells with the sign value or blank. */
3851 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003852 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 n_extra = 2;
3854
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003855 if (row == startrow
3856#ifdef FEAT_DIFF
3857 + filler_lines && filler_todo <= 0
3858#endif
3859 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
3861 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3862 SIGN_TEXT);
3863# ifdef FEAT_SIGN_ICONS
3864 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3865 SIGN_ICON);
3866 if (gui.in_use && icon_sign != 0)
3867 {
3868 /* Use the image in this position. */
3869 c_extra = SIGN_BYTE;
3870# ifdef FEAT_NETBEANS_INTG
3871 if (buf_signcount(wp->w_buffer, lnum) > 1)
3872 c_extra = MULTISIGN_BYTE;
3873# endif
3874 char_attr = icon_sign;
3875 }
3876 else
3877# endif
3878 if (text_sign != 0)
3879 {
3880 p_extra = sign_get_text(text_sign);
3881 if (p_extra != NULL)
3882 {
3883 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003884 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 }
3886 char_attr = sign_get_attr(text_sign, FALSE);
3887 }
3888 }
3889 }
3890 }
3891#endif
3892
3893 if (draw_state == WL_NR - 1 && n_extra == 0)
3894 {
3895 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003896 /* Display the absolute or relative line number. After the
3897 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3898 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 && (row == startrow
3900#ifdef FEAT_DIFF
3901 + filler_lines
3902#endif
3903 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3904 {
3905 /* Draw the line number (empty space after wrapping). */
3906 if (row == startrow
3907#ifdef FEAT_DIFF
3908 + filler_lines
3909#endif
3910 )
3911 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003912 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003913 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003914
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003915 if (wp->w_p_nu && !wp->w_p_rnu)
3916 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003917 num = (long)lnum;
3918 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003919 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003920 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003921 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003922 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003923 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003924 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003925 num = lnum;
3926 fmt = "%-*ld ";
3927 }
3928 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003929
Bram Moolenaar700e7342013-01-30 12:31:36 +01003930 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003931 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 if (wp->w_skipcol > 0)
3933 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3934 *p_extra = '-';
3935#ifdef FEAT_RIGHTLEFT
3936 if (wp->w_p_rl) /* reverse line numbers */
3937 rl_mirror(extra);
3938#endif
3939 p_extra = extra;
3940 c_extra = NUL;
3941 }
3942 else
3943 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003944 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003945 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003946#ifdef FEAT_SYN_HL
3947 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003948 * the current line differently.
3949 * TODO: Can we use CursorLine instead of CursorLineNr
3950 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003951 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003952 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003953 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
3956 }
3957
Bram Moolenaar597a4222014-06-25 14:39:50 +02003958#ifdef FEAT_LINEBREAK
3959 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3960 && n_extra == 0 && *p_sbr != NUL)
3961 /* draw indent after showbreak value */
3962 draw_state = WL_BRI;
3963 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3964 /* After the showbreak, draw the breakindent */
3965 draw_state = WL_BRI - 1;
3966
3967 /* draw 'breakindent': indent wrapped text accordingly */
3968 if (draw_state == WL_BRI - 1 && n_extra == 0)
3969 {
3970 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003971 /* if need_showbreak is set, breakindent also applies */
3972 if (wp->w_p_bri && n_extra == 0
3973 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003974# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003975 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003976# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003977 )
3978 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003979 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003980# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003981 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003982 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003983 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003984# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003985 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3986 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003987 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003988# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003989 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003990# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003991 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003992 c_extra = ' ';
3993 n_extra = get_breakindent_win(wp,
3994 ml_get_buf(wp->w_buffer, lnum, FALSE));
3995 /* Correct end of highlighted area for 'breakindent',
3996 * required when 'linebreak' is also set. */
3997 if (tocol == vcol)
3998 tocol += n_extra;
3999 }
4000 }
4001#endif
4002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4004 if (draw_state == WL_SBR - 1 && n_extra == 0)
4005 {
4006 draw_state = WL_SBR;
4007# ifdef FEAT_DIFF
4008 if (filler_todo > 0)
4009 {
4010 /* Draw "deleted" diff line(s). */
4011 if (char2cells(fill_diff) > 1)
4012 c_extra = '-';
4013 else
4014 c_extra = fill_diff;
4015# ifdef FEAT_RIGHTLEFT
4016 if (wp->w_p_rl)
4017 n_extra = col + 1;
4018 else
4019# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004020 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004021 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 }
4023# endif
4024# ifdef FEAT_LINEBREAK
4025 if (*p_sbr != NUL && need_showbreak)
4026 {
4027 /* Draw 'showbreak' at the start of each broken line. */
4028 p_extra = p_sbr;
4029 c_extra = NUL;
4030 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004031 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004033 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 /* Correct end of highlighted area for 'showbreak',
4035 * required when 'linebreak' is also set. */
4036 if (tocol == vcol)
4037 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004038#ifdef FEAT_SYN_HL
4039 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004040 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004041 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004042 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
4045# endif
4046 }
4047#endif
4048
4049 if (draw_state == WL_LINE - 1 && n_extra == 0)
4050 {
4051 draw_state = WL_LINE;
4052 if (saved_n_extra)
4053 {
4054 /* Continue item from end of wrapped line. */
4055 n_extra = saved_n_extra;
4056 c_extra = saved_c_extra;
4057 p_extra = saved_p_extra;
4058 char_attr = saved_char_attr;
4059 }
4060 else
4061 char_attr = 0;
4062 }
4063 }
4064
4065 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004066 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004067 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068#ifdef FEAT_DIFF
4069 && filler_todo <= 0
4070#endif
4071 )
4072 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004073 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004074 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004075 /* Pretend we have finished updating the window. Except when
4076 * 'cursorcolumn' is set. */
4077#ifdef FEAT_SYN_HL
4078 if (wp->w_p_cuc)
4079 row = wp->w_cline_row + wp->w_cline_height;
4080 else
4081#endif
4082 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 break;
4084 }
4085
4086 if (draw_state == WL_LINE && area_highlighting)
4087 {
4088 /* handle Visual or match highlighting in this line */
4089 if (vcol == fromcol
4090#ifdef FEAT_MBYTE
4091 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4092 && (*mb_ptr2cells)(ptr) > 1)
4093#endif
4094 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004095 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 && vcol < tocol))
4097 area_attr = attr; /* start highlighting */
4098 else if (area_attr != 0
4099 && (vcol == tocol
4100 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102
4103#ifdef FEAT_SEARCH_EXTRA
4104 if (!n_extra)
4105 {
4106 /*
4107 * Check for start/end of search pattern match.
4108 * After end, check for start/end of next match.
4109 * When another match, have to check for start again.
4110 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004111 * Do this for 'search_hl' and the match list (ordered by
4112 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004114 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004115 cur = wp->w_match_head;
4116 shl_flag = FALSE;
4117 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004119 if (shl_flag == FALSE
4120 && ((cur != NULL
4121 && cur->priority > SEARCH_HL_PRIORITY)
4122 || cur == NULL))
4123 {
4124 shl = &search_hl;
4125 shl_flag = TRUE;
4126 }
4127 else
4128 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004129 if (cur != NULL)
4130 cur->pos.cur = 0;
4131 pos_inprogress = TRUE;
4132 while (shl->rm.regprog != NULL
4133 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004135 if (shl->startcol != MAXCOL
4136 && v >= (long)shl->startcol
4137 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004139#ifdef FEAT_MBYTE
4140 int tmp_col = v + MB_PTR2LEN(ptr);
4141
4142 if (shl->endcol < tmp_col)
4143 shl->endcol = tmp_col;
4144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004146#ifdef FEAT_CONCEAL
4147 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4148 == cur->hlg_id)
4149 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004150 has_match_conc =
4151 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004152 match_conc = cur->conceal_char;
4153 }
4154 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004155 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004158 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 {
4160 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004161 next_search_hl(wp, shl, lnum, (colnr_T)v,
4162 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004163 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004164 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 /* Need to get the line again, a multi-line regexp
4167 * may have made it invalid. */
4168 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4169 ptr = line + v;
4170
4171 if (shl->lnum == lnum)
4172 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004173 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004175 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004177 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004179 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
4181 /* highlight empty match, try again after
4182 * it */
4183#ifdef FEAT_MBYTE
4184 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004185 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004186 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 else
4188#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004189 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191
4192 /* Loop to check if the match starts at the
4193 * current position */
4194 continue;
4195 }
4196 }
4197 break;
4198 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004199 if (shl != &search_hl && cur != NULL)
4200 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004202
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004203 /* Use attributes from match with highest priority among
4204 * 'search_hl' and the match list. */
4205 search_attr = search_hl.attr_cur;
4206 cur = wp->w_match_head;
4207 shl_flag = FALSE;
4208 while (cur != NULL || shl_flag == FALSE)
4209 {
4210 if (shl_flag == FALSE
4211 && ((cur != NULL
4212 && cur->priority > SEARCH_HL_PRIORITY)
4213 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004214 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004215 shl = &search_hl;
4216 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004217 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004218 else
4219 shl = &cur->hl;
4220 if (shl->attr_cur != 0)
4221 search_attr = shl->attr_cur;
4222 if (shl != &search_hl && cur != NULL)
4223 cur = cur->next;
4224 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004225 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004226 if (*ptr == NUL && (did_line_attr >= 1
4227 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004228 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 }
4230#endif
4231
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004233 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004235 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4236 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004238 if (diff_hlf == HLF_TXD && ptr - line > change_end
4239 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004241 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004242 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004243 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
4245#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004246
4247 /* Decide which of the highlight attributes to use. */
4248 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004249#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004250 if (area_attr != 0)
4251 char_attr = hl_combine_attr(line_attr, area_attr);
4252 else if (search_attr != 0)
4253 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004254 /* Use line_attr when not in the Visual or 'incsearch' area
4255 * (area_attr may be 0 when "noinvcur" is set). */
4256 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004257 || vcol < fromcol || vcol_prev < fromcol_prev
4258 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004259 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004260#else
4261 if (area_attr != 0)
4262 char_attr = area_attr;
4263 else if (search_attr != 0)
4264 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004265#endif
4266 else
4267 {
4268 attr_pri = FALSE;
4269#ifdef FEAT_SYN_HL
4270 if (has_syntax)
4271 char_attr = syntax_attr;
4272 else
4273#endif
4274 char_attr = 0;
4275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277
4278 /*
4279 * Get the next character to put on the screen.
4280 */
4281 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004282 * The "p_extra" points to the extra stuff that is inserted to
4283 * represent special characters (non-printable stuff) and other
4284 * things. When all characters are the same, c_extra is used.
4285 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4286 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4288 */
4289 if (n_extra > 0)
4290 {
4291 if (c_extra != NUL)
4292 {
4293 c = c_extra;
4294#ifdef FEAT_MBYTE
4295 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004296 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 {
4298 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004299 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004300 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 else
4303 mb_utf8 = FALSE;
4304#endif
4305 }
4306 else
4307 {
4308 c = *p_extra;
4309#ifdef FEAT_MBYTE
4310 if (has_mbyte)
4311 {
4312 mb_c = c;
4313 if (enc_utf8)
4314 {
4315 /* If the UTF-8 character is more than one byte:
4316 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004317 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 mb_utf8 = FALSE;
4319 if (mb_l > n_extra)
4320 mb_l = 1;
4321 else if (mb_l > 1)
4322 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004323 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004325 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
4327 }
4328 else
4329 {
4330 /* if this is a DBCS character, put it in "mb_c" */
4331 mb_l = MB_BYTE2LEN(c);
4332 if (mb_l >= n_extra)
4333 mb_l = 1;
4334 else if (mb_l > 1)
4335 mb_c = (c << 8) + p_extra[1];
4336 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004337 if (mb_l == 0) /* at the NUL at end-of-line */
4338 mb_l = 1;
4339
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 /* If a double-width char doesn't fit display a '>' in the
4341 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004342 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343# ifdef FEAT_RIGHTLEFT
4344 wp->w_p_rl ? (col <= 0) :
4345# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004346 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 && (*mb_char2cells)(mb_c) == 2)
4348 {
4349 c = '>';
4350 mb_c = c;
4351 mb_l = 1;
4352 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004353 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 /* put the pointer back to output the double-width
4355 * character at the start of the next line. */
4356 ++n_extra;
4357 --p_extra;
4358 }
4359 else
4360 {
4361 n_extra -= mb_l - 1;
4362 p_extra += mb_l - 1;
4363 }
4364 }
4365#endif
4366 ++p_extra;
4367 }
4368 --n_extra;
4369 }
4370 else
4371 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004372#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004373 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004374#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004375
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004376 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004377 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 /*
4379 * Get a character from the line itself.
4380 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004381 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004382#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004383 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385#ifdef FEAT_MBYTE
4386 if (has_mbyte)
4387 {
4388 mb_c = c;
4389 if (enc_utf8)
4390 {
4391 /* If the UTF-8 character is more than one byte: Decode it
4392 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004393 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 mb_utf8 = FALSE;
4395 if (mb_l > 1)
4396 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004397 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 /* Overlong encoded ASCII or ASCII with composing char
4399 * is displayed normally, except a NUL. */
4400 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004401 {
4402 c = mb_c;
4403# ifdef FEAT_LINEBREAK
4404 c0 = mb_c;
4405# endif
4406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004408
4409 /* At start of the line we can have a composing char.
4410 * Draw it as a space with a composing char. */
4411 if (utf_iscomposing(mb_c))
4412 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004413 int i;
4414
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004415 for (i = Screen_mco - 1; i > 0; --i)
4416 u8cc[i] = u8cc[i - 1];
4417 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004418 mb_c = ' ';
4419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 }
4421
4422 if ((mb_l == 1 && c >= 0x80)
4423 || (mb_l >= 1 && mb_c == 0)
4424 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004425# ifdef UNICODE16
4426 || mb_c >= 0x10000
4427# endif
4428 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
4430 /*
4431 * Illegal UTF-8 byte: display as <xx>.
4432 * Non-BMP character : display as ? or fullwidth ?.
4433 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004434# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
4438 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004439# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 if (wp->w_p_rl) /* reverse */
4441 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004444# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 else if (utf_char2cells(mb_c) != 2)
4446 STRCPY(extra, "?");
4447 else
4448 /* 0xff1f in UTF-8: full-width '?' */
4449 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
4452 p_extra = extra;
4453 c = *p_extra;
4454 mb_c = mb_ptr2char_adv(&p_extra);
4455 mb_utf8 = (c >= 0x80);
4456 n_extra = (int)STRLEN(p_extra);
4457 c_extra = NUL;
4458 if (area_attr == 0 && search_attr == 0)
4459 {
4460 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004461 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 saved_attr2 = char_attr; /* save current attr */
4463 }
4464 }
4465 else if (mb_l == 0) /* at the NUL at end-of-line */
4466 mb_l = 1;
4467#ifdef FEAT_ARABIC
4468 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4469 {
4470 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004471 int pc, pc1, nc;
4472 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 /* The idea of what is the previous and next
4475 * character depends on 'rightleft'. */
4476 if (wp->w_p_rl)
4477 {
4478 pc = prev_c;
4479 pc1 = prev_c1;
4480 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004481 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483 else
4484 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004485 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004487 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489 prev_c = mb_c;
4490
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004491 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 }
4493 else
4494 prev_c = mb_c;
4495#endif
4496 }
4497 else /* enc_dbcs */
4498 {
4499 mb_l = MB_BYTE2LEN(c);
4500 if (mb_l == 0) /* at the NUL at end-of-line */
4501 mb_l = 1;
4502 else if (mb_l > 1)
4503 {
4504 /* We assume a second byte below 32 is illegal.
4505 * Hopefully this is OK for all double-byte encodings!
4506 */
4507 if (ptr[1] >= 32)
4508 mb_c = (c << 8) + ptr[1];
4509 else
4510 {
4511 if (ptr[1] == NUL)
4512 {
4513 /* head byte at end of line */
4514 mb_l = 1;
4515 transchar_nonprint(extra, c);
4516 }
4517 else
4518 {
4519 /* illegal tail byte */
4520 mb_l = 2;
4521 STRCPY(extra, "XX");
4522 }
4523 p_extra = extra;
4524 n_extra = (int)STRLEN(extra) - 1;
4525 c_extra = NUL;
4526 c = *p_extra++;
4527 if (area_attr == 0 && search_attr == 0)
4528 {
4529 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004530 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 saved_attr2 = char_attr; /* save current attr */
4532 }
4533 mb_c = c;
4534 }
4535 }
4536 }
4537 /* If a double-width char doesn't fit display a '>' in the
4538 * last column; the character is displayed at the start of the
4539 * next line. */
4540 if ((
4541# ifdef FEAT_RIGHTLEFT
4542 wp->w_p_rl ? (col <= 0) :
4543# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004544 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 && (*mb_char2cells)(mb_c) == 2)
4546 {
4547 c = '>';
4548 mb_c = c;
4549 mb_utf8 = FALSE;
4550 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004551 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 /* Put pointer back so that the character will be
4553 * displayed at the start of the next line. */
4554 --ptr;
4555 }
4556 else if (*ptr != NUL)
4557 ptr += mb_l - 1;
4558
4559 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004560 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004561 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004562 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004565 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 c = ' ';
4567 if (area_attr == 0 && search_attr == 0)
4568 {
4569 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004570 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 saved_attr2 = char_attr; /* save current attr */
4572 }
4573 mb_c = c;
4574 mb_utf8 = FALSE;
4575 mb_l = 1;
4576 }
4577
4578 }
4579#endif
4580 ++ptr;
4581
4582 if (extra_check)
4583 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004584#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004585 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004586#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004587
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004588#ifdef FEAT_TERMINAL
4589 if (get_term_attr)
4590 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004591 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004592
4593 if (!attr_pri)
4594 char_attr = syntax_attr;
4595 else
4596 char_attr = hl_combine_attr(syntax_attr, char_attr);
4597 }
4598#endif
4599
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004600#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 /* Get syntax attribute, unless still at the start of the line
4602 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004603 v = (long)(ptr - line);
4604 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 /* Get the syntax attribute for the character. If there
4607 * is an error, disable syntax highlighting. */
4608 save_did_emsg = did_emsg;
4609 did_emsg = FALSE;
4610
Bram Moolenaar217ad922005-03-20 22:37:15 +00004611 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004612# ifdef FEAT_SPELL
4613 has_spell ? &can_spell :
4614# endif
4615 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616
4617 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004618 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004619 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004620 has_syntax = FALSE;
4621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 else
4623 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004624#ifdef SYN_TIME_LIMIT
4625 if (wp->w_s->b_syn_slow)
4626 has_syntax = FALSE;
4627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
4629 /* Need to get the line again, a multi-line regexp may
4630 * have made it invalid. */
4631 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4632 ptr = line + v;
4633
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004634 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004636 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004637 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004638# ifdef FEAT_CONCEAL
4639 /* no concealing past the end of the line, it interferes
4640 * with line highlighting */
4641 if (c == NUL)
4642 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004643 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004644 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004647#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004648
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004649#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004650 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004651 * Only do this when there is no syntax highlighting, the
4652 * @Spell cluster is not used or the current syntax item
4653 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004654 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004655 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004656 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004657# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004658 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004659 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004660# endif
4661 if (c != 0 && (
4662# ifdef FEAT_SYN_HL
4663 !has_syntax ||
4664# endif
4665 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004666 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004667 char_u *prev_ptr, *p;
4668 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004669 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004670# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004671 if (has_mbyte)
4672 {
4673 prev_ptr = ptr - mb_l;
4674 v -= mb_l - 1;
4675 }
4676 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004677# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004678 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004679
4680 /* Use nextline[] if possible, it has the start of the
4681 * next line concatenated. */
4682 if ((prev_ptr - line) - nextlinecol >= 0)
4683 p = nextline + (prev_ptr - line) - nextlinecol;
4684 else
4685 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004686 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004687 len = spell_check(wp, p, &spell_hlf, &cap_col,
4688 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004689 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004690
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004691 /* In Insert mode only highlight a word that
4692 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004693 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004694 && (State & INSERT) != 0
4695 && wp->w_cursor.lnum == lnum
4696 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004697 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004698 && wp->w_cursor.col < (colnr_T)word_end)
4699 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004700 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004701 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004702 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004703
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004704 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004705 && (p - nextline) + len > nextline_idx)
4706 {
4707 /* Remember that the good word continues at the
4708 * start of the next line. */
4709 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004710 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004711 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004712
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004713 /* Turn index into actual attributes. */
4714 if (spell_hlf != HLF_COUNT)
4715 spell_attr = highlight_attr[spell_hlf];
4716
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004717 if (cap_col > 0)
4718 {
4719 if (p != prev_ptr
4720 && (p - nextline) + cap_col >= nextline_idx)
4721 {
4722 /* Remember that the word in the next line
4723 * must start with a capital. */
4724 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004725 cap_col = (int)((p - nextline) + cap_col
4726 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004727 }
4728 else
4729 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004730 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004731 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004732 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004733 }
4734 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004735 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004736 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004737 char_attr = hl_combine_attr(char_attr, spell_attr);
4738 else
4739 char_attr = hl_combine_attr(spell_attr, char_attr);
4740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741#endif
4742#ifdef FEAT_LINEBREAK
4743 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004744 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004746 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004747 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004749# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004750 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004751# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004752 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004754 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004756 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004757
Bram Moolenaar597a4222014-06-25 14:39:50 +02004758 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004759 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004760 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004761 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004762# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004763 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004764 wp->w_buffer->b_p_vts_array) - 1;
4765# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004766 n_extra = (int)wp->w_buffer->b_p_ts
4767 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004768# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004769
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004770# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004771 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004772# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004774# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004775 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004776 {
4777#ifdef FEAT_CONCEAL
4778 if (c == TAB)
4779 /* See "Tab alignment" below. */
4780 FIX_FOR_BOGUSCOLS;
4781#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004782 if (!wp->w_p_list)
4783 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
4786#endif
4787
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004788 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4789 */
4790 if (wp->w_p_list
4791 && (((c == 160
4792#ifdef FEAT_MBYTE
4793 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4794#endif
4795 ) && lcs_nbsp)
4796 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4797 {
4798 c = (c == ' ') ? lcs_space : lcs_nbsp;
4799 if (area_attr == 0 && search_attr == 0)
4800 {
4801 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004802 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004803 saved_attr2 = char_attr; /* save current attr */
4804 }
4805#ifdef FEAT_MBYTE
4806 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004807 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004808 {
4809 mb_utf8 = TRUE;
4810 u8cc[0] = 0;
4811 c = 0xc0;
4812 }
4813 else
4814 mb_utf8 = FALSE;
4815#endif
4816 }
4817
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4819 {
4820 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004821 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 {
4823 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004824 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 saved_attr2 = char_attr; /* save current attr */
4826 }
4827#ifdef FEAT_MBYTE
4828 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004829 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
4831 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004832 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004833 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 }
4835 else
4836 mb_utf8 = FALSE;
4837#endif
4838 }
4839 }
4840
4841 /*
4842 * Handling of non-printable characters.
4843 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004844 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
4846 /*
4847 * when getting a character from the file, we may have to
4848 * turn it into something else on the way to putting it
4849 * into "ScreenLines".
4850 */
4851 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4852 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004853 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004854 long vcol_adjusted = vcol; /* removed showbreak length */
4855#ifdef FEAT_LINEBREAK
4856 /* only adjust the tab_len, when at the first column
4857 * after the showbreak value was drawn */
4858 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4859 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004862#ifdef FEAT_VARTABS
4863 tab_len = tabstop_padding(vcol_adjusted,
4864 wp->w_buffer->b_p_ts,
4865 wp->w_buffer->b_p_vts_array) - 1;
4866#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004867 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004868 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4869#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004870
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004871#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004872 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004873#endif
4874 /* tab amount depends on current column */
4875 n_extra = tab_len;
4876#ifdef FEAT_LINEBREAK
4877 else
4878 {
4879 char_u *p;
4880 int len = n_extra;
4881 int i;
4882 int saved_nextra = n_extra;
4883
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004884#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004885 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004886 /* there are characters to conceal */
4887 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004888 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4889 */
4890 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4891 && n_extra > tab_len)
4892 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004893#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004894
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004895 /* if n_extra > 0, it gives the number of chars, to
4896 * use for a tab, else we need to calculate the width
4897 * for a tab */
4898#ifdef FEAT_MBYTE
4899 len = (tab_len * mb_char2len(lcs_tab2));
4900 if (n_extra > 0)
4901 len += n_extra - tab_len;
4902#endif
4903 c = lcs_tab1;
4904 p = alloc((unsigned)(len + 1));
4905 vim_memset(p, ' ', len);
4906 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004907 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004908 p_extra_free = p;
4909 for (i = 0; i < tab_len; i++)
4910 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004911 if (*p == NUL)
4912 {
4913 tab_len = i;
4914 break;
4915 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004916#ifdef FEAT_MBYTE
4917 mb_char2bytes(lcs_tab2, p);
4918 p += mb_char2len(lcs_tab2);
4919 n_extra += mb_char2len(lcs_tab2)
4920 - (saved_nextra > 0 ? 1 : 0);
4921#else
4922 p[i] = lcs_tab2;
4923#endif
4924 }
4925 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004926#ifdef FEAT_CONCEAL
4927 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4928 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004929 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004930 n_extra -= vcol_off;
4931#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004932 }
4933#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004934#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004935 {
4936 int vc_saved = vcol_off;
4937
4938 /* Tab alignment should be identical regardless of
4939 * 'conceallevel' value. So tab compensates of all
4940 * previous concealed characters, and thus resets
4941 * vcol_off and boguscols accumulated so far in the
4942 * line. Note that the tab can be longer than
4943 * 'tabstop' when there are concealed characters. */
4944 FIX_FOR_BOGUSCOLS;
4945
4946 /* Make sure, the highlighting for the tab char will be
4947 * correctly set further below (effectively reverts the
4948 * FIX_FOR_BOGSUCOLS macro */
4949 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004950 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004951 tab_len += vc_saved;
4952 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954#ifdef FEAT_MBYTE
4955 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4956#endif
4957 if (wp->w_p_list)
4958 {
4959 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004960#ifdef FEAT_LINEBREAK
4961 if (wp->w_p_lbr)
4962 c_extra = NUL; /* using p_extra from above */
4963 else
4964#endif
4965 c_extra = lcs_tab2;
4966 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004967 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 saved_attr2 = char_attr; /* save current attr */
4969#ifdef FEAT_MBYTE
4970 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004971 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
4973 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004974 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004975 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
4977#endif
4978 }
4979 else
4980 {
4981 c_extra = ' ';
4982 c = ' ';
4983 }
4984 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004985 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004986 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004987 || ((fromcol >= 0 || fromcol_prev >= 0)
4988 && tocol > vcol
4989 && VIsual_mode != Ctrl_V
4990 && (
4991# ifdef FEAT_RIGHTLEFT
4992 wp->w_p_rl ? (col >= 0) :
4993# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004994 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004995 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004996 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004997 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004998 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005000 /* Display a '$' after the line or highlight an extra
5001 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5003 /* For a diff line the highlighting continues after the
5004 * "$". */
5005 if (
5006# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005007 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008# ifdef LINE_ATTR
5009 &&
5010# endif
5011# endif
5012# ifdef LINE_ATTR
5013 line_attr == 0
5014# endif
5015 )
5016#endif
5017 {
5018#ifdef FEAT_VIRTUALEDIT
5019 /* In virtualedit, visual selections may extend
5020 * beyond end of line. */
5021 if (area_highlighting && virtual_active()
5022 && tocol != MAXCOL && vcol < tocol)
5023 n_extra = 0;
5024 else
5025#endif
5026 {
5027 p_extra = at_end_str;
5028 n_extra = 1;
5029 c_extra = NUL;
5030 }
5031 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005032 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005033 c = lcs_eol;
5034 else
5035 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 lcs_eol_one = -1;
5037 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005038 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005040 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 n_attr = 1;
5042 }
5043#ifdef FEAT_MBYTE
5044 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005045 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
5047 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005048 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005049 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
5051 else
5052 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5053#endif
5054 }
5055 else if (c != NUL)
5056 {
5057 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005058 if (n_extra == 0)
5059 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060#ifdef FEAT_RIGHTLEFT
5061 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5062 rl_mirror(p_extra); /* reverse "<12>" */
5063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005065#ifdef FEAT_LINEBREAK
5066 if (wp->w_p_lbr)
5067 {
5068 char_u *p;
5069
5070 c = *p_extra;
5071 p = alloc((unsigned)n_extra + 1);
5072 vim_memset(p, ' ', n_extra);
5073 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5074 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005075 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005076 p_extra_free = p_extra = p;
5077 }
5078 else
5079#endif
5080 {
5081 n_extra = byte2cells(c) - 1;
5082 c = *p_extra++;
5083 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005084 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
5086 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005087 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 saved_attr2 = char_attr; /* save current attr */
5089 }
5090#ifdef FEAT_MBYTE
5091 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5092#endif
5093 }
5094#ifdef FEAT_VIRTUALEDIT
5095 else if (VIsual_active
5096 && (VIsual_mode == Ctrl_V
5097 || VIsual_mode == 'v')
5098 && virtual_active()
5099 && tocol != MAXCOL
5100 && vcol < tocol
5101 && (
5102# ifdef FEAT_RIGHTLEFT
5103 wp->w_p_rl ? (col >= 0) :
5104# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005105 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
5107 c = ' ';
5108 --ptr; /* put it back at the NUL */
5109 }
5110#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005111#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 else if ((
5113# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005114 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005116# ifdef FEAT_TERMINAL
5117 term_attr != 0 ||
5118# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 ) && (
5121# ifdef FEAT_RIGHTLEFT
5122 wp->w_p_rl ? (col >= 0) :
5123# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005124 (col
5125# ifdef FEAT_CONCEAL
5126 - boguscols
5127# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005128 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
5130 /* Highlight until the right side of the window */
5131 c = ' ';
5132 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005133
5134 /* Remember we do the char for line highlighting. */
5135 ++did_line_attr;
5136
5137 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005138 if (line_attr != 0 && char_attr == search_attr
5139 && (did_line_attr > 1
5140 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005141 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142# ifdef FEAT_DIFF
5143 if (diff_hlf == HLF_TXD)
5144 {
5145 diff_hlf = HLF_CHD;
5146 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005147 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005148 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005149 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5150 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005151 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005155# ifdef FEAT_TERMINAL
5156 if (term_attr != 0)
5157 {
5158 char_attr = term_attr;
5159 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5160 char_attr = hl_combine_attr(char_attr,
5161 HL_ATTR(HLF_CUL));
5162 }
5163# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165#endif
5166 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005167
5168#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005169 if ( wp->w_p_cole > 0
5170 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005171 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005172 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005173 && !(lnum_in_visual_area
5174 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005175 {
5176 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005177 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005178 && (syn_get_sub_char() != NUL || match_conc
5179 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005180 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005181 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005182 /* First time at this concealed item: display one
5183 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005184 if (match_conc)
5185 c = match_conc;
5186 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005187 c = syn_get_sub_char();
5188 else if (lcs_conceal != NUL)
5189 c = lcs_conceal;
5190 else
5191 c = ' ';
5192
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005193 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005194
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005195 if (n_extra > 0)
5196 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005197 vcol += n_extra;
5198 if (wp->w_p_wrap && n_extra > 0)
5199 {
5200# ifdef FEAT_RIGHTLEFT
5201 if (wp->w_p_rl)
5202 {
5203 col -= n_extra;
5204 boguscols -= n_extra;
5205 }
5206 else
5207# endif
5208 {
5209 boguscols += n_extra;
5210 col += n_extra;
5211 }
5212 }
5213 n_extra = 0;
5214 n_attr = 0;
5215 }
5216 else if (n_skip == 0)
5217 {
5218 is_concealing = TRUE;
5219 n_skip = 1;
5220 }
5221# ifdef FEAT_MBYTE
5222 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005223 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005224 {
5225 mb_utf8 = TRUE;
5226 u8cc[0] = 0;
5227 c = 0xc0;
5228 }
5229 else
5230 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5231# endif
5232 }
5233 else
5234 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005235 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005236 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005237 }
5238#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 }
5240
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005241#ifdef FEAT_CONCEAL
5242 /* In the cursor line and we may be concealing characters: correct
5243 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005244 if (!did_wcol && draw_state == WL_LINE
5245 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005246 && conceal_cursor_line(wp)
5247 && (int)wp->w_virtcol <= vcol + n_skip)
5248 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005249# ifdef FEAT_RIGHTLEFT
5250 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005251 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005252 else
5253# endif
5254 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005255 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005256 did_wcol = TRUE;
5257 }
5258#endif
5259
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 /* Don't override visual selection highlighting. */
5261 if (n_attr > 0
5262 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005263 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 char_attr = extra_attr;
5265
Bram Moolenaar81695252004-12-29 20:58:21 +00005266#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 /* XIM don't send preedit_start and preedit_end, but they send
5268 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5269 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005270 if (p_imst == IM_ON_THE_SPOT
5271 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005272 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 && (State & INSERT)
5274 && !p_imdisable
5275 && im_is_preediting()
5276 && draw_state == WL_LINE)
5277 {
5278 colnr_T tcol;
5279
5280 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005281 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 else
5283 tcol = preedit_end_col;
5284 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5285 {
5286 if (feedback_old_attr < 0)
5287 {
5288 feedback_col = 0;
5289 feedback_old_attr = char_attr;
5290 }
5291 char_attr = im_get_feedback_attr(feedback_col);
5292 if (char_attr < 0)
5293 char_attr = feedback_old_attr;
5294 feedback_col++;
5295 }
5296 else if (feedback_old_attr >= 0)
5297 {
5298 char_attr = feedback_old_attr;
5299 feedback_old_attr = -1;
5300 feedback_col = 0;
5301 }
5302 }
5303#endif
5304 /*
5305 * Handle the case where we are in column 0 but not on the first
5306 * character of the line and the user wants us to show us a
5307 * special character (via 'listchars' option "precedes:<char>".
5308 */
5309 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005310 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5312#ifdef FEAT_DIFF
5313 && filler_todo <= 0
5314#endif
5315 && draw_state > WL_NR
5316 && c != NUL)
5317 {
5318 c = lcs_prec;
5319 lcs_prec_todo = NUL;
5320#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005321 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5322 {
5323 /* Double-width character being overwritten by the "precedes"
5324 * character, need to fill up half the character. */
5325 c_extra = MB_FILLER_CHAR;
5326 n_extra = 1;
5327 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005328 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005331 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
5333 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005334 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005335 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
5337 else
5338 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5339#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005340 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
5342 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005343 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 n_attr3 = 1;
5345 }
5346 }
5347
5348 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005349 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005351 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005352#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005353 || did_line_attr == 1
5354#endif
5355 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005357#ifdef FEAT_SEARCH_EXTRA
5358 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005359
5360 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005361 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005362 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005363#endif
5364
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005365 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 * highlight match at end of line. If it's beyond the last
5367 * char on the screen, just overwrite that one (tricky!) Not
5368 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005369#ifdef FEAT_SEARCH_EXTRA
5370 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005371 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005372 prevcol_hl_flag = TRUE;
5373 else
5374 {
5375 cur = wp->w_match_head;
5376 while (cur != NULL)
5377 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005378 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005379 {
5380 prevcol_hl_flag = TRUE;
5381 break;
5382 }
5383 cur = cur->next;
5384 }
5385 }
5386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005388 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005389 && (VIsual_mode != Ctrl_V
5390 || lnum == VIsual.lnum
5391 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005392 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393#ifdef FEAT_SEARCH_EXTRA
5394 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005395 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005396# ifdef FEAT_SYN_HL
5397 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5398 && !(wp == curwin && VIsual_active))
5399# endif
5400# ifdef FEAT_DIFF
5401 && diff_hlf == (hlf_T)0
5402# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005403# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005404 && did_line_attr <= 1
5405# endif
5406 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407#endif
5408 ))
5409 {
5410 int n = 0;
5411
5412#ifdef FEAT_RIGHTLEFT
5413 if (wp->w_p_rl)
5414 {
5415 if (col < 0)
5416 n = 1;
5417 }
5418 else
5419#endif
5420 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005421 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 n = -1;
5423 }
5424 if (n != 0)
5425 {
5426 /* At the window boundary, highlight the last character
5427 * instead (better than nothing). */
5428 off += n;
5429 col += n;
5430 }
5431 else
5432 {
5433 /* Add a blank character to highlight. */
5434 ScreenLines[off] = ' ';
5435#ifdef FEAT_MBYTE
5436 if (enc_utf8)
5437 ScreenLinesUC[off] = 0;
5438#endif
5439 }
5440#ifdef FEAT_SEARCH_EXTRA
5441 if (area_attr == 0)
5442 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005443 /* Use attributes from match with highest priority among
5444 * 'search_hl' and the match list. */
5445 char_attr = search_hl.attr;
5446 cur = wp->w_match_head;
5447 shl_flag = FALSE;
5448 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005449 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005450 if (shl_flag == FALSE
5451 && ((cur != NULL
5452 && cur->priority > SEARCH_HL_PRIORITY)
5453 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005454 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005455 shl = &search_hl;
5456 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005457 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005458 else
5459 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005460 if ((ptr - line) - 1 == (long)shl->startcol
5461 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005462 char_attr = shl->attr;
5463 if (shl != &search_hl && cur != NULL)
5464 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
5467#endif
5468 ScreenAttrs[off] = char_attr;
5469#ifdef FEAT_RIGHTLEFT
5470 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005473 --off;
5474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 else
5476#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005479 ++off;
5480 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005481 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005482#ifdef FEAT_SYN_HL
5483 eol_hl_off = 1;
5484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
Bram Moolenaar91170f82006-05-05 21:15:17 +00005488 /*
5489 * At end of the text line.
5490 */
5491 if (c == NUL)
5492 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005493#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005494 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5495 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005496 {
5497 /* highlight last char after line */
5498 --col;
5499 --off;
5500 --vcol;
5501 }
5502
Bram Moolenaar1a384422010-07-14 19:53:30 +02005503 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005504 if (wp->w_p_wrap)
5505 v = wp->w_skipcol;
5506 else
5507 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005508
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005509 /* check if line ends before left margin */
5510 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005511 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005512#ifdef FEAT_CONCEAL
5513 /* Get rid of the boguscols now, we want to draw until the right
5514 * edge for 'cursorcolumn'. */
5515 col -= boguscols;
5516 boguscols = 0;
5517#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005518
5519 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005520 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005521
5522 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005523 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5524 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005525 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005526 && lnum != wp->w_cursor.lnum)
5527 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005528# ifdef FEAT_RIGHTLEFT
5529 && !wp->w_p_rl
5530# endif
5531 )
5532 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005533 int rightmost_vcol = 0;
5534 int i;
5535
5536 if (wp->w_p_cuc)
5537 rightmost_vcol = wp->w_virtcol;
5538 if (draw_color_col)
5539 /* determine rightmost colorcolumn to possibly draw */
5540 for (i = 0; color_cols[i] >= 0; ++i)
5541 if (rightmost_vcol < color_cols[i])
5542 rightmost_vcol = color_cols[i];
5543
Bram Moolenaar02631462017-09-22 15:20:32 +02005544 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005545 {
5546 ScreenLines[off] = ' ';
5547#ifdef FEAT_MBYTE
5548 if (enc_utf8)
5549 ScreenLinesUC[off] = 0;
5550#endif
5551 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005552 if (draw_color_col)
5553 draw_color_col = advance_color_col(VCOL_HLC,
5554 &color_cols);
5555
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005556 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005557 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005558 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005559 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005560 else
5561 ScreenAttrs[off++] = 0;
5562
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005563 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005564 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005565
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005566 ++vcol;
5567 }
5568 }
5569#endif
5570
Bram Moolenaar53f81742017-09-22 14:35:51 +02005571 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005572 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 row++;
5574
5575 /*
5576 * Update w_cline_height and w_cline_folded if the cursor line was
5577 * updated (saves a call to plines() later).
5578 */
5579 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5580 {
5581 curwin->w_cline_row = startrow;
5582 curwin->w_cline_height = row - startrow;
5583#ifdef FEAT_FOLDING
5584 curwin->w_cline_folded = FALSE;
5585#endif
5586 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5587 }
5588
5589 break;
5590 }
5591
5592 /* line continues beyond line end */
5593 if (lcs_ext
5594 && !wp->w_p_wrap
5595#ifdef FEAT_DIFF
5596 && filler_todo <= 0
5597#endif
5598 && (
5599#ifdef FEAT_RIGHTLEFT
5600 wp->w_p_rl ? col == 0 :
5601#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005602 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005604 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5606 {
5607 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005608 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609#ifdef FEAT_MBYTE
5610 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005611 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 {
5613 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005614 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005615 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 }
5617 else
5618 mb_utf8 = FALSE;
5619#endif
5620 }
5621
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005622#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005623 /* advance to the next 'colorcolumn' */
5624 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005625 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005626
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005627 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005628 * highlight the cursor position itself.
5629 * Also highlight the 'colorcolumn' if it is different than
5630 * 'cursorcolumn' */
5631 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005632 if (draw_state == WL_LINE && !lnum_in_visual_area
5633 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005634 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005635 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005636 && lnum != wp->w_cursor.lnum)
5637 {
5638 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005639 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005640 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005641 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005642 {
5643 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005644 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005645 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005646 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005647#endif
5648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 /*
5650 * Store character to be displayed.
5651 * Skip characters that are left of the screen for 'nowrap'.
5652 */
5653 vcol_prev = vcol;
5654 if (draw_state < WL_LINE || n_skip <= 0)
5655 {
5656 /*
5657 * Store the character.
5658 */
5659#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5660 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5661 {
5662 /* A double-wide character is: put first halve in left cell. */
5663 --off;
5664 --col;
5665 }
5666#endif
5667 ScreenLines[off] = c;
5668#ifdef FEAT_MBYTE
5669 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005670 {
5671 if ((mb_c & 0xff00) == 0x8e00)
5672 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 else if (enc_utf8)
5676 {
5677 if (mb_utf8)
5678 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005679 int i;
5680
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005682 if ((c & 0xff) == 0)
5683 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005684 for (i = 0; i < Screen_mco; ++i)
5685 {
5686 ScreenLinesC[i][off] = u8cc[i];
5687 if (u8cc[i] == 0)
5688 break;
5689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691 else
5692 ScreenLinesUC[off] = 0;
5693 }
5694 if (multi_attr)
5695 {
5696 ScreenAttrs[off] = multi_attr;
5697 multi_attr = 0;
5698 }
5699 else
5700#endif
5701 ScreenAttrs[off] = char_attr;
5702
5703#ifdef FEAT_MBYTE
5704 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5705 {
5706 /* Need to fill two screen columns. */
5707 ++off;
5708 ++col;
5709 if (enc_utf8)
5710 /* UTF-8: Put a 0 in the second screen char. */
5711 ScreenLines[off] = 0;
5712 else
5713 /* DBCS: Put second byte in the second screen char. */
5714 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005715 if (draw_state > WL_NR
5716#ifdef FEAT_DIFF
5717 && filler_todo <= 0
5718#endif
5719 )
5720 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 /* When "tocol" is halfway a character, set it to the end of
5722 * the character, otherwise highlighting won't stop. */
5723 if (tocol == vcol)
5724 ++tocol;
5725#ifdef FEAT_RIGHTLEFT
5726 if (wp->w_p_rl)
5727 {
5728 /* now it's time to backup one cell */
5729 --off;
5730 --col;
5731 }
5732#endif
5733 }
5734#endif
5735#ifdef FEAT_RIGHTLEFT
5736 if (wp->w_p_rl)
5737 {
5738 --off;
5739 --col;
5740 }
5741 else
5742#endif
5743 {
5744 ++off;
5745 ++col;
5746 }
5747 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005748#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005749 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005750 {
5751 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005752 ++vcol_off;
5753 if (n_extra > 0)
5754 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005755 if (wp->w_p_wrap)
5756 {
5757 /*
5758 * Special voodoo required if 'wrap' is on.
5759 *
5760 * Advance the column indicator to force the line
5761 * drawing to wrap early. This will make the line
5762 * take up the same screen space when parts are concealed,
5763 * so that cursor line computations aren't messed up.
5764 *
5765 * To avoid the fictitious advance of 'col' causing
5766 * trailing junk to be written out of the screen line
5767 * we are building, 'boguscols' keeps track of the number
5768 * of bad columns we have advanced.
5769 */
5770 if (n_extra > 0)
5771 {
5772 vcol += n_extra;
5773# ifdef FEAT_RIGHTLEFT
5774 if (wp->w_p_rl)
5775 {
5776 col -= n_extra;
5777 boguscols -= n_extra;
5778 }
5779 else
5780# endif
5781 {
5782 col += n_extra;
5783 boguscols += n_extra;
5784 }
5785 n_extra = 0;
5786 n_attr = 0;
5787 }
5788
5789
5790# ifdef FEAT_MBYTE
5791 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5792 {
5793 /* Need to fill two screen columns. */
5794# ifdef FEAT_RIGHTLEFT
5795 if (wp->w_p_rl)
5796 {
5797 --boguscols;
5798 --col;
5799 }
5800 else
5801# endif
5802 {
5803 ++boguscols;
5804 ++col;
5805 }
5806 }
5807# endif
5808
5809# ifdef FEAT_RIGHTLEFT
5810 if (wp->w_p_rl)
5811 {
5812 --boguscols;
5813 --col;
5814 }
5815 else
5816# endif
5817 {
5818 ++boguscols;
5819 ++col;
5820 }
5821 }
5822 else
5823 {
5824 if (n_extra > 0)
5825 {
5826 vcol += n_extra;
5827 n_extra = 0;
5828 n_attr = 0;
5829 }
5830 }
5831
5832 }
5833#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 else
5835 --n_skip;
5836
Bram Moolenaar64486672010-05-16 15:46:46 +02005837 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5838 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005839 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840#ifdef FEAT_DIFF
5841 && filler_todo <= 0
5842#endif
5843 )
5844 ++vcol;
5845
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005846#ifdef FEAT_SYN_HL
5847 if (vcol_save_attr >= 0)
5848 char_attr = vcol_save_attr;
5849#endif
5850
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 /* restore attributes after "predeces" in 'listchars' */
5852 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5853 char_attr = saved_attr3;
5854
5855 /* restore attributes after last 'listchars' or 'number' char */
5856 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5857 char_attr = saved_attr2;
5858
5859 /*
5860 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005861 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 */
5863 if ((
5864#ifdef FEAT_RIGHTLEFT
5865 wp->w_p_rl ? (col < 0) :
5866#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005867 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 && (*ptr != NUL
5869#ifdef FEAT_DIFF
5870 || filler_todo > 0
5871#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005872 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5874 )
5875 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005876#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005877 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005878 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005879 boguscols = 0;
5880#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005881 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005882 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 ++row;
5885 ++screen_row;
5886
5887 /* When not wrapping and finished diff lines, or when displayed
5888 * '$' and highlighting until last column, break here. */
5889 if ((!wp->w_p_wrap
5890#ifdef FEAT_DIFF
5891 && filler_todo <= 0
5892#endif
5893 ) || lcs_eol_one == -1)
5894 break;
5895
5896 /* When the window is too narrow draw all "@" lines. */
5897 if (draw_state != WL_LINE
5898#ifdef FEAT_DIFF
5899 && filler_todo <= 0
5900#endif
5901 )
5902 {
5903 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 row = endrow;
5906 }
5907
5908 /* When line got too long for screen break here. */
5909 if (row == endrow)
5910 {
5911 ++row;
5912 break;
5913 }
5914
5915 if (screen_cur_row == screen_row - 1
5916#ifdef FEAT_DIFF
5917 && filler_todo <= 0
5918#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005919 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 {
5921 /* Remember that the line wraps, used for modeless copy. */
5922 LineWraps[screen_row - 1] = TRUE;
5923
5924 /*
5925 * Special trick to make copy/paste of wrapped lines work with
5926 * xterm/screen: write an extra character beyond the end of
5927 * the line. This will work with all terminal types
5928 * (regardless of the xn,am settings).
5929 * Only do this on a fast tty.
5930 * Only do this if the cursor is on the current line
5931 * (something has been written in it).
5932 * Don't do this for the GUI.
5933 * Don't do this for double-width characters.
5934 * Don't do this for a window not at the right screen border.
5935 */
5936 if (p_tf
5937#ifdef FEAT_GUI
5938 && !gui.in_use
5939#endif
5940#ifdef FEAT_MBYTE
5941 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005942 && ((*mb_off2cells)(LineOffset[screen_row],
5943 LineOffset[screen_row] + screen_Columns)
5944 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005946 + (int)Columns - 2,
5947 LineOffset[screen_row] + screen_Columns)
5948 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949#endif
5950 )
5951 {
5952 /* First make sure we are at the end of the screen line,
5953 * then output the same character again to let the
5954 * terminal know about the wrap. If the terminal doesn't
5955 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005956 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 screen_char(LineOffset[screen_row - 1]
5958 + (unsigned)Columns - 1,
5959 screen_row - 1, (int)(Columns - 1));
5960
5961#ifdef FEAT_MBYTE
5962 /* When there is a multi-byte character, just output a
5963 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005964 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5965 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 out_char(' ');
5967 else
5968#endif
5969 out_char(ScreenLines[LineOffset[screen_row - 1]
5970 + (Columns - 1)]);
5971 /* force a redraw of the first char on the next line */
5972 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5973 screen_start(); /* don't know where cursor is now */
5974 }
5975 }
5976
5977 col = 0;
5978 off = (unsigned)(current_ScreenLine - ScreenLines);
5979#ifdef FEAT_RIGHTLEFT
5980 if (wp->w_p_rl)
5981 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005982 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 off += col;
5984 }
5985#endif
5986
5987 /* reset the drawing state for the start of a wrapped line */
5988 draw_state = WL_START;
5989 saved_n_extra = n_extra;
5990 saved_p_extra = p_extra;
5991 saved_c_extra = c_extra;
5992 saved_char_attr = char_attr;
5993 n_extra = 0;
5994 lcs_prec_todo = lcs_prec;
5995#ifdef FEAT_LINEBREAK
5996# ifdef FEAT_DIFF
5997 if (filler_todo <= 0)
5998# endif
5999 need_showbreak = TRUE;
6000#endif
6001#ifdef FEAT_DIFF
6002 --filler_todo;
6003 /* When the filler lines are actually below the last line of the
6004 * file, don't draw the line itself, break here. */
6005 if (filler_todo == 0 && wp->w_botfill)
6006 break;
6007#endif
6008 }
6009
6010 } /* for every character in the line */
6011
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006012#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006013 /* After an empty line check first word for capital. */
6014 if (*skipwhite(line) == NUL)
6015 {
6016 capcol_lnum = lnum + 1;
6017 cap_col = 0;
6018 }
6019#endif
6020
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006021 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 return row;
6023}
6024
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006025#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006026static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006027
6028/*
6029 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006030 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006031 */
6032 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006033comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006034{
6035 int i;
6036
6037 for (i = 0; i < Screen_mco; ++i)
6038 {
6039 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6040 return TRUE;
6041 if (ScreenLinesC[i][off_from] == 0)
6042 break;
6043 }
6044 return FALSE;
6045}
6046#endif
6047
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048/*
6049 * Check whether the given character needs redrawing:
6050 * - the (first byte of the) character is different
6051 * - the attributes are different
6052 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006053 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 */
6055 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006056char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057{
6058 if (cols > 0
6059 && ((ScreenLines[off_from] != ScreenLines[off_to]
6060 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6061
6062#ifdef FEAT_MBYTE
6063 || (enc_dbcs != 0
6064 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6065 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6066 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6067 : (cols > 1 && ScreenLines[off_from + 1]
6068 != ScreenLines[off_to + 1])))
6069 || (enc_utf8
6070 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6071 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006072 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006073 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6074 && ScreenLines[off_from + 1]
6075 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076#endif
6077 ))
6078 return TRUE;
6079 return FALSE;
6080}
6081
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006082#if defined(FEAT_TERMINAL) || defined(PROTO)
6083/*
6084 * Return the index in ScreenLines[] for the current screen line.
6085 */
6086 int
6087screen_get_current_line_off()
6088{
6089 return (int)(current_ScreenLine - ScreenLines);
6090}
6091#endif
6092
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093/*
6094 * Move one "cooked" screen line to the screen, but only the characters that
6095 * have actually changed. Handle insert/delete character.
6096 * "coloff" gives the first column on the screen for this line.
6097 * "endcol" gives the columns where valid characters are.
6098 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6099 * needs to be cleared, negative otherwise.
6100 * "rlflag" is TRUE in a rightleft window:
6101 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6102 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6103 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006104 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006105screen_line(
6106 int row,
6107 int coloff,
6108 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006109 int clear_width,
6110 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111{
6112 unsigned off_from;
6113 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006114#ifdef FEAT_MBYTE
6115 unsigned max_off_from;
6116 unsigned max_off_to;
6117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 int force = FALSE; /* force update rest of the line */
6121 int redraw_this /* bool: does character need redraw? */
6122#ifdef FEAT_GUI
6123 = TRUE /* For GUI when while-loop empty */
6124#endif
6125 ;
6126 int redraw_next; /* redraw_this for next character */
6127#ifdef FEAT_MBYTE
6128 int clear_next = FALSE;
6129 int char_cells; /* 1: normal char */
6130 /* 2: occupies two display cells */
6131# define CHAR_CELLS char_cells
6132#else
6133# define CHAR_CELLS 1
6134#endif
6135
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006136 /* Check for illegal row and col, just in case. */
6137 if (row >= Rows)
6138 row = Rows - 1;
6139 if (endcol > Columns)
6140 endcol = Columns;
6141
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142# ifdef FEAT_CLIPBOARD
6143 clip_may_clear_selection(row, row);
6144# endif
6145
6146 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6147 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006148#ifdef FEAT_MBYTE
6149 max_off_from = off_from + screen_Columns;
6150 max_off_to = LineOffset[row] + screen_Columns;
6151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152
6153#ifdef FEAT_RIGHTLEFT
6154 if (rlflag)
6155 {
6156 /* Clear rest first, because it's left of the text. */
6157 if (clear_width > 0)
6158 {
6159 while (col <= endcol && ScreenLines[off_to] == ' '
6160 && ScreenAttrs[off_to] == 0
6161# ifdef FEAT_MBYTE
6162 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6163# endif
6164 )
6165 {
6166 ++off_to;
6167 ++col;
6168 }
6169 if (col <= endcol)
6170 screen_fill(row, row + 1, col + coloff,
6171 endcol + coloff + 1, ' ', ' ', 0);
6172 }
6173 col = endcol + 1;
6174 off_to = LineOffset[row] + col + coloff;
6175 off_from += col;
6176 endcol = (clear_width > 0 ? clear_width : -clear_width);
6177 }
6178#endif /* FEAT_RIGHTLEFT */
6179
6180 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6181
6182 while (col < endcol)
6183 {
6184#ifdef FEAT_MBYTE
6185 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006186 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 else
6188 char_cells = 1;
6189#endif
6190
6191 redraw_this = redraw_next;
6192 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6193 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6194
6195#ifdef FEAT_GUI
6196 /* If the next character was bold, then redraw the current character to
6197 * remove any pixels that might have spilt over into us. This only
6198 * happens in the GUI.
6199 */
6200 if (redraw_next && gui.in_use)
6201 {
6202 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006203 if (hl > HL_ALL)
6204 hl = syn_attr2attr(hl);
6205 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 redraw_this = TRUE;
6207 }
6208#endif
6209
6210 if (redraw_this)
6211 {
6212 /*
6213 * Special handling when 'xs' termcap flag set (hpterm):
6214 * Attributes for characters are stored at the position where the
6215 * cursor is when writing the highlighting code. The
6216 * start-highlighting code must be written with the cursor on the
6217 * first highlighted character. The stop-highlighting code must
6218 * be written with the cursor just after the last highlighted
6219 * character.
6220 * Overwriting a character doesn't remove it's highlighting. Need
6221 * to clear the rest of the line, and force redrawing it
6222 * completely.
6223 */
6224 if ( p_wiv
6225 && !force
6226#ifdef FEAT_GUI
6227 && !gui.in_use
6228#endif
6229 && ScreenAttrs[off_to] != 0
6230 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6231 {
6232 /*
6233 * Need to remove highlighting attributes here.
6234 */
6235 windgoto(row, col + coloff);
6236 out_str(T_CE); /* clear rest of this screen line */
6237 screen_start(); /* don't know where cursor is now */
6238 force = TRUE; /* force redraw of rest of the line */
6239 redraw_next = TRUE; /* or else next char would miss out */
6240
6241 /*
6242 * If the previous character was highlighted, need to stop
6243 * highlighting at this character.
6244 */
6245 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6246 {
6247 screen_attr = ScreenAttrs[off_to - 1];
6248 term_windgoto(row, col + coloff);
6249 screen_stop_highlight();
6250 }
6251 else
6252 screen_attr = 0; /* highlighting has stopped */
6253 }
6254#ifdef FEAT_MBYTE
6255 if (enc_dbcs != 0)
6256 {
6257 /* Check if overwriting a double-byte with a single-byte or
6258 * the other way around requires another character to be
6259 * redrawn. For UTF-8 this isn't needed, because comparing
6260 * ScreenLinesUC[] is sufficient. */
6261 if (char_cells == 1
6262 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006263 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {
6265 /* Writing a single-cell character over a double-cell
6266 * character: need to redraw the next cell. */
6267 ScreenLines[off_to + 1] = 0;
6268 redraw_next = TRUE;
6269 }
6270 else if (char_cells == 2
6271 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006272 && (*mb_off2cells)(off_to, max_off_to) == 1
6273 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 {
6275 /* Writing the second half of a double-cell character over
6276 * a double-cell character: need to redraw the second
6277 * cell. */
6278 ScreenLines[off_to + 2] = 0;
6279 redraw_next = TRUE;
6280 }
6281
6282 if (enc_dbcs == DBCS_JPNU)
6283 ScreenLines2[off_to] = ScreenLines2[off_from];
6284 }
6285 /* When writing a single-width character over a double-width
6286 * character and at the end of the redrawn text, need to clear out
6287 * the right halve of the old character.
6288 * Also required when writing the right halve of a double-width
6289 * char over the left halve of an existing one. */
6290 if (has_mbyte && col + char_cells == endcol
6291 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006292 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006294 && (*mb_off2cells)(off_to, max_off_to) == 1
6295 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 clear_next = TRUE;
6297#endif
6298
6299 ScreenLines[off_to] = ScreenLines[off_from];
6300#ifdef FEAT_MBYTE
6301 if (enc_utf8)
6302 {
6303 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6304 if (ScreenLinesUC[off_from] != 0)
6305 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006306 int i;
6307
6308 for (i = 0; i < Screen_mco; ++i)
6309 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 }
6311 }
6312 if (char_cells == 2)
6313 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6314#endif
6315
6316#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006317 /* The bold trick makes a single column of pixels appear in the
6318 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 * character should be redrawn too. This happens for our own GUI
6320 * and for some xterms. */
6321 if (
6322# ifdef FEAT_GUI
6323 gui.in_use
6324# endif
6325# if defined(FEAT_GUI) && defined(UNIX)
6326 ||
6327# endif
6328# ifdef UNIX
6329 term_is_xterm
6330# endif
6331 )
6332 {
6333 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006334 if (hl > HL_ALL)
6335 hl = syn_attr2attr(hl);
6336 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 redraw_next = TRUE;
6338 }
6339#endif
6340 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6341#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006342 /* For simplicity set the attributes of second half of a
6343 * double-wide character equal to the first half. */
6344 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006346
6347 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 else
6350#endif
6351 screen_char(off_to, row, col + coloff);
6352 }
6353 else if ( p_wiv
6354#ifdef FEAT_GUI
6355 && !gui.in_use
6356#endif
6357 && col + coloff > 0)
6358 {
6359 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6360 {
6361 /*
6362 * Don't output stop-highlight when moving the cursor, it will
6363 * stop the highlighting when it should continue.
6364 */
6365 screen_attr = 0;
6366 }
6367 else if (screen_attr != 0)
6368 screen_stop_highlight();
6369 }
6370
6371 off_to += CHAR_CELLS;
6372 off_from += CHAR_CELLS;
6373 col += CHAR_CELLS;
6374 }
6375
6376#ifdef FEAT_MBYTE
6377 if (clear_next)
6378 {
6379 /* Clear the second half of a double-wide character of which the left
6380 * half was overwritten with a single-wide character. */
6381 ScreenLines[off_to] = ' ';
6382 if (enc_utf8)
6383 ScreenLinesUC[off_to] = 0;
6384 screen_char(off_to, row, col + coloff);
6385 }
6386#endif
6387
6388 if (clear_width > 0
6389#ifdef FEAT_RIGHTLEFT
6390 && !rlflag
6391#endif
6392 )
6393 {
6394#ifdef FEAT_GUI
6395 int startCol = col;
6396#endif
6397
6398 /* blank out the rest of the line */
6399 while (col < clear_width && ScreenLines[off_to] == ' '
6400 && ScreenAttrs[off_to] == 0
6401#ifdef FEAT_MBYTE
6402 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6403#endif
6404 )
6405 {
6406 ++off_to;
6407 ++col;
6408 }
6409 if (col < clear_width)
6410 {
6411#ifdef FEAT_GUI
6412 /*
6413 * In the GUI, clearing the rest of the line may leave pixels
6414 * behind if the first character cleared was bold. Some bold
6415 * fonts spill over the left. In this case we redraw the previous
6416 * character too. If we didn't skip any blanks above, then we
6417 * only redraw if the character wasn't already redrawn anyway.
6418 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006419 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 {
6421 hl = ScreenAttrs[off_to];
6422 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006423 {
6424 int prev_cells = 1;
6425# ifdef FEAT_MBYTE
6426 if (enc_utf8)
6427 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6428 * that its width is 2. */
6429 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6430 else if (enc_dbcs != 0)
6431 {
6432 /* find previous character by counting from first
6433 * column and get its width. */
6434 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006435 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006436
6437 while (off < off_to)
6438 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006439 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006440 off += prev_cells;
6441 }
6442 }
6443
6444 if (enc_dbcs != 0 && prev_cells > 1)
6445 screen_char_2(off_to - prev_cells, row,
6446 col + coloff - prev_cells);
6447 else
6448# endif
6449 screen_char(off_to - prev_cells, row,
6450 col + coloff - prev_cells);
6451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 }
6453#endif
6454 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6455 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 off_to += clear_width - col;
6457 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 }
6459 }
6460
6461 if (clear_width > 0)
6462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 /* For a window that's left of another, draw the separator char. */
6464 if (col + coloff < Columns)
6465 {
6466 int c;
6467
6468 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006469 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006470#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006471 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6472 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 || ScreenAttrs[off_to] != hl)
6475 {
6476 ScreenLines[off_to] = c;
6477 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006478#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 if (enc_utf8)
6480 {
6481 if (c >= 0x80)
6482 {
6483 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006484 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 }
6486 else
6487 ScreenLinesUC[off_to] = 0;
6488 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 screen_char(off_to, row, col + coloff);
6491 }
6492 }
6493 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 LineWraps[row] = FALSE;
6495 }
6496}
6497
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006498#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006500 * Mirror text "str" for right-left displaying.
6501 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006503 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006504rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
6506 char_u *p1, *p2;
6507 int t;
6508
6509 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6510 {
6511 t = *p1;
6512 *p1 = *p2;
6513 *p2 = t;
6514 }
6515}
6516#endif
6517
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518/*
6519 * mark all status lines for redraw; used after first :cd
6520 */
6521 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006522status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
6524 win_T *wp;
6525
Bram Moolenaar29323592016-07-24 22:04:11 +02006526 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 if (wp->w_status_height)
6528 {
6529 wp->w_redr_status = TRUE;
6530 redraw_later(VALID);
6531 }
6532}
6533
6534/*
6535 * mark all status lines of the current buffer for redraw
6536 */
6537 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006538status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539{
6540 win_T *wp;
6541
Bram Moolenaar29323592016-07-24 22:04:11 +02006542 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6544 {
6545 wp->w_redr_status = TRUE;
6546 redraw_later(VALID);
6547 }
6548}
6549
6550/*
6551 * Redraw all status lines that need to be redrawn.
6552 */
6553 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006554redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
6556 win_T *wp;
6557
Bram Moolenaar29323592016-07-24 22:04:11 +02006558 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006560 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006561 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006562 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564
Bram Moolenaar4033c552017-09-16 20:54:51 +02006565#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566/*
6567 * Redraw all status lines at the bottom of frame "frp".
6568 */
6569 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006570win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
6572 if (frp->fr_layout == FR_LEAF)
6573 frp->fr_win->w_redr_status = TRUE;
6574 else if (frp->fr_layout == FR_ROW)
6575 {
6576 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6577 win_redraw_last_status(frp);
6578 }
6579 else /* frp->fr_layout == FR_COL */
6580 {
6581 frp = frp->fr_child;
6582 while (frp->fr_next != NULL)
6583 frp = frp->fr_next;
6584 win_redraw_last_status(frp);
6585 }
6586}
6587#endif
6588
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589/*
6590 * Draw the verticap separator right of window "wp" starting with line "row".
6591 */
6592 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006593draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
6595 int hl;
6596 int c;
6597
6598 if (wp->w_vsep_width)
6599 {
6600 /* draw the vertical separator right of this window */
6601 c = fillchar_vsep(&hl);
6602 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6603 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6604 c, ' ', hl);
6605 }
6606}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607
6608#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006609static int status_match_len(expand_T *xp, char_u *s);
6610static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611
6612/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006613 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 */
6615 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006616status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617{
6618 int len = 0;
6619
6620#ifdef FEAT_MENU
6621 int emenu = (xp->xp_context == EXPAND_MENUS
6622 || xp->xp_context == EXPAND_MENUNAMES);
6623
6624 /* Check for menu separators - replace with '|'. */
6625 if (emenu && menu_is_separator(s))
6626 return 1;
6627#endif
6628
6629 while (*s != NUL)
6630 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006631 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006632 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006633 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 }
6635
6636 return len;
6637}
6638
6639/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006640 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006641 * These are backslashes used for escaping. Do show backslashes in help tags.
6642 */
6643 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006644skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006645{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006646 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006647#ifdef FEAT_MENU
6648 || ((xp->xp_context == EXPAND_MENUS
6649 || xp->xp_context == EXPAND_MENUNAMES)
6650 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6651#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006652 )
6653 {
6654#ifndef BACKSLASH_IN_FILENAME
6655 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6656 return 2;
6657#endif
6658 return 1;
6659 }
6660 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006661}
6662
6663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 * Show wildchar matches in the status line.
6665 * Show at least the "match" item.
6666 * We start at item 'first_match' in the list and show all matches that fit.
6667 *
6668 * If inversion is possible we use it. Else '=' characters are used.
6669 */
6670 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006671win_redr_status_matches(
6672 expand_T *xp,
6673 int num_matches,
6674 char_u **matches, /* list of matches */
6675 int match,
6676 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
6678#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6679 int row;
6680 char_u *buf;
6681 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006682 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 int fillchar;
6684 int attr;
6685 int i;
6686 int highlight = TRUE;
6687 char_u *selstart = NULL;
6688 int selstart_col = 0;
6689 char_u *selend = NULL;
6690 static int first_match = 0;
6691 int add_left = FALSE;
6692 char_u *s;
6693#ifdef FEAT_MENU
6694 int emenu;
6695#endif
6696#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6697 int l;
6698#endif
6699
6700 if (matches == NULL) /* interrupted completion? */
6701 return;
6702
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006703#ifdef FEAT_MBYTE
6704 if (has_mbyte)
6705 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6706 else
6707#endif
6708 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 if (buf == NULL)
6710 return;
6711
6712 if (match == -1) /* don't show match but original text */
6713 {
6714 match = 0;
6715 highlight = FALSE;
6716 }
6717 /* count 1 for the ending ">" */
6718 clen = status_match_len(xp, L_MATCH(match)) + 3;
6719 if (match == 0)
6720 first_match = 0;
6721 else if (match < first_match)
6722 {
6723 /* jumping left, as far as we can go */
6724 first_match = match;
6725 add_left = TRUE;
6726 }
6727 else
6728 {
6729 /* check if match fits on the screen */
6730 for (i = first_match; i < match; ++i)
6731 clen += status_match_len(xp, L_MATCH(i)) + 2;
6732 if (first_match > 0)
6733 clen += 2;
6734 /* jumping right, put match at the left */
6735 if ((long)clen > Columns)
6736 {
6737 first_match = match;
6738 /* if showing the last match, we can add some on the left */
6739 clen = 2;
6740 for (i = match; i < num_matches; ++i)
6741 {
6742 clen += status_match_len(xp, L_MATCH(i)) + 2;
6743 if ((long)clen >= Columns)
6744 break;
6745 }
6746 if (i == num_matches)
6747 add_left = TRUE;
6748 }
6749 }
6750 if (add_left)
6751 while (first_match > 0)
6752 {
6753 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6754 if ((long)clen >= Columns)
6755 break;
6756 --first_match;
6757 }
6758
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006759 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760
6761 if (first_match == 0)
6762 {
6763 *buf = NUL;
6764 len = 0;
6765 }
6766 else
6767 {
6768 STRCPY(buf, "< ");
6769 len = 2;
6770 }
6771 clen = len;
6772
6773 i = first_match;
6774 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6775 {
6776 if (i == match)
6777 {
6778 selstart = buf + len;
6779 selstart_col = clen;
6780 }
6781
6782 s = L_MATCH(i);
6783 /* Check for menu separators - replace with '|' */
6784#ifdef FEAT_MENU
6785 emenu = (xp->xp_context == EXPAND_MENUS
6786 || xp->xp_context == EXPAND_MENUNAMES);
6787 if (emenu && menu_is_separator(s))
6788 {
6789 STRCPY(buf + len, transchar('|'));
6790 l = (int)STRLEN(buf + len);
6791 len += l;
6792 clen += l;
6793 }
6794 else
6795#endif
6796 for ( ; *s != NUL; ++s)
6797 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006798 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 clen += ptr2cells(s);
6800#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006801 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 {
6803 STRNCPY(buf + len, s, l);
6804 s += l - 1;
6805 len += l;
6806 }
6807 else
6808#endif
6809 {
6810 STRCPY(buf + len, transchar_byte(*s));
6811 len += (int)STRLEN(buf + len);
6812 }
6813 }
6814 if (i == match)
6815 selend = buf + len;
6816
6817 *(buf + len++) = ' ';
6818 *(buf + len++) = ' ';
6819 clen += 2;
6820 if (++i == num_matches)
6821 break;
6822 }
6823
6824 if (i != num_matches)
6825 {
6826 *(buf + len++) = '>';
6827 ++clen;
6828 }
6829
6830 buf[len] = NUL;
6831
6832 row = cmdline_row - 1;
6833 if (row >= 0)
6834 {
6835 if (wild_menu_showing == 0)
6836 {
6837 if (msg_scrolled > 0)
6838 {
6839 /* Put the wildmenu just above the command line. If there is
6840 * no room, scroll the screen one line up. */
6841 if (cmdline_row == Rows - 1)
6842 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006843 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 ++msg_scrolled;
6845 }
6846 else
6847 {
6848 ++cmdline_row;
6849 ++row;
6850 }
6851 wild_menu_showing = WM_SCROLLED;
6852 }
6853 else
6854 {
6855 /* Create status line if needed by setting 'laststatus' to 2.
6856 * Set 'winminheight' to zero to avoid that the window is
6857 * resized. */
6858 if (lastwin->w_status_height == 0)
6859 {
6860 save_p_ls = p_ls;
6861 save_p_wmh = p_wmh;
6862 p_ls = 2;
6863 p_wmh = 0;
6864 last_status(FALSE);
6865 }
6866 wild_menu_showing = WM_SHOWN;
6867 }
6868 }
6869
6870 screen_puts(buf, row, 0, attr);
6871 if (selstart != NULL && highlight)
6872 {
6873 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006874 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
6876
6877 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6878 }
6879
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 vim_free(buf);
6882}
6883#endif
6884
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885/*
6886 * Redraw the status line of window wp.
6887 *
6888 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006889 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6890 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006892 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006893win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894{
6895 int row;
6896 char_u *p;
6897 int len;
6898 int fillchar;
6899 int attr;
6900 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006901 static int busy = FALSE;
6902
6903 /* It's possible to get here recursively when 'statusline' (indirectly)
6904 * invokes ":redrawstatus". Simply ignore the call then. */
6905 if (busy)
6906 return;
6907 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908
6909 wp->w_redr_status = FALSE;
6910 if (wp->w_status_height == 0)
6911 {
6912 /* no status line, can only be last window */
6913 redraw_cmdline = TRUE;
6914 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006915 else if (!redrawing()
6916#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006917 // don't update status line when popup menu is visible and may be
6918 // drawn over it, unless it will be redrawn later
6919 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006920#endif
6921 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 {
6923 /* Don't redraw right now, do it later. */
6924 wp->w_redr_status = TRUE;
6925 }
6926#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006927 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {
6929 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006930 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
6932#endif
6933 else
6934 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006935 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006937 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 p = NameBuff;
6939 len = (int)STRLEN(p);
6940
Bram Moolenaard85f2712017-07-28 21:51:57 +02006941 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942#ifdef FEAT_QUICKFIX
6943 || wp->w_p_pvw
6944#endif
6945 || bufIsChanged(wp->w_buffer)
6946 || wp->w_buffer->b_p_ro)
6947 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006948 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006950 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 len += (int)STRLEN(p + len);
6952 }
6953#ifdef FEAT_QUICKFIX
6954 if (wp->w_p_pvw)
6955 {
6956 STRCPY(p + len, _("[Preview]"));
6957 len += (int)STRLEN(p + len);
6958 }
6959#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006960 if (bufIsChanged(wp->w_buffer)
6961#ifdef FEAT_TERMINAL
6962 && !bt_terminal(wp->w_buffer)
6963#endif
6964 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {
6966 STRCPY(p + len, "[+]");
6967 len += 3;
6968 }
6969 if (wp->w_buffer->b_p_ro)
6970 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006971 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006972 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 }
6974
Bram Moolenaar02631462017-09-22 15:20:32 +02006975 this_ru_col = ru_col - (Columns - wp->w_width);
6976 if (this_ru_col < (wp->w_width + 1) / 2)
6977 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 if (this_ru_col <= 1)
6979 {
6980 p = (char_u *)"<"; /* No room for file name! */
6981 len = 1;
6982 }
6983 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984#ifdef FEAT_MBYTE
6985 if (has_mbyte)
6986 {
6987 int clen = 0, i;
6988
6989 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006990 clen = mb_string2cells(p, -1);
6991
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 /* Find first character that will fit.
6993 * Going from start to end is much faster for DBCS. */
6994 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006995 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 clen -= (*mb_ptr2cells)(p + i);
6997 len = clen;
6998 if (i > 0)
6999 {
7000 p = p + i - 1;
7001 *p = '<';
7002 ++len;
7003 }
7004
7005 }
7006 else
7007#endif
7008 if (len > this_ru_col - 1)
7009 {
7010 p += len - (this_ru_col - 1);
7011 *p = '<';
7012 len = this_ru_col - 1;
7013 }
7014
7015 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007016 screen_puts(p, row, wp->w_wincol, attr);
7017 screen_fill(row, row + 1, len + wp->w_wincol,
7018 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007020 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7022 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007023 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
7025#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007026 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027#endif
7028 }
7029
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 /*
7031 * May need to draw the character below the vertical separator.
7032 */
7033 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7034 {
7035 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007036 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 else
7038 fillchar = fillchar_vsep(&attr);
7039 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7040 attr);
7041 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007042 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043}
7044
Bram Moolenaar238a5642006-02-21 22:12:05 +00007045#ifdef FEAT_STL_OPT
7046/*
7047 * Redraw the status line according to 'statusline' and take care of any
7048 * errors encountered.
7049 */
7050 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007051redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007052{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007053 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007054 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007055
7056 /* When called recursively return. This can happen when the statusline
7057 * contains an expression that triggers a redraw. */
7058 if (entered)
7059 return;
7060 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007061
Bram Moolenaara742e082016-04-05 21:10:38 +02007062 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007063 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007064 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007065 {
7066 /* When there is an error disable the statusline, otherwise the
7067 * display is messed up with errors and a redraw triggers the problem
7068 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007069 set_string_option_direct((char_u *)"statusline", -1,
7070 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007071 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007072 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007073 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007074 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007075}
7076#endif
7077
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078/*
7079 * Return TRUE if the status line of window "wp" is connected to the status
7080 * line of the window right of it. If not, then it's a vertical separator.
7081 * Only call if (wp->w_vsep_width != 0).
7082 */
7083 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007084stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085{
7086 frame_T *fr;
7087
7088 fr = wp->w_frame;
7089 while (fr->fr_parent != NULL)
7090 {
7091 if (fr->fr_parent->fr_layout == FR_COL)
7092 {
7093 if (fr->fr_next != NULL)
7094 break;
7095 }
7096 else
7097 {
7098 if (fr->fr_next != NULL)
7099 return TRUE;
7100 }
7101 fr = fr->fr_parent;
7102 }
7103 return FALSE;
7104}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107/*
7108 * Get the value to show for the language mappings, active 'keymap'.
7109 */
7110 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007111get_keymap_str(
7112 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007113 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007114 char_u *buf, /* buffer for the result */
7115 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116{
7117 char_u *p;
7118
7119 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7120 return FALSE;
7121
7122 {
7123#ifdef FEAT_EVAL
7124 buf_T *old_curbuf = curbuf;
7125 win_T *old_curwin = curwin;
7126 char_u *s;
7127
7128 curbuf = wp->w_buffer;
7129 curwin = wp;
7130 STRCPY(buf, "b:keymap_name"); /* must be writable */
7131 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007132 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 --emsg_skip;
7134 curbuf = old_curbuf;
7135 curwin = old_curwin;
7136 if (p == NULL || *p == NUL)
7137#endif
7138 {
7139#ifdef FEAT_KEYMAP
7140 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7141 p = wp->w_buffer->b_p_keymap;
7142 else
7143#endif
7144 p = (char_u *)"lang";
7145 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007146 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 buf[0] = NUL;
7148#ifdef FEAT_EVAL
7149 vim_free(s);
7150#endif
7151 }
7152 return buf[0] != NUL;
7153}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
7155#if defined(FEAT_STL_OPT) || defined(PROTO)
7156/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007157 * Redraw the status line or ruler of window "wp".
7158 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 */
7160 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007161win_redr_custom(
7162 win_T *wp,
7163 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007165 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 int attr;
7167 int curattr;
7168 int row;
7169 int col = 0;
7170 int maxwidth;
7171 int width;
7172 int n;
7173 int len;
7174 int fillchar;
7175 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007176 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007178 struct stl_hlrec hltab[STL_MAX_ITEM];
7179 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007180 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007181 win_T *ewp;
7182 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
Bram Moolenaar1d633412013-12-11 15:52:01 +01007184 /* There is a tiny chance that this gets called recursively: When
7185 * redrawing a status line triggers redrawing the ruler or tabline.
7186 * Avoid trouble by not allowing recursion. */
7187 if (entered)
7188 return;
7189 entered = TRUE;
7190
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007192 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007194 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007195 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007196 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007197 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007198 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007199 maxwidth = Columns;
7200# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007201 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007202# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007204 else
7205 {
7206 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007207 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007208 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007209
7210 if (draw_ruler)
7211 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007212 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007213 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007214 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007215 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007216 if (*++stl == '-')
7217 stl++;
7218 if (atoi((char *)stl))
7219 while (VIM_ISDIGIT(*stl))
7220 stl++;
7221 if (*stl++ != '(')
7222 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007223 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007224 col = ru_col - (Columns - wp->w_width);
7225 if (col < (wp->w_width + 1) / 2)
7226 col = (wp->w_width + 1) / 2;
7227 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007228 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007229 {
7230 row = Rows - 1;
7231 --maxwidth; /* writing in last column may cause scrolling */
7232 fillchar = ' ';
7233 attr = 0;
7234 }
7235
7236# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007237 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007238# endif
7239 }
7240 else
7241 {
7242 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007243 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007244 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007245 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007246# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007247 use_sandbox = was_set_insecurely((char_u *)"statusline",
7248 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007249# endif
7250 }
7251
Bram Moolenaar53f81742017-09-22 14:35:51 +02007252 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007253 }
7254
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007256 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257
Bram Moolenaar61452852011-02-01 18:01:11 +01007258 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7259 * the cursor away and back. */
7260 ewp = wp == NULL ? curwin : wp;
7261 p_crb_save = ewp->w_p_crb;
7262 ewp->w_p_crb = FALSE;
7263
Bram Moolenaar362f3562009-11-03 16:20:34 +00007264 /* Make a copy, because the statusline may include a function call that
7265 * might change the option value and free the memory. */
7266 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007267 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007268 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007269 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007270 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007271 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007273 /* Make all characters printable. */
7274 p = transstr(buf);
7275 if (p != NULL)
7276 {
7277 vim_strncpy(buf, p, sizeof(buf) - 1);
7278 vim_free(p);
7279 }
7280
7281 /* fill up with "fillchar" */
7282 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007283 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {
7285#ifdef FEAT_MBYTE
7286 len += (*mb_char2bytes)(fillchar, buf + len);
7287#else
7288 buf[len++] = fillchar;
7289#endif
7290 ++width;
7291 }
7292 buf[len] = NUL;
7293
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007294 /*
7295 * Draw each snippet with the specified highlighting.
7296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 curattr = attr;
7298 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007299 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007301 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 screen_puts_len(p, len, row, col, curattr);
7303 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007304 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007306 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007308 else if (hltab[n].userhl < 0)
7309 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007310#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007311 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7312 && wp->w_status_height != 0)
7313 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007314 else if (wp != NULL && bt_terminal(wp->w_buffer)
7315 && wp->w_status_height != 0)
7316 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007317#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007318 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007319 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007321 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 }
7323 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007324
7325 if (wp == NULL)
7326 {
7327 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7328 col = 0;
7329 len = 0;
7330 p = buf;
7331 fillchar = 0;
7332 for (n = 0; tabtab[n].start != NULL; n++)
7333 {
7334 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7335 while (col < len)
7336 TabPageIdxs[col++] = fillchar;
7337 p = tabtab[n].start;
7338 fillchar = tabtab[n].userhl;
7339 }
7340 while (col < Columns)
7341 TabPageIdxs[col++] = fillchar;
7342 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007343
7344theend:
7345 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346}
7347
7348#endif /* FEAT_STL_OPT */
7349
7350/*
7351 * Output a single character directly to the screen and update ScreenLines.
7352 */
7353 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007354screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 char_u buf[MB_MAXBYTES + 1];
7357
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007358#ifdef FEAT_MBYTE
7359 if (has_mbyte)
7360 buf[(*mb_char2bytes)(c, buf)] = NUL;
7361 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007363 {
7364 buf[0] = c;
7365 buf[1] = NUL;
7366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 screen_puts(buf, row, col, attr);
7368}
7369
7370/*
7371 * Get a single character directly from ScreenLines into "bytes[]".
7372 * Also return its attribute in *attrp;
7373 */
7374 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007375screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376{
7377 unsigned off;
7378
7379 /* safety check */
7380 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7381 {
7382 off = LineOffset[row] + col;
7383 *attrp = ScreenAttrs[off];
7384 bytes[0] = ScreenLines[off];
7385 bytes[1] = NUL;
7386
7387#ifdef FEAT_MBYTE
7388 if (enc_utf8 && ScreenLinesUC[off] != 0)
7389 bytes[utfc_char2bytes(off, bytes)] = NUL;
7390 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7391 {
7392 bytes[0] = ScreenLines[off];
7393 bytes[1] = ScreenLines2[off];
7394 bytes[2] = NUL;
7395 }
7396 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7397 {
7398 bytes[1] = ScreenLines[off + 1];
7399 bytes[2] = NUL;
7400 }
7401#endif
7402 }
7403}
7404
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007405#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007406static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007407
7408/*
7409 * Return TRUE if composing characters for screen posn "off" differs from
7410 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007411 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007412 */
7413 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007414screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007415{
7416 int i;
7417
7418 for (i = 0; i < Screen_mco; ++i)
7419 {
7420 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7421 return TRUE;
7422 if (u8cc[i] == 0)
7423 break;
7424 }
7425 return FALSE;
7426}
7427#endif
7428
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429/*
7430 * Put string '*text' on the screen at position 'row' and 'col', with
7431 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7432 * Note: only outputs within one row, message is truncated at screen boundary!
7433 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7434 */
7435 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007436screen_puts(
7437 char_u *text,
7438 int row,
7439 int col,
7440 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
7442 screen_puts_len(text, -1, row, col, attr);
7443}
7444
7445/*
7446 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7447 * a NUL.
7448 */
7449 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007450screen_puts_len(
7451 char_u *text,
7452 int textlen,
7453 int row,
7454 int col,
7455 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456{
7457 unsigned off;
7458 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007459 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 int c;
7461#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007462 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 int mbyte_blen = 1;
7464 int mbyte_cells = 1;
7465 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007466 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 int clear_next_cell = FALSE;
7468# ifdef FEAT_ARABIC
7469 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007470 int pc, nc, nc1;
7471 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472# endif
7473#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007474#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7475 int force_redraw_this;
7476 int force_redraw_next = FALSE;
7477#endif
7478 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479
7480 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7481 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007482 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483
Bram Moolenaarc236c162008-07-13 17:41:49 +00007484#ifdef FEAT_MBYTE
7485 /* When drawing over the right halve of a double-wide char clear out the
7486 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007487 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007488# ifdef FEAT_GUI
7489 && !gui.in_use
7490# endif
7491 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007492 {
7493 ScreenLines[off - 1] = ' ';
7494 ScreenAttrs[off - 1] = 0;
7495 if (enc_utf8)
7496 {
7497 ScreenLinesUC[off - 1] = 0;
7498 ScreenLinesC[0][off - 1] = 0;
7499 }
7500 /* redraw the previous cell, make it empty */
7501 screen_char(off - 1, row, col - 1);
7502 /* force the cell at "col" to be redrawn */
7503 force_redraw_next = TRUE;
7504 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007505#endif
7506
Bram Moolenaar367329b2007-08-30 11:53:22 +00007507#ifdef FEAT_MBYTE
7508 max_off = LineOffset[row] + screen_Columns;
7509#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007510 while (col < screen_Columns
7511 && (len < 0 || (int)(ptr - text) < len)
7512 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 {
7514 c = *ptr;
7515#ifdef FEAT_MBYTE
7516 /* check if this is the first byte of a multibyte */
7517 if (has_mbyte)
7518 {
7519 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007520 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007522 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7524 mbyte_cells = 1;
7525 else if (enc_dbcs != 0)
7526 mbyte_cells = mbyte_blen;
7527 else /* enc_utf8 */
7528 {
7529 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007530 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 (int)((text + len) - ptr));
7532 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007533 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007535# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 /* Non-BMP character: display as ? or fullwidth ?. */
7537 if (u8c >= 0x10000)
7538 {
7539 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7540 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007541 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007543# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544# ifdef FEAT_ARABIC
7545 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7546 {
7547 /* Do Arabic shaping. */
7548 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7549 {
7550 /* Past end of string to be displayed. */
7551 nc = NUL;
7552 nc1 = NUL;
7553 }
7554 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007555 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007556 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7557 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007558 nc1 = pcc[0];
7559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 pc = prev_c;
7561 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007562 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 }
7564 else
7565 prev_c = u8c;
7566# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007567 if (col + mbyte_cells > screen_Columns)
7568 {
7569 /* Only 1 cell left, but character requires 2 cells:
7570 * display a '>' in the last column to avoid wrapping. */
7571 c = '>';
7572 mbyte_cells = 1;
7573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 }
7575 }
7576#endif
7577
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007578#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7579 force_redraw_this = force_redraw_next;
7580 force_redraw_next = FALSE;
7581#endif
7582
7583 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584#ifdef FEAT_MBYTE
7585 || (mbyte_cells == 2
7586 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7587 || (enc_dbcs == DBCS_JPNU
7588 && c == 0x8e
7589 && ScreenLines2[off] != ptr[1])
7590 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007591 && (ScreenLinesUC[off] !=
7592 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7593 || (ScreenLinesUC[off] != 0
7594 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595#endif
7596 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007597 || exmode_active;
7598
7599 if (need_redraw
7600#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7601 || force_redraw_this
7602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 )
7604 {
7605#if defined(FEAT_GUI) || defined(UNIX)
7606 /* The bold trick makes a single row of pixels appear in the next
7607 * character. When a bold character is removed, the next
7608 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007609 * and for some xterms. */
7610 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611# ifdef FEAT_GUI
7612 gui.in_use
7613# endif
7614# if defined(FEAT_GUI) && defined(UNIX)
7615 ||
7616# endif
7617# ifdef UNIX
7618 term_is_xterm
7619# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007620 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007622 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007624 if (n > HL_ALL)
7625 n = syn_attr2attr(n);
7626 if (n & HL_BOLD)
7627 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 }
7629#endif
7630#ifdef FEAT_MBYTE
7631 /* When at the end of the text and overwriting a two-cell
7632 * character with a one-cell character, need to clear the next
7633 * cell. Also when overwriting the left halve of a two-cell char
7634 * with the right halve of a two-cell char. Do this only once
7635 * (mb_off2cells() may return 2 on the right halve). */
7636 if (clear_next_cell)
7637 clear_next_cell = FALSE;
7638 else if (has_mbyte
7639 && (len < 0 ? ptr[mbyte_blen] == NUL
7640 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007641 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007643 && (*mb_off2cells)(off, max_off) == 1
7644 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 clear_next_cell = TRUE;
7646
7647 /* Make sure we never leave a second byte of a double-byte behind,
7648 * it confuses mb_off2cells(). */
7649 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007650 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007652 && (*mb_off2cells)(off, max_off) == 1
7653 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 ScreenLines[off + mbyte_blen] = 0;
7655#endif
7656 ScreenLines[off] = c;
7657 ScreenAttrs[off] = attr;
7658#ifdef FEAT_MBYTE
7659 if (enc_utf8)
7660 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007661 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 ScreenLinesUC[off] = 0;
7663 else
7664 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007665 int i;
7666
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007668 for (i = 0; i < Screen_mco; ++i)
7669 {
7670 ScreenLinesC[i][off] = u8cc[i];
7671 if (u8cc[i] == 0)
7672 break;
7673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 }
7675 if (mbyte_cells == 2)
7676 {
7677 ScreenLines[off + 1] = 0;
7678 ScreenAttrs[off + 1] = attr;
7679 }
7680 screen_char(off, row, col);
7681 }
7682 else if (mbyte_cells == 2)
7683 {
7684 ScreenLines[off + 1] = ptr[1];
7685 ScreenAttrs[off + 1] = attr;
7686 screen_char_2(off, row, col);
7687 }
7688 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7689 {
7690 ScreenLines2[off] = ptr[1];
7691 screen_char(off, row, col);
7692 }
7693 else
7694#endif
7695 screen_char(off, row, col);
7696 }
7697#ifdef FEAT_MBYTE
7698 if (has_mbyte)
7699 {
7700 off += mbyte_cells;
7701 col += mbyte_cells;
7702 ptr += mbyte_blen;
7703 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007704 {
7705 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007707 len = -1;
7708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 }
7710 else
7711#endif
7712 {
7713 ++off;
7714 ++col;
7715 ++ptr;
7716 }
7717 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007718
7719#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7720 /* If we detected the next character needs to be redrawn, but the text
7721 * doesn't extend up to there, update the character here. */
7722 if (force_redraw_next && col < screen_Columns)
7723 {
7724# ifdef FEAT_MBYTE
7725 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7726 screen_char_2(off, row, col);
7727 else
7728# endif
7729 screen_char(off, row, col);
7730 }
7731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732}
7733
7734#ifdef FEAT_SEARCH_EXTRA
7735/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007736 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 */
7738 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007739start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740{
7741 if (p_hls && !no_hlsearch)
7742 {
7743 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007744 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007745# ifdef FEAT_RELTIME
7746 /* Set the time limit to 'redrawtime'. */
7747 profile_setlimit(p_rdt, &search_hl.tm);
7748# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 }
7750}
7751
7752/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007753 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 */
7755 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007756end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757{
7758 if (search_hl.rm.regprog != NULL)
7759 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007760 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 search_hl.rm.regprog = NULL;
7762 }
7763}
7764
7765/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007766 * Init for calling prepare_search_hl().
7767 */
7768 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007769init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007770{
7771 matchitem_T *cur;
7772
7773 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7774 * match */
7775 cur = wp->w_match_head;
7776 while (cur != NULL)
7777 {
7778 cur->hl.rm = cur->match;
7779 if (cur->hlg_id == 0)
7780 cur->hl.attr = 0;
7781 else
7782 cur->hl.attr = syn_id2attr(cur->hlg_id);
7783 cur->hl.buf = wp->w_buffer;
7784 cur->hl.lnum = 0;
7785 cur->hl.first_lnum = 0;
7786# ifdef FEAT_RELTIME
7787 /* Set the time limit to 'redrawtime'. */
7788 profile_setlimit(p_rdt, &(cur->hl.tm));
7789# endif
7790 cur = cur->next;
7791 }
7792 search_hl.buf = wp->w_buffer;
7793 search_hl.lnum = 0;
7794 search_hl.first_lnum = 0;
7795 /* time limit is set at the toplevel, for all windows */
7796}
7797
7798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 * Advance to the match in window "wp" line "lnum" or past it.
7800 */
7801 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007802prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007804 matchitem_T *cur; /* points to the match list */
7805 match_T *shl; /* points to search_hl or a match */
7806 int shl_flag; /* flag to indicate whether search_hl
7807 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007808 int pos_inprogress; /* marks that position match search is
7809 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 int n;
7811
7812 /*
7813 * When using a multi-line pattern, start searching at the top
7814 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007815 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007817 cur = wp->w_match_head;
7818 shl_flag = FALSE;
7819 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007821 if (shl_flag == FALSE)
7822 {
7823 shl = &search_hl;
7824 shl_flag = TRUE;
7825 }
7826 else
7827 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 if (shl->rm.regprog != NULL
7829 && shl->lnum == 0
7830 && re_multiline(shl->rm.regprog))
7831 {
7832 if (shl->first_lnum == 0)
7833 {
7834# ifdef FEAT_FOLDING
7835 for (shl->first_lnum = lnum;
7836 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7837 if (hasFoldingWin(wp, shl->first_lnum - 1,
7838 NULL, NULL, TRUE, NULL))
7839 break;
7840# else
7841 shl->first_lnum = wp->w_topline;
7842# endif
7843 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007844 if (cur != NULL)
7845 cur->pos.cur = 0;
7846 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007848 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7849 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007851 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7852 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007853 pos_inprogress = cur == NULL || cur->pos.cur == 0
7854 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 if (shl->lnum != 0)
7856 {
7857 shl->first_lnum = shl->lnum
7858 + shl->rm.endpos[0].lnum
7859 - shl->rm.startpos[0].lnum;
7860 n = shl->rm.endpos[0].col;
7861 }
7862 else
7863 {
7864 ++shl->first_lnum;
7865 n = 0;
7866 }
7867 }
7868 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007869 if (shl != &search_hl && cur != NULL)
7870 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 }
7872}
7873
7874/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007875 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 * Uses shl->buf.
7877 * Sets shl->lnum and shl->rm contents.
7878 * Note: Assumes a previous match is always before "lnum", unless
7879 * shl->lnum is zero.
7880 * Careful: Any pointers for buffer lines will become invalid.
7881 */
7882 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007883next_search_hl(
7884 win_T *win,
7885 match_T *shl, /* points to search_hl or a match */
7886 linenr_T lnum,
7887 colnr_T mincol, /* minimal column for a match */
7888 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
7890 linenr_T l;
7891 colnr_T matchcol;
7892 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007893 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007895 // for :{range}s/pat only highlight inside the range
7896 if (lnum < search_first_line || lnum > search_last_line)
7897 {
7898 shl->lnum = 0;
7899 return;
7900 }
7901
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 if (shl->lnum != 0)
7903 {
7904 /* Check for three situations:
7905 * 1. If the "lnum" is below a previous match, start a new search.
7906 * 2. If the previous match includes "mincol", use it.
7907 * 3. Continue after the previous match.
7908 */
7909 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7910 if (lnum > l)
7911 shl->lnum = 0;
7912 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7913 return;
7914 }
7915
7916 /*
7917 * Repeat searching for a match until one is found that includes "mincol"
7918 * or none is found in this line.
7919 */
7920 called_emsg = FALSE;
7921 for (;;)
7922 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007923#ifdef FEAT_RELTIME
7924 /* Stop searching after passing the time limit. */
7925 if (profile_passed_limit(&(shl->tm)))
7926 {
7927 shl->lnum = 0; /* no match found in time */
7928 break;
7929 }
7930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 /* Three situations:
7932 * 1. No useful previous match: search from start of line.
7933 * 2. Not Vi compatible or empty match: continue at next character.
7934 * Break the loop if this is beyond the end of the line.
7935 * 3. Vi compatible searching: continue at end of previous match.
7936 */
7937 if (shl->lnum == 0)
7938 matchcol = 0;
7939 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7940 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007941 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007943 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007944
7945 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007946 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007947 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007949 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 shl->lnum = 0;
7951 break;
7952 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007953#ifdef FEAT_MBYTE
7954 if (has_mbyte)
7955 matchcol += mb_ptr2len(ml);
7956 else
7957#endif
7958 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 }
7960 else
7961 matchcol = shl->rm.endpos[0].col;
7962
7963 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007964 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007966 /* Remember whether shl->rm is using a copy of the regprog in
7967 * cur->match. */
7968 int regprog_is_copy = (shl != &search_hl && cur != NULL
7969 && shl == &cur->hl
7970 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007971 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007972
Bram Moolenaarb3414592014-06-17 17:48:32 +02007973 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7974 matchcol,
7975#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007976 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007977#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007978 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007979#endif
7980 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007981 /* Copy the regprog, in case it got freed and recompiled. */
7982 if (regprog_is_copy)
7983 cur->match.regprog = cur->hl.rm.regprog;
7984
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007985 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007986 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007987 /* Error while handling regexp: stop using this regexp. */
7988 if (shl == &search_hl)
7989 {
7990 /* don't free regprog in the match list, it's a copy */
7991 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007992 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007993 }
7994 shl->rm.regprog = NULL;
7995 shl->lnum = 0;
7996 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7997 message */
7998 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007999 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008000 }
8001 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008002 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008003 else
8004 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 if (nmatched == 0)
8006 {
8007 shl->lnum = 0; /* no match found */
8008 break;
8009 }
8010 if (shl->rm.startpos[0].lnum > 0
8011 || shl->rm.startpos[0].col >= mincol
8012 || nmatched > 1
8013 || shl->rm.endpos[0].col > mincol)
8014 {
8015 shl->lnum += shl->rm.startpos[0].lnum;
8016 break; /* useful match found */
8017 }
8018 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008019
8020 // Restore called_emsg for assert_fails().
8021 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023
Bram Moolenaar85077472016-10-16 14:35:48 +02008024/*
8025 * If there is a match fill "shl" and return one.
8026 * Return zero otherwise.
8027 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008028 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008029next_search_hl_pos(
8030 match_T *shl, /* points to a match */
8031 linenr_T lnum,
8032 posmatch_T *posmatch, /* match positions */
8033 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008034{
8035 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008036 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008037
Bram Moolenaarb3414592014-06-17 17:48:32 +02008038 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8039 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008040 llpos_T *pos = &posmatch->pos[i];
8041
8042 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008043 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008044 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008045 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008046 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008047 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008048 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008049 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008050 /* if this match comes before the one at "found" then swap
8051 * them */
8052 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008053 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008054 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008055
Bram Moolenaar85077472016-10-16 14:35:48 +02008056 *pos = posmatch->pos[found];
8057 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008058 }
8059 }
8060 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008061 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008062 }
8063 }
8064 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008065 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008066 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008067 colnr_T start = posmatch->pos[found].col == 0
8068 ? 0 : posmatch->pos[found].col - 1;
8069 colnr_T end = posmatch->pos[found].col == 0
8070 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008071
Bram Moolenaar85077472016-10-16 14:35:48 +02008072 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008073 shl->rm.startpos[0].lnum = 0;
8074 shl->rm.startpos[0].col = start;
8075 shl->rm.endpos[0].lnum = 0;
8076 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008077 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008078 posmatch->cur = found + 1;
8079 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008080 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008081 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008082}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008083#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008084
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008086screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
8088 attrentry_T *aep = NULL;
8089
8090 screen_attr = attr;
8091 if (full_screen
8092#ifdef WIN3264
8093 && termcap_active
8094#endif
8095 )
8096 {
8097#ifdef FEAT_GUI
8098 if (gui.in_use)
8099 {
8100 char buf[20];
8101
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008102 /* The GUI handles this internally. */
8103 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 OUT_STR(buf);
8105 }
8106 else
8107#endif
8108 {
8109 if (attr > HL_ALL) /* special HL attr. */
8110 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008111 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 aep = syn_cterm_attr2entry(attr);
8113 else
8114 aep = syn_term_attr2entry(attr);
8115 if (aep == NULL) /* did ":syntax clear" */
8116 attr = 0;
8117 else
8118 attr = aep->ae_attr;
8119 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008120 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008122 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008123#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008124 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8125 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8126 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008127#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008128 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008129 /* If the Normal FG color has BOLD attribute and the new HL
8130 * has a FG color defined, clear BOLD. */
8131 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008132 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008134 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008135 out_str(T_UCS);
8136 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008137 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8138 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008140 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008142 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008144 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008145 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146
8147 /*
8148 * Output the color or start string after bold etc., in case the
8149 * bold etc. override the color setting.
8150 */
8151 if (aep != NULL)
8152 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008153#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008154 /* When 'termguicolors' is set but fg or bg is unset,
8155 * fall back to the cterm colors. This helps for SpellBad,
8156 * where the GUI uses a red undercurl. */
8157 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008159 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008160 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008161 }
8162 else
8163#endif
8164 if (t_colors > 1)
8165 {
8166 if (aep->ae_u.cterm.fg_color)
8167 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8168 }
8169#ifdef FEAT_TERMGUICOLORS
8170 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8171 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008172 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008173 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 }
8175 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008176#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008177 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008179 if (aep->ae_u.cterm.bg_color)
8180 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8181 }
8182
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008183 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008184 {
8185 if (aep->ae_u.term.start != NULL)
8186 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 }
8188 }
8189 }
8190 }
8191}
8192
8193 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008194screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195{
8196 int do_ME = FALSE; /* output T_ME code */
8197
8198 if (screen_attr != 0
8199#ifdef WIN3264
8200 && termcap_active
8201#endif
8202 )
8203 {
8204#ifdef FEAT_GUI
8205 if (gui.in_use)
8206 {
8207 char buf[20];
8208
8209 /* use internal GUI code */
8210 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8211 OUT_STR(buf);
8212 }
8213 else
8214#endif
8215 {
8216 if (screen_attr > HL_ALL) /* special HL attr. */
8217 {
8218 attrentry_T *aep;
8219
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008220 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {
8222 /*
8223 * Assume that t_me restores the original colors!
8224 */
8225 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008226 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008227#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008228 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8229 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8230 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008231#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008232 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008233#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008234 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8235 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8236 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008237#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008238 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 do_ME = TRUE;
8240 }
8241 else
8242 {
8243 aep = syn_term_attr2entry(screen_attr);
8244 if (aep != NULL && aep->ae_u.term.stop != NULL)
8245 {
8246 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8247 do_ME = TRUE;
8248 else
8249 out_str(aep->ae_u.term.stop);
8250 }
8251 }
8252 if (aep == NULL) /* did ":syntax clear" */
8253 screen_attr = 0;
8254 else
8255 screen_attr = aep->ae_attr;
8256 }
8257
8258 /*
8259 * Often all ending-codes are equal to T_ME. Avoid outputting the
8260 * same sequence several times.
8261 */
8262 if (screen_attr & HL_STANDOUT)
8263 {
8264 if (STRCMP(T_SE, T_ME) == 0)
8265 do_ME = TRUE;
8266 else
8267 out_str(T_SE);
8268 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008269 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008270 {
8271 if (STRCMP(T_UCE, T_ME) == 0)
8272 do_ME = TRUE;
8273 else
8274 out_str(T_UCE);
8275 }
8276 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008277 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {
8279 if (STRCMP(T_UE, T_ME) == 0)
8280 do_ME = TRUE;
8281 else
8282 out_str(T_UE);
8283 }
8284 if (screen_attr & HL_ITALIC)
8285 {
8286 if (STRCMP(T_CZR, T_ME) == 0)
8287 do_ME = TRUE;
8288 else
8289 out_str(T_CZR);
8290 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008291 if (screen_attr & HL_STRIKETHROUGH)
8292 {
8293 if (STRCMP(T_STE, T_ME) == 0)
8294 do_ME = TRUE;
8295 else
8296 out_str(T_STE);
8297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8299 out_str(T_ME);
8300
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008301#ifdef FEAT_TERMGUICOLORS
8302 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008304 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008305 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008306 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008307 term_bg_rgb_color(cterm_normal_bg_gui_color);
8308 }
8309 else
8310#endif
8311 {
8312 if (t_colors > 1)
8313 {
8314 /* set Normal cterm colors */
8315 if (cterm_normal_fg_color != 0)
8316 term_fg_color(cterm_normal_fg_color - 1);
8317 if (cterm_normal_bg_color != 0)
8318 term_bg_color(cterm_normal_bg_color - 1);
8319 if (cterm_normal_fg_bold)
8320 out_str(T_MD);
8321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 }
8323 }
8324 }
8325 screen_attr = 0;
8326}
8327
8328/*
8329 * Reset the colors for a cterm. Used when leaving Vim.
8330 * The machine specific code may override this again.
8331 */
8332 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008333reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008335 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {
8337 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008338#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008339 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8340 || cterm_normal_bg_gui_color != INVALCOLOR)
8341 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008342#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 {
8346 out_str(T_OP);
8347 screen_attr = -1;
8348 }
8349 if (cterm_normal_fg_bold)
8350 {
8351 out_str(T_ME);
8352 screen_attr = -1;
8353 }
8354 }
8355}
8356
8357/*
8358 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8359 * using the attributes from ScreenAttrs["off"].
8360 */
8361 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008362screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363{
8364 int attr;
8365
8366 /* Check for illegal values, just in case (could happen just after
8367 * resizing). */
8368 if (row >= screen_Rows || col >= screen_Columns)
8369 return;
8370
Bram Moolenaar494838a2015-02-10 19:20:37 +01008371 /* Outputting a character in the last cell on the screen may scroll the
8372 * screen up. Only do it when the "xn" termcap property is set, otherwise
8373 * mark the character invalid (update it when scrolled up). */
8374 if (*T_XN == NUL
8375 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376#ifdef FEAT_RIGHTLEFT
8377 /* account for first command-line character in rightleft mode */
8378 && !cmdmsg_rl
8379#endif
8380 )
8381 {
8382 ScreenAttrs[off] = (sattr_T)-1;
8383 return;
8384 }
8385
8386 /*
8387 * Stop highlighting first, so it's easier to move the cursor.
8388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 if (screen_char_attr != 0)
8390 attr = screen_char_attr;
8391 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 attr = ScreenAttrs[off];
8393 if (screen_attr != attr)
8394 screen_stop_highlight();
8395
8396 windgoto(row, col);
8397
8398 if (screen_attr != attr)
8399 screen_start_highlight(attr);
8400
8401#ifdef FEAT_MBYTE
8402 if (enc_utf8 && ScreenLinesUC[off] != 0)
8403 {
8404 char_u buf[MB_MAXBYTES + 1];
8405
Bram Moolenaarcb070082016-04-02 22:14:51 +02008406 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008407 {
8408 if (*p_ambw == 'd'
8409# ifdef FEAT_GUI
8410 && !gui.in_use
8411# endif
8412 )
8413 {
8414 /* Clear the two screen cells. If the character is actually
8415 * single width it won't change the second cell. */
8416 out_str((char_u *)" ");
8417 term_windgoto(row, col);
8418 }
8419 /* not sure where the cursor is after drawing the ambiguous width
8420 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008421 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008422 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008423 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008425
8426 /* Convert the UTF-8 character to bytes and write it. */
8427 buf[utfc_char2bytes(off, buf)] = NUL;
8428 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 }
8430 else
8431#endif
8432 {
8433#ifdef FEAT_MBYTE
8434 out_flush_check();
8435#endif
8436 out_char(ScreenLines[off]);
8437#ifdef FEAT_MBYTE
8438 /* double-byte character in single-width cell */
8439 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8440 out_char(ScreenLines2[off]);
8441#endif
8442 }
8443
8444 screen_cur_col++;
8445}
8446
8447#ifdef FEAT_MBYTE
8448
8449/*
8450 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8451 * on the screen at position 'row' and 'col'.
8452 * The attributes of the first byte is used for all. This is required to
8453 * output the two bytes of a double-byte character with nothing in between.
8454 */
8455 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008456screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457{
8458 /* Check for illegal values (could be wrong when screen was resized). */
8459 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8460 return;
8461
8462 /* Outputting the last character on the screen may scrollup the screen.
8463 * Don't to it! Mark the character invalid (update it when scrolled up) */
8464 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8465 {
8466 ScreenAttrs[off] = (sattr_T)-1;
8467 return;
8468 }
8469
8470 /* Output the first byte normally (positions the cursor), then write the
8471 * second byte directly. */
8472 screen_char(off, row, col);
8473 out_char(ScreenLines[off + 1]);
8474 ++screen_cur_col;
8475}
8476#endif
8477
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478/*
8479 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8480 * This uses the contents of ScreenLines[] and doesn't change it.
8481 */
8482 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008483screen_draw_rectangle(
8484 int row,
8485 int col,
8486 int height,
8487 int width,
8488 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489{
8490 int r, c;
8491 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008492#ifdef FEAT_MBYTE
8493 int max_off;
8494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008496 /* Can't use ScreenLines unless initialized */
8497 if (ScreenLines == NULL)
8498 return;
8499
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 if (invert)
8501 screen_char_attr = HL_INVERSE;
8502 for (r = row; r < row + height; ++r)
8503 {
8504 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008505#ifdef FEAT_MBYTE
8506 max_off = off + screen_Columns;
8507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 for (c = col; c < col + width; ++c)
8509 {
8510#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008511 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {
8513 screen_char_2(off + c, r, c);
8514 ++c;
8515 }
8516 else
8517#endif
8518 {
8519 screen_char(off + c, r, c);
8520#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008521 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 ++c;
8523#endif
8524 }
8525 }
8526 }
8527 screen_char_attr = 0;
8528}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530/*
8531 * Redraw the characters for a vertically split window.
8532 */
8533 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008534redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535{
8536 int col;
8537 int width;
8538
8539# ifdef FEAT_CLIPBOARD
8540 clip_may_clear_selection(row, end - 1);
8541# endif
8542
8543 if (wp == NULL)
8544 {
8545 col = 0;
8546 width = Columns;
8547 }
8548 else
8549 {
8550 col = wp->w_wincol;
8551 width = wp->w_width;
8552 }
8553 screen_draw_rectangle(row, col, end - row, width, FALSE);
8554}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008556 static void
8557space_to_screenline(int off, int attr)
8558{
8559 ScreenLines[off] = ' ';
8560 ScreenAttrs[off] = attr;
8561# ifdef FEAT_MBYTE
8562 if (enc_utf8)
8563 ScreenLinesUC[off] = 0;
8564# endif
8565}
8566
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567/*
8568 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8569 * with character 'c1' in first column followed by 'c2' in the other columns.
8570 * Use attributes 'attr'.
8571 */
8572 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008573screen_fill(
8574 int start_row,
8575 int end_row,
8576 int start_col,
8577 int end_col,
8578 int c1,
8579 int c2,
8580 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581{
8582 int row;
8583 int col;
8584 int off;
8585 int end_off;
8586 int did_delete;
8587 int c;
8588 int norm_term;
8589#if defined(FEAT_GUI) || defined(UNIX)
8590 int force_next = FALSE;
8591#endif
8592
8593 if (end_row > screen_Rows) /* safety check */
8594 end_row = screen_Rows;
8595 if (end_col > screen_Columns) /* safety check */
8596 end_col = screen_Columns;
8597 if (ScreenLines == NULL
8598 || start_row >= end_row
8599 || start_col >= end_col) /* nothing to do */
8600 return;
8601
8602 /* it's a "normal" terminal when not in a GUI or cterm */
8603 norm_term = (
8604#ifdef FEAT_GUI
8605 !gui.in_use &&
8606#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008607 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 for (row = start_row; row < end_row; ++row)
8609 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008610#ifdef FEAT_MBYTE
8611 if (has_mbyte
8612# ifdef FEAT_GUI
8613 && !gui.in_use
8614# endif
8615 )
8616 {
8617 /* When drawing over the right halve of a double-wide char clear
8618 * out the left halve. When drawing over the left halve of a
8619 * double wide-char clear out the right halve. Only needed in a
8620 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008621 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008622 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008623 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008624 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008625 }
8626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 /*
8628 * Try to use delete-line termcap code, when no attributes or in a
8629 * "normal" terminal, where a bold/italic space is just a
8630 * space.
8631 */
8632 did_delete = FALSE;
8633 if (c2 == ' '
8634 && end_col == Columns
8635 && can_clear(T_CE)
8636 && (attr == 0
8637 || (norm_term
8638 && attr <= HL_ALL
8639 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8640 {
8641 /*
8642 * check if we really need to clear something
8643 */
8644 col = start_col;
8645 if (c1 != ' ') /* don't clear first char */
8646 ++col;
8647
8648 off = LineOffset[row] + col;
8649 end_off = LineOffset[row] + end_col;
8650
8651 /* skip blanks (used often, keep it fast!) */
8652#ifdef FEAT_MBYTE
8653 if (enc_utf8)
8654 while (off < end_off && ScreenLines[off] == ' '
8655 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8656 ++off;
8657 else
8658#endif
8659 while (off < end_off && ScreenLines[off] == ' '
8660 && ScreenAttrs[off] == 0)
8661 ++off;
8662 if (off < end_off) /* something to be cleared */
8663 {
8664 col = off - LineOffset[row];
8665 screen_stop_highlight();
8666 term_windgoto(row, col);/* clear rest of this screen line */
8667 out_str(T_CE);
8668 screen_start(); /* don't know where cursor is now */
8669 col = end_col - col;
8670 while (col--) /* clear chars in ScreenLines */
8671 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008672 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 ++off;
8674 }
8675 }
8676 did_delete = TRUE; /* the chars are cleared now */
8677 }
8678
8679 off = LineOffset[row] + start_col;
8680 c = c1;
8681 for (col = start_col; col < end_col; ++col)
8682 {
8683 if (ScreenLines[off] != c
8684#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008685 || (enc_utf8 && (int)ScreenLinesUC[off]
8686 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687#endif
8688 || ScreenAttrs[off] != attr
8689#if defined(FEAT_GUI) || defined(UNIX)
8690 || force_next
8691#endif
8692 )
8693 {
8694#if defined(FEAT_GUI) || defined(UNIX)
8695 /* The bold trick may make a single row of pixels appear in
8696 * the next character. When a bold character is removed, the
8697 * next character should be redrawn too. This happens for our
8698 * own GUI and for some xterms. */
8699 if (
8700# ifdef FEAT_GUI
8701 gui.in_use
8702# endif
8703# if defined(FEAT_GUI) && defined(UNIX)
8704 ||
8705# endif
8706# ifdef UNIX
8707 term_is_xterm
8708# endif
8709 )
8710 {
8711 if (ScreenLines[off] != ' '
8712 && (ScreenAttrs[off] > HL_ALL
8713 || ScreenAttrs[off] & HL_BOLD))
8714 force_next = TRUE;
8715 else
8716 force_next = FALSE;
8717 }
8718#endif
8719 ScreenLines[off] = c;
8720#ifdef FEAT_MBYTE
8721 if (enc_utf8)
8722 {
8723 if (c >= 0x80)
8724 {
8725 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008726 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 }
8728 else
8729 ScreenLinesUC[off] = 0;
8730 }
8731#endif
8732 ScreenAttrs[off] = attr;
8733 if (!did_delete || c != ' ')
8734 screen_char(off, row, col);
8735 }
8736 ++off;
8737 if (col == start_col)
8738 {
8739 if (did_delete)
8740 break;
8741 c = c2;
8742 }
8743 }
8744 if (end_col == Columns)
8745 LineWraps[row] = FALSE;
8746 if (row == Rows - 1) /* overwritten the command line */
8747 {
8748 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008749 if (start_col == 0 && end_col == Columns
8750 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008752 if (start_col == 0)
8753 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 }
8755 }
8756}
8757
8758/*
8759 * Check if there should be a delay. Used before clearing or redrawing the
8760 * screen or the command line.
8761 */
8762 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008763check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
8765 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8766 && !did_wait_return
8767 && emsg_silent == 0)
8768 {
8769 out_flush();
8770 ui_delay(1000L, TRUE);
8771 emsg_on_display = FALSE;
8772 if (check_msg_scroll)
8773 msg_scroll = FALSE;
8774 }
8775}
8776
8777/*
8778 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008779 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 * Returns TRUE if there is a valid screen to write to.
8781 * Returns FALSE when starting up and screen not initialized yet.
8782 */
8783 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008784screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008786 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 return (ScreenLines != NULL);
8788}
8789
8790/*
8791 * Resize the shell to Rows and Columns.
8792 * Allocate ScreenLines[] and associated items.
8793 *
8794 * There may be some time between setting Rows and Columns and (re)allocating
8795 * ScreenLines[]. This happens when starting up and when (manually) changing
8796 * the shell size. Always use screen_Rows and screen_Columns to access items
8797 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8798 * final size of the shell is needed.
8799 */
8800 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008801screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802{
8803 int new_row, old_row;
8804#ifdef FEAT_GUI
8805 int old_Rows;
8806#endif
8807 win_T *wp;
8808 int outofmem = FALSE;
8809 int len;
8810 schar_T *new_ScreenLines;
8811#ifdef FEAT_MBYTE
8812 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008813 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008815 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816#endif
8817 sattr_T *new_ScreenAttrs;
8818 unsigned *new_LineOffset;
8819 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008820 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008821 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008823 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008824 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008826retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 /*
8828 * Allocation of the screen buffers is done only when the size changes and
8829 * when Rows and Columns have been set and we have started doing full
8830 * screen stuff.
8831 */
8832 if ((ScreenLines != NULL
8833 && Rows == screen_Rows
8834 && Columns == screen_Columns
8835#ifdef FEAT_MBYTE
8836 && enc_utf8 == (ScreenLinesUC != NULL)
8837 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008838 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839#endif
8840 )
8841 || Rows == 0
8842 || Columns == 0
8843 || (!full_screen && ScreenLines == NULL))
8844 return;
8845
8846 /*
8847 * It's possible that we produce an out-of-memory message below, which
8848 * will cause this function to be called again. To break the loop, just
8849 * return here.
8850 */
8851 if (entered)
8852 return;
8853 entered = TRUE;
8854
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008855 /*
8856 * Note that the window sizes are updated before reallocating the arrays,
8857 * thus we must not redraw here!
8858 */
8859 ++RedrawingDisabled;
8860
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 win_new_shellsize(); /* fit the windows in the new sized shell */
8862
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 comp_col(); /* recompute columns for shown command and ruler */
8864
8865 /*
8866 * We're changing the size of the screen.
8867 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8868 * - Move lines from the old arrays into the new arrays, clear extra
8869 * lines (unless the screen is going to be cleared).
8870 * - Free the old arrays.
8871 *
8872 * If anything fails, make ScreenLines NULL, so we don't do anything!
8873 * Continuing with the old ScreenLines may result in a crash, because the
8874 * size is wrong.
8875 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008876 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008878 if (aucmd_win != NULL)
8879 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
8881 new_ScreenLines = (schar_T *)lalloc((long_u)(
8882 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8883#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008884 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 if (enc_utf8)
8886 {
8887 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8888 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008889 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008890 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8892 }
8893 if (enc_dbcs == DBCS_JPNU)
8894 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8895 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8896#endif
8897 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8898 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8899 new_LineOffset = (unsigned *)lalloc((long_u)(
8900 Rows * sizeof(unsigned)), FALSE);
8901 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008902 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008904 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 {
8906 if (win_alloc_lines(wp) == FAIL)
8907 {
8908 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008909 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 }
8911 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008912 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8913 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008914 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008915give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008917#ifdef FEAT_MBYTE
8918 for (i = 0; i < p_mco; ++i)
8919 if (new_ScreenLinesC[i] == NULL)
8920 break;
8921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 if (new_ScreenLines == NULL
8923#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008924 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8926#endif
8927 || new_ScreenAttrs == NULL
8928 || new_LineOffset == NULL
8929 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008930 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 || outofmem)
8932 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008933 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008934 {
8935 /* guess the size */
8936 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8937
8938 /* Remember we did this to avoid getting outofmem messages over
8939 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008940 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008941 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008942 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008944 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008945 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008946 VIM_CLEAR(new_ScreenLinesC[i]);
8947 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008949 VIM_CLEAR(new_ScreenAttrs);
8950 VIM_CLEAR(new_LineOffset);
8951 VIM_CLEAR(new_LineWraps);
8952 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 }
8954 else
8955 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008956 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008957
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 for (new_row = 0; new_row < Rows; ++new_row)
8959 {
8960 new_LineOffset[new_row] = new_row * Columns;
8961 new_LineWraps[new_row] = FALSE;
8962
8963 /*
8964 * If the screen is not going to be cleared, copy as much as
8965 * possible from the old screen to the new one and clear the rest
8966 * (used when resizing the window at the "--more--" prompt or when
8967 * executing an external command, for the GUI).
8968 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008969 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 {
8971 (void)vim_memset(new_ScreenLines + new_row * Columns,
8972 ' ', (size_t)Columns * sizeof(schar_T));
8973#ifdef FEAT_MBYTE
8974 if (enc_utf8)
8975 {
8976 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8977 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008978 for (i = 0; i < p_mco; ++i)
8979 (void)vim_memset(new_ScreenLinesC[i]
8980 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 0, (size_t)Columns * sizeof(u8char_T));
8982 }
8983 if (enc_dbcs == DBCS_JPNU)
8984 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8985 0, (size_t)Columns * sizeof(schar_T));
8986#endif
8987 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8988 0, (size_t)Columns * sizeof(sattr_T));
8989 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008990 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 {
8992 if (screen_Columns < Columns)
8993 len = screen_Columns;
8994 else
8995 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008996#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008997 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008998 * may be invalid now. Also when p_mco changes. */
8999 if (!(enc_utf8 && ScreenLinesUC == NULL)
9000 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009001#endif
9002 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9003 ScreenLines + LineOffset[old_row],
9004 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009006 if (enc_utf8 && ScreenLinesUC != NULL
9007 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 {
9009 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9010 ScreenLinesUC + LineOffset[old_row],
9011 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009012 for (i = 0; i < p_mco; ++i)
9013 mch_memmove(new_ScreenLinesC[i]
9014 + new_LineOffset[new_row],
9015 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 (size_t)len * sizeof(u8char_T));
9017 }
9018 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9019 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9020 ScreenLines2 + LineOffset[old_row],
9021 (size_t)len * sizeof(schar_T));
9022#endif
9023 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9024 ScreenAttrs + LineOffset[old_row],
9025 (size_t)len * sizeof(sattr_T));
9026 }
9027 }
9028 }
9029 /* Use the last line of the screen for the current line. */
9030 current_ScreenLine = new_ScreenLines + Rows * Columns;
9031 }
9032
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009033 free_screenlines();
9034
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 ScreenLines = new_ScreenLines;
9036#ifdef FEAT_MBYTE
9037 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009038 for (i = 0; i < p_mco; ++i)
9039 ScreenLinesC[i] = new_ScreenLinesC[i];
9040 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 ScreenLines2 = new_ScreenLines2;
9042#endif
9043 ScreenAttrs = new_ScreenAttrs;
9044 LineOffset = new_LineOffset;
9045 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009046 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047
9048 /* It's important that screen_Rows and screen_Columns reflect the actual
9049 * size of ScreenLines[]. Set them before calling anything. */
9050#ifdef FEAT_GUI
9051 old_Rows = screen_Rows;
9052#endif
9053 screen_Rows = Rows;
9054 screen_Columns = Columns;
9055
9056 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009057 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 screenclear2();
9059
9060#ifdef FEAT_GUI
9061 else if (gui.in_use
9062 && !gui.starting
9063 && ScreenLines != NULL
9064 && old_Rows != Rows)
9065 {
9066 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9067 /*
9068 * Adjust the position of the cursor, for when executing an external
9069 * command.
9070 */
9071 if (msg_row >= Rows) /* Rows got smaller */
9072 msg_row = Rows - 1; /* put cursor at last row */
9073 else if (Rows > old_Rows) /* Rows got bigger */
9074 msg_row += Rows - old_Rows; /* put cursor in same place */
9075 if (msg_col >= Columns) /* Columns got smaller */
9076 msg_col = Columns - 1; /* put cursor at last column */
9077 }
9078#endif
9079
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009081 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009082
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009083 /*
9084 * Do not apply autocommands more than 3 times to avoid an endless loop
9085 * in case applying autocommands always changes Rows or Columns.
9086 */
9087 if (starting == 0 && ++retry_count <= 3)
9088 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009089 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009090 /* In rare cases, autocommands may have altered Rows or Columns,
9091 * jump back to check if we need to allocate the screen again. */
9092 goto retry;
9093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094}
9095
9096 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009097free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009098{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009099#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009100 int i;
9101
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009102 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009103 for (i = 0; i < Screen_mco; ++i)
9104 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009105 vim_free(ScreenLines2);
9106#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009107 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009108 vim_free(ScreenAttrs);
9109 vim_free(LineOffset);
9110 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009111 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009112}
9113
9114 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009115screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116{
9117 check_for_delay(FALSE);
9118 screenalloc(FALSE); /* allocate screen buffers if size changed */
9119 screenclear2(); /* clear the screen */
9120}
9121
9122 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009123screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124{
9125 int i;
9126
9127 if (starting == NO_SCREEN || ScreenLines == NULL
9128#ifdef FEAT_GUI
9129 || (gui.in_use && gui.starting)
9130#endif
9131 )
9132 return;
9133
9134#ifdef FEAT_GUI
9135 if (!gui.in_use)
9136#endif
9137 screen_attr = -1; /* force setting the Normal colors */
9138 screen_stop_highlight(); /* don't want highlighting here */
9139
9140#ifdef FEAT_CLIPBOARD
9141 /* disable selection without redrawing it */
9142 clip_scroll_selection(9999);
9143#endif
9144
9145 /* blank out ScreenLines */
9146 for (i = 0; i < Rows; ++i)
9147 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009148 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 LineWraps[i] = FALSE;
9150 }
9151
9152 if (can_clear(T_CL))
9153 {
9154 out_str(T_CL); /* clear the display */
9155 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009156 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 }
9158 else
9159 {
9160 /* can't clear the screen, mark all chars with invalid attributes */
9161 for (i = 0; i < Rows; ++i)
9162 lineinvalid(LineOffset[i], (int)Columns);
9163 clear_cmdline = TRUE;
9164 }
9165
9166 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9167
9168 win_rest_invalid(firstwin);
9169 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009170 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 if (must_redraw == CLEAR) /* no need to clear again */
9172 must_redraw = NOT_VALID;
9173 compute_cmdrow();
9174 msg_row = cmdline_row; /* put cursor on last line for messages */
9175 msg_col = 0;
9176 screen_start(); /* don't know where cursor is now */
9177 msg_scrolled = 0; /* can't scroll back */
9178 msg_didany = FALSE;
9179 msg_didout = FALSE;
9180}
9181
9182/*
9183 * Clear one line in ScreenLines.
9184 */
9185 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009186lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187{
9188 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9189#ifdef FEAT_MBYTE
9190 if (enc_utf8)
9191 (void)vim_memset(ScreenLinesUC + off, 0,
9192 (size_t)width * sizeof(u8char_T));
9193#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009194 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195}
9196
9197/*
9198 * Mark one line in ScreenLines invalid by setting the attributes to an
9199 * invalid value.
9200 */
9201 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009202lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203{
9204 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9205}
9206
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207/*
9208 * Copy part of a Screenline for vertically split window "wp".
9209 */
9210 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009211linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212{
9213 unsigned off_to = LineOffset[to] + wp->w_wincol;
9214 unsigned off_from = LineOffset[from] + wp->w_wincol;
9215
9216 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9217 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009218#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 if (enc_utf8)
9220 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009221 int i;
9222
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9224 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009225 for (i = 0; i < p_mco; ++i)
9226 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9227 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 }
9229 if (enc_dbcs == DBCS_JPNU)
9230 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9231 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9234 wp->w_width * sizeof(sattr_T));
9235}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
9237/*
9238 * Return TRUE if clearing with term string "p" would work.
9239 * It can't work when the string is empty or it won't set the right background.
9240 */
9241 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009242can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243{
9244 return (*p != NUL && (t_colors <= 1
9245#ifdef FEAT_GUI
9246 || gui.in_use
9247#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009248#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009249 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009250 || (!p_tgc && cterm_normal_bg_color == 0)
9251#else
9252 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009253#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009254 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255}
9256
9257/*
9258 * Reset cursor position. Use whenever cursor was moved because of outputting
9259 * something directly to the screen (shell commands) or a terminal control
9260 * code.
9261 */
9262 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009263screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264{
9265 screen_cur_row = screen_cur_col = 9999;
9266}
9267
9268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 * Move the cursor to position "row","col" in the screen.
9270 * This tries to find the most efficient way to move, minimizing the number of
9271 * characters sent to the terminal.
9272 */
9273 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009274windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009276 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 int i;
9278 int plan;
9279 int cost;
9280 int wouldbe_col;
9281 int noinvcurs;
9282 char_u *bs;
9283 int goto_cost;
9284 int attr;
9285
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009286#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9288
9289#define PLAN_LE 1
9290#define PLAN_CR 2
9291#define PLAN_NL 3
9292#define PLAN_WRITE 4
9293 /* Can't use ScreenLines unless initialized */
9294 if (ScreenLines == NULL)
9295 return;
9296
9297 if (col != screen_cur_col || row != screen_cur_row)
9298 {
9299 /* Check for valid position. */
9300 if (row < 0) /* window without text lines? */
9301 row = 0;
9302 if (row >= screen_Rows)
9303 row = screen_Rows - 1;
9304 if (col >= screen_Columns)
9305 col = screen_Columns - 1;
9306
9307 /* check if no cursor movement is allowed in highlight mode */
9308 if (screen_attr && *T_MS == NUL)
9309 noinvcurs = HIGHL_COST;
9310 else
9311 noinvcurs = 0;
9312 goto_cost = GOTO_COST + noinvcurs;
9313
9314 /*
9315 * Plan how to do the positioning:
9316 * 1. Use CR to move it to column 0, same row.
9317 * 2. Use T_LE to move it a few columns to the left.
9318 * 3. Use NL to move a few lines down, column 0.
9319 * 4. Move a few columns to the right with T_ND or by writing chars.
9320 *
9321 * Don't do this if the cursor went beyond the last column, the cursor
9322 * position is unknown then (some terminals wrap, some don't )
9323 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009324 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 * characters to move the cursor to the right.
9326 */
9327 if (row >= screen_cur_row && screen_cur_col < Columns)
9328 {
9329 /*
9330 * If the cursor is in the same row, bigger col, we can use CR
9331 * or T_LE.
9332 */
9333 bs = NULL; /* init for GCC */
9334 attr = screen_attr;
9335 if (row == screen_cur_row && col < screen_cur_col)
9336 {
9337 /* "le" is preferred over "bc", because "bc" is obsolete */
9338 if (*T_LE)
9339 bs = T_LE; /* "cursor left" */
9340 else
9341 bs = T_BC; /* "backspace character (old) */
9342 if (*bs)
9343 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9344 else
9345 cost = 999;
9346 if (col + 1 < cost) /* using CR is less characters */
9347 {
9348 plan = PLAN_CR;
9349 wouldbe_col = 0;
9350 cost = 1; /* CR is just one character */
9351 }
9352 else
9353 {
9354 plan = PLAN_LE;
9355 wouldbe_col = col;
9356 }
9357 if (noinvcurs) /* will stop highlighting */
9358 {
9359 cost += noinvcurs;
9360 attr = 0;
9361 }
9362 }
9363
9364 /*
9365 * If the cursor is above where we want to be, we can use CR LF.
9366 */
9367 else if (row > screen_cur_row)
9368 {
9369 plan = PLAN_NL;
9370 wouldbe_col = 0;
9371 cost = (row - screen_cur_row) * 2; /* CR LF */
9372 if (noinvcurs) /* will stop highlighting */
9373 {
9374 cost += noinvcurs;
9375 attr = 0;
9376 }
9377 }
9378
9379 /*
9380 * If the cursor is in the same row, smaller col, just use write.
9381 */
9382 else
9383 {
9384 plan = PLAN_WRITE;
9385 wouldbe_col = screen_cur_col;
9386 cost = 0;
9387 }
9388
9389 /*
9390 * Check if any characters that need to be written have the
9391 * correct attributes. Also avoid UTF-8 characters.
9392 */
9393 i = col - wouldbe_col;
9394 if (i > 0)
9395 cost += i;
9396 if (cost < goto_cost && i > 0)
9397 {
9398 /*
9399 * Check if the attributes are correct without additionally
9400 * stopping highlighting.
9401 */
9402 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9403 while (i && *p++ == attr)
9404 --i;
9405 if (i != 0)
9406 {
9407 /*
9408 * Try if it works when highlighting is stopped here.
9409 */
9410 if (*--p == 0)
9411 {
9412 cost += noinvcurs;
9413 while (i && *p++ == 0)
9414 --i;
9415 }
9416 if (i != 0)
9417 cost = 999; /* different attributes, don't do it */
9418 }
9419#ifdef FEAT_MBYTE
9420 if (enc_utf8)
9421 {
9422 /* Don't use an UTF-8 char for positioning, it's slow. */
9423 for (i = wouldbe_col; i < col; ++i)
9424 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9425 {
9426 cost = 999;
9427 break;
9428 }
9429 }
9430#endif
9431 }
9432
9433 /*
9434 * We can do it without term_windgoto()!
9435 */
9436 if (cost < goto_cost)
9437 {
9438 if (plan == PLAN_LE)
9439 {
9440 if (noinvcurs)
9441 screen_stop_highlight();
9442 while (screen_cur_col > col)
9443 {
9444 out_str(bs);
9445 --screen_cur_col;
9446 }
9447 }
9448 else if (plan == PLAN_CR)
9449 {
9450 if (noinvcurs)
9451 screen_stop_highlight();
9452 out_char('\r');
9453 screen_cur_col = 0;
9454 }
9455 else if (plan == PLAN_NL)
9456 {
9457 if (noinvcurs)
9458 screen_stop_highlight();
9459 while (screen_cur_row < row)
9460 {
9461 out_char('\n');
9462 ++screen_cur_row;
9463 }
9464 screen_cur_col = 0;
9465 }
9466
9467 i = col - screen_cur_col;
9468 if (i > 0)
9469 {
9470 /*
9471 * Use cursor-right if it's one character only. Avoids
9472 * removing a line of pixels from the last bold char, when
9473 * using the bold trick in the GUI.
9474 */
9475 if (T_ND[0] != NUL && T_ND[1] == NUL)
9476 {
9477 while (i-- > 0)
9478 out_char(*T_ND);
9479 }
9480 else
9481 {
9482 int off;
9483
9484 off = LineOffset[row] + screen_cur_col;
9485 while (i-- > 0)
9486 {
9487 if (ScreenAttrs[off] != screen_attr)
9488 screen_stop_highlight();
9489#ifdef FEAT_MBYTE
9490 out_flush_check();
9491#endif
9492 out_char(ScreenLines[off]);
9493#ifdef FEAT_MBYTE
9494 if (enc_dbcs == DBCS_JPNU
9495 && ScreenLines[off] == 0x8e)
9496 out_char(ScreenLines2[off]);
9497#endif
9498 ++off;
9499 }
9500 }
9501 }
9502 }
9503 }
9504 else
9505 cost = 999;
9506
9507 if (cost >= goto_cost)
9508 {
9509 if (noinvcurs)
9510 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009511 if (row == screen_cur_row && (col > screen_cur_col)
9512 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 term_cursor_right(col - screen_cur_col);
9514 else
9515 term_windgoto(row, col);
9516 }
9517 screen_cur_row = row;
9518 screen_cur_col = col;
9519 }
9520}
9521
9522/*
9523 * Set cursor to its position in the current window.
9524 */
9525 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009526setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009528 setcursor_mayforce(FALSE);
9529}
9530
9531/*
9532 * Set cursor to its position in the current window.
9533 * When "force" is TRUE also when not redrawing.
9534 */
9535 void
9536setcursor_mayforce(int force)
9537{
9538 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 {
9540 validate_cursor();
9541 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009542 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009544 /* With 'rightleft' set and the cursor on a double-wide
9545 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009546 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009548 (has_mbyte
9549 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9550 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551# endif
9552 1)) :
9553#endif
9554 curwin->w_wcol));
9555 }
9556}
9557
9558
9559/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009560 * Insert 'line_count' lines at 'row' in window 'wp'.
9561 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9562 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 * scrolling.
9564 * Returns FAIL if the lines are not inserted, OK for success.
9565 */
9566 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009567win_ins_lines(
9568 win_T *wp,
9569 int row,
9570 int line_count,
9571 int invalid,
9572 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573{
9574 int did_delete;
9575 int nextrow;
9576 int lastrow;
9577 int retval;
9578
9579 if (invalid)
9580 wp->w_lines_valid = 0;
9581
9582 if (wp->w_height < 5)
9583 return FAIL;
9584
9585 if (line_count > wp->w_height - row)
9586 line_count = wp->w_height - row;
9587
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009588 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 if (retval != MAYBE)
9590 return retval;
9591
9592 /*
9593 * If there is a next window or a status line, we first try to delete the
9594 * lines at the bottom to avoid messing what is after the window.
9595 * If this fails and there are following windows, don't do anything to avoid
9596 * messing up those windows, better just redraw.
9597 */
9598 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 if (wp->w_next != NULL || wp->w_status_height)
9600 {
9601 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009602 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 did_delete = TRUE;
9604 else if (wp->w_next)
9605 return FAIL;
9606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 /*
9608 * if no lines deleted, blank the lines that will end up below the window
9609 */
9610 if (!did_delete)
9611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009614 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 lastrow = nextrow + line_count;
9616 if (lastrow > Rows)
9617 lastrow = Rows;
9618 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009619 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 ' ', ' ', 0);
9621 }
9622
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009623 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 == FAIL)
9625 {
9626 /* deletion will have messed up other windows */
9627 if (did_delete)
9628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 win_rest_invalid(W_NEXT(wp));
9631 }
9632 return FAIL;
9633 }
9634
9635 return OK;
9636}
9637
9638/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009639 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9641 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9642 * scrolling
9643 * Return OK for success, FAIL if the lines are not deleted.
9644 */
9645 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009646win_del_lines(
9647 win_T *wp,
9648 int row,
9649 int line_count,
9650 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009651 int mayclear,
9652 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653{
9654 int retval;
9655
9656 if (invalid)
9657 wp->w_lines_valid = 0;
9658
9659 if (line_count > wp->w_height - row)
9660 line_count = wp->w_height - row;
9661
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009662 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 if (retval != MAYBE)
9664 return retval;
9665
9666 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009667 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 return FAIL;
9669
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 /*
9671 * If there are windows or status lines below, try to put them at the
9672 * correct place. If we can't do that, they have to be redrawn.
9673 */
9674 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9675 {
9676 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009677 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 {
9679 wp->w_redr_status = TRUE;
9680 win_rest_invalid(wp->w_next);
9681 }
9682 }
9683 /*
9684 * If this is the last window and there is no status line, redraw the
9685 * command line later.
9686 */
9687 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 redraw_cmdline = TRUE;
9689 return OK;
9690}
9691
9692/*
9693 * Common code for win_ins_lines() and win_del_lines().
9694 * Returns OK or FAIL when the work has been done.
9695 * Returns MAYBE when not finished yet.
9696 */
9697 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009698win_do_lines(
9699 win_T *wp,
9700 int row,
9701 int line_count,
9702 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009703 int del,
9704 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705{
9706 int retval;
9707
9708 if (!redrawing() || line_count <= 0)
9709 return FAIL;
9710
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009711 /* When inserting lines would result in loss of command output, just redraw
9712 * the lines. */
9713 if (no_win_do_lines_ins && !del)
9714 return FAIL;
9715
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009717 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009719 if (!no_win_do_lines_ins)
9720 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 return FAIL;
9722 }
9723
9724 /*
9725 * Delete all remaining lines
9726 */
9727 if (row + line_count >= wp->w_height)
9728 {
9729 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009730 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 ' ', ' ', 0);
9732 return OK;
9733 }
9734
9735 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009736 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009738 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009740 if (!no_win_do_lines_ins)
9741 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742
9743 /*
9744 * If the terminal can set a scroll region, use that.
9745 * Always do this in a vertically split window. This will redraw from
9746 * ScreenLines[] when t_CV isn't defined. That's faster than using
9747 * win_line().
9748 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009749 * a character in the lower right corner of the scroll region may cause a
9750 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009752 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 scroll_region_set(wp, row);
9756 if (del)
9757 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009758 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 else
9760 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009761 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 scroll_region_reset();
9764 return retval;
9765 }
9766
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9768 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769
9770 return MAYBE;
9771}
9772
9773/*
9774 * window 'wp' and everything after it is messed up, mark it for redraw
9775 */
9776 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009777win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 {
9781 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 wp->w_redr_status = TRUE;
9783 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 }
9785 redraw_cmdline = TRUE;
9786}
9787
9788/*
9789 * The rest of the routines in this file perform screen manipulations. The
9790 * given operation is performed physically on the screen. The corresponding
9791 * change is also made to the internal screen image. In this way, the editor
9792 * anticipates the effect of editing changes on the appearance of the screen.
9793 * That way, when we call screenupdate a complete redraw isn't usually
9794 * necessary. Another advantage is that we can keep adding code to anticipate
9795 * screen changes, and in the meantime, everything still works.
9796 */
9797
9798/*
9799 * types for inserting or deleting lines
9800 */
9801#define USE_T_CAL 1
9802#define USE_T_CDL 2
9803#define USE_T_AL 3
9804#define USE_T_CE 4
9805#define USE_T_DL 5
9806#define USE_T_SR 6
9807#define USE_NL 7
9808#define USE_T_CD 8
9809#define USE_REDRAW 9
9810
9811/*
9812 * insert lines on the screen and update ScreenLines[]
9813 * 'end' is the line after the scrolled part. Normally it is Rows.
9814 * When scrolling region used 'off' is the offset from the top for the region.
9815 * 'row' and 'end' are relative to the start of the region.
9816 *
9817 * return FAIL for failure, OK for success.
9818 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009819 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009820screen_ins_lines(
9821 int off,
9822 int row,
9823 int line_count,
9824 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009825 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009826 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827{
9828 int i;
9829 int j;
9830 unsigned temp;
9831 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009832 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 int type;
9834 int result_empty;
9835 int can_ce = can_clear(T_CE);
9836
9837 /*
9838 * FAIL if
9839 * - there is no valid screen
9840 * - the screen has to be redrawn completely
9841 * - the line count is less than one
9842 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009843 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009845 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9846#ifdef FEAT_CLIPBOARD
9847 || (clip_star.state != SELECT_CLEARED
9848 && redrawing_for_callback > 0)
9849#endif
9850 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 return FAIL;
9852
9853 /*
9854 * There are seven ways to insert lines:
9855 * 0. When in a vertically split window and t_CV isn't set, redraw the
9856 * characters from ScreenLines[].
9857 * 1. Use T_CD (clear to end of display) if it exists and the result of
9858 * the insert is just empty lines
9859 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9860 * present or line_count > 1. It looks better if we do all the inserts
9861 * at once.
9862 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9863 * insert is just empty lines and T_CE is not present or line_count >
9864 * 1.
9865 * 4. Use T_AL (insert line) if it exists.
9866 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9867 * just empty lines.
9868 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9869 * just empty lines.
9870 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9871 * the 'da' flag is not set or we have clear line capability.
9872 * 8. redraw the characters from ScreenLines[].
9873 *
9874 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9875 * the scrollbar for the window. It does have insert line, use that if it
9876 * exists.
9877 */
9878 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9880 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009881 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 type = USE_T_CD;
9883 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9884 type = USE_T_CAL;
9885 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9886 type = USE_T_CDL;
9887 else if (*T_AL != NUL)
9888 type = USE_T_AL;
9889 else if (can_ce && result_empty)
9890 type = USE_T_CE;
9891 else if (*T_DL != NUL && result_empty)
9892 type = USE_T_DL;
9893 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9894 type = USE_T_SR;
9895 else
9896 return FAIL;
9897
9898 /*
9899 * For clearing the lines screen_del_lines() is used. This will also take
9900 * care of t_db if necessary.
9901 */
9902 if (type == USE_T_CD || type == USE_T_CDL ||
9903 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009904 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905
9906 /*
9907 * If text is retained below the screen, first clear or delete as many
9908 * lines at the bottom of the window as are about to be inserted so that
9909 * the deleted lines won't later surface during a screen_del_lines.
9910 */
9911 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009912 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
9914#ifdef FEAT_CLIPBOARD
9915 /* Remove a modeless selection when inserting lines halfway the screen
9916 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009917 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009918 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 else
9920 clip_scroll_selection(-line_count);
9921#endif
9922
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923#ifdef FEAT_GUI
9924 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9925 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009926 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927#endif
9928
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009929 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9930 cursor_col = wp->w_wincol;
9931
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 if (*T_CCS != NUL) /* cursor relative to region */
9933 cursor_row = row;
9934 else
9935 cursor_row = row + off;
9936
9937 /*
9938 * Shift LineOffset[] line_count down to reflect the inserted lines.
9939 * Clear the inserted lines in ScreenLines[].
9940 */
9941 row += off;
9942 end += off;
9943 for (i = 0; i < line_count; ++i)
9944 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 if (wp != NULL && wp->w_width != Columns)
9946 {
9947 /* need to copy part of a line */
9948 j = end - 1 - i;
9949 while ((j -= line_count) >= row)
9950 linecopy(j + line_count, j, wp);
9951 j += line_count;
9952 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009953 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9954 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 else
9956 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9957 LineWraps[j] = FALSE;
9958 }
9959 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 {
9961 j = end - 1 - i;
9962 temp = LineOffset[j];
9963 while ((j -= line_count) >= row)
9964 {
9965 LineOffset[j + line_count] = LineOffset[j];
9966 LineWraps[j + line_count] = LineWraps[j];
9967 }
9968 LineOffset[j + line_count] = temp;
9969 LineWraps[j + line_count] = FALSE;
9970 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009971 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 else
9973 lineinvalid(temp, (int)Columns);
9974 }
9975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976
9977 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009978 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009979 if (clear_attr != 0)
9980 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 /* redraw the characters */
9983 if (type == USE_REDRAW)
9984 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009985 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 {
9987 term_append_lines(line_count);
9988 screen_start(); /* don't know where cursor is now */
9989 }
9990 else
9991 {
9992 for (i = 0; i < line_count; i++)
9993 {
9994 if (type == USE_T_AL)
9995 {
9996 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009997 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 out_str(T_AL);
9999 }
10000 else /* type == USE_T_SR */
10001 out_str(T_SR);
10002 screen_start(); /* don't know where cursor is now */
10003 }
10004 }
10005
10006 /*
10007 * With scroll-reverse and 'da' flag set we need to clear the lines that
10008 * have been scrolled down into the region.
10009 */
10010 if (type == USE_T_SR && *T_DA)
10011 {
10012 for (i = 0; i < line_count; ++i)
10013 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010014 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 out_str(T_CE);
10016 screen_start(); /* don't know where cursor is now */
10017 }
10018 }
10019
10020#ifdef FEAT_GUI
10021 gui_can_update_cursor();
10022 if (gui.in_use)
10023 out_flush(); /* always flush after a scroll */
10024#endif
10025 return OK;
10026}
10027
10028/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010029 * Delete lines on the screen and update ScreenLines[].
10030 * "end" is the line after the scrolled part. Normally it is Rows.
10031 * When scrolling region used "off" is the offset from the top for the region.
10032 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 *
10034 * Return OK for success, FAIL if the lines are not deleted.
10035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010037screen_del_lines(
10038 int off,
10039 int row,
10040 int line_count,
10041 int end,
10042 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010043 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010044 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045{
10046 int j;
10047 int i;
10048 unsigned temp;
10049 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010050 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 int cursor_end;
10052 int result_empty; /* result is empty until end of region */
10053 int can_delete; /* deleting line codes can be used */
10054 int type;
10055
10056 /*
10057 * FAIL if
10058 * - there is no valid screen
10059 * - the screen has to be redrawn completely
10060 * - the line count is less than one
10061 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010062 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010064 if (!screen_valid(TRUE) || line_count <= 0
10065 || (!force && line_count > p_ttyscroll)
10066#ifdef FEAT_CLIPBOARD
10067 || (clip_star.state != SELECT_CLEARED
10068 && redrawing_for_callback > 0)
10069#endif
10070 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 return FAIL;
10072
10073 /*
10074 * Check if the rest of the current region will become empty.
10075 */
10076 result_empty = row + line_count >= end;
10077
10078 /*
10079 * We can delete lines only when 'db' flag not set or when 'ce' option
10080 * available.
10081 */
10082 can_delete = (*T_DB == NUL || can_clear(T_CE));
10083
10084 /*
10085 * There are six ways to delete lines:
10086 * 0. When in a vertically split window and t_CV isn't set, redraw the
10087 * characters from ScreenLines[].
10088 * 1. Use T_CD if it exists and the result is empty.
10089 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10090 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10091 * none of the other ways work.
10092 * 4. Use T_CE (erase line) if the result is empty.
10093 * 5. Use T_DL (delete line) if it exists.
10094 * 6. redraw the characters from ScreenLines[].
10095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10097 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010098 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 type = USE_T_CD;
10100#if defined(__BEOS__) && defined(BEOS_DR8)
10101 /*
10102 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10103 * its internal termcap... this works okay for tests which test *T_DB !=
10104 * NUL. It has the disadvantage that the user cannot use any :set t_*
10105 * command to get T_DB (back) to empty_option, only :set term=... will do
10106 * the trick...
10107 * Anyway, this hack will hopefully go away with the next OS release.
10108 * (Olaf Seibert)
10109 */
10110 else if (row == 0 && T_DB == empty_option
10111 && (line_count == 1 || *T_CDL == NUL))
10112#else
10113 else if (row == 0 && (
10114#ifndef AMIGA
10115 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10116 * up, so use delete-line command */
10117 line_count == 1 ||
10118#endif
10119 *T_CDL == NUL))
10120#endif
10121 type = USE_NL;
10122 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10123 type = USE_T_CDL;
10124 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010125 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 type = USE_T_CE;
10127 else if (*T_DL != NUL && can_delete)
10128 type = USE_T_DL;
10129 else if (*T_CDL != NUL && can_delete)
10130 type = USE_T_CDL;
10131 else
10132 return FAIL;
10133
10134#ifdef FEAT_CLIPBOARD
10135 /* Remove a modeless selection when deleting lines halfway the screen or
10136 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010137 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010138 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 else
10140 clip_scroll_selection(line_count);
10141#endif
10142
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143#ifdef FEAT_GUI
10144 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10145 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010146 gui_dont_update_cursor(gui.cursor_row >= row + off
10147 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148#endif
10149
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010150 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10151 cursor_col = wp->w_wincol;
10152
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 if (*T_CCS != NUL) /* cursor relative to region */
10154 {
10155 cursor_row = row;
10156 cursor_end = end;
10157 }
10158 else
10159 {
10160 cursor_row = row + off;
10161 cursor_end = end + off;
10162 }
10163
10164 /*
10165 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10166 * Clear the inserted lines in ScreenLines[].
10167 */
10168 row += off;
10169 end += off;
10170 for (i = 0; i < line_count; ++i)
10171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 if (wp != NULL && wp->w_width != Columns)
10173 {
10174 /* need to copy part of a line */
10175 j = row + i;
10176 while ((j += line_count) <= end - 1)
10177 linecopy(j - line_count, j, wp);
10178 j -= line_count;
10179 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010180 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10181 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 else
10183 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10184 LineWraps[j] = FALSE;
10185 }
10186 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 {
10188 /* whole width, moving the line pointers is faster */
10189 j = row + i;
10190 temp = LineOffset[j];
10191 while ((j += line_count) <= end - 1)
10192 {
10193 LineOffset[j - line_count] = LineOffset[j];
10194 LineWraps[j - line_count] = LineWraps[j];
10195 }
10196 LineOffset[j - line_count] = temp;
10197 LineWraps[j - line_count] = FALSE;
10198 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010199 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 else
10201 lineinvalid(temp, (int)Columns);
10202 }
10203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010205 if (screen_attr != clear_attr)
10206 screen_stop_highlight();
10207 if (clear_attr != 0)
10208 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 /* redraw the characters */
10211 if (type == USE_REDRAW)
10212 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010213 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010215 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 out_str(T_CD);
10217 screen_start(); /* don't know where cursor is now */
10218 }
10219 else if (type == USE_T_CDL)
10220 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010221 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 term_delete_lines(line_count);
10223 screen_start(); /* don't know where cursor is now */
10224 }
10225 /*
10226 * Deleting lines at top of the screen or scroll region: Just scroll
10227 * the whole screen (scroll region) up by outputting newlines on the
10228 * last line.
10229 */
10230 else if (type == USE_NL)
10231 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010232 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 for (i = line_count; --i >= 0; )
10234 out_char('\n'); /* cursor will remain on same line */
10235 }
10236 else
10237 {
10238 for (i = line_count; --i >= 0; )
10239 {
10240 if (type == USE_T_DL)
10241 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010242 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 out_str(T_DL); /* delete a line */
10244 }
10245 else /* type == USE_T_CE */
10246 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010247 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 out_str(T_CE); /* erase a line */
10249 }
10250 screen_start(); /* don't know where cursor is now */
10251 }
10252 }
10253
10254 /*
10255 * If the 'db' flag is set, we need to clear the lines that have been
10256 * scrolled up at the bottom of the region.
10257 */
10258 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10259 {
10260 for (i = line_count; i > 0; --i)
10261 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010262 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 out_str(T_CE); /* erase a line */
10264 screen_start(); /* don't know where cursor is now */
10265 }
10266 }
10267
10268#ifdef FEAT_GUI
10269 gui_can_update_cursor();
10270 if (gui.in_use)
10271 out_flush(); /* always flush after a scroll */
10272#endif
10273
10274 return OK;
10275}
10276
10277/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010278 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 *
10280 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10281 * If clear_cmdline is FALSE there may be a message there that needs to be
10282 * cleared only if a mode is shown.
10283 * Return the length of the message (0 if no message).
10284 */
10285 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010286showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287{
10288 int need_clear;
10289 int length = 0;
10290 int do_mode;
10291 int attr;
10292 int nwr_save;
10293#ifdef FEAT_INS_EXPAND
10294 int sub_attr;
10295#endif
10296
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010297 do_mode = ((p_smd && msg_silent == 0)
10298 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010299 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010300 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010301 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 {
10303 /*
10304 * Don't show mode right now, when not redrawing or inside a mapping.
10305 * Call char_avail() only when we are going to show something, because
10306 * it takes a bit of time.
10307 */
10308 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10309 {
10310 redraw_cmdline = TRUE; /* show mode later */
10311 return 0;
10312 }
10313
10314 nwr_save = need_wait_return;
10315
10316 /* wait a bit before overwriting an important message */
10317 check_for_delay(FALSE);
10318
10319 /* if the cmdline is more than one line high, erase top lines */
10320 need_clear = clear_cmdline;
10321 if (clear_cmdline && cmdline_row < Rows - 1)
10322 msg_clr_cmdline(); /* will reset clear_cmdline */
10323
10324 /* Position on the last line in the window, column 0 */
10325 msg_pos_mode();
10326 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010327 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 if (do_mode)
10329 {
10330 MSG_PUTS_ATTR("--", attr);
10331#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010332 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010333# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010334 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010335# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010336 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010337# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010338 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010339# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 MSG_PUTS_ATTR(" IM", attr);
10341# else
10342 MSG_PUTS_ATTR(" XIM", attr);
10343# endif
10344#endif
10345#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10346 if (gui.in_use)
10347 {
10348 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010349 {
10350 /* HANGUL */
10351 if (enc_utf8)
10352 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10353 else
10354 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 }
10357#endif
10358#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010359 /* CTRL-X in Insert mode */
10360 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 {
10362 /* These messages can get long, avoid a wrap in a narrow
10363 * window. Prefer showing edit_submode_extra. */
10364 length = (Rows - msg_row) * Columns - 3;
10365 if (edit_submode_extra != NULL)
10366 length -= vim_strsize(edit_submode_extra);
10367 if (length > 0)
10368 {
10369 if (edit_submode_pre != NULL)
10370 length -= vim_strsize(edit_submode_pre);
10371 if (length - vim_strsize(edit_submode) > 0)
10372 {
10373 if (edit_submode_pre != NULL)
10374 msg_puts_attr(edit_submode_pre, attr);
10375 msg_puts_attr(edit_submode, attr);
10376 }
10377 if (edit_submode_extra != NULL)
10378 {
10379 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10380 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010381 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 else
10383 sub_attr = attr;
10384 msg_puts_attr(edit_submode_extra, sub_attr);
10385 }
10386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 }
10388 else
10389#endif
10390 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 if (State & VREPLACE_FLAG)
10392 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010393 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10395 else if (State & INSERT)
10396 {
10397#ifdef FEAT_RIGHTLEFT
10398 if (p_ri)
10399 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10400#endif
10401 MSG_PUTS_ATTR(_(" INSERT"), attr);
10402 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010403 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 MSG_PUTS_ATTR(_(" (insert)"), attr);
10405 else if (restart_edit == 'R')
10406 MSG_PUTS_ATTR(_(" (replace)"), attr);
10407 else if (restart_edit == 'V')
10408 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10409#ifdef FEAT_RIGHTLEFT
10410 if (p_hkmap)
10411 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10412# ifdef FEAT_FKMAP
10413 if (p_fkmap)
10414 MSG_PUTS_ATTR(farsi_text_5, attr);
10415# endif
10416#endif
10417#ifdef FEAT_KEYMAP
10418 if (State & LANGMAP)
10419 {
10420# ifdef FEAT_ARABIC
10421 if (curwin->w_p_arab)
10422 MSG_PUTS_ATTR(_(" Arabic"), attr);
10423 else
10424# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010425 if (get_keymap_str(curwin, (char_u *)" (%s)",
10426 NameBuff, MAXPATHL))
10427 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 }
10429#endif
10430 if ((State & INSERT) && p_paste)
10431 MSG_PUTS_ATTR(_(" (paste)"), attr);
10432
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 if (VIsual_active)
10434 {
10435 char *p;
10436
10437 /* Don't concatenate separate words to avoid translation
10438 * problems. */
10439 switch ((VIsual_select ? 4 : 0)
10440 + (VIsual_mode == Ctrl_V) * 2
10441 + (VIsual_mode == 'V'))
10442 {
10443 case 0: p = N_(" VISUAL"); break;
10444 case 1: p = N_(" VISUAL LINE"); break;
10445 case 2: p = N_(" VISUAL BLOCK"); break;
10446 case 4: p = N_(" SELECT"); break;
10447 case 5: p = N_(" SELECT LINE"); break;
10448 default: p = N_(" SELECT BLOCK"); break;
10449 }
10450 MSG_PUTS_ATTR(_(p), attr);
10451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 MSG_PUTS_ATTR(" --", attr);
10453 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010454
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 need_clear = TRUE;
10456 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010457 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458#ifdef FEAT_INS_EXPAND
10459 && edit_submode == NULL /* otherwise it gets too long */
10460#endif
10461 )
10462 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010463 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 need_clear = TRUE;
10465 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010466
10467 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 if (need_clear || clear_cmdline)
10469 msg_clr_eos();
10470 msg_didout = FALSE; /* overwrite this message */
10471 length = msg_col;
10472 msg_col = 0;
10473 need_wait_return = nwr_save; /* never ask for hit-return for this */
10474 }
10475 else if (clear_cmdline && msg_silent == 0)
10476 /* Clear the whole command line. Will reset "clear_cmdline". */
10477 msg_clr_cmdline();
10478
10479#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 /* In Visual mode the size of the selected area must be redrawn. */
10481 if (VIsual_active)
10482 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483
10484 /* If the last window has no status line, the ruler is after the mode
10485 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010486 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010487 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488#endif
10489 redraw_cmdline = FALSE;
10490 clear_cmdline = FALSE;
10491
10492 return length;
10493}
10494
10495/*
10496 * Position for a mode message.
10497 */
10498 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010499msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
10501 msg_col = 0;
10502 msg_row = Rows - 1;
10503}
10504
10505/*
10506 * Delete mode message. Used when ESC is typed which is expected to end
10507 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010508 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 */
10510 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010511unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512{
10513 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010514 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 */
10516 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10517 redraw_cmdline = TRUE; /* delete mode later */
10518 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010519 clearmode();
10520}
10521
10522/*
10523 * Clear the mode message.
10524 */
10525 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010526clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010527{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010528 int save_msg_row = msg_row;
10529 int save_msg_col = msg_col;
10530
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010531 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010532 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010533 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010534 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010535
10536 msg_col = save_msg_col;
10537 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538}
10539
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010540 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010541recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010542{
10543 MSG_PUTS_ATTR(_("recording"), attr);
10544 if (!shortmess(SHM_RECORDING))
10545 {
10546 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010547 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010548 MSG_PUTS_ATTR(s, attr);
10549 }
10550}
10551
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010552/*
10553 * Draw the tab pages line at the top of the Vim window.
10554 */
10555 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010556draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010557{
10558 int tabcount = 0;
10559 tabpage_T *tp;
10560 int tabwidth;
10561 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010562 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010563 int attr;
10564 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010565 win_T *cwp;
10566 int wincount;
10567 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010568 int c;
10569 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010570 int attr_sel = HL_ATTR(HLF_TPS);
10571 int attr_nosel = HL_ATTR(HLF_TP);
10572 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010573 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010574 int room;
10575 int use_sep_chars = (t_colors < 8
10576#ifdef FEAT_GUI
10577 && !gui.in_use
10578#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010579#ifdef FEAT_TERMGUICOLORS
10580 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010581#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010582 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010583
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010584 if (ScreenLines == NULL)
10585 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010586 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010587
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010588#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010589 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010590 if (gui_use_tabline())
10591 {
10592 gui_update_tabline();
10593 return;
10594 }
10595#endif
10596
10597 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010598 return;
10599
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010600#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010601
10602 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10603 for (scol = 0; scol < Columns; ++scol)
10604 TabPageIdxs[scol] = 0;
10605
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010606 /* Use the 'tabline' option if it's set. */
10607 if (*p_tal != NUL)
10608 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010609 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010610
10611 /* Check for an error. If there is one we would loop in redrawing the
10612 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010613 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010614 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010615 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010616 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010617 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010618 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010619 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010620 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010621#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010622 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010623 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010624 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010625
Bram Moolenaar238a5642006-02-21 22:12:05 +000010626 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10627 if (tabwidth < 6)
10628 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010629
Bram Moolenaar238a5642006-02-21 22:12:05 +000010630 attr = attr_nosel;
10631 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010632 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010633 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10634 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010635 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010636 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010637
Bram Moolenaar238a5642006-02-21 22:12:05 +000010638 if (tp->tp_topframe == topframe)
10639 attr = attr_sel;
10640 if (use_sep_chars && col > 0)
10641 screen_putchar('|', 0, col++, attr);
10642
10643 if (tp->tp_topframe != topframe)
10644 attr = attr_nosel;
10645
10646 screen_putchar(' ', 0, col++, attr);
10647
10648 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010649 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010650 cwp = curwin;
10651 wp = firstwin;
10652 }
10653 else
10654 {
10655 cwp = tp->tp_curwin;
10656 wp = tp->tp_firstwin;
10657 }
10658
10659 modified = FALSE;
10660 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10661 if (bufIsChanged(wp->w_buffer))
10662 modified = TRUE;
10663 if (modified || wincount > 1)
10664 {
10665 if (wincount > 1)
10666 {
10667 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010668 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010669 if (col + len >= Columns - 3)
10670 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010671 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010672#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010673 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010674#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010675 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010676#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010677 );
10678 col += len;
10679 }
10680 if (modified)
10681 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10682 screen_putchar(' ', 0, col++, attr);
10683 }
10684
10685 room = scol - col + tabwidth - 1;
10686 if (room > 0)
10687 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010688 /* Get buffer name in NameBuff[] */
10689 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010690 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010691 len = vim_strsize(NameBuff);
10692 p = NameBuff;
10693#ifdef FEAT_MBYTE
10694 if (has_mbyte)
10695 while (len > room)
10696 {
10697 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010698 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010699 }
10700 else
10701#endif
10702 if (len > room)
10703 {
10704 p += len - room;
10705 len = room;
10706 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010707 if (len > Columns - col - 1)
10708 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010709
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010710 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010711 col += len;
10712 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010713 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010714
10715 /* Store the tab page number in TabPageIdxs[], so that
10716 * jump_to_mouse() knows where each one is. */
10717 ++tabcount;
10718 while (scol < col)
10719 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010720 }
10721
Bram Moolenaar238a5642006-02-21 22:12:05 +000010722 if (use_sep_chars)
10723 c = '_';
10724 else
10725 c = ' ';
10726 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010727
10728 /* Put an "X" for closing the current tab if there are several. */
10729 if (first_tabpage->tp_next != NULL)
10730 {
10731 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10732 TabPageIdxs[Columns - 1] = -999;
10733 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010734 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010735
10736 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10737 * set. */
10738 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010739}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010740
10741/*
10742 * Get buffer name for "buf" into NameBuff[].
10743 * Takes care of special buffer names and translates special characters.
10744 */
10745 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010746get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010747{
10748 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010749 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010750 else
10751 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10752 trans_characters(NameBuff, MAXPATHL);
10753}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010754
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755/*
10756 * Get the character to use in a status line. Get its attributes in "*attr".
10757 */
10758 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010759fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760{
10761 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010762
10763#ifdef FEAT_TERMINAL
10764 if (bt_terminal(wp->w_buffer))
10765 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010766 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010767 {
10768 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010769 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010770 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010771 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010772 {
10773 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010774 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010775 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010776 }
10777 else
10778#endif
10779 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010781 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 fill = fill_stl;
10783 }
10784 else
10785 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010786 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 fill = fill_stlnc;
10788 }
10789 /* Use fill when there is highlighting, and highlighting of current
10790 * window differs, or the fillchars differ, or this is not the
10791 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010792 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010793 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 || (fill_stl != fill_stlnc)))
10795 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010796 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 return '^';
10798 return '=';
10799}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801/*
10802 * Get the character to use in a separator between vertically split windows.
10803 * Get its attributes in "*attr".
10804 */
10805 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010806fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010808 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 if (*attr == 0 && fill_vert == ' ')
10810 return '|';
10811 else
10812 return fill_vert;
10813}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814
10815/*
10816 * Return TRUE if redrawing should currently be done.
10817 */
10818 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010819redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010821#ifdef FEAT_EVAL
10822 if (disable_redraw_for_testing)
10823 return 0;
10824 else
10825#endif
10826 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10828}
10829
10830/*
10831 * Return TRUE if printing messages should currently be done.
10832 */
10833 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010834messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835{
10836 return (!(p_lz && char_avail() && !KeyTyped));
10837}
10838
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010839#ifdef FEAT_MENU
10840/*
10841 * Draw the window toolbar.
10842 */
10843 static void
10844redraw_win_toolbar(win_T *wp)
10845{
10846 vimmenu_T *menu;
10847 int item_idx = 0;
10848 int item_count = 0;
10849 int col = 0;
10850 int next_col;
10851 int off = (int)(current_ScreenLine - ScreenLines);
10852 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10853 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10854
10855 vim_free(wp->w_winbar_items);
10856 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10857 ++item_count;
10858 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10859 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10860
10861 /* TODO: use fewer spaces if there is not enough room */
10862 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010863 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010864 {
10865 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010866 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010867 break;
10868 if (col > 1)
10869 {
10870 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010871 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010872 break;
10873 }
10874
10875 wp->w_winbar_items[item_idx].wb_startcol = col;
10876 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010877 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010878 break;
10879
10880 next_col = text_to_screenline(wp, menu->name, col);
10881 while (col < next_col)
10882 {
10883 ScreenAttrs[off + col] = button_attr;
10884 ++col;
10885 }
10886 wp->w_winbar_items[item_idx].wb_endcol = col;
10887 wp->w_winbar_items[item_idx].wb_menu = menu;
10888 ++item_idx;
10889
Bram Moolenaar02631462017-09-22 15:20:32 +020010890 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010891 break;
10892 space_to_screenline(off + col, button_attr);
10893 ++col;
10894 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010895 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010896 {
10897 space_to_screenline(off + col, fill_attr);
10898 ++col;
10899 }
10900 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10901
Bram Moolenaar02631462017-09-22 15:20:32 +020010902 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10903 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010904}
10905#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010906
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907/*
10908 * Show current status info in ruler and various other places
10909 * If always is FALSE, only show ruler if position has changed.
10910 */
10911 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010912showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913{
10914 if (!always && !redrawing())
10915 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010916#ifdef FEAT_INS_EXPAND
10917 if (pum_visible())
10918 {
10919 /* Don't redraw right now, do it later. */
10920 curwin->w_redr_status = TRUE;
10921 return;
10922 }
10923#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010924#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010925 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010926 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010927 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 else
10930#endif
10931#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010932 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933#endif
10934
10935#ifdef FEAT_TITLE
10936 if (need_maketitle
10937# ifdef FEAT_STL_OPT
10938 || (p_icon && (stl_syntax & STL_IN_ICON))
10939 || (p_title && (stl_syntax & STL_IN_TITLE))
10940# endif
10941 )
10942 maketitle();
10943#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010944 /* Redraw the tab pages line if needed. */
10945 if (redraw_tabline)
10946 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947}
10948
10949#ifdef FEAT_CMDL_INFO
10950 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010951win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010953#define RULER_BUF_LEN 70
10954 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 int row;
10956 int fillchar;
10957 int attr;
10958 int empty_line = FALSE;
10959 colnr_T virtcol;
10960 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010961 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 int this_ru_col;
10964 int off = 0;
10965 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966
10967 /* If 'ruler' off or redrawing disabled, don't do anything */
10968 if (!p_ru)
10969 return;
10970
10971 /*
10972 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10973 * after deleting lines, before cursor.lnum is corrected.
10974 */
10975 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10976 return;
10977
10978#ifdef FEAT_INS_EXPAND
10979 /* Don't draw the ruler while doing insert-completion, it might overwrite
10980 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 if (edit_submode != NULL)
10983 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010984 // Don't draw the ruler when the popup menu is visible, it may overlap.
10985 // Except when the popup menu will be redrawn anyway.
10986 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010987 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988#endif
10989
10990#ifdef FEAT_STL_OPT
10991 if (*p_ruf)
10992 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010993 int save_called_emsg = called_emsg;
10994
10995 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010997 if (called_emsg)
10998 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010999 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011000 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 return;
11002 }
11003#endif
11004
11005 /*
11006 * Check if not in Insert mode and the line is empty (will show "0-1").
11007 */
11008 if (!(State & INSERT)
11009 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11010 empty_line = TRUE;
11011
11012 /*
11013 * Only draw the ruler when something changed.
11014 */
11015 validate_virtcol_win(wp);
11016 if ( redraw_cmdline
11017 || always
11018 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11019 || wp->w_cursor.col != wp->w_ru_cursor.col
11020 || wp->w_virtcol != wp->w_ru_virtcol
11021#ifdef FEAT_VIRTUALEDIT
11022 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11023#endif
11024 || wp->w_topline != wp->w_ru_topline
11025 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11026#ifdef FEAT_DIFF
11027 || wp->w_topfill != wp->w_ru_topfill
11028#endif
11029 || empty_line != wp->w_ru_empty)
11030 {
11031 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 if (wp->w_status_height)
11033 {
11034 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011035 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011036 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011037 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 }
11039 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 {
11041 row = Rows - 1;
11042 fillchar = ' ';
11043 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 width = Columns;
11045 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 }
11047
11048 /* In list mode virtcol needs to be recomputed */
11049 virtcol = wp->w_virtcol;
11050 if (wp->w_p_list && lcs_tab1 == NUL)
11051 {
11052 wp->w_p_list = FALSE;
11053 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11054 wp->w_p_list = TRUE;
11055 }
11056
11057 /*
11058 * Some sprintfs return the length, some return a pointer.
11059 * To avoid portability problems we use strlen() here.
11060 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011061 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11063 ? 0L
11064 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011065 len = STRLEN(buffer);
11066 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11068 (int)virtcol + 1);
11069
11070 /*
11071 * Add a "50%" if there is room for it.
11072 * On the last line, don't print in the last column (scrolls the
11073 * screen up on some terminals).
11074 */
11075 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011076 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 this_ru_col = ru_col - (Columns - width);
11081 if (this_ru_col < 0)
11082 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 /* Never use more than half the window/screen width, leave the other
11084 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011085 if (this_ru_col < (width + 1) / 2)
11086 this_ru_col = (width + 1) / 2;
11087 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011089 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011090 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 {
11092#ifdef FEAT_MBYTE
11093 if (has_mbyte)
11094 i += (*mb_char2bytes)(fillchar, buffer + i);
11095 else
11096#endif
11097 buffer[i++] = fillchar;
11098 ++o;
11099 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011100 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 }
11102 /* Truncate at window boundary. */
11103#ifdef FEAT_MBYTE
11104 if (has_mbyte)
11105 {
11106 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011107 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 {
11109 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011110 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 {
11112 buffer[i] = NUL;
11113 break;
11114 }
11115 }
11116 }
11117 else
11118#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011119 if (this_ru_col + (int)STRLEN(buffer) > width)
11120 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121
Bram Moolenaar4033c552017-09-16 20:54:51 +020011122 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 i = redraw_cmdline;
11124 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011125 this_ru_col + off + (int)STRLEN(buffer),
11126 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127 fillchar, fillchar, attr);
11128 /* don't redraw the cmdline because of showing the ruler */
11129 redraw_cmdline = i;
11130 wp->w_ru_cursor = wp->w_cursor;
11131 wp->w_ru_virtcol = wp->w_virtcol;
11132 wp->w_ru_empty = empty_line;
11133 wp->w_ru_topline = wp->w_topline;
11134 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11135#ifdef FEAT_DIFF
11136 wp->w_ru_topfill = wp->w_topfill;
11137#endif
11138 }
11139}
11140#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011141
11142#if defined(FEAT_LINEBREAK) || defined(PROTO)
11143/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011144 * Return the width of the 'number' and 'relativenumber' column.
11145 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011146 * Otherwise it depends on 'numberwidth' and the line count.
11147 */
11148 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011149number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011150{
11151 int n;
11152 linenr_T lnum;
11153
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011154 if (wp->w_p_rnu && !wp->w_p_nu)
11155 /* cursor line shows "0" */
11156 lnum = wp->w_height;
11157 else
11158 /* cursor line shows absolute line number */
11159 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011160
Bram Moolenaar6b314672015-03-20 15:42:10 +010011161 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011162 return wp->w_nrwidth_width;
11163 wp->w_nrwidth_line_count = lnum;
11164
11165 n = 0;
11166 do
11167 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011168 lnum /= 10;
11169 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011170 } while (lnum > 0);
11171
11172 /* 'numberwidth' gives the minimal width plus one */
11173 if (n < wp->w_p_nuw - 1)
11174 n = wp->w_p_nuw - 1;
11175
11176 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011177 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011178 return n;
11179}
11180#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011181
11182/*
11183 * Return the current cursor column. This is the actual position on the
11184 * screen. First column is 0.
11185 */
11186 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011187screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011188{
11189 return screen_cur_col;
11190}
11191
11192/*
11193 * Return the current cursor row. This is the actual position on the screen.
11194 * First row is 0.
11195 */
11196 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011197screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011198{
11199 return screen_cur_row;
11200}