blob: c9f9410b6a7b1e195561ba9df4ee1bb83cf99495 [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)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200450 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100451 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200452 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 // Don't redraw when in prompt_for_number().
454 if (cmdline_row > 0)
455 {
456 // Redrawing only works when the screen didn't scroll. Don't clear
457 // wildmenu entries.
458 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200459#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200460 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200461#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 && call_update_screen)
463 update_screen(0);
464
465 // Redraw in the same position, so that the user can continue
466 // editing the command.
467 redrawcmdline_ex(FALSE);
468 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200469 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200470 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100471 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200473 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100474 setcursor();
475 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100478 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200479 // Don't update the cursor when it is blinking and off to avoid
480 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100481 out_flush_cursor(FALSE, FALSE);
482 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100483#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100484 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200485
486 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100487}
488
489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Changed something in the current window, at buffer line "lnum", that
491 * requires that line and possibly other lines to be redrawn.
492 * Used when entering/leaving Insert mode with the cursor on a folded line.
493 * Used to remove the "$" from a change command.
494 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
495 * may become invalid and the whole window will have to be redrawn.
496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100498redrawWinline(
499 linenr_T lnum,
500 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
502#ifdef FEAT_FOLDING
503 int i;
504#endif
505
506 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
507 curwin->w_redraw_top = lnum;
508 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
509 curwin->w_redraw_bot = lnum;
510 redraw_later(VALID);
511
512#ifdef FEAT_FOLDING
513 if (invalid)
514 {
515 /* A w_lines[] entry for this lnum has become invalid. */
516 i = find_wl_entry(curwin, lnum);
517 if (i >= 0)
518 curwin->w_lines[i].wl_valid = FALSE;
519 }
520#endif
521}
522
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200523 void
524reset_updating_screen(int may_resize_shell UNUSED)
525{
526 updating_screen = FALSE;
527#ifdef FEAT_GUI
528 if (may_resize_shell)
529 gui_may_resize_shell();
530#endif
531#ifdef FEAT_TERMINAL
532 term_check_channel_closed_recently();
533#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200534
535#ifdef HAVE_DROP_FILE
536 // If handle_drop() was called while updating_screen was TRUE need to
537 // handle the drop now.
538 handle_any_postponed_drop();
539#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200540}
541
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200543 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 */
545 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100546update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
548 redraw_curbuf_later(type);
549 update_screen(type);
550}
551
552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 * Based on the current value of curwin->w_topline, transfer a screenfull
554 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200555 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200557 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200558update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200560 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 win_T *wp;
562 static int did_intro = FALSE;
563#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
564 int did_one;
565#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200566#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200567 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200568 int gui_cursor_col;
569 int gui_cursor_row;
570#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200571 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000573 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200575 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200577 if (type == VALID_NO_UPDATE)
578 {
579 no_update = TRUE;
580 type = 0;
581 }
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (must_redraw)
584 {
585 if (type < must_redraw) /* use maximal type */
586 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000587
588 /* must_redraw is reset here, so that when we run into some weird
589 * reason to redraw while busy redrawing (e.g., asynchronous
590 * scrolling), or update_topline() in win_update() will cause a
591 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 must_redraw = 0;
593 }
594
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200595 /* May need to update w_lines[]. */
596 if (curwin->w_lines_valid == 0 && type < NOT_VALID
597#ifdef FEAT_TERMINAL
598 && !term_do_update_window(curwin)
599#endif
600 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 type = NOT_VALID;
602
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000603 /* Postpone the redrawing when it's not needed and when being called
604 * recursively. */
605 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {
607 redraw_later(type); /* remember type for next time */
608 must_redraw = type;
609 if (type > INVERTED_ALL)
610 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200611 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 }
613
614 updating_screen = TRUE;
615#ifdef FEAT_SYN_HL
616 ++display_tick; /* let syntax code know we're in a next round of
617 * display updating */
618#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200619 if (no_update)
620 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
622 /*
623 * if the screen was scrolled up when displaying a message, scroll it down
624 */
625 if (msg_scrolled)
626 {
627 clear_cmdline = TRUE;
628 if (msg_scrolled > Rows - 5) /* clearing is faster */
629 type = CLEAR;
630 else if (type != CLEAR)
631 {
632 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200633 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
634 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 type = CLEAR;
636 FOR_ALL_WINDOWS(wp)
637 {
638 if (W_WINROW(wp) < msg_scrolled)
639 {
640 if (W_WINROW(wp) + wp->w_height > msg_scrolled
641 && wp->w_redr_type < REDRAW_TOP
642 && wp->w_lines_valid > 0
643 && wp->w_topline == wp->w_lines[0].wl_lnum)
644 {
645 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
646 wp->w_redr_type = REDRAW_TOP;
647 }
648 else
649 {
650 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200651 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
652 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
655 }
656 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200657 if (!no_update)
658 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000659 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
661 msg_scrolled = 0;
662 need_wait_return = FALSE;
663 }
664
665 /* reset cmdline_row now (may have been changed temporarily) */
666 compute_cmdrow();
667
668 /* Check for changed highlighting */
669 if (need_highlight_changed)
670 highlight_changed();
671
672 if (type == CLEAR) /* first clear screen */
673 {
674 screenclear(); /* will reset clear_cmdline */
675 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200676 /* must_redraw may be set indirectly, avoid another redraw later */
677 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 }
679
680 if (clear_cmdline) /* going to clear cmdline (done below) */
681 check_for_delay(FALSE);
682
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000683#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200684 /* Force redraw when width of 'number' or 'relativenumber' column
685 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000686 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200687 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
688 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000689 curwin->w_redr_type = NOT_VALID;
690#endif
691
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 /*
693 * Only start redrawing if there is really something to do.
694 */
695 if (type == INVERTED)
696 update_curswant();
697 if (curwin->w_redr_type < type
698 && !((type == VALID
699 && curwin->w_lines[0].wl_valid
700#ifdef FEAT_DIFF
701 && curwin->w_topfill == curwin->w_old_topfill
702 && curwin->w_botfill == curwin->w_old_botfill
703#endif
704 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000706 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
708 && curwin->w_old_visual_mode == VIsual_mode
709 && (curwin->w_valid & VALID_VIRTCOL)
710 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 ))
712 curwin->w_redr_type = type;
713
Bram Moolenaar5a305422006-04-28 22:38:25 +0000714 /* Redraw the tab pages line if needed. */
715 if (redraw_tabline || type >= NOT_VALID)
716 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000717
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#ifdef FEAT_SYN_HL
719 /*
720 * Correct stored syntax highlighting info for changes in each displayed
721 * buffer. Each buffer must only be done once.
722 */
723 FOR_ALL_WINDOWS(wp)
724 {
725 if (wp->w_buffer->b_mod_set)
726 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 win_T *wwp;
728
729 /* Check if we already did this buffer. */
730 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
731 if (wwp->w_buffer == wp->w_buffer)
732 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200733 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 syn_stack_apply_changes(wp->w_buffer);
735 }
736 }
737#endif
738
739 /*
740 * Go from top to bottom through the windows, redrawing the ones that need
741 * it.
742 */
743#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
744 did_one = FALSE;
745#endif
746#ifdef FEAT_SEARCH_EXTRA
747 search_hl.rm.regprog = NULL;
748#endif
749 FOR_ALL_WINDOWS(wp)
750 {
751 if (wp->w_redr_type != 0)
752 {
753 cursor_off();
754#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
755 if (!did_one)
756 {
757 did_one = TRUE;
758# ifdef FEAT_SEARCH_EXTRA
759 start_search_hl();
760# endif
761# ifdef FEAT_CLIPBOARD
762 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200763 if (clip_star.available && clip_isautosel_star())
764 clip_update_selection(&clip_star);
765 if (clip_plus.available && clip_isautosel_plus())
766 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767# endif
768#ifdef FEAT_GUI
769 /* Remove the cursor before starting to do anything, because
770 * scrolling may make it difficult to redraw the text under
771 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200772 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200773 {
774 gui_cursor_col = gui.cursor_col;
775 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200777 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779#endif
780 }
781#endif
782 win_update(wp);
783 }
784
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 /* redraw status line after the window to minimize cursor movement */
786 if (wp->w_redr_status)
787 {
788 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200789 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
792#if defined(FEAT_SEARCH_EXTRA)
793 end_search_hl();
794#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100795#ifdef FEAT_INS_EXPAND
796 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200797 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 /* Reset b_mod_set flags. Going through all windows is probably faster
801 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200802 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200805 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
807 /* Clear or redraw the command line. Done last, because scrolling may
808 * mess up the command line. */
809 if (clear_cmdline || redraw_cmdline)
810 showmode();
811
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200812 if (no_update)
813 --no_win_do_lines_ins;
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200816 if (!did_intro)
817 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 did_intro = TRUE;
819
820#ifdef FEAT_GUI
821 /* Redraw the cursor and update the scrollbars when all screen updating is
822 * done. */
823 if (gui.in_use)
824 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200825 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200826 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100827 mch_disable_flush();
828 out_flush(); /* required before updating the cursor */
829 mch_enable_flush();
830
Bram Moolenaar144445d2016-07-08 21:41:54 +0200831 /* Put the GUI position where the cursor was, gui_update_cursor()
832 * uses that. */
833 gui.col = gui_cursor_col;
834 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200835# ifdef FEAT_MBYTE
836 gui.col = mb_fix_col(gui.col, gui.row);
837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100839 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200840 screen_cur_col = gui.col;
841 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200842 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100843 else
844 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 gui_update_scrollbars(FALSE);
846 }
847#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200848 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849}
850
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100851#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
852/*
853 * Prepare for updating one or more windows.
854 * Caller must check for "updating_screen" already set to avoid recursiveness.
855 */
856 static void
857update_prepare(void)
858{
859 cursor_off();
860 updating_screen = TRUE;
861#ifdef FEAT_GUI
862 /* Remove the cursor before starting to do anything, because scrolling may
863 * make it difficult to redraw the text under it. */
864 if (gui.in_use)
865 gui_undraw_cursor();
866#endif
867#ifdef FEAT_SEARCH_EXTRA
868 start_search_hl();
869#endif
870}
871
872/*
873 * Finish updating one or more windows.
874 */
875 static void
876update_finish(void)
877{
878 if (redraw_cmdline)
879 showmode();
880
881# ifdef FEAT_SEARCH_EXTRA
882 end_search_hl();
883# endif
884
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200885 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100886
887# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100888 /* Redraw the cursor and update the scrollbars when all screen updating is
889 * done. */
890 if (gui.in_use)
891 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100892 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100893 gui_update_scrollbars(FALSE);
894 }
895# endif
896}
897#endif
898
Bram Moolenaar860cae12010-06-05 23:22:07 +0200899#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200900/*
901 * Return TRUE if the cursor line in window "wp" may be concealed, according
902 * to the 'concealcursor' option.
903 */
904 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100905conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200906{
907 int c;
908
909 if (*wp->w_p_cocu == NUL)
910 return FALSE;
911 if (get_real_state() & VISUAL)
912 c = 'v';
913 else if (State & INSERT)
914 c = 'i';
915 else if (State & NORMAL)
916 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200917 else if (State & CMDLINE)
918 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200919 else
920 return FALSE;
921 return vim_strchr(wp->w_p_cocu, c) != NULL;
922}
923
924/*
925 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
926 */
927 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200928conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200929{
930 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
931 {
932 need_cursor_line_redraw = TRUE;
933 /* Need to recompute cursor column, e.g., when starting Visual mode
934 * without concealing. */
935 curs_columns(TRUE);
936 }
937}
938
Bram Moolenaar860cae12010-06-05 23:22:07 +0200939 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100940update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200941{
942 int row;
943 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200944#ifdef SYN_TIME_LIMIT
945 proftime_T syntax_tm;
946#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200947
Bram Moolenaar908be432016-05-24 10:51:30 +0200948 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100949 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200950 return;
951
Bram Moolenaar860cae12010-06-05 23:22:07 +0200952 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200953 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200954 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200955#ifdef SYN_TIME_LIMIT
956 /* Set the time limit to 'redrawtime'. */
957 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200958 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200959#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100960 update_prepare();
961
Bram Moolenaar860cae12010-06-05 23:22:07 +0200962 row = 0;
963 for (j = 0; j < wp->w_lines_valid; ++j)
964 {
965 if (lnum == wp->w_lines[j].wl_lnum)
966 {
967 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200968# ifdef FEAT_SEARCH_EXTRA
969 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200970 start_search_hl();
971 prepare_search_hl(wp, lnum);
972# endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200973 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200974# if defined(FEAT_SEARCH_EXTRA)
975 end_search_hl();
976# endif
977 break;
978 }
979 row += wp->w_lines[j].wl_size;
980 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100981
982 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200983
984#ifdef SYN_TIME_LIMIT
985 syn_set_timeout(NULL);
986#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200987 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200988 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990#endif
991
992#if defined(FEAT_SIGNS) || defined(PROTO)
993 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100994update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
996 win_T *wp;
997 int doit = FALSE;
998
999# ifdef FEAT_FOLDING
1000 win_foldinfo.fi_level = 0;
1001# endif
1002
1003 /* update/delete a specific mark */
1004 FOR_ALL_WINDOWS(wp)
1005 {
1006 if (buf != NULL && lnum > 0)
1007 {
1008 if (wp->w_buffer == buf && lnum >= wp->w_topline
1009 && lnum < wp->w_botline)
1010 {
1011 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1012 wp->w_redraw_top = lnum;
1013 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1014 wp->w_redraw_bot = lnum;
1015 redraw_win_later(wp, VALID);
1016 }
1017 }
1018 else
1019 redraw_win_later(wp, VALID);
1020 if (wp->w_redr_type != 0)
1021 doit = TRUE;
1022 }
1023
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001024 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001025 * happening (recursive call), messages on the screen or still starting up.
1026 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001027 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001028 || State == ASKMORE || State == HITRETURN
1029 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001030#ifdef FEAT_GUI
1031 || gui.starting
1032#endif
1033 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 return;
1035
1036 /* update all windows that need updating */
1037 update_prepare();
1038
Bram Moolenaar29323592016-07-24 22:04:11 +02001039 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
1041 if (wp->w_redr_type != 0)
1042 win_update(wp);
1043 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001044 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
1047 update_finish();
1048}
1049#endif
1050
1051
1052#if defined(FEAT_GUI) || defined(PROTO)
1053/*
1054 * Update a single window, its status line and maybe the command line msg.
1055 * Used for the GUI scrollbar.
1056 */
1057 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001058updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001060 /* return if already busy updating */
1061 if (updating_screen)
1062 return;
1063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 update_prepare();
1065
1066#ifdef FEAT_CLIPBOARD
1067 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001068 if (clip_star.available && clip_isautosel_star())
1069 clip_update_selection(&clip_star);
1070 if (clip_plus.available && clip_isautosel_plus())
1071 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001075
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001076 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001077 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001078 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 if (wp->w_redr_status
1081# ifdef FEAT_CMDL_INFO
1082 || p_ru
1083# endif
1084# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001085 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086# endif
1087 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001088 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
1090 update_finish();
1091}
1092#endif
1093
1094/*
1095 * Update a single window.
1096 *
1097 * This may cause the windows below it also to be redrawn (when clearing the
1098 * screen or scrolling lines).
1099 *
1100 * How the window is redrawn depends on wp->w_redr_type. Each type also
1101 * implies the one below it.
1102 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001103 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1105 * INVERTED redraw the changed part of the Visual area
1106 * INVERTED_ALL redraw the whole Visual area
1107 * VALID 1. scroll up/down to adjust for a changed w_topline
1108 * 2. update lines at the top when scrolled down
1109 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001110 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 * b_mod_top and b_mod_bot.
1112 * - if wp->w_redraw_top non-zero, redraw lines between
1113 * wp->w_redraw_top and wp->w_redr_bot.
1114 * - continue redrawing when syntax status is invalid.
1115 * 4. if scrolled up, update lines at the bottom.
1116 * This results in three areas that may need updating:
1117 * top: from first row to top_end (when scrolled down)
1118 * mid: from mid_start to mid_end (update inversion or changed text)
1119 * bot: from bot_start to last row (when scrolled up)
1120 */
1121 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001122win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
1124 buf_T *buf = wp->w_buffer;
1125 int type;
1126 int top_end = 0; /* Below last row of the top area that needs
1127 updating. 0 when no top area updating. */
1128 int mid_start = 999;/* first row of the mid area that needs
1129 updating. 999 when no mid area updating. */
1130 int mid_end = 0; /* Below last row of the mid area that needs
1131 updating. 0 when no mid area updating. */
1132 int bot_start = 999;/* first row of the bot area that needs
1133 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 int scrolled_down = FALSE; /* TRUE when scrolled down when
1135 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001137 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 int top_to_mod = FALSE; /* redraw above mod_top */
1139#endif
1140
1141 int row; /* current window row to display */
1142 linenr_T lnum; /* current buffer lnum to display */
1143 int idx; /* current index in w_lines[] */
1144 int srow; /* starting row of the current line */
1145
1146 int eof = FALSE; /* if TRUE, we hit the end of the file */
1147 int didline = FALSE; /* if TRUE, we finished the last line */
1148 int i;
1149 long j;
1150 static int recursive = FALSE; /* being called recursively */
1151 int old_botline = wp->w_botline;
1152#ifdef FEAT_FOLDING
1153 long fold_count;
1154#endif
1155#ifdef FEAT_SYN_HL
1156 /* remember what happened to the previous line, to know if
1157 * check_visual_highlight() can be used */
1158#define DID_NONE 1 /* didn't update a line */
1159#define DID_LINE 2 /* updated a normal line */
1160#define DID_FOLD 3 /* updated a folded line */
1161 int did_update = DID_NONE;
1162 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1163#endif
1164 linenr_T mod_top = 0;
1165 linenr_T mod_bot = 0;
1166#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1167 int save_got_int;
1168#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001169#ifdef SYN_TIME_LIMIT
1170 proftime_T syntax_tm;
1171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172
1173 type = wp->w_redr_type;
1174
1175 if (type == NOT_VALID)
1176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 wp->w_lines_valid = 0;
1179 }
1180
1181 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001182 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
1184 wp->w_redr_type = 0;
1185 return;
1186 }
1187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 /* Window is zero-width: Only need to draw the separator. */
1189 if (wp->w_width == 0)
1190 {
1191 /* draw the vertical separator right of this window */
1192 draw_vsep_win(wp, 0);
1193 wp->w_redr_type = 0;
1194 return;
1195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001197#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001198 // If this window contains a terminal, redraw works completely differently.
1199 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001200 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001201 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001202# ifdef FEAT_MENU
1203 /* Draw the window toolbar, if there is one. */
1204 if (winbar_height(wp) > 0)
1205 redraw_win_toolbar(wp);
1206# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001207 wp->w_redr_type = 0;
1208 return;
1209 }
1210#endif
1211
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001213 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214#endif
1215
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001216#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001217 /* Force redraw when width of 'number' or 'relativenumber' column
1218 * changes. */
1219 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001220 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001221 {
1222 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001223 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001224 }
1225 else
1226#endif
1227
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1229 {
1230 /*
1231 * When there are both inserted/deleted lines and specific lines to be
1232 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1233 * everything (only happens when redrawing is off for while).
1234 */
1235 type = NOT_VALID;
1236 }
1237 else
1238 {
1239 /*
1240 * Set mod_top to the first line that needs displaying because of
1241 * changes. Set mod_bot to the first line after the changes.
1242 */
1243 mod_top = wp->w_redraw_top;
1244 if (wp->w_redraw_bot != 0)
1245 mod_bot = wp->w_redraw_bot + 1;
1246 else
1247 mod_bot = 0;
1248 wp->w_redraw_top = 0; /* reset for next time */
1249 wp->w_redraw_bot = 0;
1250 if (buf->b_mod_set)
1251 {
1252 if (mod_top == 0 || mod_top > buf->b_mod_top)
1253 {
1254 mod_top = buf->b_mod_top;
1255#ifdef FEAT_SYN_HL
1256 /* Need to redraw lines above the change that may be included
1257 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001258 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001260 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 if (mod_top < 1)
1262 mod_top = 1;
1263 }
1264#endif
1265 }
1266 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1267 mod_bot = buf->b_mod_bot;
1268
1269#ifdef FEAT_SEARCH_EXTRA
1270 /* When 'hlsearch' is on and using a multi-line search pattern, a
1271 * change in one line may make the Search highlighting in a
1272 * previous line invalid. Simple solution: redraw all visible
1273 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001274 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001276 if (search_hl.rm.regprog != NULL
1277 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001279 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001280 {
1281 cur = wp->w_match_head;
1282 while (cur != NULL)
1283 {
1284 if (cur->match.regprog != NULL
1285 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001286 {
1287 top_to_mod = TRUE;
1288 break;
1289 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001290 cur = cur->next;
1291 }
1292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293#endif
1294 }
1295#ifdef FEAT_FOLDING
1296 if (mod_top != 0 && hasAnyFolding(wp))
1297 {
1298 linenr_T lnumt, lnumb;
1299
1300 /*
1301 * A change in a line can cause lines above it to become folded or
1302 * unfolded. Find the top most buffer line that may be affected.
1303 * If the line was previously folded and displayed, get the first
1304 * line of that fold. If the line is folded now, get the first
1305 * folded line. Use the minimum of these two.
1306 */
1307
1308 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1309 * the line below it. If there is no valid entry, use w_topline.
1310 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1311 * to this line. If there is no valid entry, use MAXLNUM. */
1312 lnumt = wp->w_topline;
1313 lnumb = MAXLNUM;
1314 for (i = 0; i < wp->w_lines_valid; ++i)
1315 if (wp->w_lines[i].wl_valid)
1316 {
1317 if (wp->w_lines[i].wl_lastlnum < mod_top)
1318 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1319 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1320 {
1321 lnumb = wp->w_lines[i].wl_lnum;
1322 /* When there is a fold column it might need updating
1323 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001324 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 ++lnumb;
1326 }
1327 }
1328
1329 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1330 if (mod_top > lnumt)
1331 mod_top = lnumt;
1332
1333 /* Now do the same for the bottom line (one above mod_bot). */
1334 --mod_bot;
1335 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1336 ++mod_bot;
1337 if (mod_bot < lnumb)
1338 mod_bot = lnumb;
1339 }
1340#endif
1341
1342 /* When a change starts above w_topline and the end is below
1343 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001344 * If the end of the change is above w_topline: do like no change was
1345 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 if (mod_top != 0 && mod_top < wp->w_topline)
1347 {
1348 if (mod_bot > wp->w_topline)
1349 mod_top = wp->w_topline;
1350#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001351 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 top_end = 1;
1353#endif
1354 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001355
1356 /* When line numbers are displayed need to redraw all lines below
1357 * inserted/deleted lines. */
1358 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1359 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361
1362 /*
1363 * When only displaying the lines at the top, set top_end. Used when
1364 * window has scrolled down for msg_scrolled.
1365 */
1366 if (type == REDRAW_TOP)
1367 {
1368 j = 0;
1369 for (i = 0; i < wp->w_lines_valid; ++i)
1370 {
1371 j += wp->w_lines[i].wl_size;
1372 if (j >= wp->w_upd_rows)
1373 {
1374 top_end = j;
1375 break;
1376 }
1377 }
1378 if (top_end == 0)
1379 /* not found (cannot happen?): redraw everything */
1380 type = NOT_VALID;
1381 else
1382 /* top area defined, the rest is VALID */
1383 type = VALID;
1384 }
1385
Bram Moolenaar367329b2007-08-30 11:53:22 +00001386 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001387 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1388 * non-zero and thus not FALSE) will indicate that screenclear() was not
1389 * called. */
1390 if (screen_cleared)
1391 screen_cleared = MAYBE;
1392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 /*
1394 * If there are no changes on the screen that require a complete redraw,
1395 * handle three cases:
1396 * 1: we are off the top of the screen by a few lines: scroll down
1397 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1398 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1399 * w_lines[] that needs updating.
1400 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001401 if ((type == VALID || type == SOME_VALID
1402 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403#ifdef FEAT_DIFF
1404 && !wp->w_botfill && !wp->w_old_botfill
1405#endif
1406 )
1407 {
1408 if (mod_top != 0 && wp->w_topline == mod_top)
1409 {
1410 /*
1411 * w_topline is the first changed line, the scrolling will be done
1412 * further down.
1413 */
1414 }
1415 else if (wp->w_lines[0].wl_valid
1416 && (wp->w_topline < wp->w_lines[0].wl_lnum
1417#ifdef FEAT_DIFF
1418 || (wp->w_topline == wp->w_lines[0].wl_lnum
1419 && wp->w_topfill > wp->w_old_topfill)
1420#endif
1421 ))
1422 {
1423 /*
1424 * New topline is above old topline: May scroll down.
1425 */
1426#ifdef FEAT_FOLDING
1427 if (hasAnyFolding(wp))
1428 {
1429 linenr_T ln;
1430
1431 /* count the number of lines we are off, counting a sequence
1432 * of folded lines as one */
1433 j = 0;
1434 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1435 {
1436 ++j;
1437 if (j >= wp->w_height - 2)
1438 break;
1439 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1440 }
1441 }
1442 else
1443#endif
1444 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1445 if (j < wp->w_height - 2) /* not too far off */
1446 {
1447 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1448#ifdef FEAT_DIFF
1449 /* insert extra lines for previously invisible filler lines */
1450 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1451 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1452 - wp->w_old_topfill;
1453#endif
1454 if (i < wp->w_height - 2) /* less than a screen off */
1455 {
1456 /*
1457 * Try to insert the correct number of lines.
1458 * If not the last window, delete the lines at the bottom.
1459 * win_ins_lines may fail when the terminal can't do it.
1460 */
1461 if (i > 0)
1462 check_for_delay(FALSE);
1463 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1464 {
1465 if (wp->w_lines_valid != 0)
1466 {
1467 /* Need to update rows that are new, stop at the
1468 * first one that scrolled down. */
1469 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471
1472 /* Move the entries that were scrolled, disable
1473 * the entries for the lines to be redrawn. */
1474 if ((wp->w_lines_valid += j) > wp->w_height)
1475 wp->w_lines_valid = wp->w_height;
1476 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1477 wp->w_lines[idx] = wp->w_lines[idx - j];
1478 while (idx >= 0)
1479 wp->w_lines[idx--].wl_valid = FALSE;
1480 }
1481 }
1482 else
1483 mid_start = 0; /* redraw all lines */
1484 }
1485 else
1486 mid_start = 0; /* redraw all lines */
1487 }
1488 else
1489 mid_start = 0; /* redraw all lines */
1490 }
1491 else
1492 {
1493 /*
1494 * New topline is at or below old topline: May scroll up.
1495 * When topline didn't change, find first entry in w_lines[] that
1496 * needs updating.
1497 */
1498
1499 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1500 j = -1;
1501 row = 0;
1502 for (i = 0; i < wp->w_lines_valid; i++)
1503 {
1504 if (wp->w_lines[i].wl_valid
1505 && wp->w_lines[i].wl_lnum == wp->w_topline)
1506 {
1507 j = i;
1508 break;
1509 }
1510 row += wp->w_lines[i].wl_size;
1511 }
1512 if (j == -1)
1513 {
1514 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1515 * lines */
1516 mid_start = 0;
1517 }
1518 else
1519 {
1520 /*
1521 * Try to delete the correct number of lines.
1522 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1523 */
1524#ifdef FEAT_DIFF
1525 /* If the topline didn't change, delete old filler lines,
1526 * otherwise delete filler lines of the new topline... */
1527 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1528 row += wp->w_old_topfill;
1529 else
1530 row += diff_check_fill(wp, wp->w_topline);
1531 /* ... but don't delete new filler lines. */
1532 row -= wp->w_topfill;
1533#endif
1534 if (row > 0)
1535 {
1536 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001537 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1538 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 bot_start = wp->w_height - row;
1540 else
1541 mid_start = 0; /* redraw all lines */
1542 }
1543 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1544 {
1545 /*
1546 * Skip the lines (below the deleted lines) that are still
1547 * valid and don't need redrawing. Copy their info
1548 * upwards, to compensate for the deleted lines. Set
1549 * bot_start to the first row that needs redrawing.
1550 */
1551 bot_start = 0;
1552 idx = 0;
1553 for (;;)
1554 {
1555 wp->w_lines[idx] = wp->w_lines[j];
1556 /* stop at line that didn't fit, unless it is still
1557 * valid (no lines deleted) */
1558 if (row > 0 && bot_start + row
1559 + (int)wp->w_lines[j].wl_size > wp->w_height)
1560 {
1561 wp->w_lines_valid = idx + 1;
1562 break;
1563 }
1564 bot_start += wp->w_lines[idx++].wl_size;
1565
1566 /* stop at the last valid entry in w_lines[].wl_size */
1567 if (++j >= wp->w_lines_valid)
1568 {
1569 wp->w_lines_valid = idx;
1570 break;
1571 }
1572 }
1573#ifdef FEAT_DIFF
1574 /* Correct the first entry for filler lines at the top
1575 * when it won't get updated below. */
1576 if (wp->w_p_diff && bot_start > 0)
1577 wp->w_lines[0].wl_size =
1578 plines_win_nofill(wp, wp->w_topline, TRUE)
1579 + wp->w_topfill;
1580#endif
1581 }
1582 }
1583 }
1584
1585 /* When starting redraw in the first line, redraw all lines. When
1586 * there is only one window it's probably faster to clear the screen
1587 * first. */
1588 if (mid_start == 0)
1589 {
1590 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001591 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001592 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001593 /* Clear the screen when it was not done by win_del_lines() or
1594 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1595 * then. */
1596 if (screen_cleared != TRUE)
1597 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001598 /* The screen was cleared, redraw the tab pages line. */
1599 if (redraw_tabline)
1600 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001603
1604 /* When win_del_lines() or win_ins_lines() caused the screen to be
1605 * cleared (only happens for the first window) or when screenclear()
1606 * was called directly above, "must_redraw" will have been set to
1607 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1608 if (screen_cleared == TRUE)
1609 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611 else
1612 {
1613 /* Not VALID or INVERTED: redraw all lines. */
1614 mid_start = 0;
1615 mid_end = wp->w_height;
1616 }
1617
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001618 if (type == SOME_VALID)
1619 {
1620 /* SOME_VALID: redraw all lines. */
1621 mid_start = 0;
1622 mid_end = wp->w_height;
1623 type = NOT_VALID;
1624 }
1625
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 /* check if we are updating or removing the inverted part */
1627 if ((VIsual_active && buf == curwin->w_buffer)
1628 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1629 {
1630 linenr_T from, to;
1631
1632 if (VIsual_active)
1633 {
1634 if (VIsual_active
1635 && (VIsual_mode != wp->w_old_visual_mode
1636 || type == INVERTED_ALL))
1637 {
1638 /*
1639 * If the type of Visual selection changed, redraw the whole
1640 * selection. Also when the ownership of the X selection is
1641 * gained or lost.
1642 */
1643 if (curwin->w_cursor.lnum < VIsual.lnum)
1644 {
1645 from = curwin->w_cursor.lnum;
1646 to = VIsual.lnum;
1647 }
1648 else
1649 {
1650 from = VIsual.lnum;
1651 to = curwin->w_cursor.lnum;
1652 }
1653 /* redraw more when the cursor moved as well */
1654 if (wp->w_old_cursor_lnum < from)
1655 from = wp->w_old_cursor_lnum;
1656 if (wp->w_old_cursor_lnum > to)
1657 to = wp->w_old_cursor_lnum;
1658 if (wp->w_old_visual_lnum < from)
1659 from = wp->w_old_visual_lnum;
1660 if (wp->w_old_visual_lnum > to)
1661 to = wp->w_old_visual_lnum;
1662 }
1663 else
1664 {
1665 /*
1666 * Find the line numbers that need to be updated: The lines
1667 * between the old cursor position and the current cursor
1668 * position. Also check if the Visual position changed.
1669 */
1670 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1671 {
1672 from = curwin->w_cursor.lnum;
1673 to = wp->w_old_cursor_lnum;
1674 }
1675 else
1676 {
1677 from = wp->w_old_cursor_lnum;
1678 to = curwin->w_cursor.lnum;
1679 if (from == 0) /* Visual mode just started */
1680 from = to;
1681 }
1682
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001683 if (VIsual.lnum != wp->w_old_visual_lnum
1684 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 {
1686 if (wp->w_old_visual_lnum < from
1687 && wp->w_old_visual_lnum != 0)
1688 from = wp->w_old_visual_lnum;
1689 if (wp->w_old_visual_lnum > to)
1690 to = wp->w_old_visual_lnum;
1691 if (VIsual.lnum < from)
1692 from = VIsual.lnum;
1693 if (VIsual.lnum > to)
1694 to = VIsual.lnum;
1695 }
1696 }
1697
1698 /*
1699 * If in block mode and changed column or curwin->w_curswant:
1700 * update all lines.
1701 * First compute the actual start and end column.
1702 */
1703 if (VIsual_mode == Ctrl_V)
1704 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001705 colnr_T fromc, toc;
1706#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1707 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708
Bram Moolenaar404406a2014-10-09 13:24:43 +02001709 if (curwin->w_p_lbr)
1710 ve_flags = VE_ALL;
1711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001713#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1714 ve_flags = save_ve_flags;
1715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 ++toc;
1717 if (curwin->w_curswant == MAXCOL)
1718 toc = MAXCOL;
1719
1720 if (fromc != wp->w_old_cursor_fcol
1721 || toc != wp->w_old_cursor_lcol)
1722 {
1723 if (from > VIsual.lnum)
1724 from = VIsual.lnum;
1725 if (to < VIsual.lnum)
1726 to = VIsual.lnum;
1727 }
1728 wp->w_old_cursor_fcol = fromc;
1729 wp->w_old_cursor_lcol = toc;
1730 }
1731 }
1732 else
1733 {
1734 /* Use the line numbers of the old Visual area. */
1735 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1736 {
1737 from = wp->w_old_cursor_lnum;
1738 to = wp->w_old_visual_lnum;
1739 }
1740 else
1741 {
1742 from = wp->w_old_visual_lnum;
1743 to = wp->w_old_cursor_lnum;
1744 }
1745 }
1746
1747 /*
1748 * There is no need to update lines above the top of the window.
1749 */
1750 if (from < wp->w_topline)
1751 from = wp->w_topline;
1752
1753 /*
1754 * If we know the value of w_botline, use it to restrict the update to
1755 * the lines that are visible in the window.
1756 */
1757 if (wp->w_valid & VALID_BOTLINE)
1758 {
1759 if (from >= wp->w_botline)
1760 from = wp->w_botline - 1;
1761 if (to >= wp->w_botline)
1762 to = wp->w_botline - 1;
1763 }
1764
1765 /*
1766 * Find the minimal part to be updated.
1767 * Watch out for scrolling that made entries in w_lines[] invalid.
1768 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1769 * top_end; need to redraw from top_end to the "to" line.
1770 * A middle mouse click with a Visual selection may change the text
1771 * above the Visual area and reset wl_valid, do count these for
1772 * mid_end (in srow).
1773 */
1774 if (mid_start > 0)
1775 {
1776 lnum = wp->w_topline;
1777 idx = 0;
1778 srow = 0;
1779 if (scrolled_down)
1780 mid_start = top_end;
1781 else
1782 mid_start = 0;
1783 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1784 {
1785 if (wp->w_lines[idx].wl_valid)
1786 mid_start += wp->w_lines[idx].wl_size;
1787 else if (!scrolled_down)
1788 srow += wp->w_lines[idx].wl_size;
1789 ++idx;
1790# ifdef FEAT_FOLDING
1791 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1792 lnum = wp->w_lines[idx].wl_lnum;
1793 else
1794# endif
1795 ++lnum;
1796 }
1797 srow += mid_start;
1798 mid_end = wp->w_height;
1799 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1800 {
1801 if (wp->w_lines[idx].wl_valid
1802 && wp->w_lines[idx].wl_lnum >= to + 1)
1803 {
1804 /* Only update until first row of this line */
1805 mid_end = srow;
1806 break;
1807 }
1808 srow += wp->w_lines[idx].wl_size;
1809 }
1810 }
1811 }
1812
1813 if (VIsual_active && buf == curwin->w_buffer)
1814 {
1815 wp->w_old_visual_mode = VIsual_mode;
1816 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1817 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001818 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 wp->w_old_curswant = curwin->w_curswant;
1820 }
1821 else
1822 {
1823 wp->w_old_visual_mode = 0;
1824 wp->w_old_cursor_lnum = 0;
1825 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001826 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828
1829#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1830 /* reset got_int, otherwise regexp won't work */
1831 save_got_int = got_int;
1832 got_int = 0;
1833#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001834#ifdef SYN_TIME_LIMIT
1835 /* Set the time limit to 'redrawtime'. */
1836 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001837 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839#ifdef FEAT_FOLDING
1840 win_foldinfo.fi_level = 0;
1841#endif
1842
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001843#ifdef FEAT_MENU
1844 /*
1845 * Draw the window toolbar, if there is one.
1846 * TODO: only when needed.
1847 */
1848 if (winbar_height(wp) > 0)
1849 redraw_win_toolbar(wp);
1850#endif
1851
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 /*
1853 * Update all the window rows.
1854 */
1855 idx = 0; /* first entry in w_lines[].wl_size */
1856 row = 0;
1857 srow = 0;
1858 lnum = wp->w_topline; /* first line shown in window */
1859 for (;;)
1860 {
1861 /* stop updating when reached the end of the window (check for _past_
1862 * the end of the window is at the end of the loop) */
1863 if (row == wp->w_height)
1864 {
1865 didline = TRUE;
1866 break;
1867 }
1868
1869 /* stop updating when hit the end of the file */
1870 if (lnum > buf->b_ml.ml_line_count)
1871 {
1872 eof = TRUE;
1873 break;
1874 }
1875
1876 /* Remember the starting row of the line that is going to be dealt
1877 * with. It is used further down when the line doesn't fit. */
1878 srow = row;
1879
1880 /*
1881 * Update a line when it is in an area that needs updating, when it
1882 * has changes or w_lines[idx] is invalid.
1883 * bot_start may be halfway a wrapped line after using
1884 * win_del_lines(), check if the current line includes it.
1885 * When syntax folding is being used, the saved syntax states will
1886 * already have been updated, we can't see where the syntax state is
1887 * the same again, just update until the end of the window.
1888 */
1889 if (row < top_end
1890 || (row >= mid_start && row < mid_end)
1891#ifdef FEAT_SEARCH_EXTRA
1892 || top_to_mod
1893#endif
1894 || idx >= wp->w_lines_valid
1895 || (row + wp->w_lines[idx].wl_size > bot_start)
1896 || (mod_top != 0
1897 && (lnum == mod_top
1898 || (lnum >= mod_top
1899 && (lnum < mod_bot
1900#ifdef FEAT_SYN_HL
1901 || did_update == DID_FOLD
1902 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001903 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 && (
1905# ifdef FEAT_FOLDING
1906 (foldmethodIsSyntax(wp)
1907 && hasAnyFolding(wp)) ||
1908# endif
1909 syntax_check_changed(lnum)))
1910#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001911#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001912 /* match in fixed position might need redraw
1913 * if lines were inserted or deleted */
1914 || (wp->w_match_head != NULL
1915 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 )))))
1918 {
1919#ifdef FEAT_SEARCH_EXTRA
1920 if (lnum == mod_top)
1921 top_to_mod = FALSE;
1922#endif
1923
1924 /*
1925 * When at start of changed lines: May scroll following lines
1926 * up or down to minimize redrawing.
1927 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001928 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 */
1930 if (lnum == mod_top
1931 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001932 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 {
1934 int old_rows = 0;
1935 int new_rows = 0;
1936 int xtra_rows;
1937 linenr_T l;
1938
1939 /* Count the old number of window rows, using w_lines[], which
1940 * should still contain the sizes for the lines as they are
1941 * currently displayed. */
1942 for (i = idx; i < wp->w_lines_valid; ++i)
1943 {
1944 /* Only valid lines have a meaningful wl_lnum. Invalid
1945 * lines are part of the changed area. */
1946 if (wp->w_lines[i].wl_valid
1947 && wp->w_lines[i].wl_lnum == mod_bot)
1948 break;
1949 old_rows += wp->w_lines[i].wl_size;
1950#ifdef FEAT_FOLDING
1951 if (wp->w_lines[i].wl_valid
1952 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1953 {
1954 /* Must have found the last valid entry above mod_bot.
1955 * Add following invalid entries. */
1956 ++i;
1957 while (i < wp->w_lines_valid
1958 && !wp->w_lines[i].wl_valid)
1959 old_rows += wp->w_lines[i++].wl_size;
1960 break;
1961 }
1962#endif
1963 }
1964
1965 if (i >= wp->w_lines_valid)
1966 {
1967 /* We can't find a valid line below the changed lines,
1968 * need to redraw until the end of the window.
1969 * Inserting/deleting lines has no use. */
1970 bot_start = 0;
1971 }
1972 else
1973 {
1974 /* Able to count old number of rows: Count new window
1975 * rows, and may insert/delete lines */
1976 j = idx;
1977 for (l = lnum; l < mod_bot; ++l)
1978 {
1979#ifdef FEAT_FOLDING
1980 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1981 ++new_rows;
1982 else
1983#endif
1984#ifdef FEAT_DIFF
1985 if (l == wp->w_topline)
1986 new_rows += plines_win_nofill(wp, l, TRUE)
1987 + wp->w_topfill;
1988 else
1989#endif
1990 new_rows += plines_win(wp, l, TRUE);
1991 ++j;
1992 if (new_rows > wp->w_height - row - 2)
1993 {
1994 /* it's getting too much, must redraw the rest */
1995 new_rows = 9999;
1996 break;
1997 }
1998 }
1999 xtra_rows = new_rows - old_rows;
2000 if (xtra_rows < 0)
2001 {
2002 /* May scroll text up. If there is not enough
2003 * remaining text or scrolling fails, must redraw the
2004 * rest. If scrolling works, must redraw the text
2005 * below the scrolled text. */
2006 if (row - xtra_rows >= wp->w_height - 2)
2007 mod_bot = MAXLNUM;
2008 else
2009 {
2010 check_for_delay(FALSE);
2011 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002012 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 mod_bot = MAXLNUM;
2014 else
2015 bot_start = wp->w_height + xtra_rows;
2016 }
2017 }
2018 else if (xtra_rows > 0)
2019 {
2020 /* May scroll text down. If there is not enough
2021 * remaining text of scrolling fails, must redraw the
2022 * rest. */
2023 if (row + xtra_rows >= wp->w_height - 2)
2024 mod_bot = MAXLNUM;
2025 else
2026 {
2027 check_for_delay(FALSE);
2028 if (win_ins_lines(wp, row + old_rows,
2029 xtra_rows, FALSE, FALSE) == FAIL)
2030 mod_bot = MAXLNUM;
2031 else if (top_end > row + old_rows)
2032 /* Scrolled the part at the top that requires
2033 * updating down. */
2034 top_end += xtra_rows;
2035 }
2036 }
2037
2038 /* When not updating the rest, may need to move w_lines[]
2039 * entries. */
2040 if (mod_bot != MAXLNUM && i != j)
2041 {
2042 if (j < i)
2043 {
2044 int x = row + new_rows;
2045
2046 /* move entries in w_lines[] upwards */
2047 for (;;)
2048 {
2049 /* stop at last valid entry in w_lines[] */
2050 if (i >= wp->w_lines_valid)
2051 {
2052 wp->w_lines_valid = j;
2053 break;
2054 }
2055 wp->w_lines[j] = wp->w_lines[i];
2056 /* stop at a line that won't fit */
2057 if (x + (int)wp->w_lines[j].wl_size
2058 > wp->w_height)
2059 {
2060 wp->w_lines_valid = j + 1;
2061 break;
2062 }
2063 x += wp->w_lines[j++].wl_size;
2064 ++i;
2065 }
2066 if (bot_start > x)
2067 bot_start = x;
2068 }
2069 else /* j > i */
2070 {
2071 /* move entries in w_lines[] downwards */
2072 j -= i;
2073 wp->w_lines_valid += j;
2074 if (wp->w_lines_valid > wp->w_height)
2075 wp->w_lines_valid = wp->w_height;
2076 for (i = wp->w_lines_valid; i - j >= idx; --i)
2077 wp->w_lines[i] = wp->w_lines[i - j];
2078
2079 /* The w_lines[] entries for inserted lines are
2080 * now invalid, but wl_size may be used above.
2081 * Reset to zero. */
2082 while (i >= idx)
2083 {
2084 wp->w_lines[i].wl_size = 0;
2085 wp->w_lines[i--].wl_valid = FALSE;
2086 }
2087 }
2088 }
2089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 }
2091
2092#ifdef FEAT_FOLDING
2093 /*
2094 * When lines are folded, display one line for all of them.
2095 * Otherwise, display normally (can be several display lines when
2096 * 'wrap' is on).
2097 */
2098 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2099 if (fold_count != 0)
2100 {
2101 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2102 ++row;
2103 --fold_count;
2104 wp->w_lines[idx].wl_folded = TRUE;
2105 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2106# ifdef FEAT_SYN_HL
2107 did_update = DID_FOLD;
2108# endif
2109 }
2110 else
2111#endif
2112 if (idx < wp->w_lines_valid
2113 && wp->w_lines[idx].wl_valid
2114 && wp->w_lines[idx].wl_lnum == lnum
2115 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002116 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 && srow + wp->w_lines[idx].wl_size > wp->w_height
2118#ifdef FEAT_DIFF
2119 && diff_check_fill(wp, lnum) == 0
2120#endif
2121 )
2122 {
2123 /* This line is not going to fit. Don't draw anything here,
2124 * will draw "@ " lines below. */
2125 row = wp->w_height + 1;
2126 }
2127 else
2128 {
2129#ifdef FEAT_SEARCH_EXTRA
2130 prepare_search_hl(wp, lnum);
2131#endif
2132#ifdef FEAT_SYN_HL
2133 /* Let the syntax stuff know we skipped a few lines. */
2134 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002135 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 syntax_end_parsing(syntax_last_parsed + 1);
2137#endif
2138
2139 /*
2140 * Display one line.
2141 */
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002142 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143
2144#ifdef FEAT_FOLDING
2145 wp->w_lines[idx].wl_folded = FALSE;
2146 wp->w_lines[idx].wl_lastlnum = lnum;
2147#endif
2148#ifdef FEAT_SYN_HL
2149 did_update = DID_LINE;
2150 syntax_last_parsed = lnum;
2151#endif
2152 }
2153
2154 wp->w_lines[idx].wl_lnum = lnum;
2155 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002156
2157 /* Past end of the window or end of the screen. Note that after
2158 * resizing wp->w_height may be end up too big. That's a problem
2159 * elsewhere, but prevent a crash here. */
2160 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
2162 /* we may need the size of that too long line later on */
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 = plines_win(wp, lnum, TRUE);
2165 ++idx;
2166 break;
2167 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002168 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 wp->w_lines[idx].wl_size = row - srow;
2170 ++idx;
2171#ifdef FEAT_FOLDING
2172 lnum += fold_count + 1;
2173#else
2174 ++lnum;
2175#endif
2176 }
2177 else
2178 {
2179 /* This line does not need updating, advance to the next one */
2180 row += wp->w_lines[idx++].wl_size;
2181 if (row > wp->w_height) /* past end of screen */
2182 break;
2183#ifdef FEAT_FOLDING
2184 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2185#else
2186 ++lnum;
2187#endif
2188#ifdef FEAT_SYN_HL
2189 did_update = DID_NONE;
2190#endif
2191 }
2192
2193 if (lnum > buf->b_ml.ml_line_count)
2194 {
2195 eof = TRUE;
2196 break;
2197 }
2198 }
2199 /*
2200 * End of loop over all window lines.
2201 */
2202
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002203#ifdef FEAT_VTP
2204 /* Rewrite the character at the end of the screen line. */
2205 if (use_vtp())
2206 {
2207 int i;
2208
2209 for (i = 0; i < Rows; ++i)
2210# ifdef FEAT_MBYTE
2211 if (enc_utf8)
2212 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2213 LineOffset[i] + screen_Columns) > 1)
2214 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2215 else
2216 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2217 else
2218# endif
2219 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2220 }
2221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223 if (idx > wp->w_lines_valid)
2224 wp->w_lines_valid = idx;
2225
2226#ifdef FEAT_SYN_HL
2227 /*
2228 * Let the syntax stuff know we stop parsing here.
2229 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002230 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 syntax_end_parsing(syntax_last_parsed + 1);
2232#endif
2233
2234 /*
2235 * If we didn't hit the end of the file, and we didn't finish the last
2236 * line we were working on, then the line didn't fit.
2237 */
2238 wp->w_empty_rows = 0;
2239#ifdef FEAT_DIFF
2240 wp->w_filler_rows = 0;
2241#endif
2242 if (!eof && !didline)
2243 {
2244 if (lnum == wp->w_topline)
2245 {
2246 /*
2247 * Single line that does not fit!
2248 * Don't overwrite it, it can be edited.
2249 */
2250 wp->w_botline = lnum + 1;
2251 }
2252#ifdef FEAT_DIFF
2253 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2254 {
2255 /* Window ends in filler lines. */
2256 wp->w_botline = lnum;
2257 wp->w_filler_rows = wp->w_height - srow;
2258 }
2259#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002260 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2261 {
2262 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2263
2264 /*
2265 * Last line isn't finished: Display "@@@" in the last screen line.
2266 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002267 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002268 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002269 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002270 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002271 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002272 set_empty_rows(wp, srow);
2273 wp->w_botline = lnum;
2274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2276 {
2277 /*
2278 * Last line isn't finished: Display "@@@" at the end.
2279 */
2280 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2281 W_WINROW(wp) + wp->w_height,
2282 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002283 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 set_empty_rows(wp, srow);
2285 wp->w_botline = lnum;
2286 }
2287 else
2288 {
2289 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2290 wp->w_botline = lnum;
2291 }
2292 }
2293 else
2294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 if (eof) /* we hit the end of the file */
2297 {
2298 wp->w_botline = buf->b_ml.ml_line_count + 1;
2299#ifdef FEAT_DIFF
2300 j = diff_check_fill(wp, wp->w_botline);
2301 if (j > 0 && !wp->w_botfill)
2302 {
2303 /*
2304 * Display filler lines at the end of the file
2305 */
2306 if (char2cells(fill_diff) > 1)
2307 i = '-';
2308 else
2309 i = fill_diff;
2310 if (row + j > wp->w_height)
2311 j = wp->w_height - row;
2312 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2313 row += j;
2314 }
2315#endif
2316 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002317 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 wp->w_botline = lnum;
2319
2320 /* make sure the rest of the screen is blank */
2321 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002322 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002325#ifdef SYN_TIME_LIMIT
2326 syn_set_timeout(NULL);
2327#endif
2328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 /* Reset the type of redrawing required, the window has been updated. */
2330 wp->w_redr_type = 0;
2331#ifdef FEAT_DIFF
2332 wp->w_old_topfill = wp->w_topfill;
2333 wp->w_old_botfill = wp->w_botfill;
2334#endif
2335
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002336 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
2338 /*
2339 * There is a trick with w_botline. If we invalidate it on each
2340 * change that might modify it, this will cause a lot of expensive
2341 * calls to plines() in update_topline() each time. Therefore the
2342 * value of w_botline is often approximated, and this value is used to
2343 * compute the value of w_topline. If the value of w_botline was
2344 * wrong, check that the value of w_topline is correct (cursor is on
2345 * the visible part of the text). If it's not, we need to redraw
2346 * again. Mostly this just means scrolling up a few lines, so it
2347 * doesn't look too bad. Only do this for the current window (where
2348 * changes are relevant).
2349 */
2350 wp->w_valid |= VALID_BOTLINE;
2351 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2352 {
2353 recursive = TRUE;
2354 curwin->w_valid &= ~VALID_TOPLINE;
2355 update_topline(); /* may invalidate w_botline again */
2356 if (must_redraw != 0)
2357 {
2358 /* Don't update for changes in buffer again. */
2359 i = curbuf->b_mod_set;
2360 curbuf->b_mod_set = FALSE;
2361 win_update(curwin);
2362 must_redraw = 0;
2363 curbuf->b_mod_set = i;
2364 }
2365 recursive = FALSE;
2366 }
2367 }
2368
2369#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2370 /* restore got_int, unless CTRL-C was hit while redrawing */
2371 if (!got_int)
2372 got_int = save_got_int;
2373#endif
2374}
2375
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376/*
2377 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2378 * as the filler character.
2379 */
2380 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002381win_draw_end(
2382 win_T *wp,
2383 int c1,
2384 int c2,
2385 int row,
2386 int endrow,
2387 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388{
2389#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2390 int n = 0;
2391# define FDC_OFF n
2392#else
2393# define FDC_OFF 0
2394#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002395#ifdef FEAT_FOLDING
2396 int fdc = compute_foldcolumn(wp, 0);
2397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399#ifdef FEAT_RIGHTLEFT
2400 if (wp->w_p_rl)
2401 {
2402 /* No check for cmdline window: should never be right-left. */
2403# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002404 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 if (n > 0)
2407 {
2408 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002409 if (n > wp->w_width)
2410 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2412 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002413 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
2415# endif
2416# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002417 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
2419 int nn = n + 2;
2420
2421 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002422 if (nn > wp->w_width)
2423 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2425 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002426 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 n = nn;
2428 }
2429# endif
2430 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002431 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002432 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2434 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002435 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437 else
2438#endif
2439 {
2440#ifdef FEAT_CMDWIN
2441 if (cmdwin_type != 0 && wp == curwin)
2442 {
2443 /* draw the cmdline character in the leftmost column */
2444 n = 1;
2445 if (n > wp->w_width)
2446 n = wp->w_width;
2447 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002448 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002449 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 }
2451#endif
2452#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002453 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002455 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
2457 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002458 if (nn > wp->w_width)
2459 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002461 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002462 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 n = nn;
2464 }
2465#endif
2466#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002467 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
2469 int nn = n + 2;
2470
2471 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002472 if (nn > wp->w_width)
2473 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002475 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002476 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 n = nn;
2478 }
2479#endif
2480 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002481 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002482 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
2484 set_empty_rows(wp, row);
2485}
2486
Bram Moolenaar1a384422010-07-14 19:53:30 +02002487#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002488static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002489
2490/*
2491 * Advance **color_cols and return TRUE when there are columns to draw.
2492 */
2493 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002494advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002495{
2496 while (**color_cols >= 0 && vcol > **color_cols)
2497 ++*color_cols;
2498 return (**color_cols >= 0);
2499}
2500#endif
2501
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002502#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2503/*
2504 * Copy "text" to ScreenLines using "attr".
2505 * Returns the next screen column.
2506 */
2507 static int
2508text_to_screenline(win_T *wp, char_u *text, int col)
2509{
2510 int off = (int)(current_ScreenLine - ScreenLines);
2511
2512#ifdef FEAT_MBYTE
2513 if (has_mbyte)
2514 {
2515 int cells;
2516 int u8c, u8cc[MAX_MCO];
2517 int i;
2518 int idx;
2519 int c_len;
2520 char_u *p;
2521# ifdef FEAT_ARABIC
2522 int prev_c = 0; /* previous Arabic character */
2523 int prev_c1 = 0; /* first composing char for prev_c */
2524# endif
2525
2526# ifdef FEAT_RIGHTLEFT
2527 if (wp->w_p_rl)
2528 idx = off;
2529 else
2530# endif
2531 idx = off + col;
2532
2533 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2534 for (p = text; *p != NUL; )
2535 {
2536 cells = (*mb_ptr2cells)(p);
2537 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002538 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002539# ifdef FEAT_RIGHTLEFT
2540 - (wp->w_p_rl ? col : 0)
2541# endif
2542 )
2543 break;
2544 ScreenLines[idx] = *p;
2545 if (enc_utf8)
2546 {
2547 u8c = utfc_ptr2char(p, u8cc);
2548 if (*p < 0x80 && u8cc[0] == 0)
2549 {
2550 ScreenLinesUC[idx] = 0;
2551#ifdef FEAT_ARABIC
2552 prev_c = u8c;
2553#endif
2554 }
2555 else
2556 {
2557#ifdef FEAT_ARABIC
2558 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2559 {
2560 /* Do Arabic shaping. */
2561 int pc, pc1, nc;
2562 int pcc[MAX_MCO];
2563 int firstbyte = *p;
2564
2565 /* The idea of what is the previous and next
2566 * character depends on 'rightleft'. */
2567 if (wp->w_p_rl)
2568 {
2569 pc = prev_c;
2570 pc1 = prev_c1;
2571 nc = utf_ptr2char(p + c_len);
2572 prev_c1 = u8cc[0];
2573 }
2574 else
2575 {
2576 pc = utfc_ptr2char(p + c_len, pcc);
2577 nc = prev_c;
2578 pc1 = pcc[0];
2579 }
2580 prev_c = u8c;
2581
2582 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2583 pc, pc1, nc);
2584 ScreenLines[idx] = firstbyte;
2585 }
2586 else
2587 prev_c = u8c;
2588#endif
2589 /* Non-BMP character: display as ? or fullwidth ?. */
2590#ifdef UNICODE16
2591 if (u8c >= 0x10000)
2592 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2593 else
2594#endif
2595 ScreenLinesUC[idx] = u8c;
2596 for (i = 0; i < Screen_mco; ++i)
2597 {
2598 ScreenLinesC[i][idx] = u8cc[i];
2599 if (u8cc[i] == 0)
2600 break;
2601 }
2602 }
2603 if (cells > 1)
2604 ScreenLines[idx + 1] = 0;
2605 }
2606 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2607 /* double-byte single width character */
2608 ScreenLines2[idx] = p[1];
2609 else if (cells > 1)
2610 /* double-width character */
2611 ScreenLines[idx + 1] = p[1];
2612 col += cells;
2613 idx += cells;
2614 p += c_len;
2615 }
2616 }
2617 else
2618#endif
2619 {
2620 int len = (int)STRLEN(text);
2621
Bram Moolenaar02631462017-09-22 15:20:32 +02002622 if (len > wp->w_width - col)
2623 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002624 if (len > 0)
2625 {
2626#ifdef FEAT_RIGHTLEFT
2627 if (wp->w_p_rl)
2628 STRNCPY(current_ScreenLine, text, len);
2629 else
2630#endif
2631 STRNCPY(current_ScreenLine + col, text, len);
2632 col += len;
2633 }
2634 }
2635 return col;
2636}
2637#endif
2638
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639#ifdef FEAT_FOLDING
2640/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002641 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2642 * space is available for window "wp", minus "col".
2643 */
2644 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002645compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002646{
2647 int fdc = wp->w_p_fdc;
2648 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002649 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002650
2651 if (fdc > wwidth - (col + wmw))
2652 fdc = wwidth - (col + wmw);
2653 return fdc;
2654}
2655
2656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 * Display one folded line.
2658 */
2659 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002660fold_line(
2661 win_T *wp,
2662 long fold_count,
2663 foldinfo_T *foldinfo,
2664 linenr_T lnum,
2665 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002667 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 pos_T *top, *bot;
2669 linenr_T lnume = lnum + fold_count - 1;
2670 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002671 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 int col;
2674 int txtcol;
2675 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002676 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
2678 /* Build the fold line:
2679 * 1. Add the cmdwin_type for the command-line window
2680 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002681 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 * 4. Compose the text
2683 * 5. Add the text
2684 * 6. set highlighting for the Visual area an other text
2685 */
2686 col = 0;
2687
2688 /*
2689 * 1. Add the cmdwin_type for the command-line window
2690 * Ignores 'rightleft', this window is never right-left.
2691 */
2692#ifdef FEAT_CMDWIN
2693 if (cmdwin_type != 0 && wp == curwin)
2694 {
2695 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002696 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697#ifdef FEAT_MBYTE
2698 if (enc_utf8)
2699 ScreenLinesUC[off] = 0;
2700#endif
2701 ++col;
2702 }
2703#endif
2704
2705 /*
2706 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002707 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002709 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (fdc > 0)
2711 {
2712 fill_foldcolumn(buf, wp, TRUE, lnum);
2713#ifdef FEAT_RIGHTLEFT
2714 if (wp->w_p_rl)
2715 {
2716 int i;
2717
Bram Moolenaar02631462017-09-22 15:20:32 +02002718 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002719 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 /* reverse the fold column */
2721 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002722 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
2724 else
2725#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002726 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 col += fdc;
2728 }
2729
2730#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002731# define RL_MEMSET(p, v, l) \
2732 do { \
2733 if (wp->w_p_rl) \
2734 for (ri = 0; ri < l; ++ri) \
2735 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2736 else \
2737 for (ri = 0; ri < l; ++ri) \
2738 ScreenAttrs[off + (p) + ri] = v; \
2739 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002741# define RL_MEMSET(p, v, l) \
2742 do { \
2743 for (ri = 0; ri < l; ++ri) \
2744 ScreenAttrs[off + (p) + ri] = v; \
2745 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746#endif
2747
Bram Moolenaar64486672010-05-16 15:46:46 +02002748 /* Set all attributes of the 'number' or 'relativenumber' column and the
2749 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002750 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
2752#ifdef FEAT_SIGNS
2753 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002754 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002756 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 if (len > 0)
2758 {
2759 if (len > 2)
2760 len = 2;
2761# ifdef FEAT_RIGHTLEFT
2762 if (wp->w_p_rl)
2763 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002764 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002765 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 else
2767# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002768 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 col += len;
2770 }
2771 }
2772#endif
2773
2774 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002775 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002777 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002779 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 if (len > 0)
2781 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002782 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002783 long num;
2784 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002785
2786 if (len > w + 1)
2787 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002788
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002789 if (wp->w_p_nu && !wp->w_p_rnu)
2790 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002791 num = (long)lnum;
2792 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002793 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002794 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002795 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002796 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002797 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002798 /* 'number' + 'relativenumber': cursor line shows absolute
2799 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002800 num = lnum;
2801 fmt = "%-*ld ";
2802 }
2803 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002804
Bram Moolenaar700e7342013-01-30 12:31:36 +01002805 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806#ifdef FEAT_RIGHTLEFT
2807 if (wp->w_p_rl)
2808 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002809 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002810 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 else
2812#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002813 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 col += len;
2815 }
2816 }
2817
2818 /*
2819 * 4. Compose the folded-line string with 'foldtext', if set.
2820 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002821 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
2823 txtcol = col; /* remember where text starts */
2824
2825 /*
2826 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2827 * Right-left text is put in columns 0 - number-col, normal text is put
2828 * in columns number-col - window-width.
2829 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002830 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
2832 /* Fill the rest of the line with the fold filler */
2833#ifdef FEAT_RIGHTLEFT
2834 if (wp->w_p_rl)
2835 col -= txtcol;
2836#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002837 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838#ifdef FEAT_RIGHTLEFT
2839 - (wp->w_p_rl ? txtcol : 0)
2840#endif
2841 )
2842 {
2843#ifdef FEAT_MBYTE
2844 if (enc_utf8)
2845 {
2846 if (fill_fold >= 0x80)
2847 {
2848 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002849 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002850 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
2852 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002853 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002855 ScreenLines[off + col] = fill_fold;
2856 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002857 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002859 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002861 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
2863
2864 if (text != buf)
2865 vim_free(text);
2866
2867 /*
2868 * 6. set highlighting for the Visual area an other text.
2869 * If all folded lines are in the Visual area, highlight the line.
2870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2872 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002873 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 {
2875 /* Visual is after curwin->w_cursor */
2876 top = &curwin->w_cursor;
2877 bot = &VIsual;
2878 }
2879 else
2880 {
2881 /* Visual is before curwin->w_cursor */
2882 top = &VIsual;
2883 bot = &curwin->w_cursor;
2884 }
2885 if (lnum >= top->lnum
2886 && lnume <= bot->lnum
2887 && (VIsual_mode != 'v'
2888 || ((lnum > top->lnum
2889 || (lnum == top->lnum
2890 && top->col == 0))
2891 && (lnume < bot->lnum
2892 || (lnume == bot->lnum
2893 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002894 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
2896 if (VIsual_mode == Ctrl_V)
2897 {
2898 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002899 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002901 if (wp->w_old_cursor_lcol != MAXCOL
2902 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002903 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 len = wp->w_old_cursor_lcol;
2905 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002906 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002907 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002908 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
2910 }
2911 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002914 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
2917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002919#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002920 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2921 if (wp->w_p_cc_cols)
2922 {
2923 int i = 0;
2924 int j = wp->w_p_cc_cols[i];
2925 int old_txtcol = txtcol;
2926
2927 while (j > -1)
2928 {
2929 txtcol += j;
2930 if (wp->w_p_wrap)
2931 txtcol -= wp->w_skipcol;
2932 else
2933 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002934 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002935 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002936 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002937 txtcol = old_txtcol;
2938 j = wp->w_p_cc_cols[++i];
2939 }
2940 }
2941
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002942 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002943 if (wp->w_p_cuc)
2944 {
2945 txtcol += wp->w_virtcol;
2946 if (wp->w_p_wrap)
2947 txtcol -= wp->w_skipcol;
2948 else
2949 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002950 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002951 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002952 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002953 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
Bram Moolenaar02631462017-09-22 15:20:32 +02002956 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2957 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
2959 /*
2960 * Update w_cline_height and w_cline_folded if the cursor line was
2961 * updated (saves a call to plines() later).
2962 */
2963 if (wp == curwin
2964 && lnum <= curwin->w_cursor.lnum
2965 && lnume >= curwin->w_cursor.lnum)
2966 {
2967 curwin->w_cline_row = row;
2968 curwin->w_cline_height = 1;
2969 curwin->w_cline_folded = TRUE;
2970 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2971 }
2972}
2973
2974/*
2975 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2976 */
2977 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002978copy_text_attr(
2979 int off,
2980 char_u *buf,
2981 int len,
2982 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002984 int i;
2985
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 mch_memmove(ScreenLines + off, buf, (size_t)len);
2987# ifdef FEAT_MBYTE
2988 if (enc_utf8)
2989 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2990# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002991 for (i = 0; i < len; ++i)
2992 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993}
2994
2995/*
2996 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002997 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 */
2999 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003000fill_foldcolumn(
3001 char_u *p,
3002 win_T *wp,
3003 int closed, /* TRUE of FALSE */
3004 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 int i = 0;
3007 int level;
3008 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003009 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003010 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003013 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
3015 level = win_foldinfo.fi_level;
3016 if (level > 0)
3017 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003018 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003019 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003020
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 /* If the column is too narrow, we start at the lowest level that
3022 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003023 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (first_level < 1)
3025 first_level = 1;
3026
Bram Moolenaar1c934292015-01-27 16:39:29 +01003027 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 {
3029 if (win_foldinfo.fi_lnum == lnum
3030 && first_level + i >= win_foldinfo.fi_low_level)
3031 p[i] = '-';
3032 else if (first_level == 1)
3033 p[i] = '|';
3034 else if (first_level + i <= 9)
3035 p[i] = '0' + first_level + i;
3036 else
3037 p[i] = '>';
3038 if (first_level + i == level)
3039 break;
3040 }
3041 }
3042 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003043 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044}
3045#endif /* FEAT_FOLDING */
3046
3047/*
3048 * Display line "lnum" of window 'wp' on the screen.
3049 * Start at row "startrow", stop when "endrow" is reached.
3050 * wp->w_virtcol needs to be valid.
3051 *
3052 * Return the number of last row the line occupies.
3053 */
3054 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003055win_line(
3056 win_T *wp,
3057 linenr_T lnum,
3058 int startrow,
3059 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003060 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003062 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3064 int c = 0; /* init for GCC */
3065 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003066#ifdef FEAT_LINEBREAK
3067 long vcol_sbr = -1; /* virtual column after showbreak */
3068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 long vcol_prev = -1; /* "vcol" of previous character */
3070 char_u *line; /* current line */
3071 char_u *ptr; /* current position in "line" */
3072 int row; /* row in the window, excl w_winrow */
3073 int screen_row; /* row on the screen, incl w_winrow */
3074
3075 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3076 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003077 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003078 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 int c_extra = NUL; /* extra chars, all the same */
3080 int extra_attr = 0; /* attributes when n_extra != 0 */
3081 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3082 displaying lcs_eol at end-of-line */
3083 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3084 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3085
3086 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3087 int saved_n_extra = 0;
3088 char_u *saved_p_extra = NULL;
3089 int saved_c_extra = 0;
3090 int saved_char_attr = 0;
3091
3092 int n_attr = 0; /* chars with special attr */
3093 int saved_attr2 = 0; /* char_attr saved for n_attr */
3094 int n_attr3 = 0; /* chars with overruling special attr */
3095 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3096
3097 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3098
3099 int fromcol, tocol; /* start/end of inverting */
3100 int fromcol_prev = -2; /* start of inverting after cursor */
3101 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003103 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 pos_T pos;
3105 long v;
3106
3107 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003108 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3110 in this line */
3111 int attr = 0; /* attributes for area highlighting */
3112 int area_attr = 0; /* attributes desired by highlighting */
3113 int search_attr = 0; /* attributes desired by 'hlsearch' */
3114#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003115 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 int syntax_attr = 0; /* attributes desired by syntax */
3117 int has_syntax = FALSE; /* this buffer has syntax highl. */
3118 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003119 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003120 int draw_color_col = FALSE; /* highlight colorcolumn */
3121 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003122#endif
3123#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003124 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003125# define SPWORDLEN 150
3126 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003127 int nextlinecol = 0; /* column where nextline[] starts */
3128 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003129 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003130 int spell_attr = 0; /* attributes desired by spelling */
3131 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003132 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3133 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003134 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003135 static int cap_col = -1; /* column to check for Cap word */
3136 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003137 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#endif
3139 int extra_check; /* has syntax or linebreak */
3140#ifdef FEAT_MBYTE
3141 int multi_attr = 0; /* attributes desired by multibyte */
3142 int mb_l = 1; /* multi-byte byte length */
3143 int mb_c = 0; /* decoded multi-byte character */
3144 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003145 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146#endif
3147#ifdef FEAT_DIFF
3148 int filler_lines; /* nr of filler lines to be drawn */
3149 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003150 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 int change_start = MAXCOL; /* first col of changed area */
3152 int change_end = -1; /* last col of changed area */
3153#endif
3154 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3155#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003156 int need_showbreak = FALSE; /* overlong line, skipping first x
3157 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003159#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003160 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003162 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#endif
3164#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003165 matchitem_T *cur; /* points to the match list */
3166 match_T *shl; /* points to search_hl or a match */
3167 int shl_flag; /* flag to indicate whether search_hl
3168 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003169 int pos_inprogress; /* marks that position match search is
3170 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003171 int prevcol_hl_flag; /* flag to indicate whether prevcol
3172 equals startcol of search_hl or one
3173 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174#endif
3175#ifdef FEAT_ARABIC
3176 int prev_c = 0; /* previous Arabic character */
3177 int prev_c1 = 0; /* first composing char for prev_c */
3178#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003179#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003180 int did_line_attr = 0;
3181#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003182#ifdef FEAT_TERMINAL
3183 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003184 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187 /* draw_state: items that are drawn in sequence: */
3188#define WL_START 0 /* nothing done yet */
3189#ifdef FEAT_CMDWIN
3190# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3191#else
3192# define WL_CMDLINE WL_START
3193#endif
3194#ifdef FEAT_FOLDING
3195# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3196#else
3197# define WL_FOLD WL_CMDLINE
3198#endif
3199#ifdef FEAT_SIGNS
3200# define WL_SIGN WL_FOLD + 1 /* column for signs */
3201#else
3202# define WL_SIGN WL_FOLD /* column for signs */
3203#endif
3204#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003205#ifdef FEAT_LINEBREAK
3206# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003208# define WL_BRI WL_NR
3209#endif
3210#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3211# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3212#else
3213# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214#endif
3215#define WL_LINE WL_SBR + 1 /* text in the line */
3216 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003217#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 int feedback_col = 0;
3219 int feedback_old_attr = -1;
3220#endif
3221
Bram Moolenaar860cae12010-06-05 23:22:07 +02003222#ifdef FEAT_CONCEAL
3223 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003224 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003225 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003226 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003227 int is_concealing = FALSE;
3228 int boguscols = 0; /* nonexistent columns added to force
3229 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003230 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003231 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003232 int match_conc = 0; /* cchar for match functions */
3233 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003234 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003235# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003236# define FIX_FOR_BOGUSCOLS \
3237 { \
3238 n_extra += vcol_off; \
3239 vcol -= vcol_off; \
3240 vcol_off = 0; \
3241 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003242 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003243 boguscols = 0; \
3244 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003245#else
3246# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
3249 if (startrow > endrow) /* past the end already! */
3250 return startrow;
3251
3252 row = startrow;
3253 screen_row = row + W_WINROW(wp);
3254
3255 /*
3256 * To speed up the loop below, set extra_check when there is linebreak,
3257 * trailing white space and/or syntax processing to be done.
3258 */
3259#ifdef FEAT_LINEBREAK
3260 extra_check = wp->w_p_lbr;
3261#else
3262 extra_check = 0;
3263#endif
3264#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003265 if (syntax_present(wp) && !wp->w_s->b_syn_error
3266# ifdef SYN_TIME_LIMIT
3267 && !wp->w_s->b_syn_slow
3268# endif
3269 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
3271 /* Prepare for syntax highlighting in this line. When there is an
3272 * error, stop syntax highlighting. */
3273 save_did_emsg = did_emsg;
3274 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003275 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003277 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 else
3279 {
3280 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003281#ifdef SYN_TIME_LIMIT
3282 if (!wp->w_s->b_syn_slow)
3283#endif
3284 {
3285 has_syntax = TRUE;
3286 extra_check = TRUE;
3287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 }
3289 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003290
3291 /* Check for columns to display for 'colorcolumn'. */
3292 color_cols = wp->w_p_cc_cols;
3293 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003294 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003295#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003296
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003297#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003298 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003299 {
3300 extra_check = TRUE;
3301 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003302 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003303 }
3304#endif
3305
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003306#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003307 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003308 && *wp->w_s->b_p_spl != NUL
3309 && wp->w_s->b_langp.ga_len > 0
3310 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003311 {
3312 /* Prepare for spell checking. */
3313 has_spell = TRUE;
3314 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003315
3316 /* Get the start of the next line, so that words that wrap to the next
3317 * line are found too: "et<line-break>al.".
3318 * Trick: skip a few chars for C/shell/Vim comments */
3319 nextline[SPWORDLEN] = NUL;
3320 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3321 {
3322 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3323 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3324 }
3325
3326 /* When a word wrapped from the previous line the start of the current
3327 * line is valid. */
3328 if (lnum == checked_lnum)
3329 cur_checked_col = checked_col;
3330 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003331
3332 /* When there was a sentence end in the previous line may require a
3333 * word starting with capital in this line. In line 1 always check
3334 * the first word. */
3335 if (lnum != capcol_lnum)
3336 cap_col = -1;
3337 if (lnum == 1)
3338 cap_col = 0;
3339 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341#endif
3342
3343 /*
3344 * handle visual active in this window
3345 */
3346 fromcol = -10;
3347 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3349 {
3350 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003351 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
3353 top = &curwin->w_cursor;
3354 bot = &VIsual;
3355 }
3356 else /* Visual is before curwin->w_cursor */
3357 {
3358 top = &VIsual;
3359 bot = &curwin->w_cursor;
3360 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003361 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (VIsual_mode == Ctrl_V) /* block mode */
3363 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003364 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 {
3366 fromcol = wp->w_old_cursor_fcol;
3367 tocol = wp->w_old_cursor_lcol;
3368 }
3369 }
3370 else /* non-block mode */
3371 {
3372 if (lnum > top->lnum && lnum <= bot->lnum)
3373 fromcol = 0;
3374 else if (lnum == top->lnum)
3375 {
3376 if (VIsual_mode == 'V') /* linewise */
3377 fromcol = 0;
3378 else
3379 {
3380 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3381 if (gchar_pos(top) == NUL)
3382 tocol = fromcol + 1;
3383 }
3384 }
3385 if (VIsual_mode != 'V' && lnum == bot->lnum)
3386 {
3387 if (*p_sel == 'e' && bot->col == 0
3388#ifdef FEAT_VIRTUALEDIT
3389 && bot->coladd == 0
3390#endif
3391 )
3392 {
3393 fromcol = -10;
3394 tocol = MAXCOL;
3395 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003396 else if (bot->col == MAXCOL)
3397 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 else
3399 {
3400 pos = *bot;
3401 if (*p_sel == 'e')
3402 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3403 else
3404 {
3405 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3406 ++tocol;
3407 }
3408 }
3409 }
3410 }
3411
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 /* Check if the character under the cursor should not be inverted */
3413 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003414#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 )
3418 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
3420 /* if inverting in this line set area_highlighting */
3421 if (fromcol >= 0)
3422 {
3423 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003424 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003426 if ((clip_star.available && !clip_star.owned
3427 && clip_isautosel_star())
3428 || (clip_plus.available && !clip_plus.owned
3429 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003430 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431#endif
3432 }
3433 }
3434
3435 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003436 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003438 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 && wp == curwin
3440 && lnum >= curwin->w_cursor.lnum
3441 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3442 {
3443 if (lnum == curwin->w_cursor.lnum)
3444 getvcol(curwin, &(curwin->w_cursor),
3445 (colnr_T *)&fromcol, NULL, NULL);
3446 else
3447 fromcol = 0;
3448 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3449 {
3450 pos.lnum = lnum;
3451 pos.col = search_match_endcol;
3452 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3453 }
3454 else
3455 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003456 /* do at least one character; happens when past end of line */
3457 if (fromcol == tocol)
3458 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003460 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
3462
3463#ifdef FEAT_DIFF
3464 filler_lines = diff_check(wp, lnum);
3465 if (filler_lines < 0)
3466 {
3467 if (filler_lines == -1)
3468 {
3469 if (diff_find_change(wp, lnum, &change_start, &change_end))
3470 diff_hlf = HLF_ADD; /* added line */
3471 else if (change_start == 0)
3472 diff_hlf = HLF_TXD; /* changed text */
3473 else
3474 diff_hlf = HLF_CHD; /* changed line */
3475 }
3476 else
3477 diff_hlf = HLF_ADD; /* added line */
3478 filler_lines = 0;
3479 area_highlighting = TRUE;
3480 }
3481 if (lnum == wp->w_topline)
3482 filler_lines = wp->w_topfill;
3483 filler_todo = filler_lines;
3484#endif
3485
3486#ifdef LINE_ATTR
3487# ifdef FEAT_SIGNS
3488 /* If this line has a sign with line highlighting set line_attr. */
3489 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3490 if (v != 0)
3491 line_attr = sign_get_attr((int)v, TRUE);
3492# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003493# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003495 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003496 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497# endif
3498 if (line_attr != 0)
3499 area_highlighting = TRUE;
3500#endif
3501
3502 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3503 ptr = line;
3504
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003505#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003506 if (has_spell)
3507 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003508 /* For checking first word with a capital skip white space. */
3509 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003510 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003511
Bram Moolenaar30abd282005-06-22 22:35:10 +00003512 /* To be able to spell-check over line boundaries copy the end of the
3513 * current line into nextline[]. Above the start of the next line was
3514 * copied to nextline[SPWORDLEN]. */
3515 if (nextline[SPWORDLEN] == NUL)
3516 {
3517 /* No next line or it is empty. */
3518 nextlinecol = MAXCOL;
3519 nextline_idx = 0;
3520 }
3521 else
3522 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003523 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003524 if (v < SPWORDLEN)
3525 {
3526 /* Short line, use it completely and append the start of the
3527 * next line. */
3528 nextlinecol = 0;
3529 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003530 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003531 nextline_idx = v + 1;
3532 }
3533 else
3534 {
3535 /* Long line, use only the last SPWORDLEN bytes. */
3536 nextlinecol = v - SPWORDLEN;
3537 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3538 nextline_idx = SPWORDLEN + 1;
3539 }
3540 }
3541 }
3542#endif
3543
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003544 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003546 if (lcs_space || lcs_trail)
3547 extra_check = TRUE;
3548 /* find start of trailing whitespace */
3549 if (lcs_trail)
3550 {
3551 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003552 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003553 --trailcol;
3554 trailcol += (colnr_T) (ptr - line);
3555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557
3558 /*
3559 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3560 * first character to be displayed.
3561 */
3562 if (wp->w_p_wrap)
3563 v = wp->w_skipcol;
3564 else
3565 v = wp->w_leftcol;
3566 if (v > 0)
3567 {
3568#ifdef FEAT_MBYTE
3569 char_u *prev_ptr = ptr;
3570#endif
3571 while (vcol < v && *ptr != NUL)
3572 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003573 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 vcol += c;
3575#ifdef FEAT_MBYTE
3576 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003578 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
3580
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003581 /* When:
3582 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003583 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003584 * - 'virtualedit' is set, or
3585 * - the visual mode is active,
3586 * the end of the line may be before the start of the displayed part.
3587 */
3588 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003589#ifdef FEAT_SYN_HL
3590 wp->w_p_cuc || draw_color_col ||
3591#endif
3592#ifdef FEAT_VIRTUALEDIT
3593 virtual_active() ||
3594#endif
3595 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599
3600 /* Handle a character that's not completely on the screen: Put ptr at
3601 * that character but skip the first few screen characters. */
3602 if (vcol > v)
3603 {
3604 vcol -= c;
3605#ifdef FEAT_MBYTE
3606 ptr = prev_ptr;
3607#else
3608 --ptr;
3609#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003610 /* If the character fits on the screen, don't need to skip it.
3611 * Except for a TAB. */
3612 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003613#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003614 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003615#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003616 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003617 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619
3620 /*
3621 * Adjust for when the inverted text is before the screen,
3622 * and when the start of the inverted text is before the screen.
3623 */
3624 if (tocol <= vcol)
3625 fromcol = 0;
3626 else if (fromcol >= 0 && fromcol < vcol)
3627 fromcol = vcol;
3628
3629#ifdef FEAT_LINEBREAK
3630 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3631 if (wp->w_p_wrap)
3632 need_showbreak = TRUE;
3633#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003634#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003635 /* When spell checking a word we need to figure out the start of the
3636 * word and if it's badly spelled or not. */
3637 if (has_spell)
3638 {
3639 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003640 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003641 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003642
3643 pos = wp->w_cursor;
3644 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003645 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003646 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003647
3648 /* spell_move_to() may call ml_get() and make "line" invalid */
3649 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3650 ptr = line + linecol;
3651
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003652 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003653 {
3654 /* no bad word found at line start, don't check until end of a
3655 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003656 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003657 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003658 }
3659 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003660 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003661 /* bad word found, use attributes until end of word */
3662 word_end = wp->w_cursor.col + len + 1;
3663
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003664 /* Turn index into actual attributes. */
3665 if (spell_hlf != HLF_COUNT)
3666 spell_attr = highlight_attr[spell_hlf];
3667 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003668 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003669
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003670# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003671 /* Need to restart syntax highlighting for this line. */
3672 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003673 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003674# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003675 }
3676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678
3679 /*
3680 * Correct highlighting for cursor that can't be disabled.
3681 * Avoids having to check this for each character.
3682 */
3683 if (fromcol >= 0)
3684 {
3685 if (noinvcur)
3686 {
3687 if ((colnr_T)fromcol == wp->w_virtcol)
3688 {
3689 /* highlighting starts at cursor, let it start just after the
3690 * cursor */
3691 fromcol_prev = fromcol;
3692 fromcol = -1;
3693 }
3694 else if ((colnr_T)fromcol < wp->w_virtcol)
3695 /* restart highlighting after the cursor */
3696 fromcol_prev = wp->w_virtcol;
3697 }
3698 if (fromcol >= tocol)
3699 fromcol = -1;
3700 }
3701
3702#ifdef FEAT_SEARCH_EXTRA
3703 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003704 * Handle highlighting the last used search pattern and matches.
3705 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003707 cur = wp->w_match_head;
3708 shl_flag = FALSE;
3709 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003711 if (shl_flag == FALSE)
3712 {
3713 shl = &search_hl;
3714 shl_flag = TRUE;
3715 }
3716 else
3717 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003718 shl->startcol = MAXCOL;
3719 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003721 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003722 v = (long)(ptr - line);
3723 if (cur != NULL)
3724 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003725 next_search_hl(wp, shl, lnum, (colnr_T)v,
3726 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003727
3728 /* Need to get the line again, a multi-line regexp may have made it
3729 * invalid. */
3730 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3731 ptr = line + v;
3732
3733 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003735 if (shl->lnum == lnum)
3736 shl->startcol = shl->rm.startpos[0].col;
3737 else
3738 shl->startcol = 0;
3739 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3740 - shl->rm.startpos[0].lnum)
3741 shl->endcol = shl->rm.endpos[0].col;
3742 else
3743 shl->endcol = MAXCOL;
3744 /* Highlight one character for an empty match. */
3745 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003748 if (has_mbyte && line[shl->endcol] != NUL)
3749 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3750 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003752 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003754 if ((long)shl->startcol < v) /* match at leftcol */
3755 {
3756 shl->attr_cur = shl->attr;
3757 search_attr = shl->attr;
3758 }
3759 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003761 if (shl != &search_hl && cur != NULL)
3762 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
3764#endif
3765
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003766#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003767 /* Cursor line highlighting for 'cursorline' in the current window. Not
3768 * when Visual mode is active, because it's not clear what is selected
3769 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003770 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003771 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003772 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003773 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003774 area_highlighting = TRUE;
3775 }
3776#endif
3777
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003778 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 col = 0;
3780#ifdef FEAT_RIGHTLEFT
3781 if (wp->w_p_rl)
3782 {
3783 /* Rightleft window: process the text in the normal direction, but put
3784 * it in current_ScreenLine[] from right to left. Start at the
3785 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003786 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 off += col;
3788 }
3789#endif
3790
3791 /*
3792 * Repeat for the whole displayed line.
3793 */
3794 for (;;)
3795 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003796#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003797 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 /* Skip this quickly when working on the text. */
3800 if (draw_state != WL_LINE)
3801 {
3802#ifdef FEAT_CMDWIN
3803 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3804 {
3805 draw_state = WL_CMDLINE;
3806 if (cmdwin_type != 0 && wp == curwin)
3807 {
3808 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003810 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003811 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 }
3813 }
3814#endif
3815
3816#ifdef FEAT_FOLDING
3817 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3818 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003819 int fdc = compute_foldcolumn(wp, 0);
3820
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003822 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003824 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003825 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003826 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003827 p_extra_free = alloc(12 + 1);
3828
3829 if (p_extra_free != NULL)
3830 {
3831 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3832 n_extra = fdc;
3833 p_extra_free[n_extra] = NUL;
3834 p_extra = p_extra_free;
3835 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003836 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839 }
3840#endif
3841
3842#ifdef FEAT_SIGNS
3843 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3844 {
3845 draw_state = WL_SIGN;
3846 /* Show the sign column when there are any signs in this
3847 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003848 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003850 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003852 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853# endif
3854
3855 /* Draw two cells with the sign value or blank. */
3856 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003857 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 n_extra = 2;
3859
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003860 if (row == startrow
3861#ifdef FEAT_DIFF
3862 + filler_lines && filler_todo <= 0
3863#endif
3864 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
3866 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3867 SIGN_TEXT);
3868# ifdef FEAT_SIGN_ICONS
3869 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3870 SIGN_ICON);
3871 if (gui.in_use && icon_sign != 0)
3872 {
3873 /* Use the image in this position. */
3874 c_extra = SIGN_BYTE;
3875# ifdef FEAT_NETBEANS_INTG
3876 if (buf_signcount(wp->w_buffer, lnum) > 1)
3877 c_extra = MULTISIGN_BYTE;
3878# endif
3879 char_attr = icon_sign;
3880 }
3881 else
3882# endif
3883 if (text_sign != 0)
3884 {
3885 p_extra = sign_get_text(text_sign);
3886 if (p_extra != NULL)
3887 {
3888 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003889 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
3891 char_attr = sign_get_attr(text_sign, FALSE);
3892 }
3893 }
3894 }
3895 }
3896#endif
3897
3898 if (draw_state == WL_NR - 1 && n_extra == 0)
3899 {
3900 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003901 /* Display the absolute or relative line number. After the
3902 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3903 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 && (row == startrow
3905#ifdef FEAT_DIFF
3906 + filler_lines
3907#endif
3908 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3909 {
3910 /* Draw the line number (empty space after wrapping). */
3911 if (row == startrow
3912#ifdef FEAT_DIFF
3913 + filler_lines
3914#endif
3915 )
3916 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003917 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003918 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003919
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003920 if (wp->w_p_nu && !wp->w_p_rnu)
3921 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003922 num = (long)lnum;
3923 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003924 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003925 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003926 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003927 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003928 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003929 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003930 num = lnum;
3931 fmt = "%-*ld ";
3932 }
3933 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003934
Bram Moolenaar700e7342013-01-30 12:31:36 +01003935 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003936 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 if (wp->w_skipcol > 0)
3938 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3939 *p_extra = '-';
3940#ifdef FEAT_RIGHTLEFT
3941 if (wp->w_p_rl) /* reverse line numbers */
3942 rl_mirror(extra);
3943#endif
3944 p_extra = extra;
3945 c_extra = NUL;
3946 }
3947 else
3948 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003949 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003950 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003951#ifdef FEAT_SYN_HL
3952 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003953 * the current line differently.
3954 * TODO: Can we use CursorLine instead of CursorLineNr
3955 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003956 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003957 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003958 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961 }
3962
Bram Moolenaar597a4222014-06-25 14:39:50 +02003963#ifdef FEAT_LINEBREAK
3964 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3965 && n_extra == 0 && *p_sbr != NUL)
3966 /* draw indent after showbreak value */
3967 draw_state = WL_BRI;
3968 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3969 /* After the showbreak, draw the breakindent */
3970 draw_state = WL_BRI - 1;
3971
3972 /* draw 'breakindent': indent wrapped text accordingly */
3973 if (draw_state == WL_BRI - 1 && n_extra == 0)
3974 {
3975 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003976 /* if need_showbreak is set, breakindent also applies */
3977 if (wp->w_p_bri && n_extra == 0
3978 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003979# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003980 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003981# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003982 )
3983 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003984 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003985# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003986 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003987 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003988 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003989# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003990 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3991 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003992 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003993# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003994 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003995# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003996 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003997 c_extra = ' ';
3998 n_extra = get_breakindent_win(wp,
3999 ml_get_buf(wp->w_buffer, lnum, FALSE));
4000 /* Correct end of highlighted area for 'breakindent',
4001 * required when 'linebreak' is also set. */
4002 if (tocol == vcol)
4003 tocol += n_extra;
4004 }
4005 }
4006#endif
4007
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4009 if (draw_state == WL_SBR - 1 && n_extra == 0)
4010 {
4011 draw_state = WL_SBR;
4012# ifdef FEAT_DIFF
4013 if (filler_todo > 0)
4014 {
4015 /* Draw "deleted" diff line(s). */
4016 if (char2cells(fill_diff) > 1)
4017 c_extra = '-';
4018 else
4019 c_extra = fill_diff;
4020# ifdef FEAT_RIGHTLEFT
4021 if (wp->w_p_rl)
4022 n_extra = col + 1;
4023 else
4024# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004025 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004026 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028# endif
4029# ifdef FEAT_LINEBREAK
4030 if (*p_sbr != NUL && need_showbreak)
4031 {
4032 /* Draw 'showbreak' at the start of each broken line. */
4033 p_extra = p_sbr;
4034 c_extra = NUL;
4035 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004036 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004038 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 /* Correct end of highlighted area for 'showbreak',
4040 * required when 'linebreak' is also set. */
4041 if (tocol == vcol)
4042 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004043#ifdef FEAT_SYN_HL
4044 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004045 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004046 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004047 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050# endif
4051 }
4052#endif
4053
4054 if (draw_state == WL_LINE - 1 && n_extra == 0)
4055 {
4056 draw_state = WL_LINE;
4057 if (saved_n_extra)
4058 {
4059 /* Continue item from end of wrapped line. */
4060 n_extra = saved_n_extra;
4061 c_extra = saved_c_extra;
4062 p_extra = saved_p_extra;
4063 char_attr = saved_char_attr;
4064 }
4065 else
4066 char_attr = 0;
4067 }
4068 }
4069
4070 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004071 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004072 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073#ifdef FEAT_DIFF
4074 && filler_todo <= 0
4075#endif
4076 )
4077 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004078 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004079 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004080 /* Pretend we have finished updating the window. Except when
4081 * 'cursorcolumn' is set. */
4082#ifdef FEAT_SYN_HL
4083 if (wp->w_p_cuc)
4084 row = wp->w_cline_row + wp->w_cline_height;
4085 else
4086#endif
4087 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 break;
4089 }
4090
4091 if (draw_state == WL_LINE && area_highlighting)
4092 {
4093 /* handle Visual or match highlighting in this line */
4094 if (vcol == fromcol
4095#ifdef FEAT_MBYTE
4096 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4097 && (*mb_ptr2cells)(ptr) > 1)
4098#endif
4099 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004100 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 && vcol < tocol))
4102 area_attr = attr; /* start highlighting */
4103 else if (area_attr != 0
4104 && (vcol == tocol
4105 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
4108#ifdef FEAT_SEARCH_EXTRA
4109 if (!n_extra)
4110 {
4111 /*
4112 * Check for start/end of search pattern match.
4113 * After end, check for start/end of next match.
4114 * When another match, have to check for start again.
4115 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004116 * Do this for 'search_hl' and the match list (ordered by
4117 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004119 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004120 cur = wp->w_match_head;
4121 shl_flag = FALSE;
4122 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004124 if (shl_flag == FALSE
4125 && ((cur != NULL
4126 && cur->priority > SEARCH_HL_PRIORITY)
4127 || cur == NULL))
4128 {
4129 shl = &search_hl;
4130 shl_flag = TRUE;
4131 }
4132 else
4133 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004134 if (cur != NULL)
4135 cur->pos.cur = 0;
4136 pos_inprogress = TRUE;
4137 while (shl->rm.regprog != NULL
4138 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004140 if (shl->startcol != MAXCOL
4141 && v >= (long)shl->startcol
4142 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004144#ifdef FEAT_MBYTE
4145 int tmp_col = v + MB_PTR2LEN(ptr);
4146
4147 if (shl->endcol < tmp_col)
4148 shl->endcol = tmp_col;
4149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004151#ifdef FEAT_CONCEAL
4152 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4153 == cur->hlg_id)
4154 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004155 has_match_conc =
4156 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004157 match_conc = cur->conceal_char;
4158 }
4159 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004160 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004163 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
4165 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004166 next_search_hl(wp, shl, lnum, (colnr_T)v,
4167 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004168 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004169 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
4171 /* Need to get the line again, a multi-line regexp
4172 * may have made it invalid. */
4173 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4174 ptr = line + v;
4175
4176 if (shl->lnum == lnum)
4177 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004178 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004180 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004182 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004184 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 {
4186 /* highlight empty match, try again after
4187 * it */
4188#ifdef FEAT_MBYTE
4189 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004190 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004191 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 else
4193#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004194 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196
4197 /* Loop to check if the match starts at the
4198 * current position */
4199 continue;
4200 }
4201 }
4202 break;
4203 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004204 if (shl != &search_hl && cur != NULL)
4205 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004207
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004208 /* Use attributes from match with highest priority among
4209 * 'search_hl' and the match list. */
4210 search_attr = search_hl.attr_cur;
4211 cur = wp->w_match_head;
4212 shl_flag = FALSE;
4213 while (cur != NULL || shl_flag == FALSE)
4214 {
4215 if (shl_flag == FALSE
4216 && ((cur != NULL
4217 && cur->priority > SEARCH_HL_PRIORITY)
4218 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004219 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004220 shl = &search_hl;
4221 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004222 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004223 else
4224 shl = &cur->hl;
4225 if (shl->attr_cur != 0)
4226 search_attr = shl->attr_cur;
4227 if (shl != &search_hl && cur != NULL)
4228 cur = cur->next;
4229 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004230 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004231 if (*ptr == NUL && (did_line_attr >= 1
4232 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004233 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235#endif
4236
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004238 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004240 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4241 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004243 if (diff_hlf == HLF_TXD && ptr - line > change_end
4244 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004246 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004247 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004248 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004251
4252 /* Decide which of the highlight attributes to use. */
4253 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004254#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004255 if (area_attr != 0)
4256 char_attr = hl_combine_attr(line_attr, area_attr);
4257 else if (search_attr != 0)
4258 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004259 /* Use line_attr when not in the Visual or 'incsearch' area
4260 * (area_attr may be 0 when "noinvcur" is set). */
4261 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004262 || vcol < fromcol || vcol_prev < fromcol_prev
4263 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004264 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004265#else
4266 if (area_attr != 0)
4267 char_attr = area_attr;
4268 else if (search_attr != 0)
4269 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004270#endif
4271 else
4272 {
4273 attr_pri = FALSE;
4274#ifdef FEAT_SYN_HL
4275 if (has_syntax)
4276 char_attr = syntax_attr;
4277 else
4278#endif
4279 char_attr = 0;
4280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282
4283 /*
4284 * Get the next character to put on the screen.
4285 */
4286 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004287 * The "p_extra" points to the extra stuff that is inserted to
4288 * represent special characters (non-printable stuff) and other
4289 * things. When all characters are the same, c_extra is used.
4290 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4291 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4293 */
4294 if (n_extra > 0)
4295 {
4296 if (c_extra != NUL)
4297 {
4298 c = c_extra;
4299#ifdef FEAT_MBYTE
4300 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004301 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 {
4303 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004304 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004305 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 }
4307 else
4308 mb_utf8 = FALSE;
4309#endif
4310 }
4311 else
4312 {
4313 c = *p_extra;
4314#ifdef FEAT_MBYTE
4315 if (has_mbyte)
4316 {
4317 mb_c = c;
4318 if (enc_utf8)
4319 {
4320 /* If the UTF-8 character is more than one byte:
4321 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004322 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 mb_utf8 = FALSE;
4324 if (mb_l > n_extra)
4325 mb_l = 1;
4326 else if (mb_l > 1)
4327 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004328 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004330 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332 }
4333 else
4334 {
4335 /* if this is a DBCS character, put it in "mb_c" */
4336 mb_l = MB_BYTE2LEN(c);
4337 if (mb_l >= n_extra)
4338 mb_l = 1;
4339 else if (mb_l > 1)
4340 mb_c = (c << 8) + p_extra[1];
4341 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004342 if (mb_l == 0) /* at the NUL at end-of-line */
4343 mb_l = 1;
4344
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 /* If a double-width char doesn't fit display a '>' in the
4346 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004347 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348# ifdef FEAT_RIGHTLEFT
4349 wp->w_p_rl ? (col <= 0) :
4350# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004351 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 && (*mb_char2cells)(mb_c) == 2)
4353 {
4354 c = '>';
4355 mb_c = c;
4356 mb_l = 1;
4357 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004358 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 /* put the pointer back to output the double-width
4360 * character at the start of the next line. */
4361 ++n_extra;
4362 --p_extra;
4363 }
4364 else
4365 {
4366 n_extra -= mb_l - 1;
4367 p_extra += mb_l - 1;
4368 }
4369 }
4370#endif
4371 ++p_extra;
4372 }
4373 --n_extra;
4374 }
4375 else
4376 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004377#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004378 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004379#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004380
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004381 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004382 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 /*
4384 * Get a character from the line itself.
4385 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004386 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004387#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004388 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390#ifdef FEAT_MBYTE
4391 if (has_mbyte)
4392 {
4393 mb_c = c;
4394 if (enc_utf8)
4395 {
4396 /* If the UTF-8 character is more than one byte: Decode it
4397 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004398 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 mb_utf8 = FALSE;
4400 if (mb_l > 1)
4401 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004402 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 /* Overlong encoded ASCII or ASCII with composing char
4404 * is displayed normally, except a NUL. */
4405 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004406 {
4407 c = mb_c;
4408# ifdef FEAT_LINEBREAK
4409 c0 = mb_c;
4410# endif
4411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004413
4414 /* At start of the line we can have a composing char.
4415 * Draw it as a space with a composing char. */
4416 if (utf_iscomposing(mb_c))
4417 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004418 int i;
4419
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004420 for (i = Screen_mco - 1; i > 0; --i)
4421 u8cc[i] = u8cc[i - 1];
4422 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004423 mb_c = ' ';
4424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426
4427 if ((mb_l == 1 && c >= 0x80)
4428 || (mb_l >= 1 && mb_c == 0)
4429 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004430# ifdef UNICODE16
4431 || mb_c >= 0x10000
4432# endif
4433 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 {
4435 /*
4436 * Illegal UTF-8 byte: display as <xx>.
4437 * Non-BMP character : display as ? or fullwidth ?.
4438 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004439# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
4443 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004444# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 if (wp->w_p_rl) /* reverse */
4446 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004449# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 else if (utf_char2cells(mb_c) != 2)
4451 STRCPY(extra, "?");
4452 else
4453 /* 0xff1f in UTF-8: full-width '?' */
4454 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
4457 p_extra = extra;
4458 c = *p_extra;
4459 mb_c = mb_ptr2char_adv(&p_extra);
4460 mb_utf8 = (c >= 0x80);
4461 n_extra = (int)STRLEN(p_extra);
4462 c_extra = NUL;
4463 if (area_attr == 0 && search_attr == 0)
4464 {
4465 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004466 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 saved_attr2 = char_attr; /* save current attr */
4468 }
4469 }
4470 else if (mb_l == 0) /* at the NUL at end-of-line */
4471 mb_l = 1;
4472#ifdef FEAT_ARABIC
4473 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4474 {
4475 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004476 int pc, pc1, nc;
4477 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479 /* The idea of what is the previous and next
4480 * character depends on 'rightleft'. */
4481 if (wp->w_p_rl)
4482 {
4483 pc = prev_c;
4484 pc1 = prev_c1;
4485 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004486 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488 else
4489 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004490 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004492 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 prev_c = mb_c;
4495
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004496 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498 else
4499 prev_c = mb_c;
4500#endif
4501 }
4502 else /* enc_dbcs */
4503 {
4504 mb_l = MB_BYTE2LEN(c);
4505 if (mb_l == 0) /* at the NUL at end-of-line */
4506 mb_l = 1;
4507 else if (mb_l > 1)
4508 {
4509 /* We assume a second byte below 32 is illegal.
4510 * Hopefully this is OK for all double-byte encodings!
4511 */
4512 if (ptr[1] >= 32)
4513 mb_c = (c << 8) + ptr[1];
4514 else
4515 {
4516 if (ptr[1] == NUL)
4517 {
4518 /* head byte at end of line */
4519 mb_l = 1;
4520 transchar_nonprint(extra, c);
4521 }
4522 else
4523 {
4524 /* illegal tail byte */
4525 mb_l = 2;
4526 STRCPY(extra, "XX");
4527 }
4528 p_extra = extra;
4529 n_extra = (int)STRLEN(extra) - 1;
4530 c_extra = NUL;
4531 c = *p_extra++;
4532 if (area_attr == 0 && search_attr == 0)
4533 {
4534 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004535 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 saved_attr2 = char_attr; /* save current attr */
4537 }
4538 mb_c = c;
4539 }
4540 }
4541 }
4542 /* If a double-width char doesn't fit display a '>' in the
4543 * last column; the character is displayed at the start of the
4544 * next line. */
4545 if ((
4546# ifdef FEAT_RIGHTLEFT
4547 wp->w_p_rl ? (col <= 0) :
4548# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004549 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 && (*mb_char2cells)(mb_c) == 2)
4551 {
4552 c = '>';
4553 mb_c = c;
4554 mb_utf8 = FALSE;
4555 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004556 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 /* Put pointer back so that the character will be
4558 * displayed at the start of the next line. */
4559 --ptr;
4560 }
4561 else if (*ptr != NUL)
4562 ptr += mb_l - 1;
4563
4564 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004565 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004566 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004567 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004570 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 c = ' ';
4572 if (area_attr == 0 && search_attr == 0)
4573 {
4574 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004575 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 saved_attr2 = char_attr; /* save current attr */
4577 }
4578 mb_c = c;
4579 mb_utf8 = FALSE;
4580 mb_l = 1;
4581 }
4582
4583 }
4584#endif
4585 ++ptr;
4586
4587 if (extra_check)
4588 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004589#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004590 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004591#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004592
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004593#ifdef FEAT_TERMINAL
4594 if (get_term_attr)
4595 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004596 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004597
4598 if (!attr_pri)
4599 char_attr = syntax_attr;
4600 else
4601 char_attr = hl_combine_attr(syntax_attr, char_attr);
4602 }
4603#endif
4604
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004605#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 /* Get syntax attribute, unless still at the start of the line
4607 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004608 v = (long)(ptr - line);
4609 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
4611 /* Get the syntax attribute for the character. If there
4612 * is an error, disable syntax highlighting. */
4613 save_did_emsg = did_emsg;
4614 did_emsg = FALSE;
4615
Bram Moolenaar217ad922005-03-20 22:37:15 +00004616 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004617# ifdef FEAT_SPELL
4618 has_spell ? &can_spell :
4619# endif
4620 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
4622 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004623 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004624 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004625 has_syntax = FALSE;
4626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 else
4628 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004629#ifdef SYN_TIME_LIMIT
4630 if (wp->w_s->b_syn_slow)
4631 has_syntax = FALSE;
4632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
4634 /* Need to get the line again, a multi-line regexp may
4635 * have made it invalid. */
4636 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4637 ptr = line + v;
4638
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004639 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004641 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004642 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004643# ifdef FEAT_CONCEAL
4644 /* no concealing past the end of the line, it interferes
4645 * with line highlighting */
4646 if (c == NUL)
4647 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004648 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004649 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004650# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004652#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004653
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004654#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004655 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004656 * Only do this when there is no syntax highlighting, the
4657 * @Spell cluster is not used or the current syntax item
4658 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004659 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004660 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004661 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004662# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004663 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004664 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004665# endif
4666 if (c != 0 && (
4667# ifdef FEAT_SYN_HL
4668 !has_syntax ||
4669# endif
4670 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004671 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004672 char_u *prev_ptr, *p;
4673 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004674 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004675# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004676 if (has_mbyte)
4677 {
4678 prev_ptr = ptr - mb_l;
4679 v -= mb_l - 1;
4680 }
4681 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004682# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004683 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004684
4685 /* Use nextline[] if possible, it has the start of the
4686 * next line concatenated. */
4687 if ((prev_ptr - line) - nextlinecol >= 0)
4688 p = nextline + (prev_ptr - line) - nextlinecol;
4689 else
4690 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004691 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004692 len = spell_check(wp, p, &spell_hlf, &cap_col,
4693 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004694 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004695
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004696 /* In Insert mode only highlight a word that
4697 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004698 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004699 && (State & INSERT) != 0
4700 && wp->w_cursor.lnum == lnum
4701 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004702 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004703 && wp->w_cursor.col < (colnr_T)word_end)
4704 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004705 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004706 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004707 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004708
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004709 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004710 && (p - nextline) + len > nextline_idx)
4711 {
4712 /* Remember that the good word continues at the
4713 * start of the next line. */
4714 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004715 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004716 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004717
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004718 /* Turn index into actual attributes. */
4719 if (spell_hlf != HLF_COUNT)
4720 spell_attr = highlight_attr[spell_hlf];
4721
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004722 if (cap_col > 0)
4723 {
4724 if (p != prev_ptr
4725 && (p - nextline) + cap_col >= nextline_idx)
4726 {
4727 /* Remember that the word in the next line
4728 * must start with a capital. */
4729 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004730 cap_col = (int)((p - nextline) + cap_col
4731 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004732 }
4733 else
4734 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004735 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004736 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004737 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004738 }
4739 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004740 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004741 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004742 char_attr = hl_combine_attr(char_attr, spell_attr);
4743 else
4744 char_attr = hl_combine_attr(spell_attr, char_attr);
4745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746#endif
4747#ifdef FEAT_LINEBREAK
4748 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004749 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004751 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004752 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004754# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004755 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004756# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004757 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004759 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004761 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004762
Bram Moolenaar597a4222014-06-25 14:39:50 +02004763 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004764 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004765 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004766 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004767# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004768 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004769 wp->w_buffer->b_p_vts_array) - 1;
4770# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004771 n_extra = (int)wp->w_buffer->b_p_ts
4772 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004773# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004774
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004775# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004776 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004777# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004779# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004780 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004781 {
4782#ifdef FEAT_CONCEAL
4783 if (c == TAB)
4784 /* See "Tab alignment" below. */
4785 FIX_FOR_BOGUSCOLS;
4786#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004787 if (!wp->w_p_list)
4788 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
4791#endif
4792
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004793 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4794 */
4795 if (wp->w_p_list
4796 && (((c == 160
4797#ifdef FEAT_MBYTE
4798 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4799#endif
4800 ) && lcs_nbsp)
4801 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4802 {
4803 c = (c == ' ') ? lcs_space : lcs_nbsp;
4804 if (area_attr == 0 && search_attr == 0)
4805 {
4806 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004807 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004808 saved_attr2 = char_attr; /* save current attr */
4809 }
4810#ifdef FEAT_MBYTE
4811 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004812 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004813 {
4814 mb_utf8 = TRUE;
4815 u8cc[0] = 0;
4816 c = 0xc0;
4817 }
4818 else
4819 mb_utf8 = FALSE;
4820#endif
4821 }
4822
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4824 {
4825 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004826 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 {
4828 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004829 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 saved_attr2 = char_attr; /* save current attr */
4831 }
4832#ifdef FEAT_MBYTE
4833 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004834 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 {
4836 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004837 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004838 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840 else
4841 mb_utf8 = FALSE;
4842#endif
4843 }
4844 }
4845
4846 /*
4847 * Handling of non-printable characters.
4848 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004849 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
4851 /*
4852 * when getting a character from the file, we may have to
4853 * turn it into something else on the way to putting it
4854 * into "ScreenLines".
4855 */
4856 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4857 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004858 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004859 long vcol_adjusted = vcol; /* removed showbreak length */
4860#ifdef FEAT_LINEBREAK
4861 /* only adjust the tab_len, when at the first column
4862 * after the showbreak value was drawn */
4863 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4864 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004867#ifdef FEAT_VARTABS
4868 tab_len = tabstop_padding(vcol_adjusted,
4869 wp->w_buffer->b_p_ts,
4870 wp->w_buffer->b_p_vts_array) - 1;
4871#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004872 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004873 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4874#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004875
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004876#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004877 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004878#endif
4879 /* tab amount depends on current column */
4880 n_extra = tab_len;
4881#ifdef FEAT_LINEBREAK
4882 else
4883 {
4884 char_u *p;
4885 int len = n_extra;
4886 int i;
4887 int saved_nextra = n_extra;
4888
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004889#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004890 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004891 /* there are characters to conceal */
4892 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004893 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4894 */
4895 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4896 && n_extra > tab_len)
4897 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004898#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004899
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004900 /* if n_extra > 0, it gives the number of chars, to
4901 * use for a tab, else we need to calculate the width
4902 * for a tab */
4903#ifdef FEAT_MBYTE
4904 len = (tab_len * mb_char2len(lcs_tab2));
4905 if (n_extra > 0)
4906 len += n_extra - tab_len;
4907#endif
4908 c = lcs_tab1;
4909 p = alloc((unsigned)(len + 1));
4910 vim_memset(p, ' ', len);
4911 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004912 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004913 p_extra_free = p;
4914 for (i = 0; i < tab_len; i++)
4915 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004916 if (*p == NUL)
4917 {
4918 tab_len = i;
4919 break;
4920 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004921#ifdef FEAT_MBYTE
4922 mb_char2bytes(lcs_tab2, p);
4923 p += mb_char2len(lcs_tab2);
4924 n_extra += mb_char2len(lcs_tab2)
4925 - (saved_nextra > 0 ? 1 : 0);
4926#else
4927 p[i] = lcs_tab2;
4928#endif
4929 }
4930 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004931#ifdef FEAT_CONCEAL
4932 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4933 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004934 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004935 n_extra -= vcol_off;
4936#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004937 }
4938#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004939#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004940 {
4941 int vc_saved = vcol_off;
4942
4943 /* Tab alignment should be identical regardless of
4944 * 'conceallevel' value. So tab compensates of all
4945 * previous concealed characters, and thus resets
4946 * vcol_off and boguscols accumulated so far in the
4947 * line. Note that the tab can be longer than
4948 * 'tabstop' when there are concealed characters. */
4949 FIX_FOR_BOGUSCOLS;
4950
4951 /* Make sure, the highlighting for the tab char will be
4952 * correctly set further below (effectively reverts the
4953 * FIX_FOR_BOGSUCOLS macro */
4954 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004955 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004956 tab_len += vc_saved;
4957 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959#ifdef FEAT_MBYTE
4960 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4961#endif
4962 if (wp->w_p_list)
4963 {
4964 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004965#ifdef FEAT_LINEBREAK
4966 if (wp->w_p_lbr)
4967 c_extra = NUL; /* using p_extra from above */
4968 else
4969#endif
4970 c_extra = lcs_tab2;
4971 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004972 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 saved_attr2 = char_attr; /* save current attr */
4974#ifdef FEAT_MBYTE
4975 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004976 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
4978 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004979 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004980 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982#endif
4983 }
4984 else
4985 {
4986 c_extra = ' ';
4987 c = ' ';
4988 }
4989 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004990 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004991 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004992 || ((fromcol >= 0 || fromcol_prev >= 0)
4993 && tocol > vcol
4994 && VIsual_mode != Ctrl_V
4995 && (
4996# ifdef FEAT_RIGHTLEFT
4997 wp->w_p_rl ? (col >= 0) :
4998# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004999 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005000 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005001 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005002 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005003 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005005 /* Display a '$' after the line or highlight an extra
5006 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5008 /* For a diff line the highlighting continues after the
5009 * "$". */
5010 if (
5011# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005012 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013# ifdef LINE_ATTR
5014 &&
5015# endif
5016# endif
5017# ifdef LINE_ATTR
5018 line_attr == 0
5019# endif
5020 )
5021#endif
5022 {
5023#ifdef FEAT_VIRTUALEDIT
5024 /* In virtualedit, visual selections may extend
5025 * beyond end of line. */
5026 if (area_highlighting && virtual_active()
5027 && tocol != MAXCOL && vcol < tocol)
5028 n_extra = 0;
5029 else
5030#endif
5031 {
5032 p_extra = at_end_str;
5033 n_extra = 1;
5034 c_extra = NUL;
5035 }
5036 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005037 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005038 c = lcs_eol;
5039 else
5040 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 lcs_eol_one = -1;
5042 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005043 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005045 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 n_attr = 1;
5047 }
5048#ifdef FEAT_MBYTE
5049 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005050 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 {
5052 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005053 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005054 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
5056 else
5057 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5058#endif
5059 }
5060 else if (c != NUL)
5061 {
5062 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005063 if (n_extra == 0)
5064 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065#ifdef FEAT_RIGHTLEFT
5066 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5067 rl_mirror(p_extra); /* reverse "<12>" */
5068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005070#ifdef FEAT_LINEBREAK
5071 if (wp->w_p_lbr)
5072 {
5073 char_u *p;
5074
5075 c = *p_extra;
5076 p = alloc((unsigned)n_extra + 1);
5077 vim_memset(p, ' ', n_extra);
5078 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5079 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005080 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005081 p_extra_free = p_extra = p;
5082 }
5083 else
5084#endif
5085 {
5086 n_extra = byte2cells(c) - 1;
5087 c = *p_extra++;
5088 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005089 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005092 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 saved_attr2 = char_attr; /* save current attr */
5094 }
5095#ifdef FEAT_MBYTE
5096 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5097#endif
5098 }
5099#ifdef FEAT_VIRTUALEDIT
5100 else if (VIsual_active
5101 && (VIsual_mode == Ctrl_V
5102 || VIsual_mode == 'v')
5103 && virtual_active()
5104 && tocol != MAXCOL
5105 && vcol < tocol
5106 && (
5107# ifdef FEAT_RIGHTLEFT
5108 wp->w_p_rl ? (col >= 0) :
5109# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005110 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
5112 c = ' ';
5113 --ptr; /* put it back at the NUL */
5114 }
5115#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005116#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 else if ((
5118# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005119 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005121# ifdef FEAT_TERMINAL
5122 term_attr != 0 ||
5123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 ) && (
5126# ifdef FEAT_RIGHTLEFT
5127 wp->w_p_rl ? (col >= 0) :
5128# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005129 (col
5130# ifdef FEAT_CONCEAL
5131 - boguscols
5132# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005133 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
5135 /* Highlight until the right side of the window */
5136 c = ' ';
5137 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005138
5139 /* Remember we do the char for line highlighting. */
5140 ++did_line_attr;
5141
5142 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005143 if (line_attr != 0 && char_attr == search_attr
5144 && (did_line_attr > 1
5145 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005146 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147# ifdef FEAT_DIFF
5148 if (diff_hlf == HLF_TXD)
5149 {
5150 diff_hlf = HLF_CHD;
5151 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005152 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005153 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005154 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5155 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005156 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
5159# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005160# ifdef FEAT_TERMINAL
5161 if (term_attr != 0)
5162 {
5163 char_attr = term_attr;
5164 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5165 char_attr = hl_combine_attr(char_attr,
5166 HL_ATTR(HLF_CUL));
5167 }
5168# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170#endif
5171 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005172
5173#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005174 if ( wp->w_p_cole > 0
5175 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005176 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005177 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005178 && !(lnum_in_visual_area
5179 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005180 {
5181 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005182 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005183 && (syn_get_sub_char() != NUL || match_conc
5184 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005185 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005186 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005187 /* First time at this concealed item: display one
5188 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005189 if (match_conc)
5190 c = match_conc;
5191 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005192 c = syn_get_sub_char();
5193 else if (lcs_conceal != NUL)
5194 c = lcs_conceal;
5195 else
5196 c = ' ';
5197
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005198 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005199
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005200 if (n_extra > 0)
5201 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005202 vcol += n_extra;
5203 if (wp->w_p_wrap && n_extra > 0)
5204 {
5205# ifdef FEAT_RIGHTLEFT
5206 if (wp->w_p_rl)
5207 {
5208 col -= n_extra;
5209 boguscols -= n_extra;
5210 }
5211 else
5212# endif
5213 {
5214 boguscols += n_extra;
5215 col += n_extra;
5216 }
5217 }
5218 n_extra = 0;
5219 n_attr = 0;
5220 }
5221 else if (n_skip == 0)
5222 {
5223 is_concealing = TRUE;
5224 n_skip = 1;
5225 }
5226# ifdef FEAT_MBYTE
5227 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005228 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005229 {
5230 mb_utf8 = TRUE;
5231 u8cc[0] = 0;
5232 c = 0xc0;
5233 }
5234 else
5235 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5236# endif
5237 }
5238 else
5239 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005240 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005241 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005242 }
5243#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005246#ifdef FEAT_CONCEAL
5247 /* In the cursor line and we may be concealing characters: correct
5248 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005249 if (!did_wcol && draw_state == WL_LINE
5250 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005251 && conceal_cursor_line(wp)
5252 && (int)wp->w_virtcol <= vcol + n_skip)
5253 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005254# ifdef FEAT_RIGHTLEFT
5255 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005256 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005257 else
5258# endif
5259 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005260 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005261 did_wcol = TRUE;
5262 }
5263#endif
5264
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 /* Don't override visual selection highlighting. */
5266 if (n_attr > 0
5267 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005268 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 char_attr = extra_attr;
5270
Bram Moolenaar81695252004-12-29 20:58:21 +00005271#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 /* XIM don't send preedit_start and preedit_end, but they send
5273 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5274 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005275 if (p_imst == IM_ON_THE_SPOT
5276 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005277 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 && (State & INSERT)
5279 && !p_imdisable
5280 && im_is_preediting()
5281 && draw_state == WL_LINE)
5282 {
5283 colnr_T tcol;
5284
5285 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005286 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 else
5288 tcol = preedit_end_col;
5289 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5290 {
5291 if (feedback_old_attr < 0)
5292 {
5293 feedback_col = 0;
5294 feedback_old_attr = char_attr;
5295 }
5296 char_attr = im_get_feedback_attr(feedback_col);
5297 if (char_attr < 0)
5298 char_attr = feedback_old_attr;
5299 feedback_col++;
5300 }
5301 else if (feedback_old_attr >= 0)
5302 {
5303 char_attr = feedback_old_attr;
5304 feedback_old_attr = -1;
5305 feedback_col = 0;
5306 }
5307 }
5308#endif
5309 /*
5310 * Handle the case where we are in column 0 but not on the first
5311 * character of the line and the user wants us to show us a
5312 * special character (via 'listchars' option "precedes:<char>".
5313 */
5314 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005315 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5317#ifdef FEAT_DIFF
5318 && filler_todo <= 0
5319#endif
5320 && draw_state > WL_NR
5321 && c != NUL)
5322 {
5323 c = lcs_prec;
5324 lcs_prec_todo = NUL;
5325#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005326 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5327 {
5328 /* Double-width character being overwritten by the "precedes"
5329 * character, need to fill up half the character. */
5330 c_extra = MB_FILLER_CHAR;
5331 n_extra = 1;
5332 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005333 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005336 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
5338 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005339 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005340 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342 else
5343 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5344#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005345 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
5347 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005348 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 n_attr3 = 1;
5350 }
5351 }
5352
5353 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005354 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005356 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005357#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005358 || did_line_attr == 1
5359#endif
5360 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005362#ifdef FEAT_SEARCH_EXTRA
5363 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005364
5365 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005366 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005367 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005368#endif
5369
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005370 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 * highlight match at end of line. If it's beyond the last
5372 * char on the screen, just overwrite that one (tricky!) Not
5373 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005374#ifdef FEAT_SEARCH_EXTRA
5375 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005376 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005377 prevcol_hl_flag = TRUE;
5378 else
5379 {
5380 cur = wp->w_match_head;
5381 while (cur != NULL)
5382 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005383 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005384 {
5385 prevcol_hl_flag = TRUE;
5386 break;
5387 }
5388 cur = cur->next;
5389 }
5390 }
5391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005393 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005394 && (VIsual_mode != Ctrl_V
5395 || lnum == VIsual.lnum
5396 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005397 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398#ifdef FEAT_SEARCH_EXTRA
5399 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005400 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005401# ifdef FEAT_SYN_HL
5402 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5403 && !(wp == curwin && VIsual_active))
5404# endif
5405# ifdef FEAT_DIFF
5406 && diff_hlf == (hlf_T)0
5407# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005408# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005409 && did_line_attr <= 1
5410# endif
5411 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412#endif
5413 ))
5414 {
5415 int n = 0;
5416
5417#ifdef FEAT_RIGHTLEFT
5418 if (wp->w_p_rl)
5419 {
5420 if (col < 0)
5421 n = 1;
5422 }
5423 else
5424#endif
5425 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005426 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 n = -1;
5428 }
5429 if (n != 0)
5430 {
5431 /* At the window boundary, highlight the last character
5432 * instead (better than nothing). */
5433 off += n;
5434 col += n;
5435 }
5436 else
5437 {
5438 /* Add a blank character to highlight. */
5439 ScreenLines[off] = ' ';
5440#ifdef FEAT_MBYTE
5441 if (enc_utf8)
5442 ScreenLinesUC[off] = 0;
5443#endif
5444 }
5445#ifdef FEAT_SEARCH_EXTRA
5446 if (area_attr == 0)
5447 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005448 /* Use attributes from match with highest priority among
5449 * 'search_hl' and the match list. */
5450 char_attr = search_hl.attr;
5451 cur = wp->w_match_head;
5452 shl_flag = FALSE;
5453 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005454 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005455 if (shl_flag == FALSE
5456 && ((cur != NULL
5457 && cur->priority > SEARCH_HL_PRIORITY)
5458 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005459 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005460 shl = &search_hl;
5461 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005462 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005463 else
5464 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005465 if ((ptr - line) - 1 == (long)shl->startcol
5466 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005467 char_attr = shl->attr;
5468 if (shl != &search_hl && cur != NULL)
5469 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472#endif
5473 ScreenAttrs[off] = char_attr;
5474#ifdef FEAT_RIGHTLEFT
5475 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005478 --off;
5479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 else
5481#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005484 ++off;
5485 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005486 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005487#ifdef FEAT_SYN_HL
5488 eol_hl_off = 1;
5489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
Bram Moolenaar91170f82006-05-05 21:15:17 +00005493 /*
5494 * At end of the text line.
5495 */
5496 if (c == NUL)
5497 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005498#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005499 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005500 if (wp->w_p_wrap)
5501 v = wp->w_skipcol;
5502 else
5503 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005504
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005505 /* check if line ends before left margin */
5506 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005507 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005508#ifdef FEAT_CONCEAL
5509 /* Get rid of the boguscols now, we want to draw until the right
5510 * edge for 'cursorcolumn'. */
5511 col -= boguscols;
5512 boguscols = 0;
5513#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005514
5515 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005516 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005517
5518 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005519 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5520 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005521 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005522 && lnum != wp->w_cursor.lnum)
5523 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005524# ifdef FEAT_RIGHTLEFT
5525 && !wp->w_p_rl
5526# endif
5527 )
5528 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005529 int rightmost_vcol = 0;
5530 int i;
5531
5532 if (wp->w_p_cuc)
5533 rightmost_vcol = wp->w_virtcol;
5534 if (draw_color_col)
5535 /* determine rightmost colorcolumn to possibly draw */
5536 for (i = 0; color_cols[i] >= 0; ++i)
5537 if (rightmost_vcol < color_cols[i])
5538 rightmost_vcol = color_cols[i];
5539
Bram Moolenaar02631462017-09-22 15:20:32 +02005540 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005541 {
5542 ScreenLines[off] = ' ';
5543#ifdef FEAT_MBYTE
5544 if (enc_utf8)
5545 ScreenLinesUC[off] = 0;
5546#endif
5547 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005548 if (draw_color_col)
5549 draw_color_col = advance_color_col(VCOL_HLC,
5550 &color_cols);
5551
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005552 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005553 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005554 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005555 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005556 else
5557 ScreenAttrs[off++] = 0;
5558
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005559 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005560 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005561
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005562 ++vcol;
5563 }
5564 }
5565#endif
5566
Bram Moolenaar53f81742017-09-22 14:35:51 +02005567 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005568 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 row++;
5570
5571 /*
5572 * Update w_cline_height and w_cline_folded if the cursor line was
5573 * updated (saves a call to plines() later).
5574 */
5575 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5576 {
5577 curwin->w_cline_row = startrow;
5578 curwin->w_cline_height = row - startrow;
5579#ifdef FEAT_FOLDING
5580 curwin->w_cline_folded = FALSE;
5581#endif
5582 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5583 }
5584
5585 break;
5586 }
5587
5588 /* line continues beyond line end */
5589 if (lcs_ext
5590 && !wp->w_p_wrap
5591#ifdef FEAT_DIFF
5592 && filler_todo <= 0
5593#endif
5594 && (
5595#ifdef FEAT_RIGHTLEFT
5596 wp->w_p_rl ? col == 0 :
5597#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005598 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005600 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5602 {
5603 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005604 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605#ifdef FEAT_MBYTE
5606 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005607 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
5609 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005610 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005611 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
5613 else
5614 mb_utf8 = FALSE;
5615#endif
5616 }
5617
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005618#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005619 /* advance to the next 'colorcolumn' */
5620 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005621 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005622
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005623 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005624 * highlight the cursor position itself.
5625 * Also highlight the 'colorcolumn' if it is different than
5626 * 'cursorcolumn' */
5627 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005628 if (draw_state == WL_LINE && !lnum_in_visual_area
5629 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005630 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005631 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005632 && lnum != wp->w_cursor.lnum)
5633 {
5634 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005635 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005636 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005637 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005638 {
5639 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005640 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005641 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005642 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005643#endif
5644
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 /*
5646 * Store character to be displayed.
5647 * Skip characters that are left of the screen for 'nowrap'.
5648 */
5649 vcol_prev = vcol;
5650 if (draw_state < WL_LINE || n_skip <= 0)
5651 {
5652 /*
5653 * Store the character.
5654 */
5655#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5656 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5657 {
5658 /* A double-wide character is: put first halve in left cell. */
5659 --off;
5660 --col;
5661 }
5662#endif
5663 ScreenLines[off] = c;
5664#ifdef FEAT_MBYTE
5665 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005666 {
5667 if ((mb_c & 0xff00) == 0x8e00)
5668 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 else if (enc_utf8)
5672 {
5673 if (mb_utf8)
5674 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005675 int i;
5676
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005678 if ((c & 0xff) == 0)
5679 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005680 for (i = 0; i < Screen_mco; ++i)
5681 {
5682 ScreenLinesC[i][off] = u8cc[i];
5683 if (u8cc[i] == 0)
5684 break;
5685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
5687 else
5688 ScreenLinesUC[off] = 0;
5689 }
5690 if (multi_attr)
5691 {
5692 ScreenAttrs[off] = multi_attr;
5693 multi_attr = 0;
5694 }
5695 else
5696#endif
5697 ScreenAttrs[off] = char_attr;
5698
5699#ifdef FEAT_MBYTE
5700 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5701 {
5702 /* Need to fill two screen columns. */
5703 ++off;
5704 ++col;
5705 if (enc_utf8)
5706 /* UTF-8: Put a 0 in the second screen char. */
5707 ScreenLines[off] = 0;
5708 else
5709 /* DBCS: Put second byte in the second screen char. */
5710 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005711 if (draw_state > WL_NR
5712#ifdef FEAT_DIFF
5713 && filler_todo <= 0
5714#endif
5715 )
5716 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 /* When "tocol" is halfway a character, set it to the end of
5718 * the character, otherwise highlighting won't stop. */
5719 if (tocol == vcol)
5720 ++tocol;
5721#ifdef FEAT_RIGHTLEFT
5722 if (wp->w_p_rl)
5723 {
5724 /* now it's time to backup one cell */
5725 --off;
5726 --col;
5727 }
5728#endif
5729 }
5730#endif
5731#ifdef FEAT_RIGHTLEFT
5732 if (wp->w_p_rl)
5733 {
5734 --off;
5735 --col;
5736 }
5737 else
5738#endif
5739 {
5740 ++off;
5741 ++col;
5742 }
5743 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005744#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005745 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005746 {
5747 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005748 ++vcol_off;
5749 if (n_extra > 0)
5750 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005751 if (wp->w_p_wrap)
5752 {
5753 /*
5754 * Special voodoo required if 'wrap' is on.
5755 *
5756 * Advance the column indicator to force the line
5757 * drawing to wrap early. This will make the line
5758 * take up the same screen space when parts are concealed,
5759 * so that cursor line computations aren't messed up.
5760 *
5761 * To avoid the fictitious advance of 'col' causing
5762 * trailing junk to be written out of the screen line
5763 * we are building, 'boguscols' keeps track of the number
5764 * of bad columns we have advanced.
5765 */
5766 if (n_extra > 0)
5767 {
5768 vcol += n_extra;
5769# ifdef FEAT_RIGHTLEFT
5770 if (wp->w_p_rl)
5771 {
5772 col -= n_extra;
5773 boguscols -= n_extra;
5774 }
5775 else
5776# endif
5777 {
5778 col += n_extra;
5779 boguscols += n_extra;
5780 }
5781 n_extra = 0;
5782 n_attr = 0;
5783 }
5784
5785
5786# ifdef FEAT_MBYTE
5787 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5788 {
5789 /* Need to fill two screen columns. */
5790# ifdef FEAT_RIGHTLEFT
5791 if (wp->w_p_rl)
5792 {
5793 --boguscols;
5794 --col;
5795 }
5796 else
5797# endif
5798 {
5799 ++boguscols;
5800 ++col;
5801 }
5802 }
5803# endif
5804
5805# ifdef FEAT_RIGHTLEFT
5806 if (wp->w_p_rl)
5807 {
5808 --boguscols;
5809 --col;
5810 }
5811 else
5812# endif
5813 {
5814 ++boguscols;
5815 ++col;
5816 }
5817 }
5818 else
5819 {
5820 if (n_extra > 0)
5821 {
5822 vcol += n_extra;
5823 n_extra = 0;
5824 n_attr = 0;
5825 }
5826 }
5827
5828 }
5829#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 else
5831 --n_skip;
5832
Bram Moolenaar64486672010-05-16 15:46:46 +02005833 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5834 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005835 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836#ifdef FEAT_DIFF
5837 && filler_todo <= 0
5838#endif
5839 )
5840 ++vcol;
5841
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005842#ifdef FEAT_SYN_HL
5843 if (vcol_save_attr >= 0)
5844 char_attr = vcol_save_attr;
5845#endif
5846
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 /* restore attributes after "predeces" in 'listchars' */
5848 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5849 char_attr = saved_attr3;
5850
5851 /* restore attributes after last 'listchars' or 'number' char */
5852 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5853 char_attr = saved_attr2;
5854
5855 /*
5856 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005857 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 */
5859 if ((
5860#ifdef FEAT_RIGHTLEFT
5861 wp->w_p_rl ? (col < 0) :
5862#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005863 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 && (*ptr != NUL
5865#ifdef FEAT_DIFF
5866 || filler_todo > 0
5867#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005868 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5870 )
5871 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005872#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005873 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005874 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005875 boguscols = 0;
5876#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005877 screen_line(screen_row, wp->w_wincol, col,
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#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 ++row;
5881 ++screen_row;
5882
5883 /* When not wrapping and finished diff lines, or when displayed
5884 * '$' and highlighting until last column, break here. */
5885 if ((!wp->w_p_wrap
5886#ifdef FEAT_DIFF
5887 && filler_todo <= 0
5888#endif
5889 ) || lcs_eol_one == -1)
5890 break;
5891
5892 /* When the window is too narrow draw all "@" lines. */
5893 if (draw_state != WL_LINE
5894#ifdef FEAT_DIFF
5895 && filler_todo <= 0
5896#endif
5897 )
5898 {
5899 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 row = endrow;
5902 }
5903
5904 /* When line got too long for screen break here. */
5905 if (row == endrow)
5906 {
5907 ++row;
5908 break;
5909 }
5910
5911 if (screen_cur_row == screen_row - 1
5912#ifdef FEAT_DIFF
5913 && filler_todo <= 0
5914#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005915 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 {
5917 /* Remember that the line wraps, used for modeless copy. */
5918 LineWraps[screen_row - 1] = TRUE;
5919
5920 /*
5921 * Special trick to make copy/paste of wrapped lines work with
5922 * xterm/screen: write an extra character beyond the end of
5923 * the line. This will work with all terminal types
5924 * (regardless of the xn,am settings).
5925 * Only do this on a fast tty.
5926 * Only do this if the cursor is on the current line
5927 * (something has been written in it).
5928 * Don't do this for the GUI.
5929 * Don't do this for double-width characters.
5930 * Don't do this for a window not at the right screen border.
5931 */
5932 if (p_tf
5933#ifdef FEAT_GUI
5934 && !gui.in_use
5935#endif
5936#ifdef FEAT_MBYTE
5937 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005938 && ((*mb_off2cells)(LineOffset[screen_row],
5939 LineOffset[screen_row] + screen_Columns)
5940 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005942 + (int)Columns - 2,
5943 LineOffset[screen_row] + screen_Columns)
5944 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945#endif
5946 )
5947 {
5948 /* First make sure we are at the end of the screen line,
5949 * then output the same character again to let the
5950 * terminal know about the wrap. If the terminal doesn't
5951 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005952 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 screen_char(LineOffset[screen_row - 1]
5954 + (unsigned)Columns - 1,
5955 screen_row - 1, (int)(Columns - 1));
5956
5957#ifdef FEAT_MBYTE
5958 /* When there is a multi-byte character, just output a
5959 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005960 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5961 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 out_char(' ');
5963 else
5964#endif
5965 out_char(ScreenLines[LineOffset[screen_row - 1]
5966 + (Columns - 1)]);
5967 /* force a redraw of the first char on the next line */
5968 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5969 screen_start(); /* don't know where cursor is now */
5970 }
5971 }
5972
5973 col = 0;
5974 off = (unsigned)(current_ScreenLine - ScreenLines);
5975#ifdef FEAT_RIGHTLEFT
5976 if (wp->w_p_rl)
5977 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005978 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 off += col;
5980 }
5981#endif
5982
5983 /* reset the drawing state for the start of a wrapped line */
5984 draw_state = WL_START;
5985 saved_n_extra = n_extra;
5986 saved_p_extra = p_extra;
5987 saved_c_extra = c_extra;
5988 saved_char_attr = char_attr;
5989 n_extra = 0;
5990 lcs_prec_todo = lcs_prec;
5991#ifdef FEAT_LINEBREAK
5992# ifdef FEAT_DIFF
5993 if (filler_todo <= 0)
5994# endif
5995 need_showbreak = TRUE;
5996#endif
5997#ifdef FEAT_DIFF
5998 --filler_todo;
5999 /* When the filler lines are actually below the last line of the
6000 * file, don't draw the line itself, break here. */
6001 if (filler_todo == 0 && wp->w_botfill)
6002 break;
6003#endif
6004 }
6005
6006 } /* for every character in the line */
6007
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006008#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006009 /* After an empty line check first word for capital. */
6010 if (*skipwhite(line) == NUL)
6011 {
6012 capcol_lnum = lnum + 1;
6013 cap_col = 0;
6014 }
6015#endif
6016
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006017 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 return row;
6019}
6020
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006021#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006022static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006023
6024/*
6025 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006026 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006027 */
6028 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006029comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006030{
6031 int i;
6032
6033 for (i = 0; i < Screen_mco; ++i)
6034 {
6035 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6036 return TRUE;
6037 if (ScreenLinesC[i][off_from] == 0)
6038 break;
6039 }
6040 return FALSE;
6041}
6042#endif
6043
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044/*
6045 * Check whether the given character needs redrawing:
6046 * - the (first byte of the) character is different
6047 * - the attributes are different
6048 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006049 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 */
6051 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006052char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 if (cols > 0
6055 && ((ScreenLines[off_from] != ScreenLines[off_to]
6056 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6057
6058#ifdef FEAT_MBYTE
6059 || (enc_dbcs != 0
6060 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6061 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6062 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6063 : (cols > 1 && ScreenLines[off_from + 1]
6064 != ScreenLines[off_to + 1])))
6065 || (enc_utf8
6066 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6067 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006068 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006069 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6070 && ScreenLines[off_from + 1]
6071 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072#endif
6073 ))
6074 return TRUE;
6075 return FALSE;
6076}
6077
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006078#if defined(FEAT_TERMINAL) || defined(PROTO)
6079/*
6080 * Return the index in ScreenLines[] for the current screen line.
6081 */
6082 int
6083screen_get_current_line_off()
6084{
6085 return (int)(current_ScreenLine - ScreenLines);
6086}
6087#endif
6088
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089/*
6090 * Move one "cooked" screen line to the screen, but only the characters that
6091 * have actually changed. Handle insert/delete character.
6092 * "coloff" gives the first column on the screen for this line.
6093 * "endcol" gives the columns where valid characters are.
6094 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6095 * needs to be cleared, negative otherwise.
6096 * "rlflag" is TRUE in a rightleft window:
6097 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6098 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6099 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006100 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006101screen_line(
6102 int row,
6103 int coloff,
6104 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006105 int clear_width,
6106 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107{
6108 unsigned off_from;
6109 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006110#ifdef FEAT_MBYTE
6111 unsigned max_off_from;
6112 unsigned max_off_to;
6113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 int force = FALSE; /* force update rest of the line */
6117 int redraw_this /* bool: does character need redraw? */
6118#ifdef FEAT_GUI
6119 = TRUE /* For GUI when while-loop empty */
6120#endif
6121 ;
6122 int redraw_next; /* redraw_this for next character */
6123#ifdef FEAT_MBYTE
6124 int clear_next = FALSE;
6125 int char_cells; /* 1: normal char */
6126 /* 2: occupies two display cells */
6127# define CHAR_CELLS char_cells
6128#else
6129# define CHAR_CELLS 1
6130#endif
6131
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006132 /* Check for illegal row and col, just in case. */
6133 if (row >= Rows)
6134 row = Rows - 1;
6135 if (endcol > Columns)
6136 endcol = Columns;
6137
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138# ifdef FEAT_CLIPBOARD
6139 clip_may_clear_selection(row, row);
6140# endif
6141
6142 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6143 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006144#ifdef FEAT_MBYTE
6145 max_off_from = off_from + screen_Columns;
6146 max_off_to = LineOffset[row] + screen_Columns;
6147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148
6149#ifdef FEAT_RIGHTLEFT
6150 if (rlflag)
6151 {
6152 /* Clear rest first, because it's left of the text. */
6153 if (clear_width > 0)
6154 {
6155 while (col <= endcol && ScreenLines[off_to] == ' '
6156 && ScreenAttrs[off_to] == 0
6157# ifdef FEAT_MBYTE
6158 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6159# endif
6160 )
6161 {
6162 ++off_to;
6163 ++col;
6164 }
6165 if (col <= endcol)
6166 screen_fill(row, row + 1, col + coloff,
6167 endcol + coloff + 1, ' ', ' ', 0);
6168 }
6169 col = endcol + 1;
6170 off_to = LineOffset[row] + col + coloff;
6171 off_from += col;
6172 endcol = (clear_width > 0 ? clear_width : -clear_width);
6173 }
6174#endif /* FEAT_RIGHTLEFT */
6175
6176 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6177
6178 while (col < endcol)
6179 {
6180#ifdef FEAT_MBYTE
6181 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006182 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 else
6184 char_cells = 1;
6185#endif
6186
6187 redraw_this = redraw_next;
6188 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6189 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6190
6191#ifdef FEAT_GUI
6192 /* If the next character was bold, then redraw the current character to
6193 * remove any pixels that might have spilt over into us. This only
6194 * happens in the GUI.
6195 */
6196 if (redraw_next && gui.in_use)
6197 {
6198 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006199 if (hl > HL_ALL)
6200 hl = syn_attr2attr(hl);
6201 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 redraw_this = TRUE;
6203 }
6204#endif
6205
6206 if (redraw_this)
6207 {
6208 /*
6209 * Special handling when 'xs' termcap flag set (hpterm):
6210 * Attributes for characters are stored at the position where the
6211 * cursor is when writing the highlighting code. The
6212 * start-highlighting code must be written with the cursor on the
6213 * first highlighted character. The stop-highlighting code must
6214 * be written with the cursor just after the last highlighted
6215 * character.
6216 * Overwriting a character doesn't remove it's highlighting. Need
6217 * to clear the rest of the line, and force redrawing it
6218 * completely.
6219 */
6220 if ( p_wiv
6221 && !force
6222#ifdef FEAT_GUI
6223 && !gui.in_use
6224#endif
6225 && ScreenAttrs[off_to] != 0
6226 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6227 {
6228 /*
6229 * Need to remove highlighting attributes here.
6230 */
6231 windgoto(row, col + coloff);
6232 out_str(T_CE); /* clear rest of this screen line */
6233 screen_start(); /* don't know where cursor is now */
6234 force = TRUE; /* force redraw of rest of the line */
6235 redraw_next = TRUE; /* or else next char would miss out */
6236
6237 /*
6238 * If the previous character was highlighted, need to stop
6239 * highlighting at this character.
6240 */
6241 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6242 {
6243 screen_attr = ScreenAttrs[off_to - 1];
6244 term_windgoto(row, col + coloff);
6245 screen_stop_highlight();
6246 }
6247 else
6248 screen_attr = 0; /* highlighting has stopped */
6249 }
6250#ifdef FEAT_MBYTE
6251 if (enc_dbcs != 0)
6252 {
6253 /* Check if overwriting a double-byte with a single-byte or
6254 * the other way around requires another character to be
6255 * redrawn. For UTF-8 this isn't needed, because comparing
6256 * ScreenLinesUC[] is sufficient. */
6257 if (char_cells == 1
6258 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006259 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
6261 /* Writing a single-cell character over a double-cell
6262 * character: need to redraw the next cell. */
6263 ScreenLines[off_to + 1] = 0;
6264 redraw_next = TRUE;
6265 }
6266 else if (char_cells == 2
6267 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006268 && (*mb_off2cells)(off_to, max_off_to) == 1
6269 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
6271 /* Writing the second half of a double-cell character over
6272 * a double-cell character: need to redraw the second
6273 * cell. */
6274 ScreenLines[off_to + 2] = 0;
6275 redraw_next = TRUE;
6276 }
6277
6278 if (enc_dbcs == DBCS_JPNU)
6279 ScreenLines2[off_to] = ScreenLines2[off_from];
6280 }
6281 /* When writing a single-width character over a double-width
6282 * character and at the end of the redrawn text, need to clear out
6283 * the right halve of the old character.
6284 * Also required when writing the right halve of a double-width
6285 * char over the left halve of an existing one. */
6286 if (has_mbyte && col + char_cells == endcol
6287 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006288 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006290 && (*mb_off2cells)(off_to, max_off_to) == 1
6291 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 clear_next = TRUE;
6293#endif
6294
6295 ScreenLines[off_to] = ScreenLines[off_from];
6296#ifdef FEAT_MBYTE
6297 if (enc_utf8)
6298 {
6299 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6300 if (ScreenLinesUC[off_from] != 0)
6301 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006302 int i;
6303
6304 for (i = 0; i < Screen_mco; ++i)
6305 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 }
6307 }
6308 if (char_cells == 2)
6309 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6310#endif
6311
6312#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006313 /* The bold trick makes a single column of pixels appear in the
6314 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 * character should be redrawn too. This happens for our own GUI
6316 * and for some xterms. */
6317 if (
6318# ifdef FEAT_GUI
6319 gui.in_use
6320# endif
6321# if defined(FEAT_GUI) && defined(UNIX)
6322 ||
6323# endif
6324# ifdef UNIX
6325 term_is_xterm
6326# endif
6327 )
6328 {
6329 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006330 if (hl > HL_ALL)
6331 hl = syn_attr2attr(hl);
6332 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 redraw_next = TRUE;
6334 }
6335#endif
6336 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6337#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006338 /* For simplicity set the attributes of second half of a
6339 * double-wide character equal to the first half. */
6340 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006342
6343 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 else
6346#endif
6347 screen_char(off_to, row, col + coloff);
6348 }
6349 else if ( p_wiv
6350#ifdef FEAT_GUI
6351 && !gui.in_use
6352#endif
6353 && col + coloff > 0)
6354 {
6355 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6356 {
6357 /*
6358 * Don't output stop-highlight when moving the cursor, it will
6359 * stop the highlighting when it should continue.
6360 */
6361 screen_attr = 0;
6362 }
6363 else if (screen_attr != 0)
6364 screen_stop_highlight();
6365 }
6366
6367 off_to += CHAR_CELLS;
6368 off_from += CHAR_CELLS;
6369 col += CHAR_CELLS;
6370 }
6371
6372#ifdef FEAT_MBYTE
6373 if (clear_next)
6374 {
6375 /* Clear the second half of a double-wide character of which the left
6376 * half was overwritten with a single-wide character. */
6377 ScreenLines[off_to] = ' ';
6378 if (enc_utf8)
6379 ScreenLinesUC[off_to] = 0;
6380 screen_char(off_to, row, col + coloff);
6381 }
6382#endif
6383
6384 if (clear_width > 0
6385#ifdef FEAT_RIGHTLEFT
6386 && !rlflag
6387#endif
6388 )
6389 {
6390#ifdef FEAT_GUI
6391 int startCol = col;
6392#endif
6393
6394 /* blank out the rest of the line */
6395 while (col < clear_width && ScreenLines[off_to] == ' '
6396 && ScreenAttrs[off_to] == 0
6397#ifdef FEAT_MBYTE
6398 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6399#endif
6400 )
6401 {
6402 ++off_to;
6403 ++col;
6404 }
6405 if (col < clear_width)
6406 {
6407#ifdef FEAT_GUI
6408 /*
6409 * In the GUI, clearing the rest of the line may leave pixels
6410 * behind if the first character cleared was bold. Some bold
6411 * fonts spill over the left. In this case we redraw the previous
6412 * character too. If we didn't skip any blanks above, then we
6413 * only redraw if the character wasn't already redrawn anyway.
6414 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006415 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 {
6417 hl = ScreenAttrs[off_to];
6418 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006419 {
6420 int prev_cells = 1;
6421# ifdef FEAT_MBYTE
6422 if (enc_utf8)
6423 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6424 * that its width is 2. */
6425 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6426 else if (enc_dbcs != 0)
6427 {
6428 /* find previous character by counting from first
6429 * column and get its width. */
6430 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006431 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006432
6433 while (off < off_to)
6434 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006435 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006436 off += prev_cells;
6437 }
6438 }
6439
6440 if (enc_dbcs != 0 && prev_cells > 1)
6441 screen_char_2(off_to - prev_cells, row,
6442 col + coloff - prev_cells);
6443 else
6444# endif
6445 screen_char(off_to - prev_cells, row,
6446 col + coloff - prev_cells);
6447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 }
6449#endif
6450 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6451 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 off_to += clear_width - col;
6453 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 }
6455 }
6456
6457 if (clear_width > 0)
6458 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 /* For a window that's left of another, draw the separator char. */
6460 if (col + coloff < Columns)
6461 {
6462 int c;
6463
6464 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006465 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006466#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006467 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6468 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 || ScreenAttrs[off_to] != hl)
6471 {
6472 ScreenLines[off_to] = c;
6473 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006474#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 if (enc_utf8)
6476 {
6477 if (c >= 0x80)
6478 {
6479 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006480 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 }
6482 else
6483 ScreenLinesUC[off_to] = 0;
6484 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 screen_char(off_to, row, col + coloff);
6487 }
6488 }
6489 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 LineWraps[row] = FALSE;
6491 }
6492}
6493
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006494#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006496 * Mirror text "str" for right-left displaying.
6497 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006499 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006500rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501{
6502 char_u *p1, *p2;
6503 int t;
6504
6505 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6506 {
6507 t = *p1;
6508 *p1 = *p2;
6509 *p2 = t;
6510 }
6511}
6512#endif
6513
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514/*
6515 * mark all status lines for redraw; used after first :cd
6516 */
6517 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006518status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
6520 win_T *wp;
6521
Bram Moolenaar29323592016-07-24 22:04:11 +02006522 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 if (wp->w_status_height)
6524 {
6525 wp->w_redr_status = TRUE;
6526 redraw_later(VALID);
6527 }
6528}
6529
6530/*
6531 * mark all status lines of the current buffer for redraw
6532 */
6533 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006534status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535{
6536 win_T *wp;
6537
Bram Moolenaar29323592016-07-24 22:04:11 +02006538 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6540 {
6541 wp->w_redr_status = TRUE;
6542 redraw_later(VALID);
6543 }
6544}
6545
6546/*
6547 * Redraw all status lines that need to be redrawn.
6548 */
6549 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006550redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551{
6552 win_T *wp;
6553
Bram Moolenaar29323592016-07-24 22:04:11 +02006554 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006556 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006557 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006558 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
Bram Moolenaar4033c552017-09-16 20:54:51 +02006561#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562/*
6563 * Redraw all status lines at the bottom of frame "frp".
6564 */
6565 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006566win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567{
6568 if (frp->fr_layout == FR_LEAF)
6569 frp->fr_win->w_redr_status = TRUE;
6570 else if (frp->fr_layout == FR_ROW)
6571 {
6572 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6573 win_redraw_last_status(frp);
6574 }
6575 else /* frp->fr_layout == FR_COL */
6576 {
6577 frp = frp->fr_child;
6578 while (frp->fr_next != NULL)
6579 frp = frp->fr_next;
6580 win_redraw_last_status(frp);
6581 }
6582}
6583#endif
6584
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585/*
6586 * Draw the verticap separator right of window "wp" starting with line "row".
6587 */
6588 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006589draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590{
6591 int hl;
6592 int c;
6593
6594 if (wp->w_vsep_width)
6595 {
6596 /* draw the vertical separator right of this window */
6597 c = fillchar_vsep(&hl);
6598 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6599 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6600 c, ' ', hl);
6601 }
6602}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
6604#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006605static int status_match_len(expand_T *xp, char_u *s);
6606static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607
6608/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006609 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 */
6611 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006612status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
6614 int len = 0;
6615
6616#ifdef FEAT_MENU
6617 int emenu = (xp->xp_context == EXPAND_MENUS
6618 || xp->xp_context == EXPAND_MENUNAMES);
6619
6620 /* Check for menu separators - replace with '|'. */
6621 if (emenu && menu_is_separator(s))
6622 return 1;
6623#endif
6624
6625 while (*s != NUL)
6626 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006627 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006628 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006629 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 }
6631
6632 return len;
6633}
6634
6635/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006636 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006637 * These are backslashes used for escaping. Do show backslashes in help tags.
6638 */
6639 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006640skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006641{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006642 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006643#ifdef FEAT_MENU
6644 || ((xp->xp_context == EXPAND_MENUS
6645 || xp->xp_context == EXPAND_MENUNAMES)
6646 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6647#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006648 )
6649 {
6650#ifndef BACKSLASH_IN_FILENAME
6651 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6652 return 2;
6653#endif
6654 return 1;
6655 }
6656 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006657}
6658
6659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 * Show wildchar matches in the status line.
6661 * Show at least the "match" item.
6662 * We start at item 'first_match' in the list and show all matches that fit.
6663 *
6664 * If inversion is possible we use it. Else '=' characters are used.
6665 */
6666 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006667win_redr_status_matches(
6668 expand_T *xp,
6669 int num_matches,
6670 char_u **matches, /* list of matches */
6671 int match,
6672 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673{
6674#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6675 int row;
6676 char_u *buf;
6677 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006678 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 int fillchar;
6680 int attr;
6681 int i;
6682 int highlight = TRUE;
6683 char_u *selstart = NULL;
6684 int selstart_col = 0;
6685 char_u *selend = NULL;
6686 static int first_match = 0;
6687 int add_left = FALSE;
6688 char_u *s;
6689#ifdef FEAT_MENU
6690 int emenu;
6691#endif
6692#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6693 int l;
6694#endif
6695
6696 if (matches == NULL) /* interrupted completion? */
6697 return;
6698
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006699#ifdef FEAT_MBYTE
6700 if (has_mbyte)
6701 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6702 else
6703#endif
6704 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 if (buf == NULL)
6706 return;
6707
6708 if (match == -1) /* don't show match but original text */
6709 {
6710 match = 0;
6711 highlight = FALSE;
6712 }
6713 /* count 1 for the ending ">" */
6714 clen = status_match_len(xp, L_MATCH(match)) + 3;
6715 if (match == 0)
6716 first_match = 0;
6717 else if (match < first_match)
6718 {
6719 /* jumping left, as far as we can go */
6720 first_match = match;
6721 add_left = TRUE;
6722 }
6723 else
6724 {
6725 /* check if match fits on the screen */
6726 for (i = first_match; i < match; ++i)
6727 clen += status_match_len(xp, L_MATCH(i)) + 2;
6728 if (first_match > 0)
6729 clen += 2;
6730 /* jumping right, put match at the left */
6731 if ((long)clen > Columns)
6732 {
6733 first_match = match;
6734 /* if showing the last match, we can add some on the left */
6735 clen = 2;
6736 for (i = match; i < num_matches; ++i)
6737 {
6738 clen += status_match_len(xp, L_MATCH(i)) + 2;
6739 if ((long)clen >= Columns)
6740 break;
6741 }
6742 if (i == num_matches)
6743 add_left = TRUE;
6744 }
6745 }
6746 if (add_left)
6747 while (first_match > 0)
6748 {
6749 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6750 if ((long)clen >= Columns)
6751 break;
6752 --first_match;
6753 }
6754
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006755 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756
6757 if (first_match == 0)
6758 {
6759 *buf = NUL;
6760 len = 0;
6761 }
6762 else
6763 {
6764 STRCPY(buf, "< ");
6765 len = 2;
6766 }
6767 clen = len;
6768
6769 i = first_match;
6770 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6771 {
6772 if (i == match)
6773 {
6774 selstart = buf + len;
6775 selstart_col = clen;
6776 }
6777
6778 s = L_MATCH(i);
6779 /* Check for menu separators - replace with '|' */
6780#ifdef FEAT_MENU
6781 emenu = (xp->xp_context == EXPAND_MENUS
6782 || xp->xp_context == EXPAND_MENUNAMES);
6783 if (emenu && menu_is_separator(s))
6784 {
6785 STRCPY(buf + len, transchar('|'));
6786 l = (int)STRLEN(buf + len);
6787 len += l;
6788 clen += l;
6789 }
6790 else
6791#endif
6792 for ( ; *s != NUL; ++s)
6793 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006794 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 clen += ptr2cells(s);
6796#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006797 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 {
6799 STRNCPY(buf + len, s, l);
6800 s += l - 1;
6801 len += l;
6802 }
6803 else
6804#endif
6805 {
6806 STRCPY(buf + len, transchar_byte(*s));
6807 len += (int)STRLEN(buf + len);
6808 }
6809 }
6810 if (i == match)
6811 selend = buf + len;
6812
6813 *(buf + len++) = ' ';
6814 *(buf + len++) = ' ';
6815 clen += 2;
6816 if (++i == num_matches)
6817 break;
6818 }
6819
6820 if (i != num_matches)
6821 {
6822 *(buf + len++) = '>';
6823 ++clen;
6824 }
6825
6826 buf[len] = NUL;
6827
6828 row = cmdline_row - 1;
6829 if (row >= 0)
6830 {
6831 if (wild_menu_showing == 0)
6832 {
6833 if (msg_scrolled > 0)
6834 {
6835 /* Put the wildmenu just above the command line. If there is
6836 * no room, scroll the screen one line up. */
6837 if (cmdline_row == Rows - 1)
6838 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006839 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 ++msg_scrolled;
6841 }
6842 else
6843 {
6844 ++cmdline_row;
6845 ++row;
6846 }
6847 wild_menu_showing = WM_SCROLLED;
6848 }
6849 else
6850 {
6851 /* Create status line if needed by setting 'laststatus' to 2.
6852 * Set 'winminheight' to zero to avoid that the window is
6853 * resized. */
6854 if (lastwin->w_status_height == 0)
6855 {
6856 save_p_ls = p_ls;
6857 save_p_wmh = p_wmh;
6858 p_ls = 2;
6859 p_wmh = 0;
6860 last_status(FALSE);
6861 }
6862 wild_menu_showing = WM_SHOWN;
6863 }
6864 }
6865
6866 screen_puts(buf, row, 0, attr);
6867 if (selstart != NULL && highlight)
6868 {
6869 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006870 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 }
6872
6873 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6874 }
6875
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 vim_free(buf);
6878}
6879#endif
6880
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881/*
6882 * Redraw the status line of window wp.
6883 *
6884 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006885 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6886 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006888 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006889win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
6891 int row;
6892 char_u *p;
6893 int len;
6894 int fillchar;
6895 int attr;
6896 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006897 static int busy = FALSE;
6898
6899 /* It's possible to get here recursively when 'statusline' (indirectly)
6900 * invokes ":redrawstatus". Simply ignore the call then. */
6901 if (busy)
6902 return;
6903 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904
6905 wp->w_redr_status = FALSE;
6906 if (wp->w_status_height == 0)
6907 {
6908 /* no status line, can only be last window */
6909 redraw_cmdline = TRUE;
6910 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006911 else if (!redrawing()
6912#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006913 // don't update status line when popup menu is visible and may be
6914 // drawn over it, unless it will be redrawn later
6915 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006916#endif
6917 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {
6919 /* Don't redraw right now, do it later. */
6920 wp->w_redr_status = TRUE;
6921 }
6922#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006923 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {
6925 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006926 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 }
6928#endif
6929 else
6930 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006931 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006933 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 p = NameBuff;
6935 len = (int)STRLEN(p);
6936
Bram Moolenaard85f2712017-07-28 21:51:57 +02006937 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938#ifdef FEAT_QUICKFIX
6939 || wp->w_p_pvw
6940#endif
6941 || bufIsChanged(wp->w_buffer)
6942 || wp->w_buffer->b_p_ro)
6943 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006944 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006946 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 len += (int)STRLEN(p + len);
6948 }
6949#ifdef FEAT_QUICKFIX
6950 if (wp->w_p_pvw)
6951 {
6952 STRCPY(p + len, _("[Preview]"));
6953 len += (int)STRLEN(p + len);
6954 }
6955#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006956 if (bufIsChanged(wp->w_buffer)
6957#ifdef FEAT_TERMINAL
6958 && !bt_terminal(wp->w_buffer)
6959#endif
6960 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 {
6962 STRCPY(p + len, "[+]");
6963 len += 3;
6964 }
6965 if (wp->w_buffer->b_p_ro)
6966 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006967 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006968 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 }
6970
Bram Moolenaar02631462017-09-22 15:20:32 +02006971 this_ru_col = ru_col - (Columns - wp->w_width);
6972 if (this_ru_col < (wp->w_width + 1) / 2)
6973 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 if (this_ru_col <= 1)
6975 {
6976 p = (char_u *)"<"; /* No room for file name! */
6977 len = 1;
6978 }
6979 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980#ifdef FEAT_MBYTE
6981 if (has_mbyte)
6982 {
6983 int clen = 0, i;
6984
6985 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006986 clen = mb_string2cells(p, -1);
6987
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 /* Find first character that will fit.
6989 * Going from start to end is much faster for DBCS. */
6990 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006991 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 clen -= (*mb_ptr2cells)(p + i);
6993 len = clen;
6994 if (i > 0)
6995 {
6996 p = p + i - 1;
6997 *p = '<';
6998 ++len;
6999 }
7000
7001 }
7002 else
7003#endif
7004 if (len > this_ru_col - 1)
7005 {
7006 p += len - (this_ru_col - 1);
7007 *p = '<';
7008 len = this_ru_col - 1;
7009 }
7010
7011 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007012 screen_puts(p, row, wp->w_wincol, attr);
7013 screen_fill(row, row + 1, len + wp->w_wincol,
7014 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007016 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7018 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007019 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020
7021#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007022 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023#endif
7024 }
7025
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 /*
7027 * May need to draw the character below the vertical separator.
7028 */
7029 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7030 {
7031 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007032 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 else
7034 fillchar = fillchar_vsep(&attr);
7035 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7036 attr);
7037 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007038 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039}
7040
Bram Moolenaar238a5642006-02-21 22:12:05 +00007041#ifdef FEAT_STL_OPT
7042/*
7043 * Redraw the status line according to 'statusline' and take care of any
7044 * errors encountered.
7045 */
7046 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007047redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007048{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007049 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007050 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007051
7052 /* When called recursively return. This can happen when the statusline
7053 * contains an expression that triggers a redraw. */
7054 if (entered)
7055 return;
7056 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007057
Bram Moolenaara742e082016-04-05 21:10:38 +02007058 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007059 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007060 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007061 {
7062 /* When there is an error disable the statusline, otherwise the
7063 * display is messed up with errors and a redraw triggers the problem
7064 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007065 set_string_option_direct((char_u *)"statusline", -1,
7066 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007067 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007068 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007069 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007070 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007071}
7072#endif
7073
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074/*
7075 * Return TRUE if the status line of window "wp" is connected to the status
7076 * line of the window right of it. If not, then it's a vertical separator.
7077 * Only call if (wp->w_vsep_width != 0).
7078 */
7079 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007080stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081{
7082 frame_T *fr;
7083
7084 fr = wp->w_frame;
7085 while (fr->fr_parent != NULL)
7086 {
7087 if (fr->fr_parent->fr_layout == FR_COL)
7088 {
7089 if (fr->fr_next != NULL)
7090 break;
7091 }
7092 else
7093 {
7094 if (fr->fr_next != NULL)
7095 return TRUE;
7096 }
7097 fr = fr->fr_parent;
7098 }
7099 return FALSE;
7100}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103/*
7104 * Get the value to show for the language mappings, active 'keymap'.
7105 */
7106 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007107get_keymap_str(
7108 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007109 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007110 char_u *buf, /* buffer for the result */
7111 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112{
7113 char_u *p;
7114
7115 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7116 return FALSE;
7117
7118 {
7119#ifdef FEAT_EVAL
7120 buf_T *old_curbuf = curbuf;
7121 win_T *old_curwin = curwin;
7122 char_u *s;
7123
7124 curbuf = wp->w_buffer;
7125 curwin = wp;
7126 STRCPY(buf, "b:keymap_name"); /* must be writable */
7127 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007128 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 --emsg_skip;
7130 curbuf = old_curbuf;
7131 curwin = old_curwin;
7132 if (p == NULL || *p == NUL)
7133#endif
7134 {
7135#ifdef FEAT_KEYMAP
7136 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7137 p = wp->w_buffer->b_p_keymap;
7138 else
7139#endif
7140 p = (char_u *)"lang";
7141 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007142 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 buf[0] = NUL;
7144#ifdef FEAT_EVAL
7145 vim_free(s);
7146#endif
7147 }
7148 return buf[0] != NUL;
7149}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150
7151#if defined(FEAT_STL_OPT) || defined(PROTO)
7152/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007153 * Redraw the status line or ruler of window "wp".
7154 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 */
7156 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007157win_redr_custom(
7158 win_T *wp,
7159 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007161 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 int attr;
7163 int curattr;
7164 int row;
7165 int col = 0;
7166 int maxwidth;
7167 int width;
7168 int n;
7169 int len;
7170 int fillchar;
7171 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007172 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007174 struct stl_hlrec hltab[STL_MAX_ITEM];
7175 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007176 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007177 win_T *ewp;
7178 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
Bram Moolenaar1d633412013-12-11 15:52:01 +01007180 /* There is a tiny chance that this gets called recursively: When
7181 * redrawing a status line triggers redrawing the ruler or tabline.
7182 * Avoid trouble by not allowing recursion. */
7183 if (entered)
7184 return;
7185 entered = TRUE;
7186
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007188 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007190 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007191 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007192 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007193 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007194 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007195 maxwidth = Columns;
7196# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007197 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007200 else
7201 {
7202 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007203 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007204 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007205
7206 if (draw_ruler)
7207 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007208 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007209 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007210 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007211 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007212 if (*++stl == '-')
7213 stl++;
7214 if (atoi((char *)stl))
7215 while (VIM_ISDIGIT(*stl))
7216 stl++;
7217 if (*stl++ != '(')
7218 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007219 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007220 col = ru_col - (Columns - wp->w_width);
7221 if (col < (wp->w_width + 1) / 2)
7222 col = (wp->w_width + 1) / 2;
7223 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007224 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007225 {
7226 row = Rows - 1;
7227 --maxwidth; /* writing in last column may cause scrolling */
7228 fillchar = ' ';
7229 attr = 0;
7230 }
7231
7232# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007233 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007234# endif
7235 }
7236 else
7237 {
7238 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007239 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007240 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007241 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007242# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007243 use_sandbox = was_set_insecurely((char_u *)"statusline",
7244 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007245# endif
7246 }
7247
Bram Moolenaar53f81742017-09-22 14:35:51 +02007248 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007249 }
7250
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007252 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253
Bram Moolenaar61452852011-02-01 18:01:11 +01007254 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7255 * the cursor away and back. */
7256 ewp = wp == NULL ? curwin : wp;
7257 p_crb_save = ewp->w_p_crb;
7258 ewp->w_p_crb = FALSE;
7259
Bram Moolenaar362f3562009-11-03 16:20:34 +00007260 /* Make a copy, because the statusline may include a function call that
7261 * might change the option value and free the memory. */
7262 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007263 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007264 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007265 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007266 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007267 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007269 /* Make all characters printable. */
7270 p = transstr(buf);
7271 if (p != NULL)
7272 {
7273 vim_strncpy(buf, p, sizeof(buf) - 1);
7274 vim_free(p);
7275 }
7276
7277 /* fill up with "fillchar" */
7278 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007279 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 {
7281#ifdef FEAT_MBYTE
7282 len += (*mb_char2bytes)(fillchar, buf + len);
7283#else
7284 buf[len++] = fillchar;
7285#endif
7286 ++width;
7287 }
7288 buf[len] = NUL;
7289
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007290 /*
7291 * Draw each snippet with the specified highlighting.
7292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 curattr = attr;
7294 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007295 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007297 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 screen_puts_len(p, len, row, col, curattr);
7299 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007300 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007302 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007304 else if (hltab[n].userhl < 0)
7305 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007306#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007307 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7308 && wp->w_status_height != 0)
7309 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007310 else if (wp != NULL && bt_terminal(wp->w_buffer)
7311 && wp->w_status_height != 0)
7312 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007313#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007314 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007315 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007317 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 }
7319 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007320
7321 if (wp == NULL)
7322 {
7323 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7324 col = 0;
7325 len = 0;
7326 p = buf;
7327 fillchar = 0;
7328 for (n = 0; tabtab[n].start != NULL; n++)
7329 {
7330 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7331 while (col < len)
7332 TabPageIdxs[col++] = fillchar;
7333 p = tabtab[n].start;
7334 fillchar = tabtab[n].userhl;
7335 }
7336 while (col < Columns)
7337 TabPageIdxs[col++] = fillchar;
7338 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007339
7340theend:
7341 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342}
7343
7344#endif /* FEAT_STL_OPT */
7345
7346/*
7347 * Output a single character directly to the screen and update ScreenLines.
7348 */
7349 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007350screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 char_u buf[MB_MAXBYTES + 1];
7353
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007354#ifdef FEAT_MBYTE
7355 if (has_mbyte)
7356 buf[(*mb_char2bytes)(c, buf)] = NUL;
7357 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007359 {
7360 buf[0] = c;
7361 buf[1] = NUL;
7362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 screen_puts(buf, row, col, attr);
7364}
7365
7366/*
7367 * Get a single character directly from ScreenLines into "bytes[]".
7368 * Also return its attribute in *attrp;
7369 */
7370 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007371screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372{
7373 unsigned off;
7374
7375 /* safety check */
7376 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7377 {
7378 off = LineOffset[row] + col;
7379 *attrp = ScreenAttrs[off];
7380 bytes[0] = ScreenLines[off];
7381 bytes[1] = NUL;
7382
7383#ifdef FEAT_MBYTE
7384 if (enc_utf8 && ScreenLinesUC[off] != 0)
7385 bytes[utfc_char2bytes(off, bytes)] = NUL;
7386 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7387 {
7388 bytes[0] = ScreenLines[off];
7389 bytes[1] = ScreenLines2[off];
7390 bytes[2] = NUL;
7391 }
7392 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7393 {
7394 bytes[1] = ScreenLines[off + 1];
7395 bytes[2] = NUL;
7396 }
7397#endif
7398 }
7399}
7400
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007401#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007402static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007403
7404/*
7405 * Return TRUE if composing characters for screen posn "off" differs from
7406 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007407 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007408 */
7409 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007410screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007411{
7412 int i;
7413
7414 for (i = 0; i < Screen_mco; ++i)
7415 {
7416 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7417 return TRUE;
7418 if (u8cc[i] == 0)
7419 break;
7420 }
7421 return FALSE;
7422}
7423#endif
7424
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425/*
7426 * Put string '*text' on the screen at position 'row' and 'col', with
7427 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7428 * Note: only outputs within one row, message is truncated at screen boundary!
7429 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7430 */
7431 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007432screen_puts(
7433 char_u *text,
7434 int row,
7435 int col,
7436 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437{
7438 screen_puts_len(text, -1, row, col, attr);
7439}
7440
7441/*
7442 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7443 * a NUL.
7444 */
7445 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007446screen_puts_len(
7447 char_u *text,
7448 int textlen,
7449 int row,
7450 int col,
7451 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
7453 unsigned off;
7454 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007455 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 int c;
7457#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007458 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 int mbyte_blen = 1;
7460 int mbyte_cells = 1;
7461 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007462 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 int clear_next_cell = FALSE;
7464# ifdef FEAT_ARABIC
7465 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007466 int pc, nc, nc1;
7467 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468# endif
7469#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007470#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7471 int force_redraw_this;
7472 int force_redraw_next = FALSE;
7473#endif
7474 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475
7476 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7477 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007478 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479
Bram Moolenaarc236c162008-07-13 17:41:49 +00007480#ifdef FEAT_MBYTE
7481 /* When drawing over the right halve of a double-wide char clear out the
7482 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007483 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007484# ifdef FEAT_GUI
7485 && !gui.in_use
7486# endif
7487 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007488 {
7489 ScreenLines[off - 1] = ' ';
7490 ScreenAttrs[off - 1] = 0;
7491 if (enc_utf8)
7492 {
7493 ScreenLinesUC[off - 1] = 0;
7494 ScreenLinesC[0][off - 1] = 0;
7495 }
7496 /* redraw the previous cell, make it empty */
7497 screen_char(off - 1, row, col - 1);
7498 /* force the cell at "col" to be redrawn */
7499 force_redraw_next = TRUE;
7500 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007501#endif
7502
Bram Moolenaar367329b2007-08-30 11:53:22 +00007503#ifdef FEAT_MBYTE
7504 max_off = LineOffset[row] + screen_Columns;
7505#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007506 while (col < screen_Columns
7507 && (len < 0 || (int)(ptr - text) < len)
7508 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 {
7510 c = *ptr;
7511#ifdef FEAT_MBYTE
7512 /* check if this is the first byte of a multibyte */
7513 if (has_mbyte)
7514 {
7515 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007516 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007518 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7520 mbyte_cells = 1;
7521 else if (enc_dbcs != 0)
7522 mbyte_cells = mbyte_blen;
7523 else /* enc_utf8 */
7524 {
7525 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007526 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 (int)((text + len) - ptr));
7528 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007529 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007531# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 /* Non-BMP character: display as ? or fullwidth ?. */
7533 if (u8c >= 0x10000)
7534 {
7535 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7536 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007537 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540# ifdef FEAT_ARABIC
7541 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7542 {
7543 /* Do Arabic shaping. */
7544 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7545 {
7546 /* Past end of string to be displayed. */
7547 nc = NUL;
7548 nc1 = NUL;
7549 }
7550 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007551 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007552 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7553 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007554 nc1 = pcc[0];
7555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 pc = prev_c;
7557 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007558 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 }
7560 else
7561 prev_c = u8c;
7562# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007563 if (col + mbyte_cells > screen_Columns)
7564 {
7565 /* Only 1 cell left, but character requires 2 cells:
7566 * display a '>' in the last column to avoid wrapping. */
7567 c = '>';
7568 mbyte_cells = 1;
7569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 }
7571 }
7572#endif
7573
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007574#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7575 force_redraw_this = force_redraw_next;
7576 force_redraw_next = FALSE;
7577#endif
7578
7579 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580#ifdef FEAT_MBYTE
7581 || (mbyte_cells == 2
7582 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7583 || (enc_dbcs == DBCS_JPNU
7584 && c == 0x8e
7585 && ScreenLines2[off] != ptr[1])
7586 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007587 && (ScreenLinesUC[off] !=
7588 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7589 || (ScreenLinesUC[off] != 0
7590 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591#endif
7592 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007593 || exmode_active;
7594
7595 if (need_redraw
7596#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7597 || force_redraw_this
7598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 )
7600 {
7601#if defined(FEAT_GUI) || defined(UNIX)
7602 /* The bold trick makes a single row of pixels appear in the next
7603 * character. When a bold character is removed, the next
7604 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007605 * and for some xterms. */
7606 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607# ifdef FEAT_GUI
7608 gui.in_use
7609# endif
7610# if defined(FEAT_GUI) && defined(UNIX)
7611 ||
7612# endif
7613# ifdef UNIX
7614 term_is_xterm
7615# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007616 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007618 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007620 if (n > HL_ALL)
7621 n = syn_attr2attr(n);
7622 if (n & HL_BOLD)
7623 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 }
7625#endif
7626#ifdef FEAT_MBYTE
7627 /* When at the end of the text and overwriting a two-cell
7628 * character with a one-cell character, need to clear the next
7629 * cell. Also when overwriting the left halve of a two-cell char
7630 * with the right halve of a two-cell char. Do this only once
7631 * (mb_off2cells() may return 2 on the right halve). */
7632 if (clear_next_cell)
7633 clear_next_cell = FALSE;
7634 else if (has_mbyte
7635 && (len < 0 ? ptr[mbyte_blen] == NUL
7636 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007637 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007639 && (*mb_off2cells)(off, max_off) == 1
7640 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 clear_next_cell = TRUE;
7642
7643 /* Make sure we never leave a second byte of a double-byte behind,
7644 * it confuses mb_off2cells(). */
7645 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007646 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007648 && (*mb_off2cells)(off, max_off) == 1
7649 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 ScreenLines[off + mbyte_blen] = 0;
7651#endif
7652 ScreenLines[off] = c;
7653 ScreenAttrs[off] = attr;
7654#ifdef FEAT_MBYTE
7655 if (enc_utf8)
7656 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007657 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 ScreenLinesUC[off] = 0;
7659 else
7660 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007661 int i;
7662
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007664 for (i = 0; i < Screen_mco; ++i)
7665 {
7666 ScreenLinesC[i][off] = u8cc[i];
7667 if (u8cc[i] == 0)
7668 break;
7669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 }
7671 if (mbyte_cells == 2)
7672 {
7673 ScreenLines[off + 1] = 0;
7674 ScreenAttrs[off + 1] = attr;
7675 }
7676 screen_char(off, row, col);
7677 }
7678 else if (mbyte_cells == 2)
7679 {
7680 ScreenLines[off + 1] = ptr[1];
7681 ScreenAttrs[off + 1] = attr;
7682 screen_char_2(off, row, col);
7683 }
7684 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7685 {
7686 ScreenLines2[off] = ptr[1];
7687 screen_char(off, row, col);
7688 }
7689 else
7690#endif
7691 screen_char(off, row, col);
7692 }
7693#ifdef FEAT_MBYTE
7694 if (has_mbyte)
7695 {
7696 off += mbyte_cells;
7697 col += mbyte_cells;
7698 ptr += mbyte_blen;
7699 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007700 {
7701 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007703 len = -1;
7704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 }
7706 else
7707#endif
7708 {
7709 ++off;
7710 ++col;
7711 ++ptr;
7712 }
7713 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007714
7715#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7716 /* If we detected the next character needs to be redrawn, but the text
7717 * doesn't extend up to there, update the character here. */
7718 if (force_redraw_next && col < screen_Columns)
7719 {
7720# ifdef FEAT_MBYTE
7721 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7722 screen_char_2(off, row, col);
7723 else
7724# endif
7725 screen_char(off, row, col);
7726 }
7727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728}
7729
7730#ifdef FEAT_SEARCH_EXTRA
7731/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007732 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 */
7734 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007735start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737 if (p_hls && !no_hlsearch)
7738 {
7739 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007740 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007741# ifdef FEAT_RELTIME
7742 /* Set the time limit to 'redrawtime'. */
7743 profile_setlimit(p_rdt, &search_hl.tm);
7744# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 }
7746}
7747
7748/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007749 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 */
7751 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007752end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
7754 if (search_hl.rm.regprog != NULL)
7755 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007756 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 search_hl.rm.regprog = NULL;
7758 }
7759}
7760
7761/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007762 * Init for calling prepare_search_hl().
7763 */
7764 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007765init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007766{
7767 matchitem_T *cur;
7768
7769 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7770 * match */
7771 cur = wp->w_match_head;
7772 while (cur != NULL)
7773 {
7774 cur->hl.rm = cur->match;
7775 if (cur->hlg_id == 0)
7776 cur->hl.attr = 0;
7777 else
7778 cur->hl.attr = syn_id2attr(cur->hlg_id);
7779 cur->hl.buf = wp->w_buffer;
7780 cur->hl.lnum = 0;
7781 cur->hl.first_lnum = 0;
7782# ifdef FEAT_RELTIME
7783 /* Set the time limit to 'redrawtime'. */
7784 profile_setlimit(p_rdt, &(cur->hl.tm));
7785# endif
7786 cur = cur->next;
7787 }
7788 search_hl.buf = wp->w_buffer;
7789 search_hl.lnum = 0;
7790 search_hl.first_lnum = 0;
7791 /* time limit is set at the toplevel, for all windows */
7792}
7793
7794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 * Advance to the match in window "wp" line "lnum" or past it.
7796 */
7797 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007798prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007800 matchitem_T *cur; /* points to the match list */
7801 match_T *shl; /* points to search_hl or a match */
7802 int shl_flag; /* flag to indicate whether search_hl
7803 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007804 int pos_inprogress; /* marks that position match search is
7805 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 int n;
7807
7808 /*
7809 * When using a multi-line pattern, start searching at the top
7810 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007811 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007813 cur = wp->w_match_head;
7814 shl_flag = FALSE;
7815 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007817 if (shl_flag == FALSE)
7818 {
7819 shl = &search_hl;
7820 shl_flag = TRUE;
7821 }
7822 else
7823 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 if (shl->rm.regprog != NULL
7825 && shl->lnum == 0
7826 && re_multiline(shl->rm.regprog))
7827 {
7828 if (shl->first_lnum == 0)
7829 {
7830# ifdef FEAT_FOLDING
7831 for (shl->first_lnum = lnum;
7832 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7833 if (hasFoldingWin(wp, shl->first_lnum - 1,
7834 NULL, NULL, TRUE, NULL))
7835 break;
7836# else
7837 shl->first_lnum = wp->w_topline;
7838# endif
7839 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007840 if (cur != NULL)
7841 cur->pos.cur = 0;
7842 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007844 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7845 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007847 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7848 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007849 pos_inprogress = cur == NULL || cur->pos.cur == 0
7850 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 if (shl->lnum != 0)
7852 {
7853 shl->first_lnum = shl->lnum
7854 + shl->rm.endpos[0].lnum
7855 - shl->rm.startpos[0].lnum;
7856 n = shl->rm.endpos[0].col;
7857 }
7858 else
7859 {
7860 ++shl->first_lnum;
7861 n = 0;
7862 }
7863 }
7864 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007865 if (shl != &search_hl && cur != NULL)
7866 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 }
7868}
7869
7870/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007871 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 * Uses shl->buf.
7873 * Sets shl->lnum and shl->rm contents.
7874 * Note: Assumes a previous match is always before "lnum", unless
7875 * shl->lnum is zero.
7876 * Careful: Any pointers for buffer lines will become invalid.
7877 */
7878 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007879next_search_hl(
7880 win_T *win,
7881 match_T *shl, /* points to search_hl or a match */
7882 linenr_T lnum,
7883 colnr_T mincol, /* minimal column for a match */
7884 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
7886 linenr_T l;
7887 colnr_T matchcol;
7888 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007889 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007891 // for :{range}s/pat only highlight inside the range
7892 if (lnum < search_first_line || lnum > search_last_line)
7893 {
7894 shl->lnum = 0;
7895 return;
7896 }
7897
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 if (shl->lnum != 0)
7899 {
7900 /* Check for three situations:
7901 * 1. If the "lnum" is below a previous match, start a new search.
7902 * 2. If the previous match includes "mincol", use it.
7903 * 3. Continue after the previous match.
7904 */
7905 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7906 if (lnum > l)
7907 shl->lnum = 0;
7908 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7909 return;
7910 }
7911
7912 /*
7913 * Repeat searching for a match until one is found that includes "mincol"
7914 * or none is found in this line.
7915 */
7916 called_emsg = FALSE;
7917 for (;;)
7918 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007919#ifdef FEAT_RELTIME
7920 /* Stop searching after passing the time limit. */
7921 if (profile_passed_limit(&(shl->tm)))
7922 {
7923 shl->lnum = 0; /* no match found in time */
7924 break;
7925 }
7926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 /* Three situations:
7928 * 1. No useful previous match: search from start of line.
7929 * 2. Not Vi compatible or empty match: continue at next character.
7930 * Break the loop if this is beyond the end of the line.
7931 * 3. Vi compatible searching: continue at end of previous match.
7932 */
7933 if (shl->lnum == 0)
7934 matchcol = 0;
7935 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7936 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007937 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007939 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007940
7941 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007942 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007943 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007945 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 shl->lnum = 0;
7947 break;
7948 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007949#ifdef FEAT_MBYTE
7950 if (has_mbyte)
7951 matchcol += mb_ptr2len(ml);
7952 else
7953#endif
7954 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 }
7956 else
7957 matchcol = shl->rm.endpos[0].col;
7958
7959 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007960 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007962 /* Remember whether shl->rm is using a copy of the regprog in
7963 * cur->match. */
7964 int regprog_is_copy = (shl != &search_hl && cur != NULL
7965 && shl == &cur->hl
7966 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007967 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007968
Bram Moolenaarb3414592014-06-17 17:48:32 +02007969 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7970 matchcol,
7971#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007972 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007973#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007974 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007975#endif
7976 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007977 /* Copy the regprog, in case it got freed and recompiled. */
7978 if (regprog_is_copy)
7979 cur->match.regprog = cur->hl.rm.regprog;
7980
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007981 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007982 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007983 /* Error while handling regexp: stop using this regexp. */
7984 if (shl == &search_hl)
7985 {
7986 /* don't free regprog in the match list, it's a copy */
7987 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007988 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007989 }
7990 shl->rm.regprog = NULL;
7991 shl->lnum = 0;
7992 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7993 message */
7994 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007995 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007996 }
7997 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007998 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007999 else
8000 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 if (nmatched == 0)
8002 {
8003 shl->lnum = 0; /* no match found */
8004 break;
8005 }
8006 if (shl->rm.startpos[0].lnum > 0
8007 || shl->rm.startpos[0].col >= mincol
8008 || nmatched > 1
8009 || shl->rm.endpos[0].col > mincol)
8010 {
8011 shl->lnum += shl->rm.startpos[0].lnum;
8012 break; /* useful match found */
8013 }
8014 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008015
8016 // Restore called_emsg for assert_fails().
8017 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
Bram Moolenaar85077472016-10-16 14:35:48 +02008020/*
8021 * If there is a match fill "shl" and return one.
8022 * Return zero otherwise.
8023 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008024 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008025next_search_hl_pos(
8026 match_T *shl, /* points to a match */
8027 linenr_T lnum,
8028 posmatch_T *posmatch, /* match positions */
8029 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008030{
8031 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008032 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008033
Bram Moolenaarb3414592014-06-17 17:48:32 +02008034 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8035 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008036 llpos_T *pos = &posmatch->pos[i];
8037
8038 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008039 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008040 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008041 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008042 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008043 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008044 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008045 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008046 /* if this match comes before the one at "found" then swap
8047 * them */
8048 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008049 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008050 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008051
Bram Moolenaar85077472016-10-16 14:35:48 +02008052 *pos = posmatch->pos[found];
8053 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008054 }
8055 }
8056 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008057 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008058 }
8059 }
8060 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008061 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008062 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008063 colnr_T start = posmatch->pos[found].col == 0
8064 ? 0 : posmatch->pos[found].col - 1;
8065 colnr_T end = posmatch->pos[found].col == 0
8066 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008067
Bram Moolenaar85077472016-10-16 14:35:48 +02008068 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008069 shl->rm.startpos[0].lnum = 0;
8070 shl->rm.startpos[0].col = start;
8071 shl->rm.endpos[0].lnum = 0;
8072 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008073 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008074 posmatch->cur = found + 1;
8075 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008076 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008077 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008078}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008079#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008080
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008082screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
8084 attrentry_T *aep = NULL;
8085
8086 screen_attr = attr;
8087 if (full_screen
8088#ifdef WIN3264
8089 && termcap_active
8090#endif
8091 )
8092 {
8093#ifdef FEAT_GUI
8094 if (gui.in_use)
8095 {
8096 char buf[20];
8097
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008098 /* The GUI handles this internally. */
8099 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 OUT_STR(buf);
8101 }
8102 else
8103#endif
8104 {
8105 if (attr > HL_ALL) /* special HL attr. */
8106 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008107 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 aep = syn_cterm_attr2entry(attr);
8109 else
8110 aep = syn_term_attr2entry(attr);
8111 if (aep == NULL) /* did ":syntax clear" */
8112 attr = 0;
8113 else
8114 attr = aep->ae_attr;
8115 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008116 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008118 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008119#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008120 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8121 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8122 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008123#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008124 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008125 /* If the Normal FG color has BOLD attribute and the new HL
8126 * has a FG color defined, clear BOLD. */
8127 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008128 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008130 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008131 out_str(T_UCS);
8132 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008133 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8134 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008136 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008138 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008140 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008141 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142
8143 /*
8144 * Output the color or start string after bold etc., in case the
8145 * bold etc. override the color setting.
8146 */
8147 if (aep != NULL)
8148 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008149#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008150 /* When 'termguicolors' is set but fg or bg is unset,
8151 * fall back to the cterm colors. This helps for SpellBad,
8152 * where the GUI uses a red undercurl. */
8153 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008155 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008156 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008157 }
8158 else
8159#endif
8160 if (t_colors > 1)
8161 {
8162 if (aep->ae_u.cterm.fg_color)
8163 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8164 }
8165#ifdef FEAT_TERMGUICOLORS
8166 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8167 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008168 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008169 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 }
8171 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008172#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008173 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008175 if (aep->ae_u.cterm.bg_color)
8176 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8177 }
8178
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008179 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008180 {
8181 if (aep->ae_u.term.start != NULL)
8182 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 }
8184 }
8185 }
8186 }
8187}
8188
8189 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008190screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191{
8192 int do_ME = FALSE; /* output T_ME code */
8193
8194 if (screen_attr != 0
8195#ifdef WIN3264
8196 && termcap_active
8197#endif
8198 )
8199 {
8200#ifdef FEAT_GUI
8201 if (gui.in_use)
8202 {
8203 char buf[20];
8204
8205 /* use internal GUI code */
8206 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8207 OUT_STR(buf);
8208 }
8209 else
8210#endif
8211 {
8212 if (screen_attr > HL_ALL) /* special HL attr. */
8213 {
8214 attrentry_T *aep;
8215
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008216 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {
8218 /*
8219 * Assume that t_me restores the original colors!
8220 */
8221 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008222 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008223#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008224 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8225 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8226 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008227#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008228 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008229#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008230 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8231 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8232 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008233#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008234 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 do_ME = TRUE;
8236 }
8237 else
8238 {
8239 aep = syn_term_attr2entry(screen_attr);
8240 if (aep != NULL && aep->ae_u.term.stop != NULL)
8241 {
8242 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8243 do_ME = TRUE;
8244 else
8245 out_str(aep->ae_u.term.stop);
8246 }
8247 }
8248 if (aep == NULL) /* did ":syntax clear" */
8249 screen_attr = 0;
8250 else
8251 screen_attr = aep->ae_attr;
8252 }
8253
8254 /*
8255 * Often all ending-codes are equal to T_ME. Avoid outputting the
8256 * same sequence several times.
8257 */
8258 if (screen_attr & HL_STANDOUT)
8259 {
8260 if (STRCMP(T_SE, T_ME) == 0)
8261 do_ME = TRUE;
8262 else
8263 out_str(T_SE);
8264 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008265 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008266 {
8267 if (STRCMP(T_UCE, T_ME) == 0)
8268 do_ME = TRUE;
8269 else
8270 out_str(T_UCE);
8271 }
8272 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008273 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {
8275 if (STRCMP(T_UE, T_ME) == 0)
8276 do_ME = TRUE;
8277 else
8278 out_str(T_UE);
8279 }
8280 if (screen_attr & HL_ITALIC)
8281 {
8282 if (STRCMP(T_CZR, T_ME) == 0)
8283 do_ME = TRUE;
8284 else
8285 out_str(T_CZR);
8286 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008287 if (screen_attr & HL_STRIKETHROUGH)
8288 {
8289 if (STRCMP(T_STE, T_ME) == 0)
8290 do_ME = TRUE;
8291 else
8292 out_str(T_STE);
8293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8295 out_str(T_ME);
8296
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008297#ifdef FEAT_TERMGUICOLORS
8298 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008300 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008301 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008302 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008303 term_bg_rgb_color(cterm_normal_bg_gui_color);
8304 }
8305 else
8306#endif
8307 {
8308 if (t_colors > 1)
8309 {
8310 /* set Normal cterm colors */
8311 if (cterm_normal_fg_color != 0)
8312 term_fg_color(cterm_normal_fg_color - 1);
8313 if (cterm_normal_bg_color != 0)
8314 term_bg_color(cterm_normal_bg_color - 1);
8315 if (cterm_normal_fg_bold)
8316 out_str(T_MD);
8317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 }
8319 }
8320 }
8321 screen_attr = 0;
8322}
8323
8324/*
8325 * Reset the colors for a cterm. Used when leaving Vim.
8326 * The machine specific code may override this again.
8327 */
8328 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008329reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008331 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {
8333 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008334#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008335 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8336 || cterm_normal_bg_gui_color != INVALCOLOR)
8337 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008338#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {
8342 out_str(T_OP);
8343 screen_attr = -1;
8344 }
8345 if (cterm_normal_fg_bold)
8346 {
8347 out_str(T_ME);
8348 screen_attr = -1;
8349 }
8350 }
8351}
8352
8353/*
8354 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8355 * using the attributes from ScreenAttrs["off"].
8356 */
8357 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008358screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359{
8360 int attr;
8361
8362 /* Check for illegal values, just in case (could happen just after
8363 * resizing). */
8364 if (row >= screen_Rows || col >= screen_Columns)
8365 return;
8366
Bram Moolenaar494838a2015-02-10 19:20:37 +01008367 /* Outputting a character in the last cell on the screen may scroll the
8368 * screen up. Only do it when the "xn" termcap property is set, otherwise
8369 * mark the character invalid (update it when scrolled up). */
8370 if (*T_XN == NUL
8371 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372#ifdef FEAT_RIGHTLEFT
8373 /* account for first command-line character in rightleft mode */
8374 && !cmdmsg_rl
8375#endif
8376 )
8377 {
8378 ScreenAttrs[off] = (sattr_T)-1;
8379 return;
8380 }
8381
8382 /*
8383 * Stop highlighting first, so it's easier to move the cursor.
8384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 if (screen_char_attr != 0)
8386 attr = screen_char_attr;
8387 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 attr = ScreenAttrs[off];
8389 if (screen_attr != attr)
8390 screen_stop_highlight();
8391
8392 windgoto(row, col);
8393
8394 if (screen_attr != attr)
8395 screen_start_highlight(attr);
8396
8397#ifdef FEAT_MBYTE
8398 if (enc_utf8 && ScreenLinesUC[off] != 0)
8399 {
8400 char_u buf[MB_MAXBYTES + 1];
8401
Bram Moolenaarcb070082016-04-02 22:14:51 +02008402 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008403 {
8404 if (*p_ambw == 'd'
8405# ifdef FEAT_GUI
8406 && !gui.in_use
8407# endif
8408 )
8409 {
8410 /* Clear the two screen cells. If the character is actually
8411 * single width it won't change the second cell. */
8412 out_str((char_u *)" ");
8413 term_windgoto(row, col);
8414 }
8415 /* not sure where the cursor is after drawing the ambiguous width
8416 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008417 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008418 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008419 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008421
8422 /* Convert the UTF-8 character to bytes and write it. */
8423 buf[utfc_char2bytes(off, buf)] = NUL;
8424 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 }
8426 else
8427#endif
8428 {
8429#ifdef FEAT_MBYTE
8430 out_flush_check();
8431#endif
8432 out_char(ScreenLines[off]);
8433#ifdef FEAT_MBYTE
8434 /* double-byte character in single-width cell */
8435 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8436 out_char(ScreenLines2[off]);
8437#endif
8438 }
8439
8440 screen_cur_col++;
8441}
8442
8443#ifdef FEAT_MBYTE
8444
8445/*
8446 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8447 * on the screen at position 'row' and 'col'.
8448 * The attributes of the first byte is used for all. This is required to
8449 * output the two bytes of a double-byte character with nothing in between.
8450 */
8451 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008452screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453{
8454 /* Check for illegal values (could be wrong when screen was resized). */
8455 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8456 return;
8457
8458 /* Outputting the last character on the screen may scrollup the screen.
8459 * Don't to it! Mark the character invalid (update it when scrolled up) */
8460 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8461 {
8462 ScreenAttrs[off] = (sattr_T)-1;
8463 return;
8464 }
8465
8466 /* Output the first byte normally (positions the cursor), then write the
8467 * second byte directly. */
8468 screen_char(off, row, col);
8469 out_char(ScreenLines[off + 1]);
8470 ++screen_cur_col;
8471}
8472#endif
8473
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474/*
8475 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8476 * This uses the contents of ScreenLines[] and doesn't change it.
8477 */
8478 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008479screen_draw_rectangle(
8480 int row,
8481 int col,
8482 int height,
8483 int width,
8484 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
8486 int r, c;
8487 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008488#ifdef FEAT_MBYTE
8489 int max_off;
8490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008492 /* Can't use ScreenLines unless initialized */
8493 if (ScreenLines == NULL)
8494 return;
8495
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 if (invert)
8497 screen_char_attr = HL_INVERSE;
8498 for (r = row; r < row + height; ++r)
8499 {
8500 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008501#ifdef FEAT_MBYTE
8502 max_off = off + screen_Columns;
8503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 for (c = col; c < col + width; ++c)
8505 {
8506#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008507 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {
8509 screen_char_2(off + c, r, c);
8510 ++c;
8511 }
8512 else
8513#endif
8514 {
8515 screen_char(off + c, r, c);
8516#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008517 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 ++c;
8519#endif
8520 }
8521 }
8522 }
8523 screen_char_attr = 0;
8524}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526/*
8527 * Redraw the characters for a vertically split window.
8528 */
8529 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008530redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531{
8532 int col;
8533 int width;
8534
8535# ifdef FEAT_CLIPBOARD
8536 clip_may_clear_selection(row, end - 1);
8537# endif
8538
8539 if (wp == NULL)
8540 {
8541 col = 0;
8542 width = Columns;
8543 }
8544 else
8545 {
8546 col = wp->w_wincol;
8547 width = wp->w_width;
8548 }
8549 screen_draw_rectangle(row, col, end - row, width, FALSE);
8550}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008552 static void
8553space_to_screenline(int off, int attr)
8554{
8555 ScreenLines[off] = ' ';
8556 ScreenAttrs[off] = attr;
8557# ifdef FEAT_MBYTE
8558 if (enc_utf8)
8559 ScreenLinesUC[off] = 0;
8560# endif
8561}
8562
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563/*
8564 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8565 * with character 'c1' in first column followed by 'c2' in the other columns.
8566 * Use attributes 'attr'.
8567 */
8568 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008569screen_fill(
8570 int start_row,
8571 int end_row,
8572 int start_col,
8573 int end_col,
8574 int c1,
8575 int c2,
8576 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577{
8578 int row;
8579 int col;
8580 int off;
8581 int end_off;
8582 int did_delete;
8583 int c;
8584 int norm_term;
8585#if defined(FEAT_GUI) || defined(UNIX)
8586 int force_next = FALSE;
8587#endif
8588
8589 if (end_row > screen_Rows) /* safety check */
8590 end_row = screen_Rows;
8591 if (end_col > screen_Columns) /* safety check */
8592 end_col = screen_Columns;
8593 if (ScreenLines == NULL
8594 || start_row >= end_row
8595 || start_col >= end_col) /* nothing to do */
8596 return;
8597
8598 /* it's a "normal" terminal when not in a GUI or cterm */
8599 norm_term = (
8600#ifdef FEAT_GUI
8601 !gui.in_use &&
8602#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008603 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 for (row = start_row; row < end_row; ++row)
8605 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008606#ifdef FEAT_MBYTE
8607 if (has_mbyte
8608# ifdef FEAT_GUI
8609 && !gui.in_use
8610# endif
8611 )
8612 {
8613 /* When drawing over the right halve of a double-wide char clear
8614 * out the left halve. When drawing over the left halve of a
8615 * double wide-char clear out the right halve. Only needed in a
8616 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008617 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008618 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008619 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008620 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008621 }
8622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 /*
8624 * Try to use delete-line termcap code, when no attributes or in a
8625 * "normal" terminal, where a bold/italic space is just a
8626 * space.
8627 */
8628 did_delete = FALSE;
8629 if (c2 == ' '
8630 && end_col == Columns
8631 && can_clear(T_CE)
8632 && (attr == 0
8633 || (norm_term
8634 && attr <= HL_ALL
8635 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8636 {
8637 /*
8638 * check if we really need to clear something
8639 */
8640 col = start_col;
8641 if (c1 != ' ') /* don't clear first char */
8642 ++col;
8643
8644 off = LineOffset[row] + col;
8645 end_off = LineOffset[row] + end_col;
8646
8647 /* skip blanks (used often, keep it fast!) */
8648#ifdef FEAT_MBYTE
8649 if (enc_utf8)
8650 while (off < end_off && ScreenLines[off] == ' '
8651 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8652 ++off;
8653 else
8654#endif
8655 while (off < end_off && ScreenLines[off] == ' '
8656 && ScreenAttrs[off] == 0)
8657 ++off;
8658 if (off < end_off) /* something to be cleared */
8659 {
8660 col = off - LineOffset[row];
8661 screen_stop_highlight();
8662 term_windgoto(row, col);/* clear rest of this screen line */
8663 out_str(T_CE);
8664 screen_start(); /* don't know where cursor is now */
8665 col = end_col - col;
8666 while (col--) /* clear chars in ScreenLines */
8667 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008668 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 ++off;
8670 }
8671 }
8672 did_delete = TRUE; /* the chars are cleared now */
8673 }
8674
8675 off = LineOffset[row] + start_col;
8676 c = c1;
8677 for (col = start_col; col < end_col; ++col)
8678 {
8679 if (ScreenLines[off] != c
8680#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008681 || (enc_utf8 && (int)ScreenLinesUC[off]
8682 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683#endif
8684 || ScreenAttrs[off] != attr
8685#if defined(FEAT_GUI) || defined(UNIX)
8686 || force_next
8687#endif
8688 )
8689 {
8690#if defined(FEAT_GUI) || defined(UNIX)
8691 /* The bold trick may make a single row of pixels appear in
8692 * the next character. When a bold character is removed, the
8693 * next character should be redrawn too. This happens for our
8694 * own GUI and for some xterms. */
8695 if (
8696# ifdef FEAT_GUI
8697 gui.in_use
8698# endif
8699# if defined(FEAT_GUI) && defined(UNIX)
8700 ||
8701# endif
8702# ifdef UNIX
8703 term_is_xterm
8704# endif
8705 )
8706 {
8707 if (ScreenLines[off] != ' '
8708 && (ScreenAttrs[off] > HL_ALL
8709 || ScreenAttrs[off] & HL_BOLD))
8710 force_next = TRUE;
8711 else
8712 force_next = FALSE;
8713 }
8714#endif
8715 ScreenLines[off] = c;
8716#ifdef FEAT_MBYTE
8717 if (enc_utf8)
8718 {
8719 if (c >= 0x80)
8720 {
8721 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008722 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 }
8724 else
8725 ScreenLinesUC[off] = 0;
8726 }
8727#endif
8728 ScreenAttrs[off] = attr;
8729 if (!did_delete || c != ' ')
8730 screen_char(off, row, col);
8731 }
8732 ++off;
8733 if (col == start_col)
8734 {
8735 if (did_delete)
8736 break;
8737 c = c2;
8738 }
8739 }
8740 if (end_col == Columns)
8741 LineWraps[row] = FALSE;
8742 if (row == Rows - 1) /* overwritten the command line */
8743 {
8744 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008745 if (start_col == 0 && end_col == Columns
8746 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008748 if (start_col == 0)
8749 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 }
8751 }
8752}
8753
8754/*
8755 * Check if there should be a delay. Used before clearing or redrawing the
8756 * screen or the command line.
8757 */
8758 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008759check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
8761 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8762 && !did_wait_return
8763 && emsg_silent == 0)
8764 {
8765 out_flush();
8766 ui_delay(1000L, TRUE);
8767 emsg_on_display = FALSE;
8768 if (check_msg_scroll)
8769 msg_scroll = FALSE;
8770 }
8771}
8772
8773/*
8774 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008775 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 * Returns TRUE if there is a valid screen to write to.
8777 * Returns FALSE when starting up and screen not initialized yet.
8778 */
8779 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008780screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008782 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 return (ScreenLines != NULL);
8784}
8785
8786/*
8787 * Resize the shell to Rows and Columns.
8788 * Allocate ScreenLines[] and associated items.
8789 *
8790 * There may be some time between setting Rows and Columns and (re)allocating
8791 * ScreenLines[]. This happens when starting up and when (manually) changing
8792 * the shell size. Always use screen_Rows and screen_Columns to access items
8793 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8794 * final size of the shell is needed.
8795 */
8796 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008797screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798{
8799 int new_row, old_row;
8800#ifdef FEAT_GUI
8801 int old_Rows;
8802#endif
8803 win_T *wp;
8804 int outofmem = FALSE;
8805 int len;
8806 schar_T *new_ScreenLines;
8807#ifdef FEAT_MBYTE
8808 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008809 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008811 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812#endif
8813 sattr_T *new_ScreenAttrs;
8814 unsigned *new_LineOffset;
8815 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008816 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008817 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008819 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008820 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008822retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 /*
8824 * Allocation of the screen buffers is done only when the size changes and
8825 * when Rows and Columns have been set and we have started doing full
8826 * screen stuff.
8827 */
8828 if ((ScreenLines != NULL
8829 && Rows == screen_Rows
8830 && Columns == screen_Columns
8831#ifdef FEAT_MBYTE
8832 && enc_utf8 == (ScreenLinesUC != NULL)
8833 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008834 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835#endif
8836 )
8837 || Rows == 0
8838 || Columns == 0
8839 || (!full_screen && ScreenLines == NULL))
8840 return;
8841
8842 /*
8843 * It's possible that we produce an out-of-memory message below, which
8844 * will cause this function to be called again. To break the loop, just
8845 * return here.
8846 */
8847 if (entered)
8848 return;
8849 entered = TRUE;
8850
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008851 /*
8852 * Note that the window sizes are updated before reallocating the arrays,
8853 * thus we must not redraw here!
8854 */
8855 ++RedrawingDisabled;
8856
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 win_new_shellsize(); /* fit the windows in the new sized shell */
8858
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 comp_col(); /* recompute columns for shown command and ruler */
8860
8861 /*
8862 * We're changing the size of the screen.
8863 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8864 * - Move lines from the old arrays into the new arrays, clear extra
8865 * lines (unless the screen is going to be cleared).
8866 * - Free the old arrays.
8867 *
8868 * If anything fails, make ScreenLines NULL, so we don't do anything!
8869 * Continuing with the old ScreenLines may result in a crash, because the
8870 * size is wrong.
8871 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008872 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008874 if (aucmd_win != NULL)
8875 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876
8877 new_ScreenLines = (schar_T *)lalloc((long_u)(
8878 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8879#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008880 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 if (enc_utf8)
8882 {
8883 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8884 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008885 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008886 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8888 }
8889 if (enc_dbcs == DBCS_JPNU)
8890 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8891 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8892#endif
8893 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8894 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8895 new_LineOffset = (unsigned *)lalloc((long_u)(
8896 Rows * sizeof(unsigned)), FALSE);
8897 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008898 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008900 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
8902 if (win_alloc_lines(wp) == FAIL)
8903 {
8904 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008905 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 }
8907 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008908 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8909 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008910 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008911give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008913#ifdef FEAT_MBYTE
8914 for (i = 0; i < p_mco; ++i)
8915 if (new_ScreenLinesC[i] == NULL)
8916 break;
8917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 if (new_ScreenLines == NULL
8919#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008920 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8922#endif
8923 || new_ScreenAttrs == NULL
8924 || new_LineOffset == NULL
8925 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008926 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 || outofmem)
8928 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008929 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008930 {
8931 /* guess the size */
8932 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8933
8934 /* Remember we did this to avoid getting outofmem messages over
8935 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008936 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008937 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008938 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008940 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008941 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008942 VIM_CLEAR(new_ScreenLinesC[i]);
8943 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008945 VIM_CLEAR(new_ScreenAttrs);
8946 VIM_CLEAR(new_LineOffset);
8947 VIM_CLEAR(new_LineWraps);
8948 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 }
8950 else
8951 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008952 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008953
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 for (new_row = 0; new_row < Rows; ++new_row)
8955 {
8956 new_LineOffset[new_row] = new_row * Columns;
8957 new_LineWraps[new_row] = FALSE;
8958
8959 /*
8960 * If the screen is not going to be cleared, copy as much as
8961 * possible from the old screen to the new one and clear the rest
8962 * (used when resizing the window at the "--more--" prompt or when
8963 * executing an external command, for the GUI).
8964 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008965 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 {
8967 (void)vim_memset(new_ScreenLines + new_row * Columns,
8968 ' ', (size_t)Columns * sizeof(schar_T));
8969#ifdef FEAT_MBYTE
8970 if (enc_utf8)
8971 {
8972 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8973 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008974 for (i = 0; i < p_mco; ++i)
8975 (void)vim_memset(new_ScreenLinesC[i]
8976 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 0, (size_t)Columns * sizeof(u8char_T));
8978 }
8979 if (enc_dbcs == DBCS_JPNU)
8980 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8981 0, (size_t)Columns * sizeof(schar_T));
8982#endif
8983 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8984 0, (size_t)Columns * sizeof(sattr_T));
8985 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008986 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 {
8988 if (screen_Columns < Columns)
8989 len = screen_Columns;
8990 else
8991 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008992#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008993 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008994 * may be invalid now. Also when p_mco changes. */
8995 if (!(enc_utf8 && ScreenLinesUC == NULL)
8996 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008997#endif
8998 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8999 ScreenLines + LineOffset[old_row],
9000 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009002 if (enc_utf8 && ScreenLinesUC != NULL
9003 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 {
9005 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9006 ScreenLinesUC + LineOffset[old_row],
9007 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009008 for (i = 0; i < p_mco; ++i)
9009 mch_memmove(new_ScreenLinesC[i]
9010 + new_LineOffset[new_row],
9011 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 (size_t)len * sizeof(u8char_T));
9013 }
9014 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9015 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9016 ScreenLines2 + LineOffset[old_row],
9017 (size_t)len * sizeof(schar_T));
9018#endif
9019 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9020 ScreenAttrs + LineOffset[old_row],
9021 (size_t)len * sizeof(sattr_T));
9022 }
9023 }
9024 }
9025 /* Use the last line of the screen for the current line. */
9026 current_ScreenLine = new_ScreenLines + Rows * Columns;
9027 }
9028
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009029 free_screenlines();
9030
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 ScreenLines = new_ScreenLines;
9032#ifdef FEAT_MBYTE
9033 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009034 for (i = 0; i < p_mco; ++i)
9035 ScreenLinesC[i] = new_ScreenLinesC[i];
9036 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 ScreenLines2 = new_ScreenLines2;
9038#endif
9039 ScreenAttrs = new_ScreenAttrs;
9040 LineOffset = new_LineOffset;
9041 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009042 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043
9044 /* It's important that screen_Rows and screen_Columns reflect the actual
9045 * size of ScreenLines[]. Set them before calling anything. */
9046#ifdef FEAT_GUI
9047 old_Rows = screen_Rows;
9048#endif
9049 screen_Rows = Rows;
9050 screen_Columns = Columns;
9051
9052 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009053 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 screenclear2();
9055
9056#ifdef FEAT_GUI
9057 else if (gui.in_use
9058 && !gui.starting
9059 && ScreenLines != NULL
9060 && old_Rows != Rows)
9061 {
9062 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9063 /*
9064 * Adjust the position of the cursor, for when executing an external
9065 * command.
9066 */
9067 if (msg_row >= Rows) /* Rows got smaller */
9068 msg_row = Rows - 1; /* put cursor at last row */
9069 else if (Rows > old_Rows) /* Rows got bigger */
9070 msg_row += Rows - old_Rows; /* put cursor in same place */
9071 if (msg_col >= Columns) /* Columns got smaller */
9072 msg_col = Columns - 1; /* put cursor at last column */
9073 }
9074#endif
9075
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009077 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009078
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009079 /*
9080 * Do not apply autocommands more than 3 times to avoid an endless loop
9081 * in case applying autocommands always changes Rows or Columns.
9082 */
9083 if (starting == 0 && ++retry_count <= 3)
9084 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009085 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009086 /* In rare cases, autocommands may have altered Rows or Columns,
9087 * jump back to check if we need to allocate the screen again. */
9088 goto retry;
9089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090}
9091
9092 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009093free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009094{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009095#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009096 int i;
9097
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009098 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009099 for (i = 0; i < Screen_mco; ++i)
9100 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009101 vim_free(ScreenLines2);
9102#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009103 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009104 vim_free(ScreenAttrs);
9105 vim_free(LineOffset);
9106 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009107 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009108}
9109
9110 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009111screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112{
9113 check_for_delay(FALSE);
9114 screenalloc(FALSE); /* allocate screen buffers if size changed */
9115 screenclear2(); /* clear the screen */
9116}
9117
9118 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009119screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
9121 int i;
9122
9123 if (starting == NO_SCREEN || ScreenLines == NULL
9124#ifdef FEAT_GUI
9125 || (gui.in_use && gui.starting)
9126#endif
9127 )
9128 return;
9129
9130#ifdef FEAT_GUI
9131 if (!gui.in_use)
9132#endif
9133 screen_attr = -1; /* force setting the Normal colors */
9134 screen_stop_highlight(); /* don't want highlighting here */
9135
9136#ifdef FEAT_CLIPBOARD
9137 /* disable selection without redrawing it */
9138 clip_scroll_selection(9999);
9139#endif
9140
9141 /* blank out ScreenLines */
9142 for (i = 0; i < Rows; ++i)
9143 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009144 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 LineWraps[i] = FALSE;
9146 }
9147
9148 if (can_clear(T_CL))
9149 {
9150 out_str(T_CL); /* clear the display */
9151 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009152 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 }
9154 else
9155 {
9156 /* can't clear the screen, mark all chars with invalid attributes */
9157 for (i = 0; i < Rows; ++i)
9158 lineinvalid(LineOffset[i], (int)Columns);
9159 clear_cmdline = TRUE;
9160 }
9161
9162 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9163
9164 win_rest_invalid(firstwin);
9165 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009166 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 if (must_redraw == CLEAR) /* no need to clear again */
9168 must_redraw = NOT_VALID;
9169 compute_cmdrow();
9170 msg_row = cmdline_row; /* put cursor on last line for messages */
9171 msg_col = 0;
9172 screen_start(); /* don't know where cursor is now */
9173 msg_scrolled = 0; /* can't scroll back */
9174 msg_didany = FALSE;
9175 msg_didout = FALSE;
9176}
9177
9178/*
9179 * Clear one line in ScreenLines.
9180 */
9181 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009182lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183{
9184 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9185#ifdef FEAT_MBYTE
9186 if (enc_utf8)
9187 (void)vim_memset(ScreenLinesUC + off, 0,
9188 (size_t)width * sizeof(u8char_T));
9189#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009190 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191}
9192
9193/*
9194 * Mark one line in ScreenLines invalid by setting the attributes to an
9195 * invalid value.
9196 */
9197 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009198lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199{
9200 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9201}
9202
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203/*
9204 * Copy part of a Screenline for vertically split window "wp".
9205 */
9206 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009207linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208{
9209 unsigned off_to = LineOffset[to] + wp->w_wincol;
9210 unsigned off_from = LineOffset[from] + wp->w_wincol;
9211
9212 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9213 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009214#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 if (enc_utf8)
9216 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009217 int i;
9218
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9220 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009221 for (i = 0; i < p_mco; ++i)
9222 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9223 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 }
9225 if (enc_dbcs == DBCS_JPNU)
9226 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9227 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9230 wp->w_width * sizeof(sattr_T));
9231}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
9233/*
9234 * Return TRUE if clearing with term string "p" would work.
9235 * It can't work when the string is empty or it won't set the right background.
9236 */
9237 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009238can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239{
9240 return (*p != NUL && (t_colors <= 1
9241#ifdef FEAT_GUI
9242 || gui.in_use
9243#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009244#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009245 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009246 || (!p_tgc && cterm_normal_bg_color == 0)
9247#else
9248 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009249#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009250 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251}
9252
9253/*
9254 * Reset cursor position. Use whenever cursor was moved because of outputting
9255 * something directly to the screen (shell commands) or a terminal control
9256 * code.
9257 */
9258 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009259screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
9261 screen_cur_row = screen_cur_col = 9999;
9262}
9263
9264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 * Move the cursor to position "row","col" in the screen.
9266 * This tries to find the most efficient way to move, minimizing the number of
9267 * characters sent to the terminal.
9268 */
9269 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009270windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009272 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 int i;
9274 int plan;
9275 int cost;
9276 int wouldbe_col;
9277 int noinvcurs;
9278 char_u *bs;
9279 int goto_cost;
9280 int attr;
9281
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009282#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9284
9285#define PLAN_LE 1
9286#define PLAN_CR 2
9287#define PLAN_NL 3
9288#define PLAN_WRITE 4
9289 /* Can't use ScreenLines unless initialized */
9290 if (ScreenLines == NULL)
9291 return;
9292
9293 if (col != screen_cur_col || row != screen_cur_row)
9294 {
9295 /* Check for valid position. */
9296 if (row < 0) /* window without text lines? */
9297 row = 0;
9298 if (row >= screen_Rows)
9299 row = screen_Rows - 1;
9300 if (col >= screen_Columns)
9301 col = screen_Columns - 1;
9302
9303 /* check if no cursor movement is allowed in highlight mode */
9304 if (screen_attr && *T_MS == NUL)
9305 noinvcurs = HIGHL_COST;
9306 else
9307 noinvcurs = 0;
9308 goto_cost = GOTO_COST + noinvcurs;
9309
9310 /*
9311 * Plan how to do the positioning:
9312 * 1. Use CR to move it to column 0, same row.
9313 * 2. Use T_LE to move it a few columns to the left.
9314 * 3. Use NL to move a few lines down, column 0.
9315 * 4. Move a few columns to the right with T_ND or by writing chars.
9316 *
9317 * Don't do this if the cursor went beyond the last column, the cursor
9318 * position is unknown then (some terminals wrap, some don't )
9319 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009320 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 * characters to move the cursor to the right.
9322 */
9323 if (row >= screen_cur_row && screen_cur_col < Columns)
9324 {
9325 /*
9326 * If the cursor is in the same row, bigger col, we can use CR
9327 * or T_LE.
9328 */
9329 bs = NULL; /* init for GCC */
9330 attr = screen_attr;
9331 if (row == screen_cur_row && col < screen_cur_col)
9332 {
9333 /* "le" is preferred over "bc", because "bc" is obsolete */
9334 if (*T_LE)
9335 bs = T_LE; /* "cursor left" */
9336 else
9337 bs = T_BC; /* "backspace character (old) */
9338 if (*bs)
9339 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9340 else
9341 cost = 999;
9342 if (col + 1 < cost) /* using CR is less characters */
9343 {
9344 plan = PLAN_CR;
9345 wouldbe_col = 0;
9346 cost = 1; /* CR is just one character */
9347 }
9348 else
9349 {
9350 plan = PLAN_LE;
9351 wouldbe_col = col;
9352 }
9353 if (noinvcurs) /* will stop highlighting */
9354 {
9355 cost += noinvcurs;
9356 attr = 0;
9357 }
9358 }
9359
9360 /*
9361 * If the cursor is above where we want to be, we can use CR LF.
9362 */
9363 else if (row > screen_cur_row)
9364 {
9365 plan = PLAN_NL;
9366 wouldbe_col = 0;
9367 cost = (row - screen_cur_row) * 2; /* CR LF */
9368 if (noinvcurs) /* will stop highlighting */
9369 {
9370 cost += noinvcurs;
9371 attr = 0;
9372 }
9373 }
9374
9375 /*
9376 * If the cursor is in the same row, smaller col, just use write.
9377 */
9378 else
9379 {
9380 plan = PLAN_WRITE;
9381 wouldbe_col = screen_cur_col;
9382 cost = 0;
9383 }
9384
9385 /*
9386 * Check if any characters that need to be written have the
9387 * correct attributes. Also avoid UTF-8 characters.
9388 */
9389 i = col - wouldbe_col;
9390 if (i > 0)
9391 cost += i;
9392 if (cost < goto_cost && i > 0)
9393 {
9394 /*
9395 * Check if the attributes are correct without additionally
9396 * stopping highlighting.
9397 */
9398 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9399 while (i && *p++ == attr)
9400 --i;
9401 if (i != 0)
9402 {
9403 /*
9404 * Try if it works when highlighting is stopped here.
9405 */
9406 if (*--p == 0)
9407 {
9408 cost += noinvcurs;
9409 while (i && *p++ == 0)
9410 --i;
9411 }
9412 if (i != 0)
9413 cost = 999; /* different attributes, don't do it */
9414 }
9415#ifdef FEAT_MBYTE
9416 if (enc_utf8)
9417 {
9418 /* Don't use an UTF-8 char for positioning, it's slow. */
9419 for (i = wouldbe_col; i < col; ++i)
9420 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9421 {
9422 cost = 999;
9423 break;
9424 }
9425 }
9426#endif
9427 }
9428
9429 /*
9430 * We can do it without term_windgoto()!
9431 */
9432 if (cost < goto_cost)
9433 {
9434 if (plan == PLAN_LE)
9435 {
9436 if (noinvcurs)
9437 screen_stop_highlight();
9438 while (screen_cur_col > col)
9439 {
9440 out_str(bs);
9441 --screen_cur_col;
9442 }
9443 }
9444 else if (plan == PLAN_CR)
9445 {
9446 if (noinvcurs)
9447 screen_stop_highlight();
9448 out_char('\r');
9449 screen_cur_col = 0;
9450 }
9451 else if (plan == PLAN_NL)
9452 {
9453 if (noinvcurs)
9454 screen_stop_highlight();
9455 while (screen_cur_row < row)
9456 {
9457 out_char('\n');
9458 ++screen_cur_row;
9459 }
9460 screen_cur_col = 0;
9461 }
9462
9463 i = col - screen_cur_col;
9464 if (i > 0)
9465 {
9466 /*
9467 * Use cursor-right if it's one character only. Avoids
9468 * removing a line of pixels from the last bold char, when
9469 * using the bold trick in the GUI.
9470 */
9471 if (T_ND[0] != NUL && T_ND[1] == NUL)
9472 {
9473 while (i-- > 0)
9474 out_char(*T_ND);
9475 }
9476 else
9477 {
9478 int off;
9479
9480 off = LineOffset[row] + screen_cur_col;
9481 while (i-- > 0)
9482 {
9483 if (ScreenAttrs[off] != screen_attr)
9484 screen_stop_highlight();
9485#ifdef FEAT_MBYTE
9486 out_flush_check();
9487#endif
9488 out_char(ScreenLines[off]);
9489#ifdef FEAT_MBYTE
9490 if (enc_dbcs == DBCS_JPNU
9491 && ScreenLines[off] == 0x8e)
9492 out_char(ScreenLines2[off]);
9493#endif
9494 ++off;
9495 }
9496 }
9497 }
9498 }
9499 }
9500 else
9501 cost = 999;
9502
9503 if (cost >= goto_cost)
9504 {
9505 if (noinvcurs)
9506 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009507 if (row == screen_cur_row && (col > screen_cur_col)
9508 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 term_cursor_right(col - screen_cur_col);
9510 else
9511 term_windgoto(row, col);
9512 }
9513 screen_cur_row = row;
9514 screen_cur_col = col;
9515 }
9516}
9517
9518/*
9519 * Set cursor to its position in the current window.
9520 */
9521 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009522setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009524 setcursor_mayforce(FALSE);
9525}
9526
9527/*
9528 * Set cursor to its position in the current window.
9529 * When "force" is TRUE also when not redrawing.
9530 */
9531 void
9532setcursor_mayforce(int force)
9533{
9534 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 {
9536 validate_cursor();
9537 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009538 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009540 /* With 'rightleft' set and the cursor on a double-wide
9541 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009542 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009544 (has_mbyte
9545 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9546 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547# endif
9548 1)) :
9549#endif
9550 curwin->w_wcol));
9551 }
9552}
9553
9554
9555/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009556 * Insert 'line_count' lines at 'row' in window 'wp'.
9557 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9558 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 * scrolling.
9560 * Returns FAIL if the lines are not inserted, OK for success.
9561 */
9562 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009563win_ins_lines(
9564 win_T *wp,
9565 int row,
9566 int line_count,
9567 int invalid,
9568 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569{
9570 int did_delete;
9571 int nextrow;
9572 int lastrow;
9573 int retval;
9574
9575 if (invalid)
9576 wp->w_lines_valid = 0;
9577
9578 if (wp->w_height < 5)
9579 return FAIL;
9580
9581 if (line_count > wp->w_height - row)
9582 line_count = wp->w_height - row;
9583
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009584 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 if (retval != MAYBE)
9586 return retval;
9587
9588 /*
9589 * If there is a next window or a status line, we first try to delete the
9590 * lines at the bottom to avoid messing what is after the window.
9591 * If this fails and there are following windows, don't do anything to avoid
9592 * messing up those windows, better just redraw.
9593 */
9594 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 if (wp->w_next != NULL || wp->w_status_height)
9596 {
9597 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009598 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 did_delete = TRUE;
9600 else if (wp->w_next)
9601 return FAIL;
9602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 /*
9604 * if no lines deleted, blank the lines that will end up below the window
9605 */
9606 if (!did_delete)
9607 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009610 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 lastrow = nextrow + line_count;
9612 if (lastrow > Rows)
9613 lastrow = Rows;
9614 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009615 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 ' ', ' ', 0);
9617 }
9618
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009619 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 == FAIL)
9621 {
9622 /* deletion will have messed up other windows */
9623 if (did_delete)
9624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 win_rest_invalid(W_NEXT(wp));
9627 }
9628 return FAIL;
9629 }
9630
9631 return OK;
9632}
9633
9634/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009635 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9637 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9638 * scrolling
9639 * Return OK for success, FAIL if the lines are not deleted.
9640 */
9641 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009642win_del_lines(
9643 win_T *wp,
9644 int row,
9645 int line_count,
9646 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009647 int mayclear,
9648 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649{
9650 int retval;
9651
9652 if (invalid)
9653 wp->w_lines_valid = 0;
9654
9655 if (line_count > wp->w_height - row)
9656 line_count = wp->w_height - row;
9657
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009658 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 if (retval != MAYBE)
9660 return retval;
9661
9662 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009663 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 return FAIL;
9665
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 /*
9667 * If there are windows or status lines below, try to put them at the
9668 * correct place. If we can't do that, they have to be redrawn.
9669 */
9670 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9671 {
9672 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009673 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 {
9675 wp->w_redr_status = TRUE;
9676 win_rest_invalid(wp->w_next);
9677 }
9678 }
9679 /*
9680 * If this is the last window and there is no status line, redraw the
9681 * command line later.
9682 */
9683 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 redraw_cmdline = TRUE;
9685 return OK;
9686}
9687
9688/*
9689 * Common code for win_ins_lines() and win_del_lines().
9690 * Returns OK or FAIL when the work has been done.
9691 * Returns MAYBE when not finished yet.
9692 */
9693 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009694win_do_lines(
9695 win_T *wp,
9696 int row,
9697 int line_count,
9698 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009699 int del,
9700 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
9702 int retval;
9703
9704 if (!redrawing() || line_count <= 0)
9705 return FAIL;
9706
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009707 /* When inserting lines would result in loss of command output, just redraw
9708 * the lines. */
9709 if (no_win_do_lines_ins && !del)
9710 return FAIL;
9711
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009713 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009715 if (!no_win_do_lines_ins)
9716 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 return FAIL;
9718 }
9719
9720 /*
9721 * Delete all remaining lines
9722 */
9723 if (row + line_count >= wp->w_height)
9724 {
9725 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009726 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 ' ', ' ', 0);
9728 return OK;
9729 }
9730
9731 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009732 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009734 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009736 if (!no_win_do_lines_ins)
9737 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738
9739 /*
9740 * If the terminal can set a scroll region, use that.
9741 * Always do this in a vertically split window. This will redraw from
9742 * ScreenLines[] when t_CV isn't defined. That's faster than using
9743 * win_line().
9744 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009745 * a character in the lower right corner of the scroll region may cause a
9746 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009748 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 scroll_region_set(wp, row);
9752 if (del)
9753 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009754 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 else
9756 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009757 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 scroll_region_reset();
9760 return retval;
9761 }
9762
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9764 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765
9766 return MAYBE;
9767}
9768
9769/*
9770 * window 'wp' and everything after it is messed up, mark it for redraw
9771 */
9772 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009773win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 {
9777 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 wp->w_redr_status = TRUE;
9779 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 }
9781 redraw_cmdline = TRUE;
9782}
9783
9784/*
9785 * The rest of the routines in this file perform screen manipulations. The
9786 * given operation is performed physically on the screen. The corresponding
9787 * change is also made to the internal screen image. In this way, the editor
9788 * anticipates the effect of editing changes on the appearance of the screen.
9789 * That way, when we call screenupdate a complete redraw isn't usually
9790 * necessary. Another advantage is that we can keep adding code to anticipate
9791 * screen changes, and in the meantime, everything still works.
9792 */
9793
9794/*
9795 * types for inserting or deleting lines
9796 */
9797#define USE_T_CAL 1
9798#define USE_T_CDL 2
9799#define USE_T_AL 3
9800#define USE_T_CE 4
9801#define USE_T_DL 5
9802#define USE_T_SR 6
9803#define USE_NL 7
9804#define USE_T_CD 8
9805#define USE_REDRAW 9
9806
9807/*
9808 * insert lines on the screen and update ScreenLines[]
9809 * 'end' is the line after the scrolled part. Normally it is Rows.
9810 * When scrolling region used 'off' is the offset from the top for the region.
9811 * 'row' and 'end' are relative to the start of the region.
9812 *
9813 * return FAIL for failure, OK for success.
9814 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009815 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009816screen_ins_lines(
9817 int off,
9818 int row,
9819 int line_count,
9820 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009821 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009822 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823{
9824 int i;
9825 int j;
9826 unsigned temp;
9827 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009828 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 int type;
9830 int result_empty;
9831 int can_ce = can_clear(T_CE);
9832
9833 /*
9834 * FAIL if
9835 * - there is no valid screen
9836 * - the screen has to be redrawn completely
9837 * - the line count is less than one
9838 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009839 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009841 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9842#ifdef FEAT_CLIPBOARD
9843 || (clip_star.state != SELECT_CLEARED
9844 && redrawing_for_callback > 0)
9845#endif
9846 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 return FAIL;
9848
9849 /*
9850 * There are seven ways to insert lines:
9851 * 0. When in a vertically split window and t_CV isn't set, redraw the
9852 * characters from ScreenLines[].
9853 * 1. Use T_CD (clear to end of display) if it exists and the result of
9854 * the insert is just empty lines
9855 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9856 * present or line_count > 1. It looks better if we do all the inserts
9857 * at once.
9858 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9859 * insert is just empty lines and T_CE is not present or line_count >
9860 * 1.
9861 * 4. Use T_AL (insert line) if it exists.
9862 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9863 * just empty lines.
9864 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9865 * just empty lines.
9866 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9867 * the 'da' flag is not set or we have clear line capability.
9868 * 8. redraw the characters from ScreenLines[].
9869 *
9870 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9871 * the scrollbar for the window. It does have insert line, use that if it
9872 * exists.
9873 */
9874 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9876 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009877 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 type = USE_T_CD;
9879 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9880 type = USE_T_CAL;
9881 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9882 type = USE_T_CDL;
9883 else if (*T_AL != NUL)
9884 type = USE_T_AL;
9885 else if (can_ce && result_empty)
9886 type = USE_T_CE;
9887 else if (*T_DL != NUL && result_empty)
9888 type = USE_T_DL;
9889 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9890 type = USE_T_SR;
9891 else
9892 return FAIL;
9893
9894 /*
9895 * For clearing the lines screen_del_lines() is used. This will also take
9896 * care of t_db if necessary.
9897 */
9898 if (type == USE_T_CD || type == USE_T_CDL ||
9899 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009900 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901
9902 /*
9903 * If text is retained below the screen, first clear or delete as many
9904 * lines at the bottom of the window as are about to be inserted so that
9905 * the deleted lines won't later surface during a screen_del_lines.
9906 */
9907 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009908 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909
9910#ifdef FEAT_CLIPBOARD
9911 /* Remove a modeless selection when inserting lines halfway the screen
9912 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009913 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009914 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 else
9916 clip_scroll_selection(-line_count);
9917#endif
9918
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919#ifdef FEAT_GUI
9920 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9921 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009922 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923#endif
9924
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009925 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9926 cursor_col = wp->w_wincol;
9927
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 if (*T_CCS != NUL) /* cursor relative to region */
9929 cursor_row = row;
9930 else
9931 cursor_row = row + off;
9932
9933 /*
9934 * Shift LineOffset[] line_count down to reflect the inserted lines.
9935 * Clear the inserted lines in ScreenLines[].
9936 */
9937 row += off;
9938 end += off;
9939 for (i = 0; i < line_count; ++i)
9940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 if (wp != NULL && wp->w_width != Columns)
9942 {
9943 /* need to copy part of a line */
9944 j = end - 1 - i;
9945 while ((j -= line_count) >= row)
9946 linecopy(j + line_count, j, wp);
9947 j += line_count;
9948 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009949 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9950 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 else
9952 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9953 LineWraps[j] = FALSE;
9954 }
9955 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 {
9957 j = end - 1 - i;
9958 temp = LineOffset[j];
9959 while ((j -= line_count) >= row)
9960 {
9961 LineOffset[j + line_count] = LineOffset[j];
9962 LineWraps[j + line_count] = LineWraps[j];
9963 }
9964 LineOffset[j + line_count] = temp;
9965 LineWraps[j + line_count] = FALSE;
9966 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009967 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 else
9969 lineinvalid(temp, (int)Columns);
9970 }
9971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972
9973 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009974 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009975 if (clear_attr != 0)
9976 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 /* redraw the characters */
9979 if (type == USE_REDRAW)
9980 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009981 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 {
9983 term_append_lines(line_count);
9984 screen_start(); /* don't know where cursor is now */
9985 }
9986 else
9987 {
9988 for (i = 0; i < line_count; i++)
9989 {
9990 if (type == USE_T_AL)
9991 {
9992 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009993 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 out_str(T_AL);
9995 }
9996 else /* type == USE_T_SR */
9997 out_str(T_SR);
9998 screen_start(); /* don't know where cursor is now */
9999 }
10000 }
10001
10002 /*
10003 * With scroll-reverse and 'da' flag set we need to clear the lines that
10004 * have been scrolled down into the region.
10005 */
10006 if (type == USE_T_SR && *T_DA)
10007 {
10008 for (i = 0; i < line_count; ++i)
10009 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010010 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 out_str(T_CE);
10012 screen_start(); /* don't know where cursor is now */
10013 }
10014 }
10015
10016#ifdef FEAT_GUI
10017 gui_can_update_cursor();
10018 if (gui.in_use)
10019 out_flush(); /* always flush after a scroll */
10020#endif
10021 return OK;
10022}
10023
10024/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010025 * Delete lines on the screen and update ScreenLines[].
10026 * "end" is the line after the scrolled part. Normally it is Rows.
10027 * When scrolling region used "off" is the offset from the top for the region.
10028 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 *
10030 * Return OK for success, FAIL if the lines are not deleted.
10031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010033screen_del_lines(
10034 int off,
10035 int row,
10036 int line_count,
10037 int end,
10038 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010039 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010040 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041{
10042 int j;
10043 int i;
10044 unsigned temp;
10045 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010046 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 int cursor_end;
10048 int result_empty; /* result is empty until end of region */
10049 int can_delete; /* deleting line codes can be used */
10050 int type;
10051
10052 /*
10053 * FAIL if
10054 * - there is no valid screen
10055 * - the screen has to be redrawn completely
10056 * - the line count is less than one
10057 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010058 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010060 if (!screen_valid(TRUE) || line_count <= 0
10061 || (!force && line_count > p_ttyscroll)
10062#ifdef FEAT_CLIPBOARD
10063 || (clip_star.state != SELECT_CLEARED
10064 && redrawing_for_callback > 0)
10065#endif
10066 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 return FAIL;
10068
10069 /*
10070 * Check if the rest of the current region will become empty.
10071 */
10072 result_empty = row + line_count >= end;
10073
10074 /*
10075 * We can delete lines only when 'db' flag not set or when 'ce' option
10076 * available.
10077 */
10078 can_delete = (*T_DB == NUL || can_clear(T_CE));
10079
10080 /*
10081 * There are six ways to delete lines:
10082 * 0. When in a vertically split window and t_CV isn't set, redraw the
10083 * characters from ScreenLines[].
10084 * 1. Use T_CD if it exists and the result is empty.
10085 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10086 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10087 * none of the other ways work.
10088 * 4. Use T_CE (erase line) if the result is empty.
10089 * 5. Use T_DL (delete line) if it exists.
10090 * 6. redraw the characters from ScreenLines[].
10091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10093 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010094 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 type = USE_T_CD;
10096#if defined(__BEOS__) && defined(BEOS_DR8)
10097 /*
10098 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10099 * its internal termcap... this works okay for tests which test *T_DB !=
10100 * NUL. It has the disadvantage that the user cannot use any :set t_*
10101 * command to get T_DB (back) to empty_option, only :set term=... will do
10102 * the trick...
10103 * Anyway, this hack will hopefully go away with the next OS release.
10104 * (Olaf Seibert)
10105 */
10106 else if (row == 0 && T_DB == empty_option
10107 && (line_count == 1 || *T_CDL == NUL))
10108#else
10109 else if (row == 0 && (
10110#ifndef AMIGA
10111 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10112 * up, so use delete-line command */
10113 line_count == 1 ||
10114#endif
10115 *T_CDL == NUL))
10116#endif
10117 type = USE_NL;
10118 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10119 type = USE_T_CDL;
10120 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010121 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 type = USE_T_CE;
10123 else if (*T_DL != NUL && can_delete)
10124 type = USE_T_DL;
10125 else if (*T_CDL != NUL && can_delete)
10126 type = USE_T_CDL;
10127 else
10128 return FAIL;
10129
10130#ifdef FEAT_CLIPBOARD
10131 /* Remove a modeless selection when deleting lines halfway the screen or
10132 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010133 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010134 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 else
10136 clip_scroll_selection(line_count);
10137#endif
10138
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139#ifdef FEAT_GUI
10140 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10141 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010142 gui_dont_update_cursor(gui.cursor_row >= row + off
10143 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144#endif
10145
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010146 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10147 cursor_col = wp->w_wincol;
10148
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 if (*T_CCS != NUL) /* cursor relative to region */
10150 {
10151 cursor_row = row;
10152 cursor_end = end;
10153 }
10154 else
10155 {
10156 cursor_row = row + off;
10157 cursor_end = end + off;
10158 }
10159
10160 /*
10161 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10162 * Clear the inserted lines in ScreenLines[].
10163 */
10164 row += off;
10165 end += off;
10166 for (i = 0; i < line_count; ++i)
10167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 if (wp != NULL && wp->w_width != Columns)
10169 {
10170 /* need to copy part of a line */
10171 j = row + i;
10172 while ((j += line_count) <= end - 1)
10173 linecopy(j - line_count, j, wp);
10174 j -= line_count;
10175 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010176 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10177 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 else
10179 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10180 LineWraps[j] = FALSE;
10181 }
10182 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 {
10184 /* whole width, moving the line pointers is faster */
10185 j = row + i;
10186 temp = LineOffset[j];
10187 while ((j += line_count) <= end - 1)
10188 {
10189 LineOffset[j - line_count] = LineOffset[j];
10190 LineWraps[j - line_count] = LineWraps[j];
10191 }
10192 LineOffset[j - line_count] = temp;
10193 LineWraps[j - line_count] = FALSE;
10194 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010195 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 else
10197 lineinvalid(temp, (int)Columns);
10198 }
10199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010201 if (screen_attr != clear_attr)
10202 screen_stop_highlight();
10203 if (clear_attr != 0)
10204 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 /* redraw the characters */
10207 if (type == USE_REDRAW)
10208 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010209 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010211 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 out_str(T_CD);
10213 screen_start(); /* don't know where cursor is now */
10214 }
10215 else if (type == USE_T_CDL)
10216 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010217 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 term_delete_lines(line_count);
10219 screen_start(); /* don't know where cursor is now */
10220 }
10221 /*
10222 * Deleting lines at top of the screen or scroll region: Just scroll
10223 * the whole screen (scroll region) up by outputting newlines on the
10224 * last line.
10225 */
10226 else if (type == USE_NL)
10227 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010228 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 for (i = line_count; --i >= 0; )
10230 out_char('\n'); /* cursor will remain on same line */
10231 }
10232 else
10233 {
10234 for (i = line_count; --i >= 0; )
10235 {
10236 if (type == USE_T_DL)
10237 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010238 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 out_str(T_DL); /* delete a line */
10240 }
10241 else /* type == USE_T_CE */
10242 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010243 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 out_str(T_CE); /* erase a line */
10245 }
10246 screen_start(); /* don't know where cursor is now */
10247 }
10248 }
10249
10250 /*
10251 * If the 'db' flag is set, we need to clear the lines that have been
10252 * scrolled up at the bottom of the region.
10253 */
10254 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10255 {
10256 for (i = line_count; i > 0; --i)
10257 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010258 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 out_str(T_CE); /* erase a line */
10260 screen_start(); /* don't know where cursor is now */
10261 }
10262 }
10263
10264#ifdef FEAT_GUI
10265 gui_can_update_cursor();
10266 if (gui.in_use)
10267 out_flush(); /* always flush after a scroll */
10268#endif
10269
10270 return OK;
10271}
10272
10273/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010274 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 *
10276 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10277 * If clear_cmdline is FALSE there may be a message there that needs to be
10278 * cleared only if a mode is shown.
10279 * Return the length of the message (0 if no message).
10280 */
10281 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010282showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283{
10284 int need_clear;
10285 int length = 0;
10286 int do_mode;
10287 int attr;
10288 int nwr_save;
10289#ifdef FEAT_INS_EXPAND
10290 int sub_attr;
10291#endif
10292
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010293 do_mode = ((p_smd && msg_silent == 0)
10294 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010295 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010296 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010297 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 {
10299 /*
10300 * Don't show mode right now, when not redrawing or inside a mapping.
10301 * Call char_avail() only when we are going to show something, because
10302 * it takes a bit of time.
10303 */
10304 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10305 {
10306 redraw_cmdline = TRUE; /* show mode later */
10307 return 0;
10308 }
10309
10310 nwr_save = need_wait_return;
10311
10312 /* wait a bit before overwriting an important message */
10313 check_for_delay(FALSE);
10314
10315 /* if the cmdline is more than one line high, erase top lines */
10316 need_clear = clear_cmdline;
10317 if (clear_cmdline && cmdline_row < Rows - 1)
10318 msg_clr_cmdline(); /* will reset clear_cmdline */
10319
10320 /* Position on the last line in the window, column 0 */
10321 msg_pos_mode();
10322 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010323 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 if (do_mode)
10325 {
10326 MSG_PUTS_ATTR("--", attr);
10327#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010328 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010329# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010330 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010331# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010332 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010333# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010334 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010335# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 MSG_PUTS_ATTR(" IM", attr);
10337# else
10338 MSG_PUTS_ATTR(" XIM", attr);
10339# endif
10340#endif
10341#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10342 if (gui.in_use)
10343 {
10344 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010345 {
10346 /* HANGUL */
10347 if (enc_utf8)
10348 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10349 else
10350 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 }
10353#endif
10354#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010355 /* CTRL-X in Insert mode */
10356 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 {
10358 /* These messages can get long, avoid a wrap in a narrow
10359 * window. Prefer showing edit_submode_extra. */
10360 length = (Rows - msg_row) * Columns - 3;
10361 if (edit_submode_extra != NULL)
10362 length -= vim_strsize(edit_submode_extra);
10363 if (length > 0)
10364 {
10365 if (edit_submode_pre != NULL)
10366 length -= vim_strsize(edit_submode_pre);
10367 if (length - vim_strsize(edit_submode) > 0)
10368 {
10369 if (edit_submode_pre != NULL)
10370 msg_puts_attr(edit_submode_pre, attr);
10371 msg_puts_attr(edit_submode, attr);
10372 }
10373 if (edit_submode_extra != NULL)
10374 {
10375 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10376 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010377 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 else
10379 sub_attr = attr;
10380 msg_puts_attr(edit_submode_extra, sub_attr);
10381 }
10382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 }
10384 else
10385#endif
10386 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 if (State & VREPLACE_FLAG)
10388 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010389 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10391 else if (State & INSERT)
10392 {
10393#ifdef FEAT_RIGHTLEFT
10394 if (p_ri)
10395 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10396#endif
10397 MSG_PUTS_ATTR(_(" INSERT"), attr);
10398 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010399 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 MSG_PUTS_ATTR(_(" (insert)"), attr);
10401 else if (restart_edit == 'R')
10402 MSG_PUTS_ATTR(_(" (replace)"), attr);
10403 else if (restart_edit == 'V')
10404 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10405#ifdef FEAT_RIGHTLEFT
10406 if (p_hkmap)
10407 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10408# ifdef FEAT_FKMAP
10409 if (p_fkmap)
10410 MSG_PUTS_ATTR(farsi_text_5, attr);
10411# endif
10412#endif
10413#ifdef FEAT_KEYMAP
10414 if (State & LANGMAP)
10415 {
10416# ifdef FEAT_ARABIC
10417 if (curwin->w_p_arab)
10418 MSG_PUTS_ATTR(_(" Arabic"), attr);
10419 else
10420# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010421 if (get_keymap_str(curwin, (char_u *)" (%s)",
10422 NameBuff, MAXPATHL))
10423 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 }
10425#endif
10426 if ((State & INSERT) && p_paste)
10427 MSG_PUTS_ATTR(_(" (paste)"), attr);
10428
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 if (VIsual_active)
10430 {
10431 char *p;
10432
10433 /* Don't concatenate separate words to avoid translation
10434 * problems. */
10435 switch ((VIsual_select ? 4 : 0)
10436 + (VIsual_mode == Ctrl_V) * 2
10437 + (VIsual_mode == 'V'))
10438 {
10439 case 0: p = N_(" VISUAL"); break;
10440 case 1: p = N_(" VISUAL LINE"); break;
10441 case 2: p = N_(" VISUAL BLOCK"); break;
10442 case 4: p = N_(" SELECT"); break;
10443 case 5: p = N_(" SELECT LINE"); break;
10444 default: p = N_(" SELECT BLOCK"); break;
10445 }
10446 MSG_PUTS_ATTR(_(p), attr);
10447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 MSG_PUTS_ATTR(" --", attr);
10449 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010450
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 need_clear = TRUE;
10452 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010453 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454#ifdef FEAT_INS_EXPAND
10455 && edit_submode == NULL /* otherwise it gets too long */
10456#endif
10457 )
10458 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010459 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 need_clear = TRUE;
10461 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010462
10463 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 if (need_clear || clear_cmdline)
10465 msg_clr_eos();
10466 msg_didout = FALSE; /* overwrite this message */
10467 length = msg_col;
10468 msg_col = 0;
10469 need_wait_return = nwr_save; /* never ask for hit-return for this */
10470 }
10471 else if (clear_cmdline && msg_silent == 0)
10472 /* Clear the whole command line. Will reset "clear_cmdline". */
10473 msg_clr_cmdline();
10474
10475#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 /* In Visual mode the size of the selected area must be redrawn. */
10477 if (VIsual_active)
10478 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479
10480 /* If the last window has no status line, the ruler is after the mode
10481 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010482 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010483 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484#endif
10485 redraw_cmdline = FALSE;
10486 clear_cmdline = FALSE;
10487
10488 return length;
10489}
10490
10491/*
10492 * Position for a mode message.
10493 */
10494 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010495msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496{
10497 msg_col = 0;
10498 msg_row = Rows - 1;
10499}
10500
10501/*
10502 * Delete mode message. Used when ESC is typed which is expected to end
10503 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010504 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 */
10506 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010507unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
10509 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010510 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 */
10512 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10513 redraw_cmdline = TRUE; /* delete mode later */
10514 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010515 clearmode();
10516}
10517
10518/*
10519 * Clear the mode message.
10520 */
10521 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010522clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010523{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010524 int save_msg_row = msg_row;
10525 int save_msg_col = msg_col;
10526
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010527 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010528 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010529 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010530 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010531
10532 msg_col = save_msg_col;
10533 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534}
10535
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010536 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010537recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010538{
10539 MSG_PUTS_ATTR(_("recording"), attr);
10540 if (!shortmess(SHM_RECORDING))
10541 {
10542 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010543 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010544 MSG_PUTS_ATTR(s, attr);
10545 }
10546}
10547
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010548/*
10549 * Draw the tab pages line at the top of the Vim window.
10550 */
10551 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010552draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010553{
10554 int tabcount = 0;
10555 tabpage_T *tp;
10556 int tabwidth;
10557 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010558 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010559 int attr;
10560 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010561 win_T *cwp;
10562 int wincount;
10563 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010564 int c;
10565 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010566 int attr_sel = HL_ATTR(HLF_TPS);
10567 int attr_nosel = HL_ATTR(HLF_TP);
10568 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010569 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010570 int room;
10571 int use_sep_chars = (t_colors < 8
10572#ifdef FEAT_GUI
10573 && !gui.in_use
10574#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010575#ifdef FEAT_TERMGUICOLORS
10576 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010577#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010578 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010579
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010580 if (ScreenLines == NULL)
10581 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010582 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010583
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010584#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010585 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010586 if (gui_use_tabline())
10587 {
10588 gui_update_tabline();
10589 return;
10590 }
10591#endif
10592
10593 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010594 return;
10595
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010596#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010597
10598 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10599 for (scol = 0; scol < Columns; ++scol)
10600 TabPageIdxs[scol] = 0;
10601
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010602 /* Use the 'tabline' option if it's set. */
10603 if (*p_tal != NUL)
10604 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010605 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010606
10607 /* Check for an error. If there is one we would loop in redrawing the
10608 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010609 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010610 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010611 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010612 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010613 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010614 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010615 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010616 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010617#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010618 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010619 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010620 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010621
Bram Moolenaar238a5642006-02-21 22:12:05 +000010622 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10623 if (tabwidth < 6)
10624 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010625
Bram Moolenaar238a5642006-02-21 22:12:05 +000010626 attr = attr_nosel;
10627 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010628 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010629 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10630 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010631 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010632 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010633
Bram Moolenaar238a5642006-02-21 22:12:05 +000010634 if (tp->tp_topframe == topframe)
10635 attr = attr_sel;
10636 if (use_sep_chars && col > 0)
10637 screen_putchar('|', 0, col++, attr);
10638
10639 if (tp->tp_topframe != topframe)
10640 attr = attr_nosel;
10641
10642 screen_putchar(' ', 0, col++, attr);
10643
10644 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010645 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010646 cwp = curwin;
10647 wp = firstwin;
10648 }
10649 else
10650 {
10651 cwp = tp->tp_curwin;
10652 wp = tp->tp_firstwin;
10653 }
10654
10655 modified = FALSE;
10656 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10657 if (bufIsChanged(wp->w_buffer))
10658 modified = TRUE;
10659 if (modified || wincount > 1)
10660 {
10661 if (wincount > 1)
10662 {
10663 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010664 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010665 if (col + len >= Columns - 3)
10666 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010667 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010668#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010669 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010670#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010671 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010672#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010673 );
10674 col += len;
10675 }
10676 if (modified)
10677 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10678 screen_putchar(' ', 0, col++, attr);
10679 }
10680
10681 room = scol - col + tabwidth - 1;
10682 if (room > 0)
10683 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010684 /* Get buffer name in NameBuff[] */
10685 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010686 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010687 len = vim_strsize(NameBuff);
10688 p = NameBuff;
10689#ifdef FEAT_MBYTE
10690 if (has_mbyte)
10691 while (len > room)
10692 {
10693 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010694 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010695 }
10696 else
10697#endif
10698 if (len > room)
10699 {
10700 p += len - room;
10701 len = room;
10702 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010703 if (len > Columns - col - 1)
10704 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010705
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010706 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010707 col += len;
10708 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010709 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010710
10711 /* Store the tab page number in TabPageIdxs[], so that
10712 * jump_to_mouse() knows where each one is. */
10713 ++tabcount;
10714 while (scol < col)
10715 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010716 }
10717
Bram Moolenaar238a5642006-02-21 22:12:05 +000010718 if (use_sep_chars)
10719 c = '_';
10720 else
10721 c = ' ';
10722 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010723
10724 /* Put an "X" for closing the current tab if there are several. */
10725 if (first_tabpage->tp_next != NULL)
10726 {
10727 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10728 TabPageIdxs[Columns - 1] = -999;
10729 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010730 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010731
10732 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10733 * set. */
10734 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010735}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010736
10737/*
10738 * Get buffer name for "buf" into NameBuff[].
10739 * Takes care of special buffer names and translates special characters.
10740 */
10741 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010742get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010743{
10744 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010745 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010746 else
10747 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10748 trans_characters(NameBuff, MAXPATHL);
10749}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010750
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751/*
10752 * Get the character to use in a status line. Get its attributes in "*attr".
10753 */
10754 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010755fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756{
10757 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010758
10759#ifdef FEAT_TERMINAL
10760 if (bt_terminal(wp->w_buffer))
10761 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010762 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010763 {
10764 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010765 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010766 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010767 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010768 {
10769 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010770 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010771 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010772 }
10773 else
10774#endif
10775 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010777 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 fill = fill_stl;
10779 }
10780 else
10781 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010782 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 fill = fill_stlnc;
10784 }
10785 /* Use fill when there is highlighting, and highlighting of current
10786 * window differs, or the fillchars differ, or this is not the
10787 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010788 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010789 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 || (fill_stl != fill_stlnc)))
10791 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010792 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 return '^';
10794 return '=';
10795}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797/*
10798 * Get the character to use in a separator between vertically split windows.
10799 * Get its attributes in "*attr".
10800 */
10801 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010802fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010804 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 if (*attr == 0 && fill_vert == ' ')
10806 return '|';
10807 else
10808 return fill_vert;
10809}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810
10811/*
10812 * Return TRUE if redrawing should currently be done.
10813 */
10814 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010815redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010817#ifdef FEAT_EVAL
10818 if (disable_redraw_for_testing)
10819 return 0;
10820 else
10821#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010822 return ((!RedrawingDisabled
10823#ifdef FEAT_EVAL
10824 || ignore_redraw_flag_for_testing
10825#endif
10826 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827}
10828
10829/*
10830 * Return TRUE if printing messages should currently be done.
10831 */
10832 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010833messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834{
10835 return (!(p_lz && char_avail() && !KeyTyped));
10836}
10837
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010838#ifdef FEAT_MENU
10839/*
10840 * Draw the window toolbar.
10841 */
10842 static void
10843redraw_win_toolbar(win_T *wp)
10844{
10845 vimmenu_T *menu;
10846 int item_idx = 0;
10847 int item_count = 0;
10848 int col = 0;
10849 int next_col;
10850 int off = (int)(current_ScreenLine - ScreenLines);
10851 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10852 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10853
10854 vim_free(wp->w_winbar_items);
10855 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10856 ++item_count;
10857 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10858 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10859
10860 /* TODO: use fewer spaces if there is not enough room */
10861 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010862 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010863 {
10864 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010865 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010866 break;
10867 if (col > 1)
10868 {
10869 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010870 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010871 break;
10872 }
10873
10874 wp->w_winbar_items[item_idx].wb_startcol = col;
10875 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010876 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010877 break;
10878
10879 next_col = text_to_screenline(wp, menu->name, col);
10880 while (col < next_col)
10881 {
10882 ScreenAttrs[off + col] = button_attr;
10883 ++col;
10884 }
10885 wp->w_winbar_items[item_idx].wb_endcol = col;
10886 wp->w_winbar_items[item_idx].wb_menu = menu;
10887 ++item_idx;
10888
Bram Moolenaar02631462017-09-22 15:20:32 +020010889 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010890 break;
10891 space_to_screenline(off + col, button_attr);
10892 ++col;
10893 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010894 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010895 {
10896 space_to_screenline(off + col, fill_attr);
10897 ++col;
10898 }
10899 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10900
Bram Moolenaar02631462017-09-22 15:20:32 +020010901 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10902 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010903}
10904#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010905
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906/*
10907 * Show current status info in ruler and various other places
10908 * If always is FALSE, only show ruler if position has changed.
10909 */
10910 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010911showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912{
10913 if (!always && !redrawing())
10914 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010915#ifdef FEAT_INS_EXPAND
10916 if (pum_visible())
10917 {
10918 /* Don't redraw right now, do it later. */
10919 curwin->w_redr_status = TRUE;
10920 return;
10921 }
10922#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010923#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010924 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010925 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010926 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 else
10929#endif
10930#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010931 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932#endif
10933
10934#ifdef FEAT_TITLE
10935 if (need_maketitle
10936# ifdef FEAT_STL_OPT
10937 || (p_icon && (stl_syntax & STL_IN_ICON))
10938 || (p_title && (stl_syntax & STL_IN_TITLE))
10939# endif
10940 )
10941 maketitle();
10942#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010943 /* Redraw the tab pages line if needed. */
10944 if (redraw_tabline)
10945 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946}
10947
10948#ifdef FEAT_CMDL_INFO
10949 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010950win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010952#define RULER_BUF_LEN 70
10953 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 int row;
10955 int fillchar;
10956 int attr;
10957 int empty_line = FALSE;
10958 colnr_T virtcol;
10959 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010960 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 int this_ru_col;
10963 int off = 0;
10964 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965
10966 /* If 'ruler' off or redrawing disabled, don't do anything */
10967 if (!p_ru)
10968 return;
10969
10970 /*
10971 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10972 * after deleting lines, before cursor.lnum is corrected.
10973 */
10974 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10975 return;
10976
10977#ifdef FEAT_INS_EXPAND
10978 /* Don't draw the ruler while doing insert-completion, it might overwrite
10979 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 if (edit_submode != NULL)
10982 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010983 // Don't draw the ruler when the popup menu is visible, it may overlap.
10984 // Except when the popup menu will be redrawn anyway.
10985 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010986 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987#endif
10988
10989#ifdef FEAT_STL_OPT
10990 if (*p_ruf)
10991 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010992 int save_called_emsg = called_emsg;
10993
10994 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010996 if (called_emsg)
10997 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010998 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010999 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 return;
11001 }
11002#endif
11003
11004 /*
11005 * Check if not in Insert mode and the line is empty (will show "0-1").
11006 */
11007 if (!(State & INSERT)
11008 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11009 empty_line = TRUE;
11010
11011 /*
11012 * Only draw the ruler when something changed.
11013 */
11014 validate_virtcol_win(wp);
11015 if ( redraw_cmdline
11016 || always
11017 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11018 || wp->w_cursor.col != wp->w_ru_cursor.col
11019 || wp->w_virtcol != wp->w_ru_virtcol
11020#ifdef FEAT_VIRTUALEDIT
11021 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11022#endif
11023 || wp->w_topline != wp->w_ru_topline
11024 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11025#ifdef FEAT_DIFF
11026 || wp->w_topfill != wp->w_ru_topfill
11027#endif
11028 || empty_line != wp->w_ru_empty)
11029 {
11030 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 if (wp->w_status_height)
11032 {
11033 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011034 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011035 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011036 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 }
11038 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 {
11040 row = Rows - 1;
11041 fillchar = ' ';
11042 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 width = Columns;
11044 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 }
11046
11047 /* In list mode virtcol needs to be recomputed */
11048 virtcol = wp->w_virtcol;
11049 if (wp->w_p_list && lcs_tab1 == NUL)
11050 {
11051 wp->w_p_list = FALSE;
11052 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11053 wp->w_p_list = TRUE;
11054 }
11055
11056 /*
11057 * Some sprintfs return the length, some return a pointer.
11058 * To avoid portability problems we use strlen() here.
11059 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011060 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11062 ? 0L
11063 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011064 len = STRLEN(buffer);
11065 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11067 (int)virtcol + 1);
11068
11069 /*
11070 * Add a "50%" if there is room for it.
11071 * On the last line, don't print in the last column (scrolls the
11072 * screen up on some terminals).
11073 */
11074 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011075 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 this_ru_col = ru_col - (Columns - width);
11080 if (this_ru_col < 0)
11081 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 /* Never use more than half the window/screen width, leave the other
11083 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011084 if (this_ru_col < (width + 1) / 2)
11085 this_ru_col = (width + 1) / 2;
11086 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011088 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011089 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 {
11091#ifdef FEAT_MBYTE
11092 if (has_mbyte)
11093 i += (*mb_char2bytes)(fillchar, buffer + i);
11094 else
11095#endif
11096 buffer[i++] = fillchar;
11097 ++o;
11098 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011099 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 }
11101 /* Truncate at window boundary. */
11102#ifdef FEAT_MBYTE
11103 if (has_mbyte)
11104 {
11105 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011106 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 {
11108 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011109 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 {
11111 buffer[i] = NUL;
11112 break;
11113 }
11114 }
11115 }
11116 else
11117#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011118 if (this_ru_col + (int)STRLEN(buffer) > width)
11119 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120
Bram Moolenaar4033c552017-09-16 20:54:51 +020011121 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 i = redraw_cmdline;
11123 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011124 this_ru_col + off + (int)STRLEN(buffer),
11125 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126 fillchar, fillchar, attr);
11127 /* don't redraw the cmdline because of showing the ruler */
11128 redraw_cmdline = i;
11129 wp->w_ru_cursor = wp->w_cursor;
11130 wp->w_ru_virtcol = wp->w_virtcol;
11131 wp->w_ru_empty = empty_line;
11132 wp->w_ru_topline = wp->w_topline;
11133 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11134#ifdef FEAT_DIFF
11135 wp->w_ru_topfill = wp->w_topfill;
11136#endif
11137 }
11138}
11139#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011140
11141#if defined(FEAT_LINEBREAK) || defined(PROTO)
11142/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011143 * Return the width of the 'number' and 'relativenumber' column.
11144 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011145 * Otherwise it depends on 'numberwidth' and the line count.
11146 */
11147 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011148number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011149{
11150 int n;
11151 linenr_T lnum;
11152
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011153 if (wp->w_p_rnu && !wp->w_p_nu)
11154 /* cursor line shows "0" */
11155 lnum = wp->w_height;
11156 else
11157 /* cursor line shows absolute line number */
11158 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011159
Bram Moolenaar6b314672015-03-20 15:42:10 +010011160 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011161 return wp->w_nrwidth_width;
11162 wp->w_nrwidth_line_count = lnum;
11163
11164 n = 0;
11165 do
11166 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011167 lnum /= 10;
11168 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011169 } while (lnum > 0);
11170
11171 /* 'numberwidth' gives the minimal width plus one */
11172 if (n < wp->w_p_nuw - 1)
11173 n = wp->w_p_nuw - 1;
11174
11175 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011176 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011177 return n;
11178}
11179#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011180
11181/*
11182 * Return the current cursor column. This is the actual position on the
11183 * screen. First column is 0.
11184 */
11185 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011186screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011187{
11188 return screen_cur_col;
11189}
11190
11191/*
11192 * Return the current cursor row. This is the actual position on the screen.
11193 * First row is 0.
11194 */
11195 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011196screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011197{
11198 return screen_cur_row;
11199}