blob: 325d9ae297964ad1d87d5d288de1827dd16a79c1 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
115/*
116 * Buffer for one screen line (characters and attributes).
117 */
118static schar_T *current_ScreenLine;
119
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100120static void win_update(win_T *wp);
121static 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 +0000122#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
124static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
125static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static int win_line(win_T *, linenr_T, int, int, int nochange);
128static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_RIGHTLEFT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void screen_line(int row, int coloff, int endcol, int clear_width, int rlflag);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
132#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void screen_line(int row, int coloff, int endcol, int clear_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
135#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100136#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200143# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void start_search_hl(void);
145static void end_search_hl(void);
146static void init_search_hl(win_T *wp);
147static void prepare_search_hl(win_T *wp, linenr_T lnum);
148static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
149static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_start_highlight(int attr);
152static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void screenclear2(void);
157static void lineclear(unsigned off, int width);
158static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100159#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void linecopy(int to, int from, win_T *wp);
161static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del);
164static void win_rest_invalid(win_T *wp);
165static void msg_pos_mode(void);
166static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000167#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_status(int *attr, int is_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100173#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100180static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#endif
182
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100183#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/* Ugly global: overrule attribute used by screen_char() */
185static int screen_char_attr = 0;
186#endif
187
188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
191 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 if (wp->w_redr_type < type)
205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
242 {
243 redraw_win_later(wp, type);
244 }
245}
246
247/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000248 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100251redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 redraw_buf_later(curbuf, type);
254}
255
256 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100257redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259 win_T *wp;
260
261 FOR_ALL_WINDOWS(wp)
262 {
263 if (wp->w_buffer == buf)
264 redraw_win_later(wp, type);
265 }
266}
267
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200268 void
269redraw_buf_and_status_later(buf_T *buf, int type)
270{
271 win_T *wp;
272
273 FOR_ALL_WINDOWS(wp)
274 {
275 if (wp->w_buffer == buf)
276 {
277 redraw_win_later(wp, type);
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200278#ifdef FEAT_WINDOWS
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200279 wp->w_redr_status = TRUE;
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200280#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200281 }
282 }
283}
284
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200286 * Redraw as soon as possible. When the command line is not scrolled redraw
287 * right away and restore what was on the command line.
288 * Return a code indicating what happened.
289 */
290 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100291redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200292{
293 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200294 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 int r;
296 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200297 schar_T *screenline; /* copy from ScreenLines[] */
298 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299#ifdef FEAT_MBYTE
300 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200301 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200302 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200304#endif
305
306 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200307 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 return ret;
309
310 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 if (screenline == NULL || screenattr == NULL)
317 ret = 2;
318#ifdef FEAT_MBYTE
319 if (enc_utf8)
320 {
321 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200322 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200323 if (screenlineUC == NULL)
324 ret = 2;
325 for (i = 0; i < p_mco; ++i)
326 {
327 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenlineC[i] == NULL)
330 ret = 2;
331 }
332 }
333 if (enc_dbcs == DBCS_JPNU)
334 {
335 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200336 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenline2 == NULL)
338 ret = 2;
339 }
340#endif
341
342 if (ret != 2)
343 {
344 /* Save the text displayed in the command line area. */
345 for (r = 0; r < rows; ++r)
346 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200347 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200348 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 (size_t)cols * sizeof(schar_T));
350 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200352 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353#ifdef FEAT_MBYTE
354 if (enc_utf8)
355 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200360 mch_memmove(screenlineC[i] + r * cols,
361 ScreenLinesC[i] + LineOffset[cmdline_row + r],
362 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 }
364 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368#endif
369 }
370
371 update_screen(0);
372 ret = 3;
373
374 if (must_redraw == 0)
375 {
376 int off = (int)(current_ScreenLine - ScreenLines);
377
378 /* Restore the text displayed in the command line area. */
379 for (r = 0; r < rows; ++r)
380 {
381 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200382 screenline + r * cols,
383 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200384 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200385 screenattr + r * cols,
386 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387#ifdef FEAT_MBYTE
388 if (enc_utf8)
389 {
390 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenlineUC + r * cols,
392 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 for (i = 0; i < p_mco; ++i)
394 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenlineC[i] + r * cols,
396 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 }
398 if (enc_dbcs == DBCS_JPNU)
399 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenline2 + r * cols,
401 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402#endif
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200403 SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 }
405 ret = 4;
406 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 }
408
409 vim_free(screenline);
410 vim_free(screenattr);
411#ifdef FEAT_MBYTE
412 if (enc_utf8)
413 {
414 vim_free(screenlineUC);
415 for (i = 0; i < p_mco; ++i)
416 vim_free(screenlineC[i]);
417 }
418 if (enc_dbcs == DBCS_JPNU)
419 vim_free(screenline2);
420#endif
421
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200422 /* Show the intro message when appropriate. */
423 maybe_intro_message();
424
425 setcursor();
426
Bram Moolenaar2951b772013-07-03 12:45:31 +0200427 return ret;
428}
429
430/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100431 * Invoked after an asynchronous callback is called.
432 * If an echo command was used the cursor needs to be put back where
433 * it belongs. If highlighting was changed a redraw is needed.
434 */
435 void
Bram Moolenaarcf089462016-06-12 21:18:43 +0200436redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100437{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100438 if (State == HITRETURN || State == ASKMORE)
439 ; /* do nothing */
440 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200441 {
442 /* Redrawing only works when the screen didn't scroll. */
443 if (msg_scrolled == 0)
444 {
445 update_screen(0);
446 compute_cmdrow();
447 }
448 else
449 {
450 /* Redraw in the same position, so that the user can continue
451 * editing the command. */
452 compute_cmdrow();
453 if (cmdline_row > msg_scrolled)
454 cmdline_row -= msg_scrolled;
455 else
456 cmdline_row = 0;
457 }
458 redrawcmdline_ex(FALSE);
459 }
Bram Moolenaar144445d2016-07-08 21:41:54 +0200460 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100461 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200462 /* keep the command line if possible */
463 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100464 setcursor();
465 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100466 cursor_on();
467 out_flush();
468#ifdef FEAT_GUI
469 if (gui.in_use)
470 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200471 /* Don't update the cursor when it is blinking and off to avoid
472 * flicker. */
473 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200474 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100475 gui_mch_flush();
476 }
477#endif
478}
479
480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 * Changed something in the current window, at buffer line "lnum", that
482 * requires that line and possibly other lines to be redrawn.
483 * Used when entering/leaving Insert mode with the cursor on a folded line.
484 * Used to remove the "$" from a change command.
485 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
486 * may become invalid and the whole window will have to be redrawn.
487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100489redrawWinline(
490 linenr_T lnum,
491 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493#ifdef FEAT_FOLDING
494 int i;
495#endif
496
497 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
498 curwin->w_redraw_top = lnum;
499 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
500 curwin->w_redraw_bot = lnum;
501 redraw_later(VALID);
502
503#ifdef FEAT_FOLDING
504 if (invalid)
505 {
506 /* A w_lines[] entry for this lnum has become invalid. */
507 i = find_wl_entry(curwin, lnum);
508 if (i >= 0)
509 curwin->w_lines[i].wl_valid = FALSE;
510 }
511#endif
512}
513
514/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200515 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 */
517 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100518update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519{
520 redraw_curbuf_later(type);
521 update_screen(type);
522}
523
524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 * Based on the current value of curwin->w_topline, transfer a screenfull
526 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
527 */
528 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200529update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200531 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 win_T *wp;
533 static int did_intro = FALSE;
534#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
535 int did_one;
536#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200537#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200538 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200539 int gui_cursor_col;
540 int gui_cursor_row;
541#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200542 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000544 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 if (!screen_valid(TRUE))
546 return;
547
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200548 if (type == VALID_NO_UPDATE)
549 {
550 no_update = TRUE;
551 type = 0;
552 }
553
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 if (must_redraw)
555 {
556 if (type < must_redraw) /* use maximal type */
557 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000558
559 /* must_redraw is reset here, so that when we run into some weird
560 * reason to redraw while busy redrawing (e.g., asynchronous
561 * scrolling), or update_topline() in win_update() will cause a
562 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 must_redraw = 0;
564 }
565
566 /* Need to update w_lines[]. */
567 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
568 type = NOT_VALID;
569
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000570 /* Postpone the redrawing when it's not needed and when being called
571 * recursively. */
572 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
574 redraw_later(type); /* remember type for next time */
575 must_redraw = type;
576 if (type > INVERTED_ALL)
577 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
578 return;
579 }
580
581 updating_screen = TRUE;
582#ifdef FEAT_SYN_HL
583 ++display_tick; /* let syntax code know we're in a next round of
584 * display updating */
585#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200586 if (no_update)
587 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588
589 /*
590 * if the screen was scrolled up when displaying a message, scroll it down
591 */
592 if (msg_scrolled)
593 {
594 clear_cmdline = TRUE;
595 if (msg_scrolled > Rows - 5) /* clearing is faster */
596 type = CLEAR;
597 else if (type != CLEAR)
598 {
599 check_for_delay(FALSE);
600 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
601 type = CLEAR;
602 FOR_ALL_WINDOWS(wp)
603 {
604 if (W_WINROW(wp) < msg_scrolled)
605 {
606 if (W_WINROW(wp) + wp->w_height > msg_scrolled
607 && wp->w_redr_type < REDRAW_TOP
608 && wp->w_lines_valid > 0
609 && wp->w_topline == wp->w_lines[0].wl_lnum)
610 {
611 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
612 wp->w_redr_type = REDRAW_TOP;
613 }
614 else
615 {
616 wp->w_redr_type = NOT_VALID;
617#ifdef FEAT_WINDOWS
618 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
619 <= msg_scrolled)
620 wp->w_redr_status = TRUE;
621#endif
622 }
623 }
624 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200625 if (!no_update)
626 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000627#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000628 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 }
631 msg_scrolled = 0;
632 need_wait_return = FALSE;
633 }
634
635 /* reset cmdline_row now (may have been changed temporarily) */
636 compute_cmdrow();
637
638 /* Check for changed highlighting */
639 if (need_highlight_changed)
640 highlight_changed();
641
642 if (type == CLEAR) /* first clear screen */
643 {
644 screenclear(); /* will reset clear_cmdline */
645 type = NOT_VALID;
646 }
647
648 if (clear_cmdline) /* going to clear cmdline (done below) */
649 check_for_delay(FALSE);
650
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000651#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200652 /* Force redraw when width of 'number' or 'relativenumber' column
653 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000654 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200655 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
656 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000657 curwin->w_redr_type = NOT_VALID;
658#endif
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 /*
661 * Only start redrawing if there is really something to do.
662 */
663 if (type == INVERTED)
664 update_curswant();
665 if (curwin->w_redr_type < type
666 && !((type == VALID
667 && curwin->w_lines[0].wl_valid
668#ifdef FEAT_DIFF
669 && curwin->w_topfill == curwin->w_old_topfill
670 && curwin->w_botfill == curwin->w_old_botfill
671#endif
672 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000674 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
676 && curwin->w_old_visual_mode == VIsual_mode
677 && (curwin->w_valid & VALID_VIRTCOL)
678 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 ))
680 curwin->w_redr_type = type;
681
Bram Moolenaar5a305422006-04-28 22:38:25 +0000682#ifdef FEAT_WINDOWS
683 /* Redraw the tab pages line if needed. */
684 if (redraw_tabline || type >= NOT_VALID)
685 draw_tabline();
686#endif
687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#ifdef FEAT_SYN_HL
689 /*
690 * Correct stored syntax highlighting info for changes in each displayed
691 * buffer. Each buffer must only be done once.
692 */
693 FOR_ALL_WINDOWS(wp)
694 {
695 if (wp->w_buffer->b_mod_set)
696 {
697# ifdef FEAT_WINDOWS
698 win_T *wwp;
699
700 /* Check if we already did this buffer. */
701 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
702 if (wwp->w_buffer == wp->w_buffer)
703 break;
704# endif
705 if (
706# ifdef FEAT_WINDOWS
707 wwp == wp &&
708# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200709 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 syn_stack_apply_changes(wp->w_buffer);
711 }
712 }
713#endif
714
715 /*
716 * Go from top to bottom through the windows, redrawing the ones that need
717 * it.
718 */
719#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
720 did_one = FALSE;
721#endif
722#ifdef FEAT_SEARCH_EXTRA
723 search_hl.rm.regprog = NULL;
724#endif
725 FOR_ALL_WINDOWS(wp)
726 {
727 if (wp->w_redr_type != 0)
728 {
729 cursor_off();
730#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
731 if (!did_one)
732 {
733 did_one = TRUE;
734# ifdef FEAT_SEARCH_EXTRA
735 start_search_hl();
736# endif
737# ifdef FEAT_CLIPBOARD
738 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200739 if (clip_star.available && clip_isautosel_star())
740 clip_update_selection(&clip_star);
741 if (clip_plus.available && clip_isautosel_plus())
742 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743# endif
744#ifdef FEAT_GUI
745 /* Remove the cursor before starting to do anything, because
746 * scrolling may make it difficult to redraw the text under
747 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200748 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200749 {
750 gui_cursor_col = gui.cursor_col;
751 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200753 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755#endif
756 }
757#endif
758 win_update(wp);
759 }
760
761#ifdef FEAT_WINDOWS
762 /* redraw status line after the window to minimize cursor movement */
763 if (wp->w_redr_status)
764 {
765 cursor_off();
766 win_redr_status(wp);
767 }
768#endif
769 }
770#if defined(FEAT_SEARCH_EXTRA)
771 end_search_hl();
772#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100773#ifdef FEAT_INS_EXPAND
774 /* May need to redraw the popup menu. */
775 if (pum_visible())
776 pum_redraw();
777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
779#ifdef FEAT_WINDOWS
780 /* Reset b_mod_set flags. Going through all windows is probably faster
781 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200782 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 wp->w_buffer->b_mod_set = FALSE;
784#else
785 curbuf->b_mod_set = FALSE;
786#endif
787
788 updating_screen = FALSE;
789#ifdef FEAT_GUI
790 gui_may_resize_shell();
791#endif
792
793 /* Clear or redraw the command line. Done last, because scrolling may
794 * mess up the command line. */
795 if (clear_cmdline || redraw_cmdline)
796 showmode();
797
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200798 if (no_update)
799 --no_win_do_lines_ins;
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200802 if (!did_intro)
803 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 did_intro = TRUE;
805
806#ifdef FEAT_GUI
807 /* Redraw the cursor and update the scrollbars when all screen updating is
808 * done. */
809 if (gui.in_use)
810 {
811 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200812 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200813 {
814 /* Put the GUI position where the cursor was, gui_update_cursor()
815 * uses that. */
816 gui.col = gui_cursor_col;
817 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200818# ifdef FEAT_MBYTE
819 gui.col = mb_fix_col(gui.col, gui.row);
820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200822 screen_cur_col = gui.col;
823 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 gui_update_scrollbars(FALSE);
826 }
827#endif
828}
829
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100830#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
831/*
832 * Prepare for updating one or more windows.
833 * Caller must check for "updating_screen" already set to avoid recursiveness.
834 */
835 static void
836update_prepare(void)
837{
838 cursor_off();
839 updating_screen = TRUE;
840#ifdef FEAT_GUI
841 /* Remove the cursor before starting to do anything, because scrolling may
842 * make it difficult to redraw the text under it. */
843 if (gui.in_use)
844 gui_undraw_cursor();
845#endif
846#ifdef FEAT_SEARCH_EXTRA
847 start_search_hl();
848#endif
849}
850
851/*
852 * Finish updating one or more windows.
853 */
854 static void
855update_finish(void)
856{
857 if (redraw_cmdline)
858 showmode();
859
860# ifdef FEAT_SEARCH_EXTRA
861 end_search_hl();
862# endif
863
864 updating_screen = FALSE;
865
866# ifdef FEAT_GUI
867 gui_may_resize_shell();
868
869 /* Redraw the cursor and update the scrollbars when all screen updating is
870 * done. */
871 if (gui.in_use)
872 {
873 out_flush(); /* required before updating the cursor */
874 gui_update_cursor(FALSE, FALSE);
875 gui_update_scrollbars(FALSE);
876 }
877# endif
878}
879#endif
880
Bram Moolenaar860cae12010-06-05 23:22:07 +0200881#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200882/*
883 * Return TRUE if the cursor line in window "wp" may be concealed, according
884 * to the 'concealcursor' option.
885 */
886 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100887conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200888{
889 int c;
890
891 if (*wp->w_p_cocu == NUL)
892 return FALSE;
893 if (get_real_state() & VISUAL)
894 c = 'v';
895 else if (State & INSERT)
896 c = 'i';
897 else if (State & NORMAL)
898 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200899 else if (State & CMDLINE)
900 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200901 else
902 return FALSE;
903 return vim_strchr(wp->w_p_cocu, c) != NULL;
904}
905
906/*
907 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
908 */
909 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100910conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200911{
912 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
913 {
914 need_cursor_line_redraw = TRUE;
915 /* Need to recompute cursor column, e.g., when starting Visual mode
916 * without concealing. */
917 curs_columns(TRUE);
918 }
919}
920
Bram Moolenaar860cae12010-06-05 23:22:07 +0200921 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100922update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200923{
924 int row;
925 int j;
926
Bram Moolenaar908be432016-05-24 10:51:30 +0200927 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100928 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200929 return;
930
Bram Moolenaar860cae12010-06-05 23:22:07 +0200931 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200932 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200933 {
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100934 update_prepare();
935
Bram Moolenaar860cae12010-06-05 23:22:07 +0200936 row = 0;
937 for (j = 0; j < wp->w_lines_valid; ++j)
938 {
939 if (lnum == wp->w_lines[j].wl_lnum)
940 {
941 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200942# ifdef FEAT_SEARCH_EXTRA
943 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200944 start_search_hl();
945 prepare_search_hl(wp, lnum);
946# endif
947 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
948# if defined(FEAT_SEARCH_EXTRA)
949 end_search_hl();
950# endif
951 break;
952 }
953 row += wp->w_lines[j].wl_size;
954 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100955
956 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200957 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200958 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959}
960#endif
961
962#if defined(FEAT_SIGNS) || defined(PROTO)
963 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100964update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
966 win_T *wp;
967 int doit = FALSE;
968
969# ifdef FEAT_FOLDING
970 win_foldinfo.fi_level = 0;
971# endif
972
973 /* update/delete a specific mark */
974 FOR_ALL_WINDOWS(wp)
975 {
976 if (buf != NULL && lnum > 0)
977 {
978 if (wp->w_buffer == buf && lnum >= wp->w_topline
979 && lnum < wp->w_botline)
980 {
981 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
982 wp->w_redraw_top = lnum;
983 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
984 wp->w_redraw_bot = lnum;
985 redraw_win_later(wp, VALID);
986 }
987 }
988 else
989 redraw_win_later(wp, VALID);
990 if (wp->w_redr_type != 0)
991 doit = TRUE;
992 }
993
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100994 /* Return when there is nothing to do, screen updating is already
995 * happening (recursive call) or still starting up. */
996 if (!doit || updating_screen
997#ifdef FEAT_GUI
998 || gui.starting
999#endif
1000 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 return;
1002
1003 /* update all windows that need updating */
1004 update_prepare();
1005
1006# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001007 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 {
1009 if (wp->w_redr_type != 0)
1010 win_update(wp);
1011 if (wp->w_redr_status)
1012 win_redr_status(wp);
1013 }
1014# else
1015 if (curwin->w_redr_type != 0)
1016 win_update(curwin);
1017# endif
1018
1019 update_finish();
1020}
1021#endif
1022
1023
1024#if defined(FEAT_GUI) || defined(PROTO)
1025/*
1026 * Update a single window, its status line and maybe the command line msg.
1027 * Used for the GUI scrollbar.
1028 */
1029 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001030updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001032 /* return if already busy updating */
1033 if (updating_screen)
1034 return;
1035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 update_prepare();
1037
1038#ifdef FEAT_CLIPBOARD
1039 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001040 if (clip_star.available && clip_isautosel_star())
1041 clip_update_selection(&clip_star);
1042 if (clip_plus.available && clip_isautosel_plus())
1043 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001049 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001050 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001051 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001052
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 if (wp->w_redr_status
1054# ifdef FEAT_CMDL_INFO
1055 || p_ru
1056# endif
1057# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001058 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059# endif
1060 )
1061 win_redr_status(wp);
1062#endif
1063
1064 update_finish();
1065}
1066#endif
1067
1068/*
1069 * Update a single window.
1070 *
1071 * This may cause the windows below it also to be redrawn (when clearing the
1072 * screen or scrolling lines).
1073 *
1074 * How the window is redrawn depends on wp->w_redr_type. Each type also
1075 * implies the one below it.
1076 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001077 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1079 * INVERTED redraw the changed part of the Visual area
1080 * INVERTED_ALL redraw the whole Visual area
1081 * VALID 1. scroll up/down to adjust for a changed w_topline
1082 * 2. update lines at the top when scrolled down
1083 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001084 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 * b_mod_top and b_mod_bot.
1086 * - if wp->w_redraw_top non-zero, redraw lines between
1087 * wp->w_redraw_top and wp->w_redr_bot.
1088 * - continue redrawing when syntax status is invalid.
1089 * 4. if scrolled up, update lines at the bottom.
1090 * This results in three areas that may need updating:
1091 * top: from first row to top_end (when scrolled down)
1092 * mid: from mid_start to mid_end (update inversion or changed text)
1093 * bot: from bot_start to last row (when scrolled up)
1094 */
1095 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001096win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
1098 buf_T *buf = wp->w_buffer;
1099 int type;
1100 int top_end = 0; /* Below last row of the top area that needs
1101 updating. 0 when no top area updating. */
1102 int mid_start = 999;/* first row of the mid area that needs
1103 updating. 999 when no mid area updating. */
1104 int mid_end = 0; /* Below last row of the mid area that needs
1105 updating. 0 when no mid area updating. */
1106 int bot_start = 999;/* first row of the bot area that needs
1107 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 int scrolled_down = FALSE; /* TRUE when scrolled down when
1109 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001111 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 int top_to_mod = FALSE; /* redraw above mod_top */
1113#endif
1114
1115 int row; /* current window row to display */
1116 linenr_T lnum; /* current buffer lnum to display */
1117 int idx; /* current index in w_lines[] */
1118 int srow; /* starting row of the current line */
1119
1120 int eof = FALSE; /* if TRUE, we hit the end of the file */
1121 int didline = FALSE; /* if TRUE, we finished the last line */
1122 int i;
1123 long j;
1124 static int recursive = FALSE; /* being called recursively */
1125 int old_botline = wp->w_botline;
1126#ifdef FEAT_FOLDING
1127 long fold_count;
1128#endif
1129#ifdef FEAT_SYN_HL
1130 /* remember what happened to the previous line, to know if
1131 * check_visual_highlight() can be used */
1132#define DID_NONE 1 /* didn't update a line */
1133#define DID_LINE 2 /* updated a normal line */
1134#define DID_FOLD 3 /* updated a folded line */
1135 int did_update = DID_NONE;
1136 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1137#endif
1138 linenr_T mod_top = 0;
1139 linenr_T mod_bot = 0;
1140#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1141 int save_got_int;
1142#endif
1143
1144 type = wp->w_redr_type;
1145
1146 if (type == NOT_VALID)
1147 {
1148#ifdef FEAT_WINDOWS
1149 wp->w_redr_status = TRUE;
1150#endif
1151 wp->w_lines_valid = 0;
1152 }
1153
1154 /* Window is zero-height: nothing to draw. */
1155 if (wp->w_height == 0)
1156 {
1157 wp->w_redr_type = 0;
1158 return;
1159 }
1160
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001161#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 /* Window is zero-width: Only need to draw the separator. */
1163 if (wp->w_width == 0)
1164 {
1165 /* draw the vertical separator right of this window */
1166 draw_vsep_win(wp, 0);
1167 wp->w_redr_type = 0;
1168 return;
1169 }
1170#endif
1171
1172#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001173 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174#endif
1175
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001176#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001177 /* Force redraw when width of 'number' or 'relativenumber' column
1178 * changes. */
1179 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001180 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001181 {
1182 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001183 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001184 }
1185 else
1186#endif
1187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1189 {
1190 /*
1191 * When there are both inserted/deleted lines and specific lines to be
1192 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1193 * everything (only happens when redrawing is off for while).
1194 */
1195 type = NOT_VALID;
1196 }
1197 else
1198 {
1199 /*
1200 * Set mod_top to the first line that needs displaying because of
1201 * changes. Set mod_bot to the first line after the changes.
1202 */
1203 mod_top = wp->w_redraw_top;
1204 if (wp->w_redraw_bot != 0)
1205 mod_bot = wp->w_redraw_bot + 1;
1206 else
1207 mod_bot = 0;
1208 wp->w_redraw_top = 0; /* reset for next time */
1209 wp->w_redraw_bot = 0;
1210 if (buf->b_mod_set)
1211 {
1212 if (mod_top == 0 || mod_top > buf->b_mod_top)
1213 {
1214 mod_top = buf->b_mod_top;
1215#ifdef FEAT_SYN_HL
1216 /* Need to redraw lines above the change that may be included
1217 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001218 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001220 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (mod_top < 1)
1222 mod_top = 1;
1223 }
1224#endif
1225 }
1226 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1227 mod_bot = buf->b_mod_bot;
1228
1229#ifdef FEAT_SEARCH_EXTRA
1230 /* When 'hlsearch' is on and using a multi-line search pattern, a
1231 * change in one line may make the Search highlighting in a
1232 * previous line invalid. Simple solution: redraw all visible
1233 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001234 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001236 if (search_hl.rm.regprog != NULL
1237 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001239 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001240 {
1241 cur = wp->w_match_head;
1242 while (cur != NULL)
1243 {
1244 if (cur->match.regprog != NULL
1245 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001246 {
1247 top_to_mod = TRUE;
1248 break;
1249 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001250 cur = cur->next;
1251 }
1252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253#endif
1254 }
1255#ifdef FEAT_FOLDING
1256 if (mod_top != 0 && hasAnyFolding(wp))
1257 {
1258 linenr_T lnumt, lnumb;
1259
1260 /*
1261 * A change in a line can cause lines above it to become folded or
1262 * unfolded. Find the top most buffer line that may be affected.
1263 * If the line was previously folded and displayed, get the first
1264 * line of that fold. If the line is folded now, get the first
1265 * folded line. Use the minimum of these two.
1266 */
1267
1268 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1269 * the line below it. If there is no valid entry, use w_topline.
1270 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1271 * to this line. If there is no valid entry, use MAXLNUM. */
1272 lnumt = wp->w_topline;
1273 lnumb = MAXLNUM;
1274 for (i = 0; i < wp->w_lines_valid; ++i)
1275 if (wp->w_lines[i].wl_valid)
1276 {
1277 if (wp->w_lines[i].wl_lastlnum < mod_top)
1278 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1279 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1280 {
1281 lnumb = wp->w_lines[i].wl_lnum;
1282 /* When there is a fold column it might need updating
1283 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001284 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 ++lnumb;
1286 }
1287 }
1288
1289 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1290 if (mod_top > lnumt)
1291 mod_top = lnumt;
1292
1293 /* Now do the same for the bottom line (one above mod_bot). */
1294 --mod_bot;
1295 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1296 ++mod_bot;
1297 if (mod_bot < lnumb)
1298 mod_bot = lnumb;
1299 }
1300#endif
1301
1302 /* When a change starts above w_topline and the end is below
1303 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001304 * If the end of the change is above w_topline: do like no change was
1305 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if (mod_top != 0 && mod_top < wp->w_topline)
1307 {
1308 if (mod_bot > wp->w_topline)
1309 mod_top = wp->w_topline;
1310#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001311 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 top_end = 1;
1313#endif
1314 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001315
1316 /* When line numbers are displayed need to redraw all lines below
1317 * inserted/deleted lines. */
1318 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1319 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321
1322 /*
1323 * When only displaying the lines at the top, set top_end. Used when
1324 * window has scrolled down for msg_scrolled.
1325 */
1326 if (type == REDRAW_TOP)
1327 {
1328 j = 0;
1329 for (i = 0; i < wp->w_lines_valid; ++i)
1330 {
1331 j += wp->w_lines[i].wl_size;
1332 if (j >= wp->w_upd_rows)
1333 {
1334 top_end = j;
1335 break;
1336 }
1337 }
1338 if (top_end == 0)
1339 /* not found (cannot happen?): redraw everything */
1340 type = NOT_VALID;
1341 else
1342 /* top area defined, the rest is VALID */
1343 type = VALID;
1344 }
1345
Bram Moolenaar367329b2007-08-30 11:53:22 +00001346 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001347 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1348 * non-zero and thus not FALSE) will indicate that screenclear() was not
1349 * called. */
1350 if (screen_cleared)
1351 screen_cleared = MAYBE;
1352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 /*
1354 * If there are no changes on the screen that require a complete redraw,
1355 * handle three cases:
1356 * 1: we are off the top of the screen by a few lines: scroll down
1357 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1358 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1359 * w_lines[] that needs updating.
1360 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001361 if ((type == VALID || type == SOME_VALID
1362 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363#ifdef FEAT_DIFF
1364 && !wp->w_botfill && !wp->w_old_botfill
1365#endif
1366 )
1367 {
1368 if (mod_top != 0 && wp->w_topline == mod_top)
1369 {
1370 /*
1371 * w_topline is the first changed line, the scrolling will be done
1372 * further down.
1373 */
1374 }
1375 else if (wp->w_lines[0].wl_valid
1376 && (wp->w_topline < wp->w_lines[0].wl_lnum
1377#ifdef FEAT_DIFF
1378 || (wp->w_topline == wp->w_lines[0].wl_lnum
1379 && wp->w_topfill > wp->w_old_topfill)
1380#endif
1381 ))
1382 {
1383 /*
1384 * New topline is above old topline: May scroll down.
1385 */
1386#ifdef FEAT_FOLDING
1387 if (hasAnyFolding(wp))
1388 {
1389 linenr_T ln;
1390
1391 /* count the number of lines we are off, counting a sequence
1392 * of folded lines as one */
1393 j = 0;
1394 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1395 {
1396 ++j;
1397 if (j >= wp->w_height - 2)
1398 break;
1399 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1400 }
1401 }
1402 else
1403#endif
1404 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1405 if (j < wp->w_height - 2) /* not too far off */
1406 {
1407 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1408#ifdef FEAT_DIFF
1409 /* insert extra lines for previously invisible filler lines */
1410 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1411 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1412 - wp->w_old_topfill;
1413#endif
1414 if (i < wp->w_height - 2) /* less than a screen off */
1415 {
1416 /*
1417 * Try to insert the correct number of lines.
1418 * If not the last window, delete the lines at the bottom.
1419 * win_ins_lines may fail when the terminal can't do it.
1420 */
1421 if (i > 0)
1422 check_for_delay(FALSE);
1423 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1424 {
1425 if (wp->w_lines_valid != 0)
1426 {
1427 /* Need to update rows that are new, stop at the
1428 * first one that scrolled down. */
1429 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431
1432 /* Move the entries that were scrolled, disable
1433 * the entries for the lines to be redrawn. */
1434 if ((wp->w_lines_valid += j) > wp->w_height)
1435 wp->w_lines_valid = wp->w_height;
1436 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1437 wp->w_lines[idx] = wp->w_lines[idx - j];
1438 while (idx >= 0)
1439 wp->w_lines[idx--].wl_valid = FALSE;
1440 }
1441 }
1442 else
1443 mid_start = 0; /* redraw all lines */
1444 }
1445 else
1446 mid_start = 0; /* redraw all lines */
1447 }
1448 else
1449 mid_start = 0; /* redraw all lines */
1450 }
1451 else
1452 {
1453 /*
1454 * New topline is at or below old topline: May scroll up.
1455 * When topline didn't change, find first entry in w_lines[] that
1456 * needs updating.
1457 */
1458
1459 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1460 j = -1;
1461 row = 0;
1462 for (i = 0; i < wp->w_lines_valid; i++)
1463 {
1464 if (wp->w_lines[i].wl_valid
1465 && wp->w_lines[i].wl_lnum == wp->w_topline)
1466 {
1467 j = i;
1468 break;
1469 }
1470 row += wp->w_lines[i].wl_size;
1471 }
1472 if (j == -1)
1473 {
1474 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1475 * lines */
1476 mid_start = 0;
1477 }
1478 else
1479 {
1480 /*
1481 * Try to delete the correct number of lines.
1482 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1483 */
1484#ifdef FEAT_DIFF
1485 /* If the topline didn't change, delete old filler lines,
1486 * otherwise delete filler lines of the new topline... */
1487 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1488 row += wp->w_old_topfill;
1489 else
1490 row += diff_check_fill(wp, wp->w_topline);
1491 /* ... but don't delete new filler lines. */
1492 row -= wp->w_topfill;
1493#endif
1494 if (row > 0)
1495 {
1496 check_for_delay(FALSE);
1497 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1498 bot_start = wp->w_height - row;
1499 else
1500 mid_start = 0; /* redraw all lines */
1501 }
1502 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1503 {
1504 /*
1505 * Skip the lines (below the deleted lines) that are still
1506 * valid and don't need redrawing. Copy their info
1507 * upwards, to compensate for the deleted lines. Set
1508 * bot_start to the first row that needs redrawing.
1509 */
1510 bot_start = 0;
1511 idx = 0;
1512 for (;;)
1513 {
1514 wp->w_lines[idx] = wp->w_lines[j];
1515 /* stop at line that didn't fit, unless it is still
1516 * valid (no lines deleted) */
1517 if (row > 0 && bot_start + row
1518 + (int)wp->w_lines[j].wl_size > wp->w_height)
1519 {
1520 wp->w_lines_valid = idx + 1;
1521 break;
1522 }
1523 bot_start += wp->w_lines[idx++].wl_size;
1524
1525 /* stop at the last valid entry in w_lines[].wl_size */
1526 if (++j >= wp->w_lines_valid)
1527 {
1528 wp->w_lines_valid = idx;
1529 break;
1530 }
1531 }
1532#ifdef FEAT_DIFF
1533 /* Correct the first entry for filler lines at the top
1534 * when it won't get updated below. */
1535 if (wp->w_p_diff && bot_start > 0)
1536 wp->w_lines[0].wl_size =
1537 plines_win_nofill(wp, wp->w_topline, TRUE)
1538 + wp->w_topfill;
1539#endif
1540 }
1541 }
1542 }
1543
1544 /* When starting redraw in the first line, redraw all lines. When
1545 * there is only one window it's probably faster to clear the screen
1546 * first. */
1547 if (mid_start == 0)
1548 {
1549 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001550 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001551 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001552 /* Clear the screen when it was not done by win_del_lines() or
1553 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1554 * then. */
1555 if (screen_cleared != TRUE)
1556 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001557#ifdef FEAT_WINDOWS
1558 /* The screen was cleared, redraw the tab pages line. */
1559 if (redraw_tabline)
1560 draw_tabline();
1561#endif
1562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001564
1565 /* When win_del_lines() or win_ins_lines() caused the screen to be
1566 * cleared (only happens for the first window) or when screenclear()
1567 * was called directly above, "must_redraw" will have been set to
1568 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1569 if (screen_cleared == TRUE)
1570 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 }
1572 else
1573 {
1574 /* Not VALID or INVERTED: redraw all lines. */
1575 mid_start = 0;
1576 mid_end = wp->w_height;
1577 }
1578
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001579 if (type == SOME_VALID)
1580 {
1581 /* SOME_VALID: redraw all lines. */
1582 mid_start = 0;
1583 mid_end = wp->w_height;
1584 type = NOT_VALID;
1585 }
1586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 /* check if we are updating or removing the inverted part */
1588 if ((VIsual_active && buf == curwin->w_buffer)
1589 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1590 {
1591 linenr_T from, to;
1592
1593 if (VIsual_active)
1594 {
1595 if (VIsual_active
1596 && (VIsual_mode != wp->w_old_visual_mode
1597 || type == INVERTED_ALL))
1598 {
1599 /*
1600 * If the type of Visual selection changed, redraw the whole
1601 * selection. Also when the ownership of the X selection is
1602 * gained or lost.
1603 */
1604 if (curwin->w_cursor.lnum < VIsual.lnum)
1605 {
1606 from = curwin->w_cursor.lnum;
1607 to = VIsual.lnum;
1608 }
1609 else
1610 {
1611 from = VIsual.lnum;
1612 to = curwin->w_cursor.lnum;
1613 }
1614 /* redraw more when the cursor moved as well */
1615 if (wp->w_old_cursor_lnum < from)
1616 from = wp->w_old_cursor_lnum;
1617 if (wp->w_old_cursor_lnum > to)
1618 to = wp->w_old_cursor_lnum;
1619 if (wp->w_old_visual_lnum < from)
1620 from = wp->w_old_visual_lnum;
1621 if (wp->w_old_visual_lnum > to)
1622 to = wp->w_old_visual_lnum;
1623 }
1624 else
1625 {
1626 /*
1627 * Find the line numbers that need to be updated: The lines
1628 * between the old cursor position and the current cursor
1629 * position. Also check if the Visual position changed.
1630 */
1631 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1632 {
1633 from = curwin->w_cursor.lnum;
1634 to = wp->w_old_cursor_lnum;
1635 }
1636 else
1637 {
1638 from = wp->w_old_cursor_lnum;
1639 to = curwin->w_cursor.lnum;
1640 if (from == 0) /* Visual mode just started */
1641 from = to;
1642 }
1643
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001644 if (VIsual.lnum != wp->w_old_visual_lnum
1645 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
1647 if (wp->w_old_visual_lnum < from
1648 && wp->w_old_visual_lnum != 0)
1649 from = wp->w_old_visual_lnum;
1650 if (wp->w_old_visual_lnum > to)
1651 to = wp->w_old_visual_lnum;
1652 if (VIsual.lnum < from)
1653 from = VIsual.lnum;
1654 if (VIsual.lnum > to)
1655 to = VIsual.lnum;
1656 }
1657 }
1658
1659 /*
1660 * If in block mode and changed column or curwin->w_curswant:
1661 * update all lines.
1662 * First compute the actual start and end column.
1663 */
1664 if (VIsual_mode == Ctrl_V)
1665 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001666 colnr_T fromc, toc;
1667#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1668 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
Bram Moolenaar404406a2014-10-09 13:24:43 +02001670 if (curwin->w_p_lbr)
1671 ve_flags = VE_ALL;
1672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001674#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1675 ve_flags = save_ve_flags;
1676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 ++toc;
1678 if (curwin->w_curswant == MAXCOL)
1679 toc = MAXCOL;
1680
1681 if (fromc != wp->w_old_cursor_fcol
1682 || toc != wp->w_old_cursor_lcol)
1683 {
1684 if (from > VIsual.lnum)
1685 from = VIsual.lnum;
1686 if (to < VIsual.lnum)
1687 to = VIsual.lnum;
1688 }
1689 wp->w_old_cursor_fcol = fromc;
1690 wp->w_old_cursor_lcol = toc;
1691 }
1692 }
1693 else
1694 {
1695 /* Use the line numbers of the old Visual area. */
1696 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1697 {
1698 from = wp->w_old_cursor_lnum;
1699 to = wp->w_old_visual_lnum;
1700 }
1701 else
1702 {
1703 from = wp->w_old_visual_lnum;
1704 to = wp->w_old_cursor_lnum;
1705 }
1706 }
1707
1708 /*
1709 * There is no need to update lines above the top of the window.
1710 */
1711 if (from < wp->w_topline)
1712 from = wp->w_topline;
1713
1714 /*
1715 * If we know the value of w_botline, use it to restrict the update to
1716 * the lines that are visible in the window.
1717 */
1718 if (wp->w_valid & VALID_BOTLINE)
1719 {
1720 if (from >= wp->w_botline)
1721 from = wp->w_botline - 1;
1722 if (to >= wp->w_botline)
1723 to = wp->w_botline - 1;
1724 }
1725
1726 /*
1727 * Find the minimal part to be updated.
1728 * Watch out for scrolling that made entries in w_lines[] invalid.
1729 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1730 * top_end; need to redraw from top_end to the "to" line.
1731 * A middle mouse click with a Visual selection may change the text
1732 * above the Visual area and reset wl_valid, do count these for
1733 * mid_end (in srow).
1734 */
1735 if (mid_start > 0)
1736 {
1737 lnum = wp->w_topline;
1738 idx = 0;
1739 srow = 0;
1740 if (scrolled_down)
1741 mid_start = top_end;
1742 else
1743 mid_start = 0;
1744 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1745 {
1746 if (wp->w_lines[idx].wl_valid)
1747 mid_start += wp->w_lines[idx].wl_size;
1748 else if (!scrolled_down)
1749 srow += wp->w_lines[idx].wl_size;
1750 ++idx;
1751# ifdef FEAT_FOLDING
1752 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1753 lnum = wp->w_lines[idx].wl_lnum;
1754 else
1755# endif
1756 ++lnum;
1757 }
1758 srow += mid_start;
1759 mid_end = wp->w_height;
1760 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1761 {
1762 if (wp->w_lines[idx].wl_valid
1763 && wp->w_lines[idx].wl_lnum >= to + 1)
1764 {
1765 /* Only update until first row of this line */
1766 mid_end = srow;
1767 break;
1768 }
1769 srow += wp->w_lines[idx].wl_size;
1770 }
1771 }
1772 }
1773
1774 if (VIsual_active && buf == curwin->w_buffer)
1775 {
1776 wp->w_old_visual_mode = VIsual_mode;
1777 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1778 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001779 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 wp->w_old_curswant = curwin->w_curswant;
1781 }
1782 else
1783 {
1784 wp->w_old_visual_mode = 0;
1785 wp->w_old_cursor_lnum = 0;
1786 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001787 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
1790#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1791 /* reset got_int, otherwise regexp won't work */
1792 save_got_int = got_int;
1793 got_int = 0;
1794#endif
1795#ifdef FEAT_FOLDING
1796 win_foldinfo.fi_level = 0;
1797#endif
1798
1799 /*
1800 * Update all the window rows.
1801 */
1802 idx = 0; /* first entry in w_lines[].wl_size */
1803 row = 0;
1804 srow = 0;
1805 lnum = wp->w_topline; /* first line shown in window */
1806 for (;;)
1807 {
1808 /* stop updating when reached the end of the window (check for _past_
1809 * the end of the window is at the end of the loop) */
1810 if (row == wp->w_height)
1811 {
1812 didline = TRUE;
1813 break;
1814 }
1815
1816 /* stop updating when hit the end of the file */
1817 if (lnum > buf->b_ml.ml_line_count)
1818 {
1819 eof = TRUE;
1820 break;
1821 }
1822
1823 /* Remember the starting row of the line that is going to be dealt
1824 * with. It is used further down when the line doesn't fit. */
1825 srow = row;
1826
1827 /*
1828 * Update a line when it is in an area that needs updating, when it
1829 * has changes or w_lines[idx] is invalid.
1830 * bot_start may be halfway a wrapped line after using
1831 * win_del_lines(), check if the current line includes it.
1832 * When syntax folding is being used, the saved syntax states will
1833 * already have been updated, we can't see where the syntax state is
1834 * the same again, just update until the end of the window.
1835 */
1836 if (row < top_end
1837 || (row >= mid_start && row < mid_end)
1838#ifdef FEAT_SEARCH_EXTRA
1839 || top_to_mod
1840#endif
1841 || idx >= wp->w_lines_valid
1842 || (row + wp->w_lines[idx].wl_size > bot_start)
1843 || (mod_top != 0
1844 && (lnum == mod_top
1845 || (lnum >= mod_top
1846 && (lnum < mod_bot
1847#ifdef FEAT_SYN_HL
1848 || did_update == DID_FOLD
1849 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001850 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 && (
1852# ifdef FEAT_FOLDING
1853 (foldmethodIsSyntax(wp)
1854 && hasAnyFolding(wp)) ||
1855# endif
1856 syntax_check_changed(lnum)))
1857#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001858#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001859 /* match in fixed position might need redraw
1860 * if lines were inserted or deleted */
1861 || (wp->w_match_head != NULL
1862 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 )))))
1865 {
1866#ifdef FEAT_SEARCH_EXTRA
1867 if (lnum == mod_top)
1868 top_to_mod = FALSE;
1869#endif
1870
1871 /*
1872 * When at start of changed lines: May scroll following lines
1873 * up or down to minimize redrawing.
1874 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001875 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 */
1877 if (lnum == mod_top
1878 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001879 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 {
1881 int old_rows = 0;
1882 int new_rows = 0;
1883 int xtra_rows;
1884 linenr_T l;
1885
1886 /* Count the old number of window rows, using w_lines[], which
1887 * should still contain the sizes for the lines as they are
1888 * currently displayed. */
1889 for (i = idx; i < wp->w_lines_valid; ++i)
1890 {
1891 /* Only valid lines have a meaningful wl_lnum. Invalid
1892 * lines are part of the changed area. */
1893 if (wp->w_lines[i].wl_valid
1894 && wp->w_lines[i].wl_lnum == mod_bot)
1895 break;
1896 old_rows += wp->w_lines[i].wl_size;
1897#ifdef FEAT_FOLDING
1898 if (wp->w_lines[i].wl_valid
1899 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1900 {
1901 /* Must have found the last valid entry above mod_bot.
1902 * Add following invalid entries. */
1903 ++i;
1904 while (i < wp->w_lines_valid
1905 && !wp->w_lines[i].wl_valid)
1906 old_rows += wp->w_lines[i++].wl_size;
1907 break;
1908 }
1909#endif
1910 }
1911
1912 if (i >= wp->w_lines_valid)
1913 {
1914 /* We can't find a valid line below the changed lines,
1915 * need to redraw until the end of the window.
1916 * Inserting/deleting lines has no use. */
1917 bot_start = 0;
1918 }
1919 else
1920 {
1921 /* Able to count old number of rows: Count new window
1922 * rows, and may insert/delete lines */
1923 j = idx;
1924 for (l = lnum; l < mod_bot; ++l)
1925 {
1926#ifdef FEAT_FOLDING
1927 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1928 ++new_rows;
1929 else
1930#endif
1931#ifdef FEAT_DIFF
1932 if (l == wp->w_topline)
1933 new_rows += plines_win_nofill(wp, l, TRUE)
1934 + wp->w_topfill;
1935 else
1936#endif
1937 new_rows += plines_win(wp, l, TRUE);
1938 ++j;
1939 if (new_rows > wp->w_height - row - 2)
1940 {
1941 /* it's getting too much, must redraw the rest */
1942 new_rows = 9999;
1943 break;
1944 }
1945 }
1946 xtra_rows = new_rows - old_rows;
1947 if (xtra_rows < 0)
1948 {
1949 /* May scroll text up. If there is not enough
1950 * remaining text or scrolling fails, must redraw the
1951 * rest. If scrolling works, must redraw the text
1952 * below the scrolled text. */
1953 if (row - xtra_rows >= wp->w_height - 2)
1954 mod_bot = MAXLNUM;
1955 else
1956 {
1957 check_for_delay(FALSE);
1958 if (win_del_lines(wp, row,
1959 -xtra_rows, FALSE, FALSE) == FAIL)
1960 mod_bot = MAXLNUM;
1961 else
1962 bot_start = wp->w_height + xtra_rows;
1963 }
1964 }
1965 else if (xtra_rows > 0)
1966 {
1967 /* May scroll text down. If there is not enough
1968 * remaining text of scrolling fails, must redraw the
1969 * rest. */
1970 if (row + xtra_rows >= wp->w_height - 2)
1971 mod_bot = MAXLNUM;
1972 else
1973 {
1974 check_for_delay(FALSE);
1975 if (win_ins_lines(wp, row + old_rows,
1976 xtra_rows, FALSE, FALSE) == FAIL)
1977 mod_bot = MAXLNUM;
1978 else if (top_end > row + old_rows)
1979 /* Scrolled the part at the top that requires
1980 * updating down. */
1981 top_end += xtra_rows;
1982 }
1983 }
1984
1985 /* When not updating the rest, may need to move w_lines[]
1986 * entries. */
1987 if (mod_bot != MAXLNUM && i != j)
1988 {
1989 if (j < i)
1990 {
1991 int x = row + new_rows;
1992
1993 /* move entries in w_lines[] upwards */
1994 for (;;)
1995 {
1996 /* stop at last valid entry in w_lines[] */
1997 if (i >= wp->w_lines_valid)
1998 {
1999 wp->w_lines_valid = j;
2000 break;
2001 }
2002 wp->w_lines[j] = wp->w_lines[i];
2003 /* stop at a line that won't fit */
2004 if (x + (int)wp->w_lines[j].wl_size
2005 > wp->w_height)
2006 {
2007 wp->w_lines_valid = j + 1;
2008 break;
2009 }
2010 x += wp->w_lines[j++].wl_size;
2011 ++i;
2012 }
2013 if (bot_start > x)
2014 bot_start = x;
2015 }
2016 else /* j > i */
2017 {
2018 /* move entries in w_lines[] downwards */
2019 j -= i;
2020 wp->w_lines_valid += j;
2021 if (wp->w_lines_valid > wp->w_height)
2022 wp->w_lines_valid = wp->w_height;
2023 for (i = wp->w_lines_valid; i - j >= idx; --i)
2024 wp->w_lines[i] = wp->w_lines[i - j];
2025
2026 /* The w_lines[] entries for inserted lines are
2027 * now invalid, but wl_size may be used above.
2028 * Reset to zero. */
2029 while (i >= idx)
2030 {
2031 wp->w_lines[i].wl_size = 0;
2032 wp->w_lines[i--].wl_valid = FALSE;
2033 }
2034 }
2035 }
2036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038
2039#ifdef FEAT_FOLDING
2040 /*
2041 * When lines are folded, display one line for all of them.
2042 * Otherwise, display normally (can be several display lines when
2043 * 'wrap' is on).
2044 */
2045 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2046 if (fold_count != 0)
2047 {
2048 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2049 ++row;
2050 --fold_count;
2051 wp->w_lines[idx].wl_folded = TRUE;
2052 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2053# ifdef FEAT_SYN_HL
2054 did_update = DID_FOLD;
2055# endif
2056 }
2057 else
2058#endif
2059 if (idx < wp->w_lines_valid
2060 && wp->w_lines[idx].wl_valid
2061 && wp->w_lines[idx].wl_lnum == lnum
2062 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002063 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 && srow + wp->w_lines[idx].wl_size > wp->w_height
2065#ifdef FEAT_DIFF
2066 && diff_check_fill(wp, lnum) == 0
2067#endif
2068 )
2069 {
2070 /* This line is not going to fit. Don't draw anything here,
2071 * will draw "@ " lines below. */
2072 row = wp->w_height + 1;
2073 }
2074 else
2075 {
2076#ifdef FEAT_SEARCH_EXTRA
2077 prepare_search_hl(wp, lnum);
2078#endif
2079#ifdef FEAT_SYN_HL
2080 /* Let the syntax stuff know we skipped a few lines. */
2081 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002082 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 syntax_end_parsing(syntax_last_parsed + 1);
2084#endif
2085
2086 /*
2087 * Display one line.
2088 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002089 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091#ifdef FEAT_FOLDING
2092 wp->w_lines[idx].wl_folded = FALSE;
2093 wp->w_lines[idx].wl_lastlnum = lnum;
2094#endif
2095#ifdef FEAT_SYN_HL
2096 did_update = DID_LINE;
2097 syntax_last_parsed = lnum;
2098#endif
2099 }
2100
2101 wp->w_lines[idx].wl_lnum = lnum;
2102 wp->w_lines[idx].wl_valid = TRUE;
2103 if (row > wp->w_height) /* past end of screen */
2104 {
2105 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002106 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2108 ++idx;
2109 break;
2110 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002111 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 wp->w_lines[idx].wl_size = row - srow;
2113 ++idx;
2114#ifdef FEAT_FOLDING
2115 lnum += fold_count + 1;
2116#else
2117 ++lnum;
2118#endif
2119 }
2120 else
2121 {
2122 /* This line does not need updating, advance to the next one */
2123 row += wp->w_lines[idx++].wl_size;
2124 if (row > wp->w_height) /* past end of screen */
2125 break;
2126#ifdef FEAT_FOLDING
2127 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2128#else
2129 ++lnum;
2130#endif
2131#ifdef FEAT_SYN_HL
2132 did_update = DID_NONE;
2133#endif
2134 }
2135
2136 if (lnum > buf->b_ml.ml_line_count)
2137 {
2138 eof = TRUE;
2139 break;
2140 }
2141 }
2142 /*
2143 * End of loop over all window lines.
2144 */
2145
2146
2147 if (idx > wp->w_lines_valid)
2148 wp->w_lines_valid = idx;
2149
2150#ifdef FEAT_SYN_HL
2151 /*
2152 * Let the syntax stuff know we stop parsing here.
2153 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002154 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 syntax_end_parsing(syntax_last_parsed + 1);
2156#endif
2157
2158 /*
2159 * If we didn't hit the end of the file, and we didn't finish the last
2160 * line we were working on, then the line didn't fit.
2161 */
2162 wp->w_empty_rows = 0;
2163#ifdef FEAT_DIFF
2164 wp->w_filler_rows = 0;
2165#endif
2166 if (!eof && !didline)
2167 {
2168 if (lnum == wp->w_topline)
2169 {
2170 /*
2171 * Single line that does not fit!
2172 * Don't overwrite it, it can be edited.
2173 */
2174 wp->w_botline = lnum + 1;
2175 }
2176#ifdef FEAT_DIFF
2177 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2178 {
2179 /* Window ends in filler lines. */
2180 wp->w_botline = lnum;
2181 wp->w_filler_rows = wp->w_height - srow;
2182 }
2183#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002184 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2185 {
2186 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2187
2188 /*
2189 * Last line isn't finished: Display "@@@" in the last screen line.
2190 */
2191 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002192 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002193 screen_fill(scr_row, scr_row + 1,
2194 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002195 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002196 set_empty_rows(wp, srow);
2197 wp->w_botline = lnum;
2198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2200 {
2201 /*
2202 * Last line isn't finished: Display "@@@" at the end.
2203 */
2204 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2205 W_WINROW(wp) + wp->w_height,
2206 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002207 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 set_empty_rows(wp, srow);
2209 wp->w_botline = lnum;
2210 }
2211 else
2212 {
2213 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2214 wp->w_botline = lnum;
2215 }
2216 }
2217 else
2218 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002219#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 draw_vsep_win(wp, row);
2221#endif
2222 if (eof) /* we hit the end of the file */
2223 {
2224 wp->w_botline = buf->b_ml.ml_line_count + 1;
2225#ifdef FEAT_DIFF
2226 j = diff_check_fill(wp, wp->w_botline);
2227 if (j > 0 && !wp->w_botfill)
2228 {
2229 /*
2230 * Display filler lines at the end of the file
2231 */
2232 if (char2cells(fill_diff) > 1)
2233 i = '-';
2234 else
2235 i = fill_diff;
2236 if (row + j > wp->w_height)
2237 j = wp->w_height - row;
2238 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2239 row += j;
2240 }
2241#endif
2242 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002243 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 wp->w_botline = lnum;
2245
2246 /* make sure the rest of the screen is blank */
2247 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002248 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 }
2250
2251 /* Reset the type of redrawing required, the window has been updated. */
2252 wp->w_redr_type = 0;
2253#ifdef FEAT_DIFF
2254 wp->w_old_topfill = wp->w_topfill;
2255 wp->w_old_botfill = wp->w_botfill;
2256#endif
2257
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002258 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
2260 /*
2261 * There is a trick with w_botline. If we invalidate it on each
2262 * change that might modify it, this will cause a lot of expensive
2263 * calls to plines() in update_topline() each time. Therefore the
2264 * value of w_botline is often approximated, and this value is used to
2265 * compute the value of w_topline. If the value of w_botline was
2266 * wrong, check that the value of w_topline is correct (cursor is on
2267 * the visible part of the text). If it's not, we need to redraw
2268 * again. Mostly this just means scrolling up a few lines, so it
2269 * doesn't look too bad. Only do this for the current window (where
2270 * changes are relevant).
2271 */
2272 wp->w_valid |= VALID_BOTLINE;
2273 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2274 {
2275 recursive = TRUE;
2276 curwin->w_valid &= ~VALID_TOPLINE;
2277 update_topline(); /* may invalidate w_botline again */
2278 if (must_redraw != 0)
2279 {
2280 /* Don't update for changes in buffer again. */
2281 i = curbuf->b_mod_set;
2282 curbuf->b_mod_set = FALSE;
2283 win_update(curwin);
2284 must_redraw = 0;
2285 curbuf->b_mod_set = i;
2286 }
2287 recursive = FALSE;
2288 }
2289 }
2290
2291#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2292 /* restore got_int, unless CTRL-C was hit while redrawing */
2293 if (!got_int)
2294 got_int = save_got_int;
2295#endif
2296}
2297
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298/*
2299 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2300 * as the filler character.
2301 */
2302 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002303win_draw_end(
2304 win_T *wp,
2305 int c1,
2306 int c2,
2307 int row,
2308 int endrow,
2309 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
2311#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2312 int n = 0;
2313# define FDC_OFF n
2314#else
2315# define FDC_OFF 0
2316#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002317#ifdef FEAT_FOLDING
2318 int fdc = compute_foldcolumn(wp, 0);
2319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
2321#ifdef FEAT_RIGHTLEFT
2322 if (wp->w_p_rl)
2323 {
2324 /* No check for cmdline window: should never be right-left. */
2325# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002326 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
2328 if (n > 0)
2329 {
2330 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002331 if (n > W_WIDTH(wp))
2332 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2334 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002335 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
2337# endif
2338# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002339 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
2341 int nn = n + 2;
2342
2343 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002344 if (nn > W_WIDTH(wp))
2345 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2347 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002348 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 n = nn;
2350 }
2351# endif
2352 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2353 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002354 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2356 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002357 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359 else
2360#endif
2361 {
2362#ifdef FEAT_CMDWIN
2363 if (cmdwin_type != 0 && wp == curwin)
2364 {
2365 /* draw the cmdline character in the leftmost column */
2366 n = 1;
2367 if (n > wp->w_width)
2368 n = wp->w_width;
2369 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2370 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002371 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373#endif
2374#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002375 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002377 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379 /* draw the fold column at the left */
2380 if (nn > W_WIDTH(wp))
2381 nn = W_WIDTH(wp);
2382 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2383 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002384 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 n = nn;
2386 }
2387#endif
2388#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002389 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
2391 int nn = n + 2;
2392
2393 /* draw the sign column after the fold column */
2394 if (nn > W_WIDTH(wp))
2395 nn = W_WIDTH(wp);
2396 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2397 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002398 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 n = nn;
2400 }
2401#endif
2402 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2403 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002404 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 }
2406 set_empty_rows(wp, row);
2407}
2408
Bram Moolenaar1a384422010-07-14 19:53:30 +02002409#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002410static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002411
2412/*
2413 * Advance **color_cols and return TRUE when there are columns to draw.
2414 */
2415 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002416advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002417{
2418 while (**color_cols >= 0 && vcol > **color_cols)
2419 ++*color_cols;
2420 return (**color_cols >= 0);
2421}
2422#endif
2423
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424#ifdef FEAT_FOLDING
2425/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002426 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2427 * space is available for window "wp", minus "col".
2428 */
2429 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002430compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002431{
2432 int fdc = wp->w_p_fdc;
2433 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2434 int wwidth = W_WIDTH(wp);
2435
2436 if (fdc > wwidth - (col + wmw))
2437 fdc = wwidth - (col + wmw);
2438 return fdc;
2439}
2440
2441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 * Display one folded line.
2443 */
2444 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002445fold_line(
2446 win_T *wp,
2447 long fold_count,
2448 foldinfo_T *foldinfo,
2449 linenr_T lnum,
2450 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002452 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 pos_T *top, *bot;
2454 linenr_T lnume = lnum + fold_count - 1;
2455 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002456 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 int col;
2459 int txtcol;
2460 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002461 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462
2463 /* Build the fold line:
2464 * 1. Add the cmdwin_type for the command-line window
2465 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002466 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 * 4. Compose the text
2468 * 5. Add the text
2469 * 6. set highlighting for the Visual area an other text
2470 */
2471 col = 0;
2472
2473 /*
2474 * 1. Add the cmdwin_type for the command-line window
2475 * Ignores 'rightleft', this window is never right-left.
2476 */
2477#ifdef FEAT_CMDWIN
2478 if (cmdwin_type != 0 && wp == curwin)
2479 {
2480 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002481 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482#ifdef FEAT_MBYTE
2483 if (enc_utf8)
2484 ScreenLinesUC[off] = 0;
2485#endif
2486 ++col;
2487 }
2488#endif
2489
2490 /*
2491 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002492 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002494 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 if (fdc > 0)
2496 {
2497 fill_foldcolumn(buf, wp, TRUE, lnum);
2498#ifdef FEAT_RIGHTLEFT
2499 if (wp->w_p_rl)
2500 {
2501 int i;
2502
2503 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002504 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 /* reverse the fold column */
2506 for (i = 0; i < fdc; ++i)
2507 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2508 }
2509 else
2510#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002511 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 col += fdc;
2513 }
2514
2515#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002516# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2517 for (ri = 0; ri < l; ++ri) \
2518 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2519 else \
2520 for (ri = 0; ri < l; ++ri) \
2521 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002523# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2524 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525#endif
2526
Bram Moolenaar64486672010-05-16 15:46:46 +02002527 /* Set all attributes of the 'number' or 'relativenumber' column and the
2528 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002529 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531#ifdef FEAT_SIGNS
2532 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002533 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
2535 len = W_WIDTH(wp) - col;
2536 if (len > 0)
2537 {
2538 if (len > 2)
2539 len = 2;
2540# ifdef FEAT_RIGHTLEFT
2541 if (wp->w_p_rl)
2542 /* the line number isn't reversed */
2543 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002544 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 else
2546# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002547 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 col += len;
2549 }
2550 }
2551#endif
2552
2553 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002554 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002556 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
2558 len = W_WIDTH(wp) - col;
2559 if (len > 0)
2560 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002561 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002562 long num;
2563 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002564
2565 if (len > w + 1)
2566 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002567
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002568 if (wp->w_p_nu && !wp->w_p_rnu)
2569 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002570 num = (long)lnum;
2571 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002572 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002573 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002574 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002575 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002576 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002577 /* 'number' + 'relativenumber': cursor line shows absolute
2578 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002579 num = lnum;
2580 fmt = "%-*ld ";
2581 }
2582 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002583
Bram Moolenaar700e7342013-01-30 12:31:36 +01002584 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585#ifdef FEAT_RIGHTLEFT
2586 if (wp->w_p_rl)
2587 /* the line number isn't reversed */
2588 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002589 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 else
2591#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002592 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 col += len;
2594 }
2595 }
2596
2597 /*
2598 * 4. Compose the folded-line string with 'foldtext', if set.
2599 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002600 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
2602 txtcol = col; /* remember where text starts */
2603
2604 /*
2605 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2606 * Right-left text is put in columns 0 - number-col, normal text is put
2607 * in columns number-col - window-width.
2608 */
2609#ifdef FEAT_MBYTE
2610 if (has_mbyte)
2611 {
2612 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002613 int u8c, u8cc[MAX_MCO];
2614 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 int idx;
2616 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002617 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618# ifdef FEAT_ARABIC
2619 int prev_c = 0; /* previous Arabic character */
2620 int prev_c1 = 0; /* first composing char for prev_c */
2621# endif
2622
2623# ifdef FEAT_RIGHTLEFT
2624 if (wp->w_p_rl)
2625 idx = off;
2626 else
2627# endif
2628 idx = off + col;
2629
2630 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2631 for (p = text; *p != NUL; )
2632 {
2633 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002634 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if (col + cells > W_WIDTH(wp)
2636# ifdef FEAT_RIGHTLEFT
2637 - (wp->w_p_rl ? col : 0)
2638# endif
2639 )
2640 break;
2641 ScreenLines[idx] = *p;
2642 if (enc_utf8)
2643 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002644 u8c = utfc_ptr2char(p, u8cc);
2645 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
2647 ScreenLinesUC[idx] = 0;
2648#ifdef FEAT_ARABIC
2649 prev_c = u8c;
2650#endif
2651 }
2652 else
2653 {
2654#ifdef FEAT_ARABIC
2655 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2656 {
2657 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002658 int pc, pc1, nc;
2659 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 int firstbyte = *p;
2661
2662 /* The idea of what is the previous and next
2663 * character depends on 'rightleft'. */
2664 if (wp->w_p_rl)
2665 {
2666 pc = prev_c;
2667 pc1 = prev_c1;
2668 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002669 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
2671 else
2672 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002673 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002675 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
2677 prev_c = u8c;
2678
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002679 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 pc, pc1, nc);
2681 ScreenLines[idx] = firstbyte;
2682 }
2683 else
2684 prev_c = u8c;
2685#endif
2686 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002687#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 if (u8c >= 0x10000)
2689 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2690 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002693 for (i = 0; i < Screen_mco; ++i)
2694 {
2695 ScreenLinesC[i][idx] = u8cc[i];
2696 if (u8cc[i] == 0)
2697 break;
2698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 }
2700 if (cells > 1)
2701 ScreenLines[idx + 1] = 0;
2702 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002703 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2704 /* double-byte single width character */
2705 ScreenLines2[idx] = p[1];
2706 else if (cells > 1)
2707 /* double-width character */
2708 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 col += cells;
2710 idx += cells;
2711 p += c_len;
2712 }
2713 }
2714 else
2715#endif
2716 {
2717 len = (int)STRLEN(text);
2718 if (len > W_WIDTH(wp) - col)
2719 len = W_WIDTH(wp) - col;
2720 if (len > 0)
2721 {
2722#ifdef FEAT_RIGHTLEFT
2723 if (wp->w_p_rl)
2724 STRNCPY(current_ScreenLine, text, len);
2725 else
2726#endif
2727 STRNCPY(current_ScreenLine + col, text, len);
2728 col += len;
2729 }
2730 }
2731
2732 /* Fill the rest of the line with the fold filler */
2733#ifdef FEAT_RIGHTLEFT
2734 if (wp->w_p_rl)
2735 col -= txtcol;
2736#endif
2737 while (col < W_WIDTH(wp)
2738#ifdef FEAT_RIGHTLEFT
2739 - (wp->w_p_rl ? txtcol : 0)
2740#endif
2741 )
2742 {
2743#ifdef FEAT_MBYTE
2744 if (enc_utf8)
2745 {
2746 if (fill_fold >= 0x80)
2747 {
2748 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002749 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002750 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 }
2752 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002753 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002755 ScreenLines[off + col] = fill_fold;
2756 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002757 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002759 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002761 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
2763
2764 if (text != buf)
2765 vim_free(text);
2766
2767 /*
2768 * 6. set highlighting for the Visual area an other text.
2769 * If all folded lines are in the Visual area, highlight the line.
2770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2772 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002773 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
2775 /* Visual is after curwin->w_cursor */
2776 top = &curwin->w_cursor;
2777 bot = &VIsual;
2778 }
2779 else
2780 {
2781 /* Visual is before curwin->w_cursor */
2782 top = &VIsual;
2783 bot = &curwin->w_cursor;
2784 }
2785 if (lnum >= top->lnum
2786 && lnume <= bot->lnum
2787 && (VIsual_mode != 'v'
2788 || ((lnum > top->lnum
2789 || (lnum == top->lnum
2790 && top->col == 0))
2791 && (lnume < bot->lnum
2792 || (lnume == bot->lnum
2793 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002794 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 {
2796 if (VIsual_mode == Ctrl_V)
2797 {
2798 /* Visual block mode: highlight the chars part of the block */
2799 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2800 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002801 if (wp->w_old_cursor_lcol != MAXCOL
2802 && wp->w_old_cursor_lcol + txtcol
2803 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 len = wp->w_old_cursor_lcol;
2805 else
2806 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002807 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002808 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
2810 }
2811 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002812 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002814 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
2817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002819#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002820 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2821 if (wp->w_p_cc_cols)
2822 {
2823 int i = 0;
2824 int j = wp->w_p_cc_cols[i];
2825 int old_txtcol = txtcol;
2826
2827 while (j > -1)
2828 {
2829 txtcol += j;
2830 if (wp->w_p_wrap)
2831 txtcol -= wp->w_skipcol;
2832 else
2833 txtcol -= wp->w_leftcol;
2834 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2835 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002836 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002837 txtcol = old_txtcol;
2838 j = wp->w_p_cc_cols[++i];
2839 }
2840 }
2841
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002842 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002843 if (wp->w_p_cuc)
2844 {
2845 txtcol += wp->w_virtcol;
2846 if (wp->w_p_wrap)
2847 txtcol -= wp->w_skipcol;
2848 else
2849 txtcol -= wp->w_leftcol;
2850 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2851 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002852 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002853 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
2856 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2857 (int)W_WIDTH(wp), FALSE);
2858
2859 /*
2860 * Update w_cline_height and w_cline_folded if the cursor line was
2861 * updated (saves a call to plines() later).
2862 */
2863 if (wp == curwin
2864 && lnum <= curwin->w_cursor.lnum
2865 && lnume >= curwin->w_cursor.lnum)
2866 {
2867 curwin->w_cline_row = row;
2868 curwin->w_cline_height = 1;
2869 curwin->w_cline_folded = TRUE;
2870 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2871 }
2872}
2873
2874/*
2875 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2876 */
2877 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002878copy_text_attr(
2879 int off,
2880 char_u *buf,
2881 int len,
2882 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002884 int i;
2885
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 mch_memmove(ScreenLines + off, buf, (size_t)len);
2887# ifdef FEAT_MBYTE
2888 if (enc_utf8)
2889 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2890# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002891 for (i = 0; i < len; ++i)
2892 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893}
2894
2895/*
2896 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002897 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 */
2899 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002900fill_foldcolumn(
2901 char_u *p,
2902 win_T *wp,
2903 int closed, /* TRUE of FALSE */
2904 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 int i = 0;
2907 int level;
2908 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002909 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002910 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
2912 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002913 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
2915 level = win_foldinfo.fi_level;
2916 if (level > 0)
2917 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002918 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002919 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002920
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 /* If the column is too narrow, we start at the lowest level that
2922 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002923 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 if (first_level < 1)
2925 first_level = 1;
2926
Bram Moolenaar1c934292015-01-27 16:39:29 +01002927 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 {
2929 if (win_foldinfo.fi_lnum == lnum
2930 && first_level + i >= win_foldinfo.fi_low_level)
2931 p[i] = '-';
2932 else if (first_level == 1)
2933 p[i] = '|';
2934 else if (first_level + i <= 9)
2935 p[i] = '0' + first_level + i;
2936 else
2937 p[i] = '>';
2938 if (first_level + i == level)
2939 break;
2940 }
2941 }
2942 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002943 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944}
2945#endif /* FEAT_FOLDING */
2946
2947/*
2948 * Display line "lnum" of window 'wp' on the screen.
2949 * Start at row "startrow", stop when "endrow" is reached.
2950 * wp->w_virtcol needs to be valid.
2951 *
2952 * Return the number of last row the line occupies.
2953 */
2954 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002955win_line(
2956 win_T *wp,
2957 linenr_T lnum,
2958 int startrow,
2959 int endrow,
2960 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01002962 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2964 int c = 0; /* init for GCC */
2965 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002966#ifdef FEAT_LINEBREAK
2967 long vcol_sbr = -1; /* virtual column after showbreak */
2968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 long vcol_prev = -1; /* "vcol" of previous character */
2970 char_u *line; /* current line */
2971 char_u *ptr; /* current position in "line" */
2972 int row; /* row in the window, excl w_winrow */
2973 int screen_row; /* row on the screen, incl w_winrow */
2974
2975 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2976 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002977 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002978 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 int c_extra = NUL; /* extra chars, all the same */
2980 int extra_attr = 0; /* attributes when n_extra != 0 */
2981 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2982 displaying lcs_eol at end-of-line */
2983 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2984 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2985
2986 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2987 int saved_n_extra = 0;
2988 char_u *saved_p_extra = NULL;
2989 int saved_c_extra = 0;
2990 int saved_char_attr = 0;
2991
2992 int n_attr = 0; /* chars with special attr */
2993 int saved_attr2 = 0; /* char_attr saved for n_attr */
2994 int n_attr3 = 0; /* chars with overruling special attr */
2995 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2996
2997 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2998
2999 int fromcol, tocol; /* start/end of inverting */
3000 int fromcol_prev = -2; /* start of inverting after cursor */
3001 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003003 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 pos_T pos;
3005 long v;
3006
3007 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003008 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3010 in this line */
3011 int attr = 0; /* attributes for area highlighting */
3012 int area_attr = 0; /* attributes desired by highlighting */
3013 int search_attr = 0; /* attributes desired by 'hlsearch' */
3014#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003015 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 int syntax_attr = 0; /* attributes desired by syntax */
3017 int has_syntax = FALSE; /* this buffer has syntax highl. */
3018 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003019 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003020 int draw_color_col = FALSE; /* highlight colorcolumn */
3021 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003022#endif
3023#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003024 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003025# define SPWORDLEN 150
3026 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003027 int nextlinecol = 0; /* column where nextline[] starts */
3028 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003029 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003030 int spell_attr = 0; /* attributes desired by spelling */
3031 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003032 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3033 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003034 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003035 static int cap_col = -1; /* column to check for Cap word */
3036 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003037 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038#endif
3039 int extra_check; /* has syntax or linebreak */
3040#ifdef FEAT_MBYTE
3041 int multi_attr = 0; /* attributes desired by multibyte */
3042 int mb_l = 1; /* multi-byte byte length */
3043 int mb_c = 0; /* decoded multi-byte character */
3044 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003045 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046#endif
3047#ifdef FEAT_DIFF
3048 int filler_lines; /* nr of filler lines to be drawn */
3049 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003050 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 int change_start = MAXCOL; /* first col of changed area */
3052 int change_end = -1; /* last col of changed area */
3053#endif
3054 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3055#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003056 int need_showbreak = FALSE; /* overlong line, skipping first x
3057 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003059#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3060 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003062 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063#endif
3064#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003065 matchitem_T *cur; /* points to the match list */
3066 match_T *shl; /* points to search_hl or a match */
3067 int shl_flag; /* flag to indicate whether search_hl
3068 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003069 int pos_inprogress; /* marks that position match search is
3070 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003071 int prevcol_hl_flag; /* flag to indicate whether prevcol
3072 equals startcol of search_hl or one
3073 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074#endif
3075#ifdef FEAT_ARABIC
3076 int prev_c = 0; /* previous Arabic character */
3077 int prev_c1 = 0; /* first composing char for prev_c */
3078#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003079#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003080 int did_line_attr = 0;
3081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
3083 /* draw_state: items that are drawn in sequence: */
3084#define WL_START 0 /* nothing done yet */
3085#ifdef FEAT_CMDWIN
3086# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3087#else
3088# define WL_CMDLINE WL_START
3089#endif
3090#ifdef FEAT_FOLDING
3091# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3092#else
3093# define WL_FOLD WL_CMDLINE
3094#endif
3095#ifdef FEAT_SIGNS
3096# define WL_SIGN WL_FOLD + 1 /* column for signs */
3097#else
3098# define WL_SIGN WL_FOLD /* column for signs */
3099#endif
3100#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003101#ifdef FEAT_LINEBREAK
3102# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003104# define WL_BRI WL_NR
3105#endif
3106#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3107# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3108#else
3109# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110#endif
3111#define WL_LINE WL_SBR + 1 /* text in the line */
3112 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003113#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 int feedback_col = 0;
3115 int feedback_old_attr = -1;
3116#endif
3117
Bram Moolenaar860cae12010-06-05 23:22:07 +02003118#ifdef FEAT_CONCEAL
3119 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003120 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003121 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003122 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003123 int is_concealing = FALSE;
3124 int boguscols = 0; /* nonexistent columns added to force
3125 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003126 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003127 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003128 int match_conc = 0; /* cchar for match functions */
3129 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003130 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003131# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003132# define FIX_FOR_BOGUSCOLS \
3133 { \
3134 n_extra += vcol_off; \
3135 vcol -= vcol_off; \
3136 vcol_off = 0; \
3137 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003138 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003139 boguscols = 0; \
3140 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003141#else
3142# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144
3145 if (startrow > endrow) /* past the end already! */
3146 return startrow;
3147
3148 row = startrow;
3149 screen_row = row + W_WINROW(wp);
3150
3151 /*
3152 * To speed up the loop below, set extra_check when there is linebreak,
3153 * trailing white space and/or syntax processing to be done.
3154 */
3155#ifdef FEAT_LINEBREAK
3156 extra_check = wp->w_p_lbr;
3157#else
3158 extra_check = 0;
3159#endif
3160#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003161 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
3163 /* Prepare for syntax highlighting in this line. When there is an
3164 * error, stop syntax highlighting. */
3165 save_did_emsg = did_emsg;
3166 did_emsg = FALSE;
3167 syntax_start(wp, lnum);
3168 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003169 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 else
3171 {
3172 did_emsg = save_did_emsg;
3173 has_syntax = TRUE;
3174 extra_check = TRUE;
3175 }
3176 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003177
3178 /* Check for columns to display for 'colorcolumn'. */
3179 color_cols = wp->w_p_cc_cols;
3180 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003181 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003182#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003183
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003184#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003185 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003186 && *wp->w_s->b_p_spl != NUL
3187 && wp->w_s->b_langp.ga_len > 0
3188 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003189 {
3190 /* Prepare for spell checking. */
3191 has_spell = TRUE;
3192 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003193
3194 /* Get the start of the next line, so that words that wrap to the next
3195 * line are found too: "et<line-break>al.".
3196 * Trick: skip a few chars for C/shell/Vim comments */
3197 nextline[SPWORDLEN] = NUL;
3198 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3199 {
3200 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3201 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3202 }
3203
3204 /* When a word wrapped from the previous line the start of the current
3205 * line is valid. */
3206 if (lnum == checked_lnum)
3207 cur_checked_col = checked_col;
3208 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003209
3210 /* When there was a sentence end in the previous line may require a
3211 * word starting with capital in this line. In line 1 always check
3212 * the first word. */
3213 if (lnum != capcol_lnum)
3214 cap_col = -1;
3215 if (lnum == 1)
3216 cap_col = 0;
3217 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219#endif
3220
3221 /*
3222 * handle visual active in this window
3223 */
3224 fromcol = -10;
3225 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3227 {
3228 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003229 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
3231 top = &curwin->w_cursor;
3232 bot = &VIsual;
3233 }
3234 else /* Visual is before curwin->w_cursor */
3235 {
3236 top = &VIsual;
3237 bot = &curwin->w_cursor;
3238 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003239 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 if (VIsual_mode == Ctrl_V) /* block mode */
3241 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003242 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 {
3244 fromcol = wp->w_old_cursor_fcol;
3245 tocol = wp->w_old_cursor_lcol;
3246 }
3247 }
3248 else /* non-block mode */
3249 {
3250 if (lnum > top->lnum && lnum <= bot->lnum)
3251 fromcol = 0;
3252 else if (lnum == top->lnum)
3253 {
3254 if (VIsual_mode == 'V') /* linewise */
3255 fromcol = 0;
3256 else
3257 {
3258 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3259 if (gchar_pos(top) == NUL)
3260 tocol = fromcol + 1;
3261 }
3262 }
3263 if (VIsual_mode != 'V' && lnum == bot->lnum)
3264 {
3265 if (*p_sel == 'e' && bot->col == 0
3266#ifdef FEAT_VIRTUALEDIT
3267 && bot->coladd == 0
3268#endif
3269 )
3270 {
3271 fromcol = -10;
3272 tocol = MAXCOL;
3273 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003274 else if (bot->col == MAXCOL)
3275 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 else
3277 {
3278 pos = *bot;
3279 if (*p_sel == 'e')
3280 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3281 else
3282 {
3283 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3284 ++tocol;
3285 }
3286 }
3287 }
3288 }
3289
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 /* Check if the character under the cursor should not be inverted */
3291 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003292#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 )
3296 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297
3298 /* if inverting in this line set area_highlighting */
3299 if (fromcol >= 0)
3300 {
3301 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003302 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003304 if ((clip_star.available && !clip_star.owned
3305 && clip_isautosel_star())
3306 || (clip_plus.available && !clip_plus.owned
3307 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003308 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#endif
3310 }
3311 }
3312
3313 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003314 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003316 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 && wp == curwin
3318 && lnum >= curwin->w_cursor.lnum
3319 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3320 {
3321 if (lnum == curwin->w_cursor.lnum)
3322 getvcol(curwin, &(curwin->w_cursor),
3323 (colnr_T *)&fromcol, NULL, NULL);
3324 else
3325 fromcol = 0;
3326 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3327 {
3328 pos.lnum = lnum;
3329 pos.col = search_match_endcol;
3330 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3331 }
3332 else
3333 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003334 /* do at least one character; happens when past end of line */
3335 if (fromcol == tocol)
3336 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003338 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 }
3340
3341#ifdef FEAT_DIFF
3342 filler_lines = diff_check(wp, lnum);
3343 if (filler_lines < 0)
3344 {
3345 if (filler_lines == -1)
3346 {
3347 if (diff_find_change(wp, lnum, &change_start, &change_end))
3348 diff_hlf = HLF_ADD; /* added line */
3349 else if (change_start == 0)
3350 diff_hlf = HLF_TXD; /* changed text */
3351 else
3352 diff_hlf = HLF_CHD; /* changed line */
3353 }
3354 else
3355 diff_hlf = HLF_ADD; /* added line */
3356 filler_lines = 0;
3357 area_highlighting = TRUE;
3358 }
3359 if (lnum == wp->w_topline)
3360 filler_lines = wp->w_topfill;
3361 filler_todo = filler_lines;
3362#endif
3363
3364#ifdef LINE_ATTR
3365# ifdef FEAT_SIGNS
3366 /* If this line has a sign with line highlighting set line_attr. */
3367 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3368 if (v != 0)
3369 line_attr = sign_get_attr((int)v, TRUE);
3370# endif
3371# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3372 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003373 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003374 line_attr = HL_ATTR(HLF_L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375# endif
3376 if (line_attr != 0)
3377 area_highlighting = TRUE;
3378#endif
3379
3380 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3381 ptr = line;
3382
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003383#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003384 if (has_spell)
3385 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003386 /* For checking first word with a capital skip white space. */
3387 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003388 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003389
Bram Moolenaar30abd282005-06-22 22:35:10 +00003390 /* To be able to spell-check over line boundaries copy the end of the
3391 * current line into nextline[]. Above the start of the next line was
3392 * copied to nextline[SPWORDLEN]. */
3393 if (nextline[SPWORDLEN] == NUL)
3394 {
3395 /* No next line or it is empty. */
3396 nextlinecol = MAXCOL;
3397 nextline_idx = 0;
3398 }
3399 else
3400 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003401 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003402 if (v < SPWORDLEN)
3403 {
3404 /* Short line, use it completely and append the start of the
3405 * next line. */
3406 nextlinecol = 0;
3407 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003408 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003409 nextline_idx = v + 1;
3410 }
3411 else
3412 {
3413 /* Long line, use only the last SPWORDLEN bytes. */
3414 nextlinecol = v - SPWORDLEN;
3415 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3416 nextline_idx = SPWORDLEN + 1;
3417 }
3418 }
3419 }
3420#endif
3421
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003422 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003424 if (lcs_space || lcs_trail)
3425 extra_check = TRUE;
3426 /* find start of trailing whitespace */
3427 if (lcs_trail)
3428 {
3429 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003430 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003431 --trailcol;
3432 trailcol += (colnr_T) (ptr - line);
3433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 }
3435
3436 /*
3437 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3438 * first character to be displayed.
3439 */
3440 if (wp->w_p_wrap)
3441 v = wp->w_skipcol;
3442 else
3443 v = wp->w_leftcol;
3444 if (v > 0)
3445 {
3446#ifdef FEAT_MBYTE
3447 char_u *prev_ptr = ptr;
3448#endif
3449 while (vcol < v && *ptr != NUL)
3450 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003451 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 vcol += c;
3453#ifdef FEAT_MBYTE
3454 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003456 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 }
3458
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003459 /* When:
3460 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003461 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003462 * - 'virtualedit' is set, or
3463 * - the visual mode is active,
3464 * the end of the line may be before the start of the displayed part.
3465 */
3466 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003467#ifdef FEAT_SYN_HL
3468 wp->w_p_cuc || draw_color_col ||
3469#endif
3470#ifdef FEAT_VIRTUALEDIT
3471 virtual_active() ||
3472#endif
3473 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003474 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
3478 /* Handle a character that's not completely on the screen: Put ptr at
3479 * that character but skip the first few screen characters. */
3480 if (vcol > v)
3481 {
3482 vcol -= c;
3483#ifdef FEAT_MBYTE
3484 ptr = prev_ptr;
3485#else
3486 --ptr;
3487#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003488 /* If the character fits on the screen, don't need to skip it.
3489 * Except for a TAB. */
3490 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003491#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003492 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003493#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003494 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003495 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497
3498 /*
3499 * Adjust for when the inverted text is before the screen,
3500 * and when the start of the inverted text is before the screen.
3501 */
3502 if (tocol <= vcol)
3503 fromcol = 0;
3504 else if (fromcol >= 0 && fromcol < vcol)
3505 fromcol = vcol;
3506
3507#ifdef FEAT_LINEBREAK
3508 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3509 if (wp->w_p_wrap)
3510 need_showbreak = TRUE;
3511#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003512#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003513 /* When spell checking a word we need to figure out the start of the
3514 * word and if it's badly spelled or not. */
3515 if (has_spell)
3516 {
3517 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003518 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003519 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003520
3521 pos = wp->w_cursor;
3522 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003523 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003524 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003525
3526 /* spell_move_to() may call ml_get() and make "line" invalid */
3527 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3528 ptr = line + linecol;
3529
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003530 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003531 {
3532 /* no bad word found at line start, don't check until end of a
3533 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003534 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003535 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003536 }
3537 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003538 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003539 /* bad word found, use attributes until end of word */
3540 word_end = wp->w_cursor.col + len + 1;
3541
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003542 /* Turn index into actual attributes. */
3543 if (spell_hlf != HLF_COUNT)
3544 spell_attr = highlight_attr[spell_hlf];
3545 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003546 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003547
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003548# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003549 /* Need to restart syntax highlighting for this line. */
3550 if (has_syntax)
3551 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003552# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003553 }
3554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
3556
3557 /*
3558 * Correct highlighting for cursor that can't be disabled.
3559 * Avoids having to check this for each character.
3560 */
3561 if (fromcol >= 0)
3562 {
3563 if (noinvcur)
3564 {
3565 if ((colnr_T)fromcol == wp->w_virtcol)
3566 {
3567 /* highlighting starts at cursor, let it start just after the
3568 * cursor */
3569 fromcol_prev = fromcol;
3570 fromcol = -1;
3571 }
3572 else if ((colnr_T)fromcol < wp->w_virtcol)
3573 /* restart highlighting after the cursor */
3574 fromcol_prev = wp->w_virtcol;
3575 }
3576 if (fromcol >= tocol)
3577 fromcol = -1;
3578 }
3579
3580#ifdef FEAT_SEARCH_EXTRA
3581 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003582 * Handle highlighting the last used search pattern and matches.
3583 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003585 cur = wp->w_match_head;
3586 shl_flag = FALSE;
3587 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003589 if (shl_flag == FALSE)
3590 {
3591 shl = &search_hl;
3592 shl_flag = TRUE;
3593 }
3594 else
3595 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003596 shl->startcol = MAXCOL;
3597 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003599 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003600 v = (long)(ptr - line);
3601 if (cur != NULL)
3602 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003603 next_search_hl(wp, shl, lnum, (colnr_T)v,
3604 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003605
3606 /* Need to get the line again, a multi-line regexp may have made it
3607 * invalid. */
3608 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3609 ptr = line + v;
3610
3611 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003613 if (shl->lnum == lnum)
3614 shl->startcol = shl->rm.startpos[0].col;
3615 else
3616 shl->startcol = 0;
3617 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3618 - shl->rm.startpos[0].lnum)
3619 shl->endcol = shl->rm.endpos[0].col;
3620 else
3621 shl->endcol = MAXCOL;
3622 /* Highlight one character for an empty match. */
3623 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003626 if (has_mbyte && line[shl->endcol] != NUL)
3627 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3628 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003630 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003632 if ((long)shl->startcol < v) /* match at leftcol */
3633 {
3634 shl->attr_cur = shl->attr;
3635 search_attr = shl->attr;
3636 }
3637 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003639 if (shl != &search_hl && cur != NULL)
3640 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 }
3642#endif
3643
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003644#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003645 /* Cursor line highlighting for 'cursorline' in the current window. Not
3646 * when Visual mode is active, because it's not clear what is selected
3647 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003648 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003649 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003650 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003651 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003652 area_highlighting = TRUE;
3653 }
3654#endif
3655
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003656 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 col = 0;
3658#ifdef FEAT_RIGHTLEFT
3659 if (wp->w_p_rl)
3660 {
3661 /* Rightleft window: process the text in the normal direction, but put
3662 * it in current_ScreenLine[] from right to left. Start at the
3663 * rightmost column of the window. */
3664 col = W_WIDTH(wp) - 1;
3665 off += col;
3666 }
3667#endif
3668
3669 /*
3670 * Repeat for the whole displayed line.
3671 */
3672 for (;;)
3673 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003674#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003675 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 /* Skip this quickly when working on the text. */
3678 if (draw_state != WL_LINE)
3679 {
3680#ifdef FEAT_CMDWIN
3681 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3682 {
3683 draw_state = WL_CMDLINE;
3684 if (cmdwin_type != 0 && wp == curwin)
3685 {
3686 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003688 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003689 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691 }
3692#endif
3693
3694#ifdef FEAT_FOLDING
3695 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3696 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003697 int fdc = compute_foldcolumn(wp, 0);
3698
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003700 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003702 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003703 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003704 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003705 p_extra_free = alloc(12 + 1);
3706
3707 if (p_extra_free != NULL)
3708 {
3709 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3710 n_extra = fdc;
3711 p_extra_free[n_extra] = NUL;
3712 p_extra = p_extra_free;
3713 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003714 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717 }
3718#endif
3719
3720#ifdef FEAT_SIGNS
3721 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3722 {
3723 draw_state = WL_SIGN;
3724 /* Show the sign column when there are any signs in this
3725 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003726 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003728 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003730 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731# endif
3732
3733 /* Draw two cells with the sign value or blank. */
3734 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003735 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 n_extra = 2;
3737
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003738 if (row == startrow
3739#ifdef FEAT_DIFF
3740 + filler_lines && filler_todo <= 0
3741#endif
3742 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
3744 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3745 SIGN_TEXT);
3746# ifdef FEAT_SIGN_ICONS
3747 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3748 SIGN_ICON);
3749 if (gui.in_use && icon_sign != 0)
3750 {
3751 /* Use the image in this position. */
3752 c_extra = SIGN_BYTE;
3753# ifdef FEAT_NETBEANS_INTG
3754 if (buf_signcount(wp->w_buffer, lnum) > 1)
3755 c_extra = MULTISIGN_BYTE;
3756# endif
3757 char_attr = icon_sign;
3758 }
3759 else
3760# endif
3761 if (text_sign != 0)
3762 {
3763 p_extra = sign_get_text(text_sign);
3764 if (p_extra != NULL)
3765 {
3766 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003767 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769 char_attr = sign_get_attr(text_sign, FALSE);
3770 }
3771 }
3772 }
3773 }
3774#endif
3775
3776 if (draw_state == WL_NR - 1 && n_extra == 0)
3777 {
3778 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003779 /* Display the absolute or relative line number. After the
3780 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3781 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 && (row == startrow
3783#ifdef FEAT_DIFF
3784 + filler_lines
3785#endif
3786 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3787 {
3788 /* Draw the line number (empty space after wrapping). */
3789 if (row == startrow
3790#ifdef FEAT_DIFF
3791 + filler_lines
3792#endif
3793 )
3794 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003795 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003796 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003797
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003798 if (wp->w_p_nu && !wp->w_p_rnu)
3799 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003800 num = (long)lnum;
3801 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003802 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003803 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003804 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003805 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003806 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003807 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003808 num = lnum;
3809 fmt = "%-*ld ";
3810 }
3811 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003812
Bram Moolenaar700e7342013-01-30 12:31:36 +01003813 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003814 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 if (wp->w_skipcol > 0)
3816 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3817 *p_extra = '-';
3818#ifdef FEAT_RIGHTLEFT
3819 if (wp->w_p_rl) /* reverse line numbers */
3820 rl_mirror(extra);
3821#endif
3822 p_extra = extra;
3823 c_extra = NUL;
3824 }
3825 else
3826 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003827 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003828 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003829#ifdef FEAT_SYN_HL
3830 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003831 * the current line differently.
3832 * TODO: Can we use CursorLine instead of CursorLineNr
3833 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003834 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003835 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003836 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839 }
3840
Bram Moolenaar597a4222014-06-25 14:39:50 +02003841#ifdef FEAT_LINEBREAK
3842 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3843 && n_extra == 0 && *p_sbr != NUL)
3844 /* draw indent after showbreak value */
3845 draw_state = WL_BRI;
3846 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3847 /* After the showbreak, draw the breakindent */
3848 draw_state = WL_BRI - 1;
3849
3850 /* draw 'breakindent': indent wrapped text accordingly */
3851 if (draw_state == WL_BRI - 1 && n_extra == 0)
3852 {
3853 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003854 /* if need_showbreak is set, breakindent also applies */
3855 if (wp->w_p_bri && n_extra == 0
3856 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003857# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003858 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003859# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003860 )
3861 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003862 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003863# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003864 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003865 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003866 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003867# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003868 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3869 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003870 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003871# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003872 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003873# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003874 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003875 c_extra = ' ';
3876 n_extra = get_breakindent_win(wp,
3877 ml_get_buf(wp->w_buffer, lnum, FALSE));
3878 /* Correct end of highlighted area for 'breakindent',
3879 * required when 'linebreak' is also set. */
3880 if (tocol == vcol)
3881 tocol += n_extra;
3882 }
3883 }
3884#endif
3885
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3887 if (draw_state == WL_SBR - 1 && n_extra == 0)
3888 {
3889 draw_state = WL_SBR;
3890# ifdef FEAT_DIFF
3891 if (filler_todo > 0)
3892 {
3893 /* Draw "deleted" diff line(s). */
3894 if (char2cells(fill_diff) > 1)
3895 c_extra = '-';
3896 else
3897 c_extra = fill_diff;
3898# ifdef FEAT_RIGHTLEFT
3899 if (wp->w_p_rl)
3900 n_extra = col + 1;
3901 else
3902# endif
3903 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003904 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 }
3906# endif
3907# ifdef FEAT_LINEBREAK
3908 if (*p_sbr != NUL && need_showbreak)
3909 {
3910 /* Draw 'showbreak' at the start of each broken line. */
3911 p_extra = p_sbr;
3912 c_extra = NUL;
3913 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003914 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003916 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 /* Correct end of highlighted area for 'showbreak',
3918 * required when 'linebreak' is also set. */
3919 if (tocol == vcol)
3920 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003921#ifdef FEAT_SYN_HL
3922 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003923 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003924 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003925 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
3928# endif
3929 }
3930#endif
3931
3932 if (draw_state == WL_LINE - 1 && n_extra == 0)
3933 {
3934 draw_state = WL_LINE;
3935 if (saved_n_extra)
3936 {
3937 /* Continue item from end of wrapped line. */
3938 n_extra = saved_n_extra;
3939 c_extra = saved_c_extra;
3940 p_extra = saved_p_extra;
3941 char_attr = saved_char_attr;
3942 }
3943 else
3944 char_attr = 0;
3945 }
3946 }
3947
3948 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003949 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003950 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951#ifdef FEAT_DIFF
3952 && filler_todo <= 0
3953#endif
3954 )
3955 {
3956 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3957 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003958 /* Pretend we have finished updating the window. Except when
3959 * 'cursorcolumn' is set. */
3960#ifdef FEAT_SYN_HL
3961 if (wp->w_p_cuc)
3962 row = wp->w_cline_row + wp->w_cline_height;
3963 else
3964#endif
3965 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 break;
3967 }
3968
3969 if (draw_state == WL_LINE && area_highlighting)
3970 {
3971 /* handle Visual or match highlighting in this line */
3972 if (vcol == fromcol
3973#ifdef FEAT_MBYTE
3974 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3975 && (*mb_ptr2cells)(ptr) > 1)
3976#endif
3977 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003978 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 && vcol < tocol))
3980 area_attr = attr; /* start highlighting */
3981 else if (area_attr != 0
3982 && (vcol == tocol
3983 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
3986#ifdef FEAT_SEARCH_EXTRA
3987 if (!n_extra)
3988 {
3989 /*
3990 * Check for start/end of search pattern match.
3991 * After end, check for start/end of next match.
3992 * When another match, have to check for start again.
3993 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003994 * Do this for 'search_hl' and the match list (ordered by
3995 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003997 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003998 cur = wp->w_match_head;
3999 shl_flag = FALSE;
4000 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004002 if (shl_flag == FALSE
4003 && ((cur != NULL
4004 && cur->priority > SEARCH_HL_PRIORITY)
4005 || cur == NULL))
4006 {
4007 shl = &search_hl;
4008 shl_flag = TRUE;
4009 }
4010 else
4011 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004012 if (cur != NULL)
4013 cur->pos.cur = 0;
4014 pos_inprogress = TRUE;
4015 while (shl->rm.regprog != NULL
4016 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004018 if (shl->startcol != MAXCOL
4019 && v >= (long)shl->startcol
4020 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004022#ifdef FEAT_MBYTE
4023 int tmp_col = v + MB_PTR2LEN(ptr);
4024
4025 if (shl->endcol < tmp_col)
4026 shl->endcol = tmp_col;
4027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004029#ifdef FEAT_CONCEAL
4030 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4031 == cur->hlg_id)
4032 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004033 has_match_conc =
4034 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004035 match_conc = cur->conceal_char;
4036 }
4037 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004038 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004041 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
4043 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004044 next_search_hl(wp, shl, lnum, (colnr_T)v,
4045 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004046 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004047 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
4049 /* Need to get the line again, a multi-line regexp
4050 * may have made it invalid. */
4051 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4052 ptr = line + v;
4053
4054 if (shl->lnum == lnum)
4055 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004056 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004058 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004060 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004062 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
4064 /* highlight empty match, try again after
4065 * it */
4066#ifdef FEAT_MBYTE
4067 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004068 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004069 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 else
4071#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004072 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074
4075 /* Loop to check if the match starts at the
4076 * current position */
4077 continue;
4078 }
4079 }
4080 break;
4081 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004082 if (shl != &search_hl && cur != NULL)
4083 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004085
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004086 /* Use attributes from match with highest priority among
4087 * 'search_hl' and the match list. */
4088 search_attr = search_hl.attr_cur;
4089 cur = wp->w_match_head;
4090 shl_flag = FALSE;
4091 while (cur != NULL || shl_flag == FALSE)
4092 {
4093 if (shl_flag == FALSE
4094 && ((cur != NULL
4095 && cur->priority > SEARCH_HL_PRIORITY)
4096 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004097 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004098 shl = &search_hl;
4099 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004100 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004101 else
4102 shl = &cur->hl;
4103 if (shl->attr_cur != 0)
4104 search_attr = shl->attr_cur;
4105 if (shl != &search_hl && cur != NULL)
4106 cur = cur->next;
4107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109#endif
4110
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004112 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004114 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4115 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004117 if (diff_hlf == HLF_TXD && ptr - line > change_end
4118 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004120 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004121 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004122 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004125
4126 /* Decide which of the highlight attributes to use. */
4127 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004128#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004129 if (area_attr != 0)
4130 char_attr = hl_combine_attr(line_attr, area_attr);
4131 else if (search_attr != 0)
4132 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004133 /* Use line_attr when not in the Visual or 'incsearch' area
4134 * (area_attr may be 0 when "noinvcur" is set). */
4135 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004136 || vcol < fromcol || vcol_prev < fromcol_prev
4137 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004138 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004139#else
4140 if (area_attr != 0)
4141 char_attr = area_attr;
4142 else if (search_attr != 0)
4143 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004144#endif
4145 else
4146 {
4147 attr_pri = FALSE;
4148#ifdef FEAT_SYN_HL
4149 if (has_syntax)
4150 char_attr = syntax_attr;
4151 else
4152#endif
4153 char_attr = 0;
4154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156
4157 /*
4158 * Get the next character to put on the screen.
4159 */
4160 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004161 * The "p_extra" points to the extra stuff that is inserted to
4162 * represent special characters (non-printable stuff) and other
4163 * things. When all characters are the same, c_extra is used.
4164 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4165 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4167 */
4168 if (n_extra > 0)
4169 {
4170 if (c_extra != NUL)
4171 {
4172 c = c_extra;
4173#ifdef FEAT_MBYTE
4174 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004175 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
4177 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004178 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004179 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 else
4182 mb_utf8 = FALSE;
4183#endif
4184 }
4185 else
4186 {
4187 c = *p_extra;
4188#ifdef FEAT_MBYTE
4189 if (has_mbyte)
4190 {
4191 mb_c = c;
4192 if (enc_utf8)
4193 {
4194 /* If the UTF-8 character is more than one byte:
4195 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004196 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 mb_utf8 = FALSE;
4198 if (mb_l > n_extra)
4199 mb_l = 1;
4200 else if (mb_l > 1)
4201 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004202 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004204 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206 }
4207 else
4208 {
4209 /* if this is a DBCS character, put it in "mb_c" */
4210 mb_l = MB_BYTE2LEN(c);
4211 if (mb_l >= n_extra)
4212 mb_l = 1;
4213 else if (mb_l > 1)
4214 mb_c = (c << 8) + p_extra[1];
4215 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004216 if (mb_l == 0) /* at the NUL at end-of-line */
4217 mb_l = 1;
4218
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 /* If a double-width char doesn't fit display a '>' in the
4220 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004221 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222# ifdef FEAT_RIGHTLEFT
4223 wp->w_p_rl ? (col <= 0) :
4224# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004225 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 && (*mb_char2cells)(mb_c) == 2)
4227 {
4228 c = '>';
4229 mb_c = c;
4230 mb_l = 1;
4231 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004232 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 /* put the pointer back to output the double-width
4234 * character at the start of the next line. */
4235 ++n_extra;
4236 --p_extra;
4237 }
4238 else
4239 {
4240 n_extra -= mb_l - 1;
4241 p_extra += mb_l - 1;
4242 }
4243 }
4244#endif
4245 ++p_extra;
4246 }
4247 --n_extra;
4248 }
4249 else
4250 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004251#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004252 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004253#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004254
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004255 if (p_extra_free != NULL)
4256 {
4257 vim_free(p_extra_free);
4258 p_extra_free = NULL;
4259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 /*
4261 * Get a character from the line itself.
4262 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004263 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004264#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004265 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267#ifdef FEAT_MBYTE
4268 if (has_mbyte)
4269 {
4270 mb_c = c;
4271 if (enc_utf8)
4272 {
4273 /* If the UTF-8 character is more than one byte: Decode it
4274 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004275 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 mb_utf8 = FALSE;
4277 if (mb_l > 1)
4278 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004279 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 /* Overlong encoded ASCII or ASCII with composing char
4281 * is displayed normally, except a NUL. */
4282 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004283 {
4284 c = mb_c;
4285# ifdef FEAT_LINEBREAK
4286 c0 = mb_c;
4287# endif
4288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004290
4291 /* At start of the line we can have a composing char.
4292 * Draw it as a space with a composing char. */
4293 if (utf_iscomposing(mb_c))
4294 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004295 int i;
4296
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004297 for (i = Screen_mco - 1; i > 0; --i)
4298 u8cc[i] = u8cc[i - 1];
4299 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004300 mb_c = ' ';
4301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303
4304 if ((mb_l == 1 && c >= 0x80)
4305 || (mb_l >= 1 && mb_c == 0)
4306 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004307# ifdef UNICODE16
4308 || mb_c >= 0x10000
4309# endif
4310 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 {
4312 /*
4313 * Illegal UTF-8 byte: display as <xx>.
4314 * Non-BMP character : display as ? or fullwidth ?.
4315 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004316# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004321# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 if (wp->w_p_rl) /* reverse */
4323 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004326# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 else if (utf_char2cells(mb_c) != 2)
4328 STRCPY(extra, "?");
4329 else
4330 /* 0xff1f in UTF-8: full-width '?' */
4331 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
4334 p_extra = extra;
4335 c = *p_extra;
4336 mb_c = mb_ptr2char_adv(&p_extra);
4337 mb_utf8 = (c >= 0x80);
4338 n_extra = (int)STRLEN(p_extra);
4339 c_extra = NUL;
4340 if (area_attr == 0 && search_attr == 0)
4341 {
4342 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004343 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 saved_attr2 = char_attr; /* save current attr */
4345 }
4346 }
4347 else if (mb_l == 0) /* at the NUL at end-of-line */
4348 mb_l = 1;
4349#ifdef FEAT_ARABIC
4350 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4351 {
4352 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004353 int pc, pc1, nc;
4354 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
4356 /* The idea of what is the previous and next
4357 * character depends on 'rightleft'. */
4358 if (wp->w_p_rl)
4359 {
4360 pc = prev_c;
4361 pc1 = prev_c1;
4362 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004363 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365 else
4366 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004367 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004369 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
4371 prev_c = mb_c;
4372
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004373 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 }
4375 else
4376 prev_c = mb_c;
4377#endif
4378 }
4379 else /* enc_dbcs */
4380 {
4381 mb_l = MB_BYTE2LEN(c);
4382 if (mb_l == 0) /* at the NUL at end-of-line */
4383 mb_l = 1;
4384 else if (mb_l > 1)
4385 {
4386 /* We assume a second byte below 32 is illegal.
4387 * Hopefully this is OK for all double-byte encodings!
4388 */
4389 if (ptr[1] >= 32)
4390 mb_c = (c << 8) + ptr[1];
4391 else
4392 {
4393 if (ptr[1] == NUL)
4394 {
4395 /* head byte at end of line */
4396 mb_l = 1;
4397 transchar_nonprint(extra, c);
4398 }
4399 else
4400 {
4401 /* illegal tail byte */
4402 mb_l = 2;
4403 STRCPY(extra, "XX");
4404 }
4405 p_extra = extra;
4406 n_extra = (int)STRLEN(extra) - 1;
4407 c_extra = NUL;
4408 c = *p_extra++;
4409 if (area_attr == 0 && search_attr == 0)
4410 {
4411 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004412 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 saved_attr2 = char_attr; /* save current attr */
4414 }
4415 mb_c = c;
4416 }
4417 }
4418 }
4419 /* If a double-width char doesn't fit display a '>' in the
4420 * last column; the character is displayed at the start of the
4421 * next line. */
4422 if ((
4423# ifdef FEAT_RIGHTLEFT
4424 wp->w_p_rl ? (col <= 0) :
4425# endif
4426 (col >= W_WIDTH(wp) - 1))
4427 && (*mb_char2cells)(mb_c) == 2)
4428 {
4429 c = '>';
4430 mb_c = c;
4431 mb_utf8 = FALSE;
4432 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004433 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 /* Put pointer back so that the character will be
4435 * displayed at the start of the next line. */
4436 --ptr;
4437 }
4438 else if (*ptr != NUL)
4439 ptr += mb_l - 1;
4440
4441 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004442 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004443 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004444 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004447 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 c = ' ';
4449 if (area_attr == 0 && search_attr == 0)
4450 {
4451 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004452 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 saved_attr2 = char_attr; /* save current attr */
4454 }
4455 mb_c = c;
4456 mb_utf8 = FALSE;
4457 mb_l = 1;
4458 }
4459
4460 }
4461#endif
4462 ++ptr;
4463
4464 if (extra_check)
4465 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004466#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004467 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004468#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004469
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004470#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 /* Get syntax attribute, unless still at the start of the line
4472 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004473 v = (long)(ptr - line);
4474 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 {
4476 /* Get the syntax attribute for the character. If there
4477 * is an error, disable syntax highlighting. */
4478 save_did_emsg = did_emsg;
4479 did_emsg = FALSE;
4480
Bram Moolenaar217ad922005-03-20 22:37:15 +00004481 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004482# ifdef FEAT_SPELL
4483 has_spell ? &can_spell :
4484# endif
4485 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
4487 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004488 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004489 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004490 has_syntax = FALSE;
4491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 else
4493 did_emsg = save_did_emsg;
4494
4495 /* Need to get the line again, a multi-line regexp may
4496 * have made it invalid. */
4497 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4498 ptr = line + v;
4499
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004500 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004502 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004503 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004504# ifdef FEAT_CONCEAL
4505 /* no concealing past the end of the line, it interferes
4506 * with line highlighting */
4507 if (c == NUL)
4508 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004509 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004510 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004513#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004514
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004515#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004516 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004517 * Only do this when there is no syntax highlighting, the
4518 * @Spell cluster is not used or the current syntax item
4519 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004520 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004521 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004522 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004523# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004524 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004525 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004526# endif
4527 if (c != 0 && (
4528# ifdef FEAT_SYN_HL
4529 !has_syntax ||
4530# endif
4531 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004532 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004533 char_u *prev_ptr, *p;
4534 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004535 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004536# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004537 if (has_mbyte)
4538 {
4539 prev_ptr = ptr - mb_l;
4540 v -= mb_l - 1;
4541 }
4542 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004543# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004544 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004545
4546 /* Use nextline[] if possible, it has the start of the
4547 * next line concatenated. */
4548 if ((prev_ptr - line) - nextlinecol >= 0)
4549 p = nextline + (prev_ptr - line) - nextlinecol;
4550 else
4551 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004552 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004553 len = spell_check(wp, p, &spell_hlf, &cap_col,
4554 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004555 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004556
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004557 /* In Insert mode only highlight a word that
4558 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004559 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004560 && (State & INSERT) != 0
4561 && wp->w_cursor.lnum == lnum
4562 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004563 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004564 && wp->w_cursor.col < (colnr_T)word_end)
4565 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004566 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004567 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004568 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004569
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004570 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004571 && (p - nextline) + len > nextline_idx)
4572 {
4573 /* Remember that the good word continues at the
4574 * start of the next line. */
4575 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004576 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004577 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004578
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004579 /* Turn index into actual attributes. */
4580 if (spell_hlf != HLF_COUNT)
4581 spell_attr = highlight_attr[spell_hlf];
4582
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004583 if (cap_col > 0)
4584 {
4585 if (p != prev_ptr
4586 && (p - nextline) + cap_col >= nextline_idx)
4587 {
4588 /* Remember that the word in the next line
4589 * must start with a capital. */
4590 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004591 cap_col = (int)((p - nextline) + cap_col
4592 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004593 }
4594 else
4595 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004596 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004597 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004598 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004599 }
4600 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004601 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004602 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004603 char_attr = hl_combine_attr(char_attr, spell_attr);
4604 else
4605 char_attr = hl_combine_attr(spell_attr, char_attr);
4606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607#endif
4608#ifdef FEAT_LINEBREAK
4609 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004610 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004612 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004613 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004615# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004616 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004617# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004618 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004620 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004622 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004623
Bram Moolenaar597a4222014-06-25 14:39:50 +02004624 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004625 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004626 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004627 if (c == TAB && n_extra + col > W_WIDTH(wp))
4628 n_extra = (int)wp->w_buffer->b_p_ts
4629 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4630
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004631# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004632 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004633# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004635# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004636 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004637 {
4638#ifdef FEAT_CONCEAL
4639 if (c == TAB)
4640 /* See "Tab alignment" below. */
4641 FIX_FOR_BOGUSCOLS;
4642#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004643 if (!wp->w_p_list)
4644 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
4647#endif
4648
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004649 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4650 */
4651 if (wp->w_p_list
4652 && (((c == 160
4653#ifdef FEAT_MBYTE
4654 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4655#endif
4656 ) && lcs_nbsp)
4657 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4658 {
4659 c = (c == ' ') ? lcs_space : lcs_nbsp;
4660 if (area_attr == 0 && search_attr == 0)
4661 {
4662 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004663 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004664 saved_attr2 = char_attr; /* save current attr */
4665 }
4666#ifdef FEAT_MBYTE
4667 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004668 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004669 {
4670 mb_utf8 = TRUE;
4671 u8cc[0] = 0;
4672 c = 0xc0;
4673 }
4674 else
4675 mb_utf8 = FALSE;
4676#endif
4677 }
4678
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4680 {
4681 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004682 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 {
4684 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004685 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 saved_attr2 = char_attr; /* save current attr */
4687 }
4688#ifdef FEAT_MBYTE
4689 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004690 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
4692 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004693 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004694 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
4696 else
4697 mb_utf8 = FALSE;
4698#endif
4699 }
4700 }
4701
4702 /*
4703 * Handling of non-printable characters.
4704 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004705 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
4707 /*
4708 * when getting a character from the file, we may have to
4709 * turn it into something else on the way to putting it
4710 * into "ScreenLines".
4711 */
4712 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4713 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004714 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004715 long vcol_adjusted = vcol; /* removed showbreak length */
4716#ifdef FEAT_LINEBREAK
4717 /* only adjust the tab_len, when at the first column
4718 * after the showbreak value was drawn */
4719 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4720 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004723 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004724 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4725
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004726#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004727 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004728#endif
4729 /* tab amount depends on current column */
4730 n_extra = tab_len;
4731#ifdef FEAT_LINEBREAK
4732 else
4733 {
4734 char_u *p;
4735 int len = n_extra;
4736 int i;
4737 int saved_nextra = n_extra;
4738
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004739#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004740 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004741 /* there are characters to conceal */
4742 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004743 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4744 */
4745 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4746 && n_extra > tab_len)
4747 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004748#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004749
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004750 /* if n_extra > 0, it gives the number of chars, to
4751 * use for a tab, else we need to calculate the width
4752 * for a tab */
4753#ifdef FEAT_MBYTE
4754 len = (tab_len * mb_char2len(lcs_tab2));
4755 if (n_extra > 0)
4756 len += n_extra - tab_len;
4757#endif
4758 c = lcs_tab1;
4759 p = alloc((unsigned)(len + 1));
4760 vim_memset(p, ' ', len);
4761 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004762 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004763 p_extra_free = p;
4764 for (i = 0; i < tab_len; i++)
4765 {
4766#ifdef FEAT_MBYTE
4767 mb_char2bytes(lcs_tab2, p);
4768 p += mb_char2len(lcs_tab2);
4769 n_extra += mb_char2len(lcs_tab2)
4770 - (saved_nextra > 0 ? 1 : 0);
4771#else
4772 p[i] = lcs_tab2;
4773#endif
4774 }
4775 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004776#ifdef FEAT_CONCEAL
4777 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4778 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004779 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004780 n_extra -= vcol_off;
4781#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004782 }
4783#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004784#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004785 {
4786 int vc_saved = vcol_off;
4787
4788 /* Tab alignment should be identical regardless of
4789 * 'conceallevel' value. So tab compensates of all
4790 * previous concealed characters, and thus resets
4791 * vcol_off and boguscols accumulated so far in the
4792 * line. Note that the tab can be longer than
4793 * 'tabstop' when there are concealed characters. */
4794 FIX_FOR_BOGUSCOLS;
4795
4796 /* Make sure, the highlighting for the tab char will be
4797 * correctly set further below (effectively reverts the
4798 * FIX_FOR_BOGSUCOLS macro */
4799 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004800 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004801 tab_len += vc_saved;
4802 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804#ifdef FEAT_MBYTE
4805 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4806#endif
4807 if (wp->w_p_list)
4808 {
4809 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004810#ifdef FEAT_LINEBREAK
4811 if (wp->w_p_lbr)
4812 c_extra = NUL; /* using p_extra from above */
4813 else
4814#endif
4815 c_extra = lcs_tab2;
4816 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004817 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 saved_attr2 = char_attr; /* save current attr */
4819#ifdef FEAT_MBYTE
4820 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004821 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 {
4823 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004824 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004825 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
4827#endif
4828 }
4829 else
4830 {
4831 c_extra = ' ';
4832 c = ' ';
4833 }
4834 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004835 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004836 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004837 || ((fromcol >= 0 || fromcol_prev >= 0)
4838 && tocol > vcol
4839 && VIsual_mode != Ctrl_V
4840 && (
4841# ifdef FEAT_RIGHTLEFT
4842 wp->w_p_rl ? (col >= 0) :
4843# endif
4844 (col < W_WIDTH(wp)))
4845 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004846 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004847 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004848 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004850 /* Display a '$' after the line or highlight an extra
4851 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4853 /* For a diff line the highlighting continues after the
4854 * "$". */
4855 if (
4856# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004857 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858# ifdef LINE_ATTR
4859 &&
4860# endif
4861# endif
4862# ifdef LINE_ATTR
4863 line_attr == 0
4864# endif
4865 )
4866#endif
4867 {
4868#ifdef FEAT_VIRTUALEDIT
4869 /* In virtualedit, visual selections may extend
4870 * beyond end of line. */
4871 if (area_highlighting && virtual_active()
4872 && tocol != MAXCOL && vcol < tocol)
4873 n_extra = 0;
4874 else
4875#endif
4876 {
4877 p_extra = at_end_str;
4878 n_extra = 1;
4879 c_extra = NUL;
4880 }
4881 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004882 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004883 c = lcs_eol;
4884 else
4885 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 lcs_eol_one = -1;
4887 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004888 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004890 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 n_attr = 1;
4892 }
4893#ifdef FEAT_MBYTE
4894 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004895 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
4897 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004898 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004899 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 }
4901 else
4902 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4903#endif
4904 }
4905 else if (c != NUL)
4906 {
4907 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004908 if (n_extra == 0)
4909 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910#ifdef FEAT_RIGHTLEFT
4911 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4912 rl_mirror(p_extra); /* reverse "<12>" */
4913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004915#ifdef FEAT_LINEBREAK
4916 if (wp->w_p_lbr)
4917 {
4918 char_u *p;
4919
4920 c = *p_extra;
4921 p = alloc((unsigned)n_extra + 1);
4922 vim_memset(p, ' ', n_extra);
4923 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4924 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004925 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004926 p_extra_free = p_extra = p;
4927 }
4928 else
4929#endif
4930 {
4931 n_extra = byte2cells(c) - 1;
4932 c = *p_extra++;
4933 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004934 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
4936 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004937 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 saved_attr2 = char_attr; /* save current attr */
4939 }
4940#ifdef FEAT_MBYTE
4941 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4942#endif
4943 }
4944#ifdef FEAT_VIRTUALEDIT
4945 else if (VIsual_active
4946 && (VIsual_mode == Ctrl_V
4947 || VIsual_mode == 'v')
4948 && virtual_active()
4949 && tocol != MAXCOL
4950 && vcol < tocol
4951 && (
4952# ifdef FEAT_RIGHTLEFT
4953 wp->w_p_rl ? (col >= 0) :
4954# endif
4955 (col < W_WIDTH(wp))))
4956 {
4957 c = ' ';
4958 --ptr; /* put it back at the NUL */
4959 }
4960#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004961#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 else if ((
4963# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004964 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 ) && (
4968# ifdef FEAT_RIGHTLEFT
4969 wp->w_p_rl ? (col >= 0) :
4970# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004971 (col
4972# ifdef FEAT_CONCEAL
4973 - boguscols
4974# endif
4975 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
4977 /* Highlight until the right side of the window */
4978 c = ' ';
4979 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004980
4981 /* Remember we do the char for line highlighting. */
4982 ++did_line_attr;
4983
4984 /* don't do search HL for the rest of the line */
4985 if (line_attr != 0 && char_attr == search_attr && col > 0)
4986 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987# ifdef FEAT_DIFF
4988 if (diff_hlf == HLF_TXD)
4989 {
4990 diff_hlf = HLF_CHD;
4991 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004992 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004993 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004994 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4995 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004996 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02004997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
4999# endif
5000 }
5001#endif
5002 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005003
5004#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005005 if ( wp->w_p_cole > 0
5006 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005007 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005008 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005009 && !(lnum_in_visual_area
5010 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005011 {
5012 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005013 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005014 && (syn_get_sub_char() != NUL || match_conc
5015 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005016 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005017 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005018 /* First time at this concealed item: display one
5019 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005020 if (match_conc)
5021 c = match_conc;
5022 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005023 c = syn_get_sub_char();
5024 else if (lcs_conceal != NUL)
5025 c = lcs_conceal;
5026 else
5027 c = ' ';
5028
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005029 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005030
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005031 if (n_extra > 0)
5032 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005033 vcol += n_extra;
5034 if (wp->w_p_wrap && n_extra > 0)
5035 {
5036# ifdef FEAT_RIGHTLEFT
5037 if (wp->w_p_rl)
5038 {
5039 col -= n_extra;
5040 boguscols -= n_extra;
5041 }
5042 else
5043# endif
5044 {
5045 boguscols += n_extra;
5046 col += n_extra;
5047 }
5048 }
5049 n_extra = 0;
5050 n_attr = 0;
5051 }
5052 else if (n_skip == 0)
5053 {
5054 is_concealing = TRUE;
5055 n_skip = 1;
5056 }
5057# ifdef FEAT_MBYTE
5058 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005059 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005060 {
5061 mb_utf8 = TRUE;
5062 u8cc[0] = 0;
5063 c = 0xc0;
5064 }
5065 else
5066 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5067# endif
5068 }
5069 else
5070 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005071 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005072 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005073 }
5074#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
5076
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005077#ifdef FEAT_CONCEAL
5078 /* In the cursor line and we may be concealing characters: correct
5079 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005080 if (!did_wcol && draw_state == WL_LINE
5081 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005082 && conceal_cursor_line(wp)
5083 && (int)wp->w_virtcol <= vcol + n_skip)
5084 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005085# ifdef FEAT_RIGHTLEFT
5086 if (wp->w_p_rl)
5087 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5088 else
5089# endif
5090 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005091 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005092 did_wcol = TRUE;
5093 }
5094#endif
5095
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 /* Don't override visual selection highlighting. */
5097 if (n_attr > 0
5098 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005099 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 char_attr = extra_attr;
5101
Bram Moolenaar81695252004-12-29 20:58:21 +00005102#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 /* XIM don't send preedit_start and preedit_end, but they send
5104 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5105 * im_is_preediting() here. */
5106 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005107 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 && (State & INSERT)
5109 && !p_imdisable
5110 && im_is_preediting()
5111 && draw_state == WL_LINE)
5112 {
5113 colnr_T tcol;
5114
5115 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005116 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 else
5118 tcol = preedit_end_col;
5119 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5120 {
5121 if (feedback_old_attr < 0)
5122 {
5123 feedback_col = 0;
5124 feedback_old_attr = char_attr;
5125 }
5126 char_attr = im_get_feedback_attr(feedback_col);
5127 if (char_attr < 0)
5128 char_attr = feedback_old_attr;
5129 feedback_col++;
5130 }
5131 else if (feedback_old_attr >= 0)
5132 {
5133 char_attr = feedback_old_attr;
5134 feedback_old_attr = -1;
5135 feedback_col = 0;
5136 }
5137 }
5138#endif
5139 /*
5140 * Handle the case where we are in column 0 but not on the first
5141 * character of the line and the user wants us to show us a
5142 * special character (via 'listchars' option "precedes:<char>".
5143 */
5144 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005145 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5147#ifdef FEAT_DIFF
5148 && filler_todo <= 0
5149#endif
5150 && draw_state > WL_NR
5151 && c != NUL)
5152 {
5153 c = lcs_prec;
5154 lcs_prec_todo = NUL;
5155#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005156 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5157 {
5158 /* Double-width character being overwritten by the "precedes"
5159 * character, need to fill up half the character. */
5160 c_extra = MB_FILLER_CHAR;
5161 n_extra = 1;
5162 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005163 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005166 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {
5168 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005169 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005170 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172 else
5173 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5174#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005175 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
5177 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005178 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 n_attr3 = 1;
5180 }
5181 }
5182
5183 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005184 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005186 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005187#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005188 || did_line_attr == 1
5189#endif
5190 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005192#ifdef FEAT_SEARCH_EXTRA
5193 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005194
5195 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005196 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005197 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005198#endif
5199
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005200 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 * highlight match at end of line. If it's beyond the last
5202 * char on the screen, just overwrite that one (tricky!) Not
5203 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005204#ifdef FEAT_SEARCH_EXTRA
5205 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005206 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005207 prevcol_hl_flag = TRUE;
5208 else
5209 {
5210 cur = wp->w_match_head;
5211 while (cur != NULL)
5212 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005213 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005214 {
5215 prevcol_hl_flag = TRUE;
5216 break;
5217 }
5218 cur = cur->next;
5219 }
5220 }
5221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005223 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005224 && (VIsual_mode != Ctrl_V
5225 || lnum == VIsual.lnum
5226 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005227 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228#ifdef FEAT_SEARCH_EXTRA
5229 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005230 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005231# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005232 && did_line_attr <= 1
5233# endif
5234 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235#endif
5236 ))
5237 {
5238 int n = 0;
5239
5240#ifdef FEAT_RIGHTLEFT
5241 if (wp->w_p_rl)
5242 {
5243 if (col < 0)
5244 n = 1;
5245 }
5246 else
5247#endif
5248 {
5249 if (col >= W_WIDTH(wp))
5250 n = -1;
5251 }
5252 if (n != 0)
5253 {
5254 /* At the window boundary, highlight the last character
5255 * instead (better than nothing). */
5256 off += n;
5257 col += n;
5258 }
5259 else
5260 {
5261 /* Add a blank character to highlight. */
5262 ScreenLines[off] = ' ';
5263#ifdef FEAT_MBYTE
5264 if (enc_utf8)
5265 ScreenLinesUC[off] = 0;
5266#endif
5267 }
5268#ifdef FEAT_SEARCH_EXTRA
5269 if (area_attr == 0)
5270 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005271 /* Use attributes from match with highest priority among
5272 * 'search_hl' and the match list. */
5273 char_attr = search_hl.attr;
5274 cur = wp->w_match_head;
5275 shl_flag = FALSE;
5276 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005277 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005278 if (shl_flag == FALSE
5279 && ((cur != NULL
5280 && cur->priority > SEARCH_HL_PRIORITY)
5281 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005282 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005283 shl = &search_hl;
5284 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005285 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005286 else
5287 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005288 if ((ptr - line) - 1 == (long)shl->startcol
5289 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005290 char_attr = shl->attr;
5291 if (shl != &search_hl && cur != NULL)
5292 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 }
5295#endif
5296 ScreenAttrs[off] = char_attr;
5297#ifdef FEAT_RIGHTLEFT
5298 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005301 --off;
5302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 else
5304#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005307 ++off;
5308 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005309 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005310#ifdef FEAT_SYN_HL
5311 eol_hl_off = 1;
5312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
Bram Moolenaar91170f82006-05-05 21:15:17 +00005316 /*
5317 * At end of the text line.
5318 */
5319 if (c == NUL)
5320 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005321#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005322 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5323 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005324 {
5325 /* highlight last char after line */
5326 --col;
5327 --off;
5328 --vcol;
5329 }
5330
Bram Moolenaar1a384422010-07-14 19:53:30 +02005331 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005332 if (wp->w_p_wrap)
5333 v = wp->w_skipcol;
5334 else
5335 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005336
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005337 /* check if line ends before left margin */
5338 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005339 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005340#ifdef FEAT_CONCEAL
5341 /* Get rid of the boguscols now, we want to draw until the right
5342 * edge for 'cursorcolumn'. */
5343 col -= boguscols;
5344 boguscols = 0;
5345#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005346
5347 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005348 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005349
5350 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005351 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5352 && (int)wp->w_virtcol <
5353 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005354 && lnum != wp->w_cursor.lnum)
5355 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005356# ifdef FEAT_RIGHTLEFT
5357 && !wp->w_p_rl
5358# endif
5359 )
5360 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005361 int rightmost_vcol = 0;
5362 int i;
5363
5364 if (wp->w_p_cuc)
5365 rightmost_vcol = wp->w_virtcol;
5366 if (draw_color_col)
5367 /* determine rightmost colorcolumn to possibly draw */
5368 for (i = 0; color_cols[i] >= 0; ++i)
5369 if (rightmost_vcol < color_cols[i])
5370 rightmost_vcol = color_cols[i];
5371
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005372 while (col < W_WIDTH(wp))
5373 {
5374 ScreenLines[off] = ' ';
5375#ifdef FEAT_MBYTE
5376 if (enc_utf8)
5377 ScreenLinesUC[off] = 0;
5378#endif
5379 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005380 if (draw_color_col)
5381 draw_color_col = advance_color_col(VCOL_HLC,
5382 &color_cols);
5383
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005384 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005385 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005386 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005387 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005388 else
5389 ScreenAttrs[off++] = 0;
5390
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005391 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005392 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005393
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005394 ++vcol;
5395 }
5396 }
5397#endif
5398
Bram Moolenaar860cae12010-06-05 23:22:07 +02005399 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5400 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 row++;
5402
5403 /*
5404 * Update w_cline_height and w_cline_folded if the cursor line was
5405 * updated (saves a call to plines() later).
5406 */
5407 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5408 {
5409 curwin->w_cline_row = startrow;
5410 curwin->w_cline_height = row - startrow;
5411#ifdef FEAT_FOLDING
5412 curwin->w_cline_folded = FALSE;
5413#endif
5414 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5415 }
5416
5417 break;
5418 }
5419
5420 /* line continues beyond line end */
5421 if (lcs_ext
5422 && !wp->w_p_wrap
5423#ifdef FEAT_DIFF
5424 && filler_todo <= 0
5425#endif
5426 && (
5427#ifdef FEAT_RIGHTLEFT
5428 wp->w_p_rl ? col == 0 :
5429#endif
5430 col == W_WIDTH(wp) - 1)
5431 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005432 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5434 {
5435 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005436 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437#ifdef FEAT_MBYTE
5438 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005439 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 {
5441 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005442 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005443 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
5445 else
5446 mb_utf8 = FALSE;
5447#endif
5448 }
5449
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005450#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005451 /* advance to the next 'colorcolumn' */
5452 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005453 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005454
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005455 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005456 * highlight the cursor position itself.
5457 * Also highlight the 'colorcolumn' if it is different than
5458 * 'cursorcolumn' */
5459 vcol_save_attr = -1;
5460 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005461 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005462 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005463 && lnum != wp->w_cursor.lnum)
5464 {
5465 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005466 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005467 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005468 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005469 {
5470 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005471 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005472 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005473 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005474#endif
5475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 /*
5477 * Store character to be displayed.
5478 * Skip characters that are left of the screen for 'nowrap'.
5479 */
5480 vcol_prev = vcol;
5481 if (draw_state < WL_LINE || n_skip <= 0)
5482 {
5483 /*
5484 * Store the character.
5485 */
5486#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5487 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5488 {
5489 /* A double-wide character is: put first halve in left cell. */
5490 --off;
5491 --col;
5492 }
5493#endif
5494 ScreenLines[off] = c;
5495#ifdef FEAT_MBYTE
5496 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005497 {
5498 if ((mb_c & 0xff00) == 0x8e00)
5499 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 else if (enc_utf8)
5503 {
5504 if (mb_utf8)
5505 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005506 int i;
5507
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005509 if ((c & 0xff) == 0)
5510 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005511 for (i = 0; i < Screen_mco; ++i)
5512 {
5513 ScreenLinesC[i][off] = u8cc[i];
5514 if (u8cc[i] == 0)
5515 break;
5516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 }
5518 else
5519 ScreenLinesUC[off] = 0;
5520 }
5521 if (multi_attr)
5522 {
5523 ScreenAttrs[off] = multi_attr;
5524 multi_attr = 0;
5525 }
5526 else
5527#endif
5528 ScreenAttrs[off] = char_attr;
5529
5530#ifdef FEAT_MBYTE
5531 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5532 {
5533 /* Need to fill two screen columns. */
5534 ++off;
5535 ++col;
5536 if (enc_utf8)
5537 /* UTF-8: Put a 0 in the second screen char. */
5538 ScreenLines[off] = 0;
5539 else
5540 /* DBCS: Put second byte in the second screen char. */
5541 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005542 if (draw_state > WL_NR
5543#ifdef FEAT_DIFF
5544 && filler_todo <= 0
5545#endif
5546 )
5547 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 /* When "tocol" is halfway a character, set it to the end of
5549 * the character, otherwise highlighting won't stop. */
5550 if (tocol == vcol)
5551 ++tocol;
5552#ifdef FEAT_RIGHTLEFT
5553 if (wp->w_p_rl)
5554 {
5555 /* now it's time to backup one cell */
5556 --off;
5557 --col;
5558 }
5559#endif
5560 }
5561#endif
5562#ifdef FEAT_RIGHTLEFT
5563 if (wp->w_p_rl)
5564 {
5565 --off;
5566 --col;
5567 }
5568 else
5569#endif
5570 {
5571 ++off;
5572 ++col;
5573 }
5574 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005575#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005576 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005577 {
5578 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005579 ++vcol_off;
5580 if (n_extra > 0)
5581 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005582 if (wp->w_p_wrap)
5583 {
5584 /*
5585 * Special voodoo required if 'wrap' is on.
5586 *
5587 * Advance the column indicator to force the line
5588 * drawing to wrap early. This will make the line
5589 * take up the same screen space when parts are concealed,
5590 * so that cursor line computations aren't messed up.
5591 *
5592 * To avoid the fictitious advance of 'col' causing
5593 * trailing junk to be written out of the screen line
5594 * we are building, 'boguscols' keeps track of the number
5595 * of bad columns we have advanced.
5596 */
5597 if (n_extra > 0)
5598 {
5599 vcol += n_extra;
5600# ifdef FEAT_RIGHTLEFT
5601 if (wp->w_p_rl)
5602 {
5603 col -= n_extra;
5604 boguscols -= n_extra;
5605 }
5606 else
5607# endif
5608 {
5609 col += n_extra;
5610 boguscols += n_extra;
5611 }
5612 n_extra = 0;
5613 n_attr = 0;
5614 }
5615
5616
5617# ifdef FEAT_MBYTE
5618 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5619 {
5620 /* Need to fill two screen columns. */
5621# ifdef FEAT_RIGHTLEFT
5622 if (wp->w_p_rl)
5623 {
5624 --boguscols;
5625 --col;
5626 }
5627 else
5628# endif
5629 {
5630 ++boguscols;
5631 ++col;
5632 }
5633 }
5634# endif
5635
5636# ifdef FEAT_RIGHTLEFT
5637 if (wp->w_p_rl)
5638 {
5639 --boguscols;
5640 --col;
5641 }
5642 else
5643# endif
5644 {
5645 ++boguscols;
5646 ++col;
5647 }
5648 }
5649 else
5650 {
5651 if (n_extra > 0)
5652 {
5653 vcol += n_extra;
5654 n_extra = 0;
5655 n_attr = 0;
5656 }
5657 }
5658
5659 }
5660#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 else
5662 --n_skip;
5663
Bram Moolenaar64486672010-05-16 15:46:46 +02005664 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5665 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005666 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667#ifdef FEAT_DIFF
5668 && filler_todo <= 0
5669#endif
5670 )
5671 ++vcol;
5672
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005673#ifdef FEAT_SYN_HL
5674 if (vcol_save_attr >= 0)
5675 char_attr = vcol_save_attr;
5676#endif
5677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 /* restore attributes after "predeces" in 'listchars' */
5679 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5680 char_attr = saved_attr3;
5681
5682 /* restore attributes after last 'listchars' or 'number' char */
5683 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5684 char_attr = saved_attr2;
5685
5686 /*
5687 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005688 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 */
5690 if ((
5691#ifdef FEAT_RIGHTLEFT
5692 wp->w_p_rl ? (col < 0) :
5693#endif
5694 (col >= W_WIDTH(wp)))
5695 && (*ptr != NUL
5696#ifdef FEAT_DIFF
5697 || filler_todo > 0
5698#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005699 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5701 )
5702 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005703#ifdef FEAT_CONCEAL
5704 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5705 (int)W_WIDTH(wp), wp->w_p_rl);
5706 boguscols = 0;
5707#else
5708 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5709 (int)W_WIDTH(wp), wp->w_p_rl);
5710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 ++row;
5712 ++screen_row;
5713
5714 /* When not wrapping and finished diff lines, or when displayed
5715 * '$' and highlighting until last column, break here. */
5716 if ((!wp->w_p_wrap
5717#ifdef FEAT_DIFF
5718 && filler_todo <= 0
5719#endif
5720 ) || lcs_eol_one == -1)
5721 break;
5722
5723 /* When the window is too narrow draw all "@" lines. */
5724 if (draw_state != WL_LINE
5725#ifdef FEAT_DIFF
5726 && filler_todo <= 0
5727#endif
5728 )
5729 {
5730 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005731#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 draw_vsep_win(wp, row);
5733#endif
5734 row = endrow;
5735 }
5736
5737 /* When line got too long for screen break here. */
5738 if (row == endrow)
5739 {
5740 ++row;
5741 break;
5742 }
5743
5744 if (screen_cur_row == screen_row - 1
5745#ifdef FEAT_DIFF
5746 && filler_todo <= 0
5747#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005748#ifdef FEAT_WINDOWS
5749 && W_WIDTH(wp) == Columns
5750#endif
5751 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753 /* Remember that the line wraps, used for modeless copy. */
5754 LineWraps[screen_row - 1] = TRUE;
5755
5756 /*
5757 * Special trick to make copy/paste of wrapped lines work with
5758 * xterm/screen: write an extra character beyond the end of
5759 * the line. This will work with all terminal types
5760 * (regardless of the xn,am settings).
5761 * Only do this on a fast tty.
5762 * Only do this if the cursor is on the current line
5763 * (something has been written in it).
5764 * Don't do this for the GUI.
5765 * Don't do this for double-width characters.
5766 * Don't do this for a window not at the right screen border.
5767 */
5768 if (p_tf
5769#ifdef FEAT_GUI
5770 && !gui.in_use
5771#endif
5772#ifdef FEAT_MBYTE
5773 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005774 && ((*mb_off2cells)(LineOffset[screen_row],
5775 LineOffset[screen_row] + screen_Columns)
5776 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005778 + (int)Columns - 2,
5779 LineOffset[screen_row] + screen_Columns)
5780 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781#endif
5782 )
5783 {
5784 /* First make sure we are at the end of the screen line,
5785 * then output the same character again to let the
5786 * terminal know about the wrap. If the terminal doesn't
5787 * auto-wrap, we overwrite the character. */
5788 if (screen_cur_col != W_WIDTH(wp))
5789 screen_char(LineOffset[screen_row - 1]
5790 + (unsigned)Columns - 1,
5791 screen_row - 1, (int)(Columns - 1));
5792
5793#ifdef FEAT_MBYTE
5794 /* When there is a multi-byte character, just output a
5795 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005796 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5797 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 out_char(' ');
5799 else
5800#endif
5801 out_char(ScreenLines[LineOffset[screen_row - 1]
5802 + (Columns - 1)]);
5803 /* force a redraw of the first char on the next line */
5804 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5805 screen_start(); /* don't know where cursor is now */
5806 }
5807 }
5808
5809 col = 0;
5810 off = (unsigned)(current_ScreenLine - ScreenLines);
5811#ifdef FEAT_RIGHTLEFT
5812 if (wp->w_p_rl)
5813 {
5814 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5815 off += col;
5816 }
5817#endif
5818
5819 /* reset the drawing state for the start of a wrapped line */
5820 draw_state = WL_START;
5821 saved_n_extra = n_extra;
5822 saved_p_extra = p_extra;
5823 saved_c_extra = c_extra;
5824 saved_char_attr = char_attr;
5825 n_extra = 0;
5826 lcs_prec_todo = lcs_prec;
5827#ifdef FEAT_LINEBREAK
5828# ifdef FEAT_DIFF
5829 if (filler_todo <= 0)
5830# endif
5831 need_showbreak = TRUE;
5832#endif
5833#ifdef FEAT_DIFF
5834 --filler_todo;
5835 /* When the filler lines are actually below the last line of the
5836 * file, don't draw the line itself, break here. */
5837 if (filler_todo == 0 && wp->w_botfill)
5838 break;
5839#endif
5840 }
5841
5842 } /* for every character in the line */
5843
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005844#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005845 /* After an empty line check first word for capital. */
5846 if (*skipwhite(line) == NUL)
5847 {
5848 capcol_lnum = lnum + 1;
5849 cap_col = 0;
5850 }
5851#endif
5852
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005853 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 return row;
5855}
5856
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005857#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005858static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005859
5860/*
5861 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005862 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005863 */
5864 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005865comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005866{
5867 int i;
5868
5869 for (i = 0; i < Screen_mco; ++i)
5870 {
5871 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5872 return TRUE;
5873 if (ScreenLinesC[i][off_from] == 0)
5874 break;
5875 }
5876 return FALSE;
5877}
5878#endif
5879
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880/*
5881 * Check whether the given character needs redrawing:
5882 * - the (first byte of the) character is different
5883 * - the attributes are different
5884 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005885 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 */
5887 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005888char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
5890 if (cols > 0
5891 && ((ScreenLines[off_from] != ScreenLines[off_to]
5892 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5893
5894#ifdef FEAT_MBYTE
5895 || (enc_dbcs != 0
5896 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5897 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5898 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5899 : (cols > 1 && ScreenLines[off_from + 1]
5900 != ScreenLines[off_to + 1])))
5901 || (enc_utf8
5902 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5903 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005904 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005905 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5906 && ScreenLines[off_from + 1]
5907 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908#endif
5909 ))
5910 return TRUE;
5911 return FALSE;
5912}
5913
5914/*
5915 * Move one "cooked" screen line to the screen, but only the characters that
5916 * have actually changed. Handle insert/delete character.
5917 * "coloff" gives the first column on the screen for this line.
5918 * "endcol" gives the columns where valid characters are.
5919 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5920 * needs to be cleared, negative otherwise.
5921 * "rlflag" is TRUE in a rightleft window:
5922 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5923 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5924 */
5925 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005926screen_line(
5927 int row,
5928 int coloff,
5929 int endcol,
5930 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005932 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005934 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935{
5936 unsigned off_from;
5937 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005938#ifdef FEAT_MBYTE
5939 unsigned max_off_from;
5940 unsigned max_off_to;
5941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005943#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 int hl;
5945#endif
5946 int force = FALSE; /* force update rest of the line */
5947 int redraw_this /* bool: does character need redraw? */
5948#ifdef FEAT_GUI
5949 = TRUE /* For GUI when while-loop empty */
5950#endif
5951 ;
5952 int redraw_next; /* redraw_this for next character */
5953#ifdef FEAT_MBYTE
5954 int clear_next = FALSE;
5955 int char_cells; /* 1: normal char */
5956 /* 2: occupies two display cells */
5957# define CHAR_CELLS char_cells
5958#else
5959# define CHAR_CELLS 1
5960#endif
5961
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005962 /* Check for illegal row and col, just in case. */
5963 if (row >= Rows)
5964 row = Rows - 1;
5965 if (endcol > Columns)
5966 endcol = Columns;
5967
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968# ifdef FEAT_CLIPBOARD
5969 clip_may_clear_selection(row, row);
5970# endif
5971
5972 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5973 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005974#ifdef FEAT_MBYTE
5975 max_off_from = off_from + screen_Columns;
5976 max_off_to = LineOffset[row] + screen_Columns;
5977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978
5979#ifdef FEAT_RIGHTLEFT
5980 if (rlflag)
5981 {
5982 /* Clear rest first, because it's left of the text. */
5983 if (clear_width > 0)
5984 {
5985 while (col <= endcol && ScreenLines[off_to] == ' '
5986 && ScreenAttrs[off_to] == 0
5987# ifdef FEAT_MBYTE
5988 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5989# endif
5990 )
5991 {
5992 ++off_to;
5993 ++col;
5994 }
5995 if (col <= endcol)
5996 screen_fill(row, row + 1, col + coloff,
5997 endcol + coloff + 1, ' ', ' ', 0);
5998 }
5999 col = endcol + 1;
6000 off_to = LineOffset[row] + col + coloff;
6001 off_from += col;
6002 endcol = (clear_width > 0 ? clear_width : -clear_width);
6003 }
6004#endif /* FEAT_RIGHTLEFT */
6005
6006 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6007
6008 while (col < endcol)
6009 {
6010#ifdef FEAT_MBYTE
6011 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006012 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 else
6014 char_cells = 1;
6015#endif
6016
6017 redraw_this = redraw_next;
6018 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6019 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6020
6021#ifdef FEAT_GUI
6022 /* If the next character was bold, then redraw the current character to
6023 * remove any pixels that might have spilt over into us. This only
6024 * happens in the GUI.
6025 */
6026 if (redraw_next && gui.in_use)
6027 {
6028 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006029 if (hl > HL_ALL)
6030 hl = syn_attr2attr(hl);
6031 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 redraw_this = TRUE;
6033 }
6034#endif
6035
6036 if (redraw_this)
6037 {
6038 /*
6039 * Special handling when 'xs' termcap flag set (hpterm):
6040 * Attributes for characters are stored at the position where the
6041 * cursor is when writing the highlighting code. The
6042 * start-highlighting code must be written with the cursor on the
6043 * first highlighted character. The stop-highlighting code must
6044 * be written with the cursor just after the last highlighted
6045 * character.
6046 * Overwriting a character doesn't remove it's highlighting. Need
6047 * to clear the rest of the line, and force redrawing it
6048 * completely.
6049 */
6050 if ( p_wiv
6051 && !force
6052#ifdef FEAT_GUI
6053 && !gui.in_use
6054#endif
6055 && ScreenAttrs[off_to] != 0
6056 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6057 {
6058 /*
6059 * Need to remove highlighting attributes here.
6060 */
6061 windgoto(row, col + coloff);
6062 out_str(T_CE); /* clear rest of this screen line */
6063 screen_start(); /* don't know where cursor is now */
6064 force = TRUE; /* force redraw of rest of the line */
6065 redraw_next = TRUE; /* or else next char would miss out */
6066
6067 /*
6068 * If the previous character was highlighted, need to stop
6069 * highlighting at this character.
6070 */
6071 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6072 {
6073 screen_attr = ScreenAttrs[off_to - 1];
6074 term_windgoto(row, col + coloff);
6075 screen_stop_highlight();
6076 }
6077 else
6078 screen_attr = 0; /* highlighting has stopped */
6079 }
6080#ifdef FEAT_MBYTE
6081 if (enc_dbcs != 0)
6082 {
6083 /* Check if overwriting a double-byte with a single-byte or
6084 * the other way around requires another character to be
6085 * redrawn. For UTF-8 this isn't needed, because comparing
6086 * ScreenLinesUC[] is sufficient. */
6087 if (char_cells == 1
6088 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006089 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 {
6091 /* Writing a single-cell character over a double-cell
6092 * character: need to redraw the next cell. */
6093 ScreenLines[off_to + 1] = 0;
6094 redraw_next = TRUE;
6095 }
6096 else if (char_cells == 2
6097 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006098 && (*mb_off2cells)(off_to, max_off_to) == 1
6099 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {
6101 /* Writing the second half of a double-cell character over
6102 * a double-cell character: need to redraw the second
6103 * cell. */
6104 ScreenLines[off_to + 2] = 0;
6105 redraw_next = TRUE;
6106 }
6107
6108 if (enc_dbcs == DBCS_JPNU)
6109 ScreenLines2[off_to] = ScreenLines2[off_from];
6110 }
6111 /* When writing a single-width character over a double-width
6112 * character and at the end of the redrawn text, need to clear out
6113 * the right halve of the old character.
6114 * Also required when writing the right halve of a double-width
6115 * char over the left halve of an existing one. */
6116 if (has_mbyte && col + char_cells == endcol
6117 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006118 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006120 && (*mb_off2cells)(off_to, max_off_to) == 1
6121 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 clear_next = TRUE;
6123#endif
6124
6125 ScreenLines[off_to] = ScreenLines[off_from];
6126#ifdef FEAT_MBYTE
6127 if (enc_utf8)
6128 {
6129 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6130 if (ScreenLinesUC[off_from] != 0)
6131 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006132 int i;
6133
6134 for (i = 0; i < Screen_mco; ++i)
6135 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 }
6137 }
6138 if (char_cells == 2)
6139 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6140#endif
6141
6142#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006143 /* The bold trick makes a single column of pixels appear in the
6144 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 * character should be redrawn too. This happens for our own GUI
6146 * and for some xterms. */
6147 if (
6148# ifdef FEAT_GUI
6149 gui.in_use
6150# endif
6151# if defined(FEAT_GUI) && defined(UNIX)
6152 ||
6153# endif
6154# ifdef UNIX
6155 term_is_xterm
6156# endif
6157 )
6158 {
6159 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006160 if (hl > HL_ALL)
6161 hl = syn_attr2attr(hl);
6162 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 redraw_next = TRUE;
6164 }
6165#endif
6166 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6167#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006168 /* For simplicity set the attributes of second half of a
6169 * double-wide character equal to the first half. */
6170 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006172
6173 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 else
6176#endif
6177 screen_char(off_to, row, col + coloff);
6178 }
6179 else if ( p_wiv
6180#ifdef FEAT_GUI
6181 && !gui.in_use
6182#endif
6183 && col + coloff > 0)
6184 {
6185 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6186 {
6187 /*
6188 * Don't output stop-highlight when moving the cursor, it will
6189 * stop the highlighting when it should continue.
6190 */
6191 screen_attr = 0;
6192 }
6193 else if (screen_attr != 0)
6194 screen_stop_highlight();
6195 }
6196
6197 off_to += CHAR_CELLS;
6198 off_from += CHAR_CELLS;
6199 col += CHAR_CELLS;
6200 }
6201
6202#ifdef FEAT_MBYTE
6203 if (clear_next)
6204 {
6205 /* Clear the second half of a double-wide character of which the left
6206 * half was overwritten with a single-wide character. */
6207 ScreenLines[off_to] = ' ';
6208 if (enc_utf8)
6209 ScreenLinesUC[off_to] = 0;
6210 screen_char(off_to, row, col + coloff);
6211 }
6212#endif
6213
6214 if (clear_width > 0
6215#ifdef FEAT_RIGHTLEFT
6216 && !rlflag
6217#endif
6218 )
6219 {
6220#ifdef FEAT_GUI
6221 int startCol = col;
6222#endif
6223
6224 /* blank out the rest of the line */
6225 while (col < clear_width && ScreenLines[off_to] == ' '
6226 && ScreenAttrs[off_to] == 0
6227#ifdef FEAT_MBYTE
6228 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6229#endif
6230 )
6231 {
6232 ++off_to;
6233 ++col;
6234 }
6235 if (col < clear_width)
6236 {
6237#ifdef FEAT_GUI
6238 /*
6239 * In the GUI, clearing the rest of the line may leave pixels
6240 * behind if the first character cleared was bold. Some bold
6241 * fonts spill over the left. In this case we redraw the previous
6242 * character too. If we didn't skip any blanks above, then we
6243 * only redraw if the character wasn't already redrawn anyway.
6244 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006245 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 {
6247 hl = ScreenAttrs[off_to];
6248 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006249 {
6250 int prev_cells = 1;
6251# ifdef FEAT_MBYTE
6252 if (enc_utf8)
6253 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6254 * that its width is 2. */
6255 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6256 else if (enc_dbcs != 0)
6257 {
6258 /* find previous character by counting from first
6259 * column and get its width. */
6260 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006261 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006262
6263 while (off < off_to)
6264 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006265 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006266 off += prev_cells;
6267 }
6268 }
6269
6270 if (enc_dbcs != 0 && prev_cells > 1)
6271 screen_char_2(off_to - prev_cells, row,
6272 col + coloff - prev_cells);
6273 else
6274# endif
6275 screen_char(off_to - prev_cells, row,
6276 col + coloff - prev_cells);
6277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 }
6279#endif
6280 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6281 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006282#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 off_to += clear_width - col;
6284 col = clear_width;
6285#endif
6286 }
6287 }
6288
6289 if (clear_width > 0)
6290 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006291#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 /* For a window that's left of another, draw the separator char. */
6293 if (col + coloff < Columns)
6294 {
6295 int c;
6296
6297 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006298 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006300 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6301 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302# endif
6303 || ScreenAttrs[off_to] != hl)
6304 {
6305 ScreenLines[off_to] = c;
6306 ScreenAttrs[off_to] = hl;
6307# ifdef FEAT_MBYTE
6308 if (enc_utf8)
6309 {
6310 if (c >= 0x80)
6311 {
6312 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006313 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 }
6315 else
6316 ScreenLinesUC[off_to] = 0;
6317 }
6318# endif
6319 screen_char(off_to, row, col + coloff);
6320 }
6321 }
6322 else
6323#endif
6324 LineWraps[row] = FALSE;
6325 }
6326}
6327
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006328#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006330 * Mirror text "str" for right-left displaying.
6331 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006333 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006334rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335{
6336 char_u *p1, *p2;
6337 int t;
6338
6339 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6340 {
6341 t = *p1;
6342 *p1 = *p2;
6343 *p2 = t;
6344 }
6345}
6346#endif
6347
6348#if defined(FEAT_WINDOWS) || defined(PROTO)
6349/*
6350 * mark all status lines for redraw; used after first :cd
6351 */
6352 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006353status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354{
6355 win_T *wp;
6356
Bram Moolenaar29323592016-07-24 22:04:11 +02006357 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 if (wp->w_status_height)
6359 {
6360 wp->w_redr_status = TRUE;
6361 redraw_later(VALID);
6362 }
6363}
6364
6365/*
6366 * mark all status lines of the current buffer for redraw
6367 */
6368 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006369status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
6371 win_T *wp;
6372
Bram Moolenaar29323592016-07-24 22:04:11 +02006373 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6375 {
6376 wp->w_redr_status = TRUE;
6377 redraw_later(VALID);
6378 }
6379}
6380
6381/*
6382 * Redraw all status lines that need to be redrawn.
6383 */
6384 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006385redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 win_T *wp;
6388
Bram Moolenaar29323592016-07-24 22:04:11 +02006389 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (wp->w_redr_status)
6391 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006392 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006393 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394}
6395#endif
6396
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006397#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398/*
6399 * Redraw all status lines at the bottom of frame "frp".
6400 */
6401 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006402win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403{
6404 if (frp->fr_layout == FR_LEAF)
6405 frp->fr_win->w_redr_status = TRUE;
6406 else if (frp->fr_layout == FR_ROW)
6407 {
6408 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6409 win_redraw_last_status(frp);
6410 }
6411 else /* frp->fr_layout == FR_COL */
6412 {
6413 frp = frp->fr_child;
6414 while (frp->fr_next != NULL)
6415 frp = frp->fr_next;
6416 win_redraw_last_status(frp);
6417 }
6418}
6419#endif
6420
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006421#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422/*
6423 * Draw the verticap separator right of window "wp" starting with line "row".
6424 */
6425 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006426draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
6428 int hl;
6429 int c;
6430
6431 if (wp->w_vsep_width)
6432 {
6433 /* draw the vertical separator right of this window */
6434 c = fillchar_vsep(&hl);
6435 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6436 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6437 c, ' ', hl);
6438 }
6439}
6440#endif
6441
6442#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006443static int status_match_len(expand_T *xp, char_u *s);
6444static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445
6446/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006447 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 */
6449 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006450status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
6452 int len = 0;
6453
6454#ifdef FEAT_MENU
6455 int emenu = (xp->xp_context == EXPAND_MENUS
6456 || xp->xp_context == EXPAND_MENUNAMES);
6457
6458 /* Check for menu separators - replace with '|'. */
6459 if (emenu && menu_is_separator(s))
6460 return 1;
6461#endif
6462
6463 while (*s != NUL)
6464 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006465 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006466 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006467 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 }
6469
6470 return len;
6471}
6472
6473/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006474 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006475 * These are backslashes used for escaping. Do show backslashes in help tags.
6476 */
6477 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006478skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006479{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006480 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006481#ifdef FEAT_MENU
6482 || ((xp->xp_context == EXPAND_MENUS
6483 || xp->xp_context == EXPAND_MENUNAMES)
6484 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6485#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006486 )
6487 {
6488#ifndef BACKSLASH_IN_FILENAME
6489 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6490 return 2;
6491#endif
6492 return 1;
6493 }
6494 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006495}
6496
6497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 * Show wildchar matches in the status line.
6499 * Show at least the "match" item.
6500 * We start at item 'first_match' in the list and show all matches that fit.
6501 *
6502 * If inversion is possible we use it. Else '=' characters are used.
6503 */
6504 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006505win_redr_status_matches(
6506 expand_T *xp,
6507 int num_matches,
6508 char_u **matches, /* list of matches */
6509 int match,
6510 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511{
6512#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6513 int row;
6514 char_u *buf;
6515 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006516 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 int fillchar;
6518 int attr;
6519 int i;
6520 int highlight = TRUE;
6521 char_u *selstart = NULL;
6522 int selstart_col = 0;
6523 char_u *selend = NULL;
6524 static int first_match = 0;
6525 int add_left = FALSE;
6526 char_u *s;
6527#ifdef FEAT_MENU
6528 int emenu;
6529#endif
6530#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6531 int l;
6532#endif
6533
6534 if (matches == NULL) /* interrupted completion? */
6535 return;
6536
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006537#ifdef FEAT_MBYTE
6538 if (has_mbyte)
6539 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6540 else
6541#endif
6542 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 if (buf == NULL)
6544 return;
6545
6546 if (match == -1) /* don't show match but original text */
6547 {
6548 match = 0;
6549 highlight = FALSE;
6550 }
6551 /* count 1 for the ending ">" */
6552 clen = status_match_len(xp, L_MATCH(match)) + 3;
6553 if (match == 0)
6554 first_match = 0;
6555 else if (match < first_match)
6556 {
6557 /* jumping left, as far as we can go */
6558 first_match = match;
6559 add_left = TRUE;
6560 }
6561 else
6562 {
6563 /* check if match fits on the screen */
6564 for (i = first_match; i < match; ++i)
6565 clen += status_match_len(xp, L_MATCH(i)) + 2;
6566 if (first_match > 0)
6567 clen += 2;
6568 /* jumping right, put match at the left */
6569 if ((long)clen > Columns)
6570 {
6571 first_match = match;
6572 /* if showing the last match, we can add some on the left */
6573 clen = 2;
6574 for (i = match; i < num_matches; ++i)
6575 {
6576 clen += status_match_len(xp, L_MATCH(i)) + 2;
6577 if ((long)clen >= Columns)
6578 break;
6579 }
6580 if (i == num_matches)
6581 add_left = TRUE;
6582 }
6583 }
6584 if (add_left)
6585 while (first_match > 0)
6586 {
6587 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6588 if ((long)clen >= Columns)
6589 break;
6590 --first_match;
6591 }
6592
6593 fillchar = fillchar_status(&attr, TRUE);
6594
6595 if (first_match == 0)
6596 {
6597 *buf = NUL;
6598 len = 0;
6599 }
6600 else
6601 {
6602 STRCPY(buf, "< ");
6603 len = 2;
6604 }
6605 clen = len;
6606
6607 i = first_match;
6608 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6609 {
6610 if (i == match)
6611 {
6612 selstart = buf + len;
6613 selstart_col = clen;
6614 }
6615
6616 s = L_MATCH(i);
6617 /* Check for menu separators - replace with '|' */
6618#ifdef FEAT_MENU
6619 emenu = (xp->xp_context == EXPAND_MENUS
6620 || xp->xp_context == EXPAND_MENUNAMES);
6621 if (emenu && menu_is_separator(s))
6622 {
6623 STRCPY(buf + len, transchar('|'));
6624 l = (int)STRLEN(buf + len);
6625 len += l;
6626 clen += l;
6627 }
6628 else
6629#endif
6630 for ( ; *s != NUL; ++s)
6631 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006632 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 clen += ptr2cells(s);
6634#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006635 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 {
6637 STRNCPY(buf + len, s, l);
6638 s += l - 1;
6639 len += l;
6640 }
6641 else
6642#endif
6643 {
6644 STRCPY(buf + len, transchar_byte(*s));
6645 len += (int)STRLEN(buf + len);
6646 }
6647 }
6648 if (i == match)
6649 selend = buf + len;
6650
6651 *(buf + len++) = ' ';
6652 *(buf + len++) = ' ';
6653 clen += 2;
6654 if (++i == num_matches)
6655 break;
6656 }
6657
6658 if (i != num_matches)
6659 {
6660 *(buf + len++) = '>';
6661 ++clen;
6662 }
6663
6664 buf[len] = NUL;
6665
6666 row = cmdline_row - 1;
6667 if (row >= 0)
6668 {
6669 if (wild_menu_showing == 0)
6670 {
6671 if (msg_scrolled > 0)
6672 {
6673 /* Put the wildmenu just above the command line. If there is
6674 * no room, scroll the screen one line up. */
6675 if (cmdline_row == Rows - 1)
6676 {
6677 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6678 ++msg_scrolled;
6679 }
6680 else
6681 {
6682 ++cmdline_row;
6683 ++row;
6684 }
6685 wild_menu_showing = WM_SCROLLED;
6686 }
6687 else
6688 {
6689 /* Create status line if needed by setting 'laststatus' to 2.
6690 * Set 'winminheight' to zero to avoid that the window is
6691 * resized. */
6692 if (lastwin->w_status_height == 0)
6693 {
6694 save_p_ls = p_ls;
6695 save_p_wmh = p_wmh;
6696 p_ls = 2;
6697 p_wmh = 0;
6698 last_status(FALSE);
6699 }
6700 wild_menu_showing = WM_SHOWN;
6701 }
6702 }
6703
6704 screen_puts(buf, row, 0, attr);
6705 if (selstart != NULL && highlight)
6706 {
6707 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006708 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 }
6710
6711 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6712 }
6713
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006714#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 win_redraw_last_status(topframe);
6716#else
6717 lastwin->w_redr_status = TRUE;
6718#endif
6719 vim_free(buf);
6720}
6721#endif
6722
6723#if defined(FEAT_WINDOWS) || defined(PROTO)
6724/*
6725 * Redraw the status line of window wp.
6726 *
6727 * If inversion is possible we use it. Else '=' characters are used.
6728 */
6729 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006730win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731{
6732 int row;
6733 char_u *p;
6734 int len;
6735 int fillchar;
6736 int attr;
6737 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006738 static int busy = FALSE;
6739
6740 /* It's possible to get here recursively when 'statusline' (indirectly)
6741 * invokes ":redrawstatus". Simply ignore the call then. */
6742 if (busy)
6743 return;
6744 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745
6746 wp->w_redr_status = FALSE;
6747 if (wp->w_status_height == 0)
6748 {
6749 /* no status line, can only be last window */
6750 redraw_cmdline = TRUE;
6751 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006752 else if (!redrawing()
6753#ifdef FEAT_INS_EXPAND
6754 /* don't update status line when popup menu is visible and may be
6755 * drawn over it */
6756 || pum_visible()
6757#endif
6758 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {
6760 /* Don't redraw right now, do it later. */
6761 wp->w_redr_status = TRUE;
6762 }
6763#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006764 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 {
6766 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006767 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 }
6769#endif
6770 else
6771 {
6772 fillchar = fillchar_status(&attr, wp == curwin);
6773
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006774 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 p = NameBuff;
6776 len = (int)STRLEN(p);
6777
6778 if (wp->w_buffer->b_help
6779#ifdef FEAT_QUICKFIX
6780 || wp->w_p_pvw
6781#endif
6782 || bufIsChanged(wp->w_buffer)
6783 || wp->w_buffer->b_p_ro)
6784 *(p + len++) = ' ';
6785 if (wp->w_buffer->b_help)
6786 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006787 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 len += (int)STRLEN(p + len);
6789 }
6790#ifdef FEAT_QUICKFIX
6791 if (wp->w_p_pvw)
6792 {
6793 STRCPY(p + len, _("[Preview]"));
6794 len += (int)STRLEN(p + len);
6795 }
6796#endif
6797 if (bufIsChanged(wp->w_buffer))
6798 {
6799 STRCPY(p + len, "[+]");
6800 len += 3;
6801 }
6802 if (wp->w_buffer->b_p_ro)
6803 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006804 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006805 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 }
6807
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6809 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6810 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6811 if (this_ru_col <= 1)
6812 {
6813 p = (char_u *)"<"; /* No room for file name! */
6814 len = 1;
6815 }
6816 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817#ifdef FEAT_MBYTE
6818 if (has_mbyte)
6819 {
6820 int clen = 0, i;
6821
6822 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006823 clen = mb_string2cells(p, -1);
6824
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 /* Find first character that will fit.
6826 * Going from start to end is much faster for DBCS. */
6827 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006828 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 clen -= (*mb_ptr2cells)(p + i);
6830 len = clen;
6831 if (i > 0)
6832 {
6833 p = p + i - 1;
6834 *p = '<';
6835 ++len;
6836 }
6837
6838 }
6839 else
6840#endif
6841 if (len > this_ru_col - 1)
6842 {
6843 p += len - (this_ru_col - 1);
6844 *p = '<';
6845 len = this_ru_col - 1;
6846 }
6847
6848 row = W_WINROW(wp) + wp->w_height;
6849 screen_puts(p, row, W_WINCOL(wp), attr);
6850 screen_fill(row, row + 1, len + W_WINCOL(wp),
6851 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6852
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006853 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6855 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6856 - 1 + W_WINCOL(wp)), attr);
6857
6858#ifdef FEAT_CMDL_INFO
6859 win_redr_ruler(wp, TRUE);
6860#endif
6861 }
6862
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 /*
6864 * May need to draw the character below the vertical separator.
6865 */
6866 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6867 {
6868 if (stl_connected(wp))
6869 fillchar = fillchar_status(&attr, wp == curwin);
6870 else
6871 fillchar = fillchar_vsep(&attr);
6872 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6873 attr);
6874 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006875 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876}
6877
Bram Moolenaar238a5642006-02-21 22:12:05 +00006878#ifdef FEAT_STL_OPT
6879/*
6880 * Redraw the status line according to 'statusline' and take care of any
6881 * errors encountered.
6882 */
6883 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006884redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006885{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006886 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006887 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006888
6889 /* When called recursively return. This can happen when the statusline
6890 * contains an expression that triggers a redraw. */
6891 if (entered)
6892 return;
6893 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006894
Bram Moolenaara742e082016-04-05 21:10:38 +02006895 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006896 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006897 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006898 {
6899 /* When there is an error disable the statusline, otherwise the
6900 * display is messed up with errors and a redraw triggers the problem
6901 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006902 set_string_option_direct((char_u *)"statusline", -1,
6903 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006904 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006905 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006906 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006907 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006908}
6909#endif
6910
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911/*
6912 * Return TRUE if the status line of window "wp" is connected to the status
6913 * line of the window right of it. If not, then it's a vertical separator.
6914 * Only call if (wp->w_vsep_width != 0).
6915 */
6916 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006917stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918{
6919 frame_T *fr;
6920
6921 fr = wp->w_frame;
6922 while (fr->fr_parent != NULL)
6923 {
6924 if (fr->fr_parent->fr_layout == FR_COL)
6925 {
6926 if (fr->fr_next != NULL)
6927 break;
6928 }
6929 else
6930 {
6931 if (fr->fr_next != NULL)
6932 return TRUE;
6933 }
6934 fr = fr->fr_parent;
6935 }
6936 return FALSE;
6937}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
6939#endif /* FEAT_WINDOWS */
6940
6941#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6942/*
6943 * Get the value to show for the language mappings, active 'keymap'.
6944 */
6945 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006946get_keymap_str(
6947 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006948 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01006949 char_u *buf, /* buffer for the result */
6950 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951{
6952 char_u *p;
6953
6954 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6955 return FALSE;
6956
6957 {
6958#ifdef FEAT_EVAL
6959 buf_T *old_curbuf = curbuf;
6960 win_T *old_curwin = curwin;
6961 char_u *s;
6962
6963 curbuf = wp->w_buffer;
6964 curwin = wp;
6965 STRCPY(buf, "b:keymap_name"); /* must be writable */
6966 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006967 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 --emsg_skip;
6969 curbuf = old_curbuf;
6970 curwin = old_curwin;
6971 if (p == NULL || *p == NUL)
6972#endif
6973 {
6974#ifdef FEAT_KEYMAP
6975 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6976 p = wp->w_buffer->b_p_keymap;
6977 else
6978#endif
6979 p = (char_u *)"lang";
6980 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006981 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 buf[0] = NUL;
6983#ifdef FEAT_EVAL
6984 vim_free(s);
6985#endif
6986 }
6987 return buf[0] != NUL;
6988}
6989#endif
6990
6991#if defined(FEAT_STL_OPT) || defined(PROTO)
6992/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006993 * Redraw the status line or ruler of window "wp".
6994 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 */
6996 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006997win_redr_custom(
6998 win_T *wp,
6999 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007001 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 int attr;
7003 int curattr;
7004 int row;
7005 int col = 0;
7006 int maxwidth;
7007 int width;
7008 int n;
7009 int len;
7010 int fillchar;
7011 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007012 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007014 struct stl_hlrec hltab[STL_MAX_ITEM];
7015 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007016 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007017 win_T *ewp;
7018 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019
Bram Moolenaar1d633412013-12-11 15:52:01 +01007020 /* There is a tiny chance that this gets called recursively: When
7021 * redrawing a status line triggers redrawing the ruler or tabline.
7022 * Avoid trouble by not allowing recursion. */
7023 if (entered)
7024 return;
7025 entered = TRUE;
7026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007028 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007030 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007031 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007032 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007033 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007034 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007035 maxwidth = Columns;
7036# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007037 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007040 else
7041 {
7042 row = W_WINROW(wp) + wp->w_height;
7043 fillchar = fillchar_status(&attr, wp == curwin);
7044 maxwidth = W_WIDTH(wp);
7045
7046 if (draw_ruler)
7047 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007048 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007049 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007050 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007051 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007052 if (*++stl == '-')
7053 stl++;
7054 if (atoi((char *)stl))
7055 while (VIM_ISDIGIT(*stl))
7056 stl++;
7057 if (*stl++ != '(')
7058 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007059 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007060#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007061 col = ru_col - (Columns - W_WIDTH(wp));
7062 if (col < (W_WIDTH(wp) + 1) / 2)
7063 col = (W_WIDTH(wp) + 1) / 2;
7064#else
7065 col = ru_col;
7066 if (col > (Columns + 1) / 2)
7067 col = (Columns + 1) / 2;
7068#endif
7069 maxwidth = W_WIDTH(wp) - col;
7070#ifdef FEAT_WINDOWS
7071 if (!wp->w_status_height)
7072#endif
7073 {
7074 row = Rows - 1;
7075 --maxwidth; /* writing in last column may cause scrolling */
7076 fillchar = ' ';
7077 attr = 0;
7078 }
7079
7080# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007081 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007082# endif
7083 }
7084 else
7085 {
7086 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007087 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007088 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007089 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007090# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007091 use_sandbox = was_set_insecurely((char_u *)"statusline",
7092 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007093# endif
7094 }
7095
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007096#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007097 col += W_WINCOL(wp);
7098#endif
7099 }
7100
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007102 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
Bram Moolenaar61452852011-02-01 18:01:11 +01007104 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7105 * the cursor away and back. */
7106 ewp = wp == NULL ? curwin : wp;
7107 p_crb_save = ewp->w_p_crb;
7108 ewp->w_p_crb = FALSE;
7109
Bram Moolenaar362f3562009-11-03 16:20:34 +00007110 /* Make a copy, because the statusline may include a function call that
7111 * might change the option value and free the memory. */
7112 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007113 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007114 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007115 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007116 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007117 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007119 /* Make all characters printable. */
7120 p = transstr(buf);
7121 if (p != NULL)
7122 {
7123 vim_strncpy(buf, p, sizeof(buf) - 1);
7124 vim_free(p);
7125 }
7126
7127 /* fill up with "fillchar" */
7128 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007129 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 {
7131#ifdef FEAT_MBYTE
7132 len += (*mb_char2bytes)(fillchar, buf + len);
7133#else
7134 buf[len++] = fillchar;
7135#endif
7136 ++width;
7137 }
7138 buf[len] = NUL;
7139
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007140 /*
7141 * Draw each snippet with the specified highlighting.
7142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 curattr = attr;
7144 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007145 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007147 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 screen_puts_len(p, len, row, col, curattr);
7149 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007150 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007152 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007154 else if (hltab[n].userhl < 0)
7155 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007157 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007158 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159#endif
7160 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007161 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 }
7163 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007164
7165 if (wp == NULL)
7166 {
7167 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7168 col = 0;
7169 len = 0;
7170 p = buf;
7171 fillchar = 0;
7172 for (n = 0; tabtab[n].start != NULL; n++)
7173 {
7174 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7175 while (col < len)
7176 TabPageIdxs[col++] = fillchar;
7177 p = tabtab[n].start;
7178 fillchar = tabtab[n].userhl;
7179 }
7180 while (col < Columns)
7181 TabPageIdxs[col++] = fillchar;
7182 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007183
7184theend:
7185 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186}
7187
7188#endif /* FEAT_STL_OPT */
7189
7190/*
7191 * Output a single character directly to the screen and update ScreenLines.
7192 */
7193 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007194screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 char_u buf[MB_MAXBYTES + 1];
7197
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007198#ifdef FEAT_MBYTE
7199 if (has_mbyte)
7200 buf[(*mb_char2bytes)(c, buf)] = NUL;
7201 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007203 {
7204 buf[0] = c;
7205 buf[1] = NUL;
7206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 screen_puts(buf, row, col, attr);
7208}
7209
7210/*
7211 * Get a single character directly from ScreenLines into "bytes[]".
7212 * Also return its attribute in *attrp;
7213 */
7214 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007215screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 unsigned off;
7218
7219 /* safety check */
7220 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7221 {
7222 off = LineOffset[row] + col;
7223 *attrp = ScreenAttrs[off];
7224 bytes[0] = ScreenLines[off];
7225 bytes[1] = NUL;
7226
7227#ifdef FEAT_MBYTE
7228 if (enc_utf8 && ScreenLinesUC[off] != 0)
7229 bytes[utfc_char2bytes(off, bytes)] = NUL;
7230 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7231 {
7232 bytes[0] = ScreenLines[off];
7233 bytes[1] = ScreenLines2[off];
7234 bytes[2] = NUL;
7235 }
7236 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7237 {
7238 bytes[1] = ScreenLines[off + 1];
7239 bytes[2] = NUL;
7240 }
7241#endif
7242 }
7243}
7244
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007245#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007246static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007247
7248/*
7249 * Return TRUE if composing characters for screen posn "off" differs from
7250 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007251 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007252 */
7253 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007254screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007255{
7256 int i;
7257
7258 for (i = 0; i < Screen_mco; ++i)
7259 {
7260 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7261 return TRUE;
7262 if (u8cc[i] == 0)
7263 break;
7264 }
7265 return FALSE;
7266}
7267#endif
7268
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269/*
7270 * Put string '*text' on the screen at position 'row' and 'col', with
7271 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7272 * Note: only outputs within one row, message is truncated at screen boundary!
7273 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7274 */
7275 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007276screen_puts(
7277 char_u *text,
7278 int row,
7279 int col,
7280 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281{
7282 screen_puts_len(text, -1, row, col, attr);
7283}
7284
7285/*
7286 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7287 * a NUL.
7288 */
7289 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007290screen_puts_len(
7291 char_u *text,
7292 int textlen,
7293 int row,
7294 int col,
7295 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296{
7297 unsigned off;
7298 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007299 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 int c;
7301#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007302 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 int mbyte_blen = 1;
7304 int mbyte_cells = 1;
7305 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007306 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 int clear_next_cell = FALSE;
7308# ifdef FEAT_ARABIC
7309 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007310 int pc, nc, nc1;
7311 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312# endif
7313#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007314#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7315 int force_redraw_this;
7316 int force_redraw_next = FALSE;
7317#endif
7318 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7321 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007322 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323
Bram Moolenaarc236c162008-07-13 17:41:49 +00007324#ifdef FEAT_MBYTE
7325 /* When drawing over the right halve of a double-wide char clear out the
7326 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007327 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007328# ifdef FEAT_GUI
7329 && !gui.in_use
7330# endif
7331 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007332 {
7333 ScreenLines[off - 1] = ' ';
7334 ScreenAttrs[off - 1] = 0;
7335 if (enc_utf8)
7336 {
7337 ScreenLinesUC[off - 1] = 0;
7338 ScreenLinesC[0][off - 1] = 0;
7339 }
7340 /* redraw the previous cell, make it empty */
7341 screen_char(off - 1, row, col - 1);
7342 /* force the cell at "col" to be redrawn */
7343 force_redraw_next = TRUE;
7344 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007345#endif
7346
Bram Moolenaar367329b2007-08-30 11:53:22 +00007347#ifdef FEAT_MBYTE
7348 max_off = LineOffset[row] + screen_Columns;
7349#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007350 while (col < screen_Columns
7351 && (len < 0 || (int)(ptr - text) < len)
7352 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 {
7354 c = *ptr;
7355#ifdef FEAT_MBYTE
7356 /* check if this is the first byte of a multibyte */
7357 if (has_mbyte)
7358 {
7359 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007360 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007362 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7364 mbyte_cells = 1;
7365 else if (enc_dbcs != 0)
7366 mbyte_cells = mbyte_blen;
7367 else /* enc_utf8 */
7368 {
7369 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007370 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 (int)((text + len) - ptr));
7372 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007373 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007375# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 /* Non-BMP character: display as ? or fullwidth ?. */
7377 if (u8c >= 0x10000)
7378 {
7379 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7380 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007381 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384# ifdef FEAT_ARABIC
7385 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7386 {
7387 /* Do Arabic shaping. */
7388 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7389 {
7390 /* Past end of string to be displayed. */
7391 nc = NUL;
7392 nc1 = NUL;
7393 }
7394 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007395 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007396 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7397 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007398 nc1 = pcc[0];
7399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 pc = prev_c;
7401 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007402 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 }
7404 else
7405 prev_c = u8c;
7406# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007407 if (col + mbyte_cells > screen_Columns)
7408 {
7409 /* Only 1 cell left, but character requires 2 cells:
7410 * display a '>' in the last column to avoid wrapping. */
7411 c = '>';
7412 mbyte_cells = 1;
7413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 }
7415 }
7416#endif
7417
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007418#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7419 force_redraw_this = force_redraw_next;
7420 force_redraw_next = FALSE;
7421#endif
7422
7423 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424#ifdef FEAT_MBYTE
7425 || (mbyte_cells == 2
7426 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7427 || (enc_dbcs == DBCS_JPNU
7428 && c == 0x8e
7429 && ScreenLines2[off] != ptr[1])
7430 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007431 && (ScreenLinesUC[off] !=
7432 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7433 || (ScreenLinesUC[off] != 0
7434 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435#endif
7436 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007437 || exmode_active;
7438
7439 if (need_redraw
7440#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7441 || force_redraw_this
7442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 )
7444 {
7445#if defined(FEAT_GUI) || defined(UNIX)
7446 /* The bold trick makes a single row of pixels appear in the next
7447 * character. When a bold character is removed, the next
7448 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007449 * and for some xterms. */
7450 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451# ifdef FEAT_GUI
7452 gui.in_use
7453# endif
7454# if defined(FEAT_GUI) && defined(UNIX)
7455 ||
7456# endif
7457# ifdef UNIX
7458 term_is_xterm
7459# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007460 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007462 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007464 if (n > HL_ALL)
7465 n = syn_attr2attr(n);
7466 if (n & HL_BOLD)
7467 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 }
7469#endif
7470#ifdef FEAT_MBYTE
7471 /* When at the end of the text and overwriting a two-cell
7472 * character with a one-cell character, need to clear the next
7473 * cell. Also when overwriting the left halve of a two-cell char
7474 * with the right halve of a two-cell char. Do this only once
7475 * (mb_off2cells() may return 2 on the right halve). */
7476 if (clear_next_cell)
7477 clear_next_cell = FALSE;
7478 else if (has_mbyte
7479 && (len < 0 ? ptr[mbyte_blen] == NUL
7480 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007481 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007483 && (*mb_off2cells)(off, max_off) == 1
7484 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 clear_next_cell = TRUE;
7486
7487 /* Make sure we never leave a second byte of a double-byte behind,
7488 * it confuses mb_off2cells(). */
7489 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007490 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007492 && (*mb_off2cells)(off, max_off) == 1
7493 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 ScreenLines[off + mbyte_blen] = 0;
7495#endif
7496 ScreenLines[off] = c;
7497 ScreenAttrs[off] = attr;
7498#ifdef FEAT_MBYTE
7499 if (enc_utf8)
7500 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007501 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 ScreenLinesUC[off] = 0;
7503 else
7504 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007505 int i;
7506
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007508 for (i = 0; i < Screen_mco; ++i)
7509 {
7510 ScreenLinesC[i][off] = u8cc[i];
7511 if (u8cc[i] == 0)
7512 break;
7513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 }
7515 if (mbyte_cells == 2)
7516 {
7517 ScreenLines[off + 1] = 0;
7518 ScreenAttrs[off + 1] = attr;
7519 }
7520 screen_char(off, row, col);
7521 }
7522 else if (mbyte_cells == 2)
7523 {
7524 ScreenLines[off + 1] = ptr[1];
7525 ScreenAttrs[off + 1] = attr;
7526 screen_char_2(off, row, col);
7527 }
7528 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7529 {
7530 ScreenLines2[off] = ptr[1];
7531 screen_char(off, row, col);
7532 }
7533 else
7534#endif
7535 screen_char(off, row, col);
7536 }
7537#ifdef FEAT_MBYTE
7538 if (has_mbyte)
7539 {
7540 off += mbyte_cells;
7541 col += mbyte_cells;
7542 ptr += mbyte_blen;
7543 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007544 {
7545 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007547 len = -1;
7548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 }
7550 else
7551#endif
7552 {
7553 ++off;
7554 ++col;
7555 ++ptr;
7556 }
7557 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007558
7559#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7560 /* If we detected the next character needs to be redrawn, but the text
7561 * doesn't extend up to there, update the character here. */
7562 if (force_redraw_next && col < screen_Columns)
7563 {
7564# ifdef FEAT_MBYTE
7565 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7566 screen_char_2(off, row, col);
7567 else
7568# endif
7569 screen_char(off, row, col);
7570 }
7571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572}
7573
7574#ifdef FEAT_SEARCH_EXTRA
7575/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007576 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 */
7578 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007579start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580{
7581 if (p_hls && !no_hlsearch)
7582 {
7583 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007584 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007585# ifdef FEAT_RELTIME
7586 /* Set the time limit to 'redrawtime'. */
7587 profile_setlimit(p_rdt, &search_hl.tm);
7588# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 }
7590}
7591
7592/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007593 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 */
7595 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007596end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
7598 if (search_hl.rm.regprog != NULL)
7599 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007600 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 search_hl.rm.regprog = NULL;
7602 }
7603}
7604
7605/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007606 * Init for calling prepare_search_hl().
7607 */
7608 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007609init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007610{
7611 matchitem_T *cur;
7612
7613 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7614 * match */
7615 cur = wp->w_match_head;
7616 while (cur != NULL)
7617 {
7618 cur->hl.rm = cur->match;
7619 if (cur->hlg_id == 0)
7620 cur->hl.attr = 0;
7621 else
7622 cur->hl.attr = syn_id2attr(cur->hlg_id);
7623 cur->hl.buf = wp->w_buffer;
7624 cur->hl.lnum = 0;
7625 cur->hl.first_lnum = 0;
7626# ifdef FEAT_RELTIME
7627 /* Set the time limit to 'redrawtime'. */
7628 profile_setlimit(p_rdt, &(cur->hl.tm));
7629# endif
7630 cur = cur->next;
7631 }
7632 search_hl.buf = wp->w_buffer;
7633 search_hl.lnum = 0;
7634 search_hl.first_lnum = 0;
7635 /* time limit is set at the toplevel, for all windows */
7636}
7637
7638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 * Advance to the match in window "wp" line "lnum" or past it.
7640 */
7641 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007642prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007644 matchitem_T *cur; /* points to the match list */
7645 match_T *shl; /* points to search_hl or a match */
7646 int shl_flag; /* flag to indicate whether search_hl
7647 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007648 int pos_inprogress; /* marks that position match search is
7649 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 int n;
7651
7652 /*
7653 * When using a multi-line pattern, start searching at the top
7654 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007655 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007657 cur = wp->w_match_head;
7658 shl_flag = FALSE;
7659 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007661 if (shl_flag == FALSE)
7662 {
7663 shl = &search_hl;
7664 shl_flag = TRUE;
7665 }
7666 else
7667 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 if (shl->rm.regprog != NULL
7669 && shl->lnum == 0
7670 && re_multiline(shl->rm.regprog))
7671 {
7672 if (shl->first_lnum == 0)
7673 {
7674# ifdef FEAT_FOLDING
7675 for (shl->first_lnum = lnum;
7676 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7677 if (hasFoldingWin(wp, shl->first_lnum - 1,
7678 NULL, NULL, TRUE, NULL))
7679 break;
7680# else
7681 shl->first_lnum = wp->w_topline;
7682# endif
7683 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007684 if (cur != NULL)
7685 cur->pos.cur = 0;
7686 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007688 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7689 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007691 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7692 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007693 pos_inprogress = cur == NULL || cur->pos.cur == 0
7694 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 if (shl->lnum != 0)
7696 {
7697 shl->first_lnum = shl->lnum
7698 + shl->rm.endpos[0].lnum
7699 - shl->rm.startpos[0].lnum;
7700 n = shl->rm.endpos[0].col;
7701 }
7702 else
7703 {
7704 ++shl->first_lnum;
7705 n = 0;
7706 }
7707 }
7708 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007709 if (shl != &search_hl && cur != NULL)
7710 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 }
7712}
7713
7714/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007715 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 * Uses shl->buf.
7717 * Sets shl->lnum and shl->rm contents.
7718 * Note: Assumes a previous match is always before "lnum", unless
7719 * shl->lnum is zero.
7720 * Careful: Any pointers for buffer lines will become invalid.
7721 */
7722 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007723next_search_hl(
7724 win_T *win,
7725 match_T *shl, /* points to search_hl or a match */
7726 linenr_T lnum,
7727 colnr_T mincol, /* minimal column for a match */
7728 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729{
7730 linenr_T l;
7731 colnr_T matchcol;
7732 long nmatched;
7733
7734 if (shl->lnum != 0)
7735 {
7736 /* Check for three situations:
7737 * 1. If the "lnum" is below a previous match, start a new search.
7738 * 2. If the previous match includes "mincol", use it.
7739 * 3. Continue after the previous match.
7740 */
7741 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7742 if (lnum > l)
7743 shl->lnum = 0;
7744 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7745 return;
7746 }
7747
7748 /*
7749 * Repeat searching for a match until one is found that includes "mincol"
7750 * or none is found in this line.
7751 */
7752 called_emsg = FALSE;
7753 for (;;)
7754 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007755#ifdef FEAT_RELTIME
7756 /* Stop searching after passing the time limit. */
7757 if (profile_passed_limit(&(shl->tm)))
7758 {
7759 shl->lnum = 0; /* no match found in time */
7760 break;
7761 }
7762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 /* Three situations:
7764 * 1. No useful previous match: search from start of line.
7765 * 2. Not Vi compatible or empty match: continue at next character.
7766 * Break the loop if this is beyond the end of the line.
7767 * 3. Vi compatible searching: continue at end of previous match.
7768 */
7769 if (shl->lnum == 0)
7770 matchcol = 0;
7771 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7772 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007773 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007775 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007776
7777 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007778 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007779 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007781 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 shl->lnum = 0;
7783 break;
7784 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007785#ifdef FEAT_MBYTE
7786 if (has_mbyte)
7787 matchcol += mb_ptr2len(ml);
7788 else
7789#endif
7790 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 }
7792 else
7793 matchcol = shl->rm.endpos[0].col;
7794
7795 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007796 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007798 /* Remember whether shl->rm is using a copy of the regprog in
7799 * cur->match. */
7800 int regprog_is_copy = (shl != &search_hl && cur != NULL
7801 && shl == &cur->hl
7802 && cur->match.regprog == cur->hl.rm.regprog);
7803
Bram Moolenaarb3414592014-06-17 17:48:32 +02007804 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7805 matchcol,
7806#ifdef FEAT_RELTIME
7807 &(shl->tm)
7808#else
7809 NULL
7810#endif
7811 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007812 /* Copy the regprog, in case it got freed and recompiled. */
7813 if (regprog_is_copy)
7814 cur->match.regprog = cur->hl.rm.regprog;
7815
Bram Moolenaarb3414592014-06-17 17:48:32 +02007816 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007817 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007818 /* Error while handling regexp: stop using this regexp. */
7819 if (shl == &search_hl)
7820 {
7821 /* don't free regprog in the match list, it's a copy */
7822 vim_regfree(shl->rm.regprog);
7823 SET_NO_HLSEARCH(TRUE);
7824 }
7825 shl->rm.regprog = NULL;
7826 shl->lnum = 0;
7827 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7828 message */
7829 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007830 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007831 }
7832 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007833 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007834 else
7835 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 if (nmatched == 0)
7837 {
7838 shl->lnum = 0; /* no match found */
7839 break;
7840 }
7841 if (shl->rm.startpos[0].lnum > 0
7842 || shl->rm.startpos[0].col >= mincol
7843 || nmatched > 1
7844 || shl->rm.endpos[0].col > mincol)
7845 {
7846 shl->lnum += shl->rm.startpos[0].lnum;
7847 break; /* useful match found */
7848 }
7849 }
7850}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851
Bram Moolenaar85077472016-10-16 14:35:48 +02007852/*
7853 * If there is a match fill "shl" and return one.
7854 * Return zero otherwise.
7855 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007856 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007857next_search_hl_pos(
7858 match_T *shl, /* points to a match */
7859 linenr_T lnum,
7860 posmatch_T *posmatch, /* match positions */
7861 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007862{
7863 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007864 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007865
Bram Moolenaarb3414592014-06-17 17:48:32 +02007866 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7867 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007868 llpos_T *pos = &posmatch->pos[i];
7869
7870 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007871 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007872 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007873 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007874 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007875 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007876 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007877 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007878 /* if this match comes before the one at "found" then swap
7879 * them */
7880 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007881 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007882 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007883
Bram Moolenaar85077472016-10-16 14:35:48 +02007884 *pos = posmatch->pos[found];
7885 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007886 }
7887 }
7888 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007889 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007890 }
7891 }
7892 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007893 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007894 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007895 colnr_T start = posmatch->pos[found].col == 0
7896 ? 0 : posmatch->pos[found].col - 1;
7897 colnr_T end = posmatch->pos[found].col == 0
7898 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007899
Bram Moolenaar85077472016-10-16 14:35:48 +02007900 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007901 shl->rm.startpos[0].lnum = 0;
7902 shl->rm.startpos[0].col = start;
7903 shl->rm.endpos[0].lnum = 0;
7904 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007905 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007906 posmatch->cur = found + 1;
7907 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007908 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007909 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007910}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007911#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007912
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007914screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
7916 attrentry_T *aep = NULL;
7917
7918 screen_attr = attr;
7919 if (full_screen
7920#ifdef WIN3264
7921 && termcap_active
7922#endif
7923 )
7924 {
7925#ifdef FEAT_GUI
7926 if (gui.in_use)
7927 {
7928 char buf[20];
7929
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007930 /* The GUI handles this internally. */
7931 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 OUT_STR(buf);
7933 }
7934 else
7935#endif
7936 {
7937 if (attr > HL_ALL) /* special HL attr. */
7938 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007939 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 aep = syn_cterm_attr2entry(attr);
7941 else
7942 aep = syn_term_attr2entry(attr);
7943 if (aep == NULL) /* did ":syntax clear" */
7944 attr = 0;
7945 else
7946 attr = aep->ae_attr;
7947 }
7948 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7949 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007950 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007951#ifdef FEAT_TERMGUICOLORS
7952 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007953 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007954#endif
7955 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007956#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007957 )
7958#endif
7959 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007960 /* If the Normal FG color has BOLD attribute and the new HL
7961 * has a FG color defined, clear BOLD. */
7962 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7964 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007965 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7966 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 out_str(T_US);
7968 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7969 out_str(T_CZH);
7970 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7971 out_str(T_MR);
7972
7973 /*
7974 * Output the color or start string after bold etc., in case the
7975 * bold etc. override the color setting.
7976 */
7977 if (aep != NULL)
7978 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007979#ifdef FEAT_TERMGUICOLORS
7980 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007982 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007983 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007984 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007985 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 }
7987 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007990 if (t_colors > 1)
7991 {
7992 if (aep->ae_u.cterm.fg_color)
7993 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7994 if (aep->ae_u.cterm.bg_color)
7995 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7996 }
7997 else
7998 {
7999 if (aep->ae_u.term.start != NULL)
8000 out_str(aep->ae_u.term.start);
8001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 }
8003 }
8004 }
8005 }
8006}
8007
8008 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008009screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010{
8011 int do_ME = FALSE; /* output T_ME code */
8012
8013 if (screen_attr != 0
8014#ifdef WIN3264
8015 && termcap_active
8016#endif
8017 )
8018 {
8019#ifdef FEAT_GUI
8020 if (gui.in_use)
8021 {
8022 char buf[20];
8023
8024 /* use internal GUI code */
8025 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8026 OUT_STR(buf);
8027 }
8028 else
8029#endif
8030 {
8031 if (screen_attr > HL_ALL) /* special HL attr. */
8032 {
8033 attrentry_T *aep;
8034
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008035 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {
8037 /*
8038 * Assume that t_me restores the original colors!
8039 */
8040 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008041 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008042#ifdef FEAT_TERMGUICOLORS
8043 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008044 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8045 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008046#endif
8047 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008048#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008049 )
8050#endif
8051 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 do_ME = TRUE;
8053 }
8054 else
8055 {
8056 aep = syn_term_attr2entry(screen_attr);
8057 if (aep != NULL && aep->ae_u.term.stop != NULL)
8058 {
8059 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8060 do_ME = TRUE;
8061 else
8062 out_str(aep->ae_u.term.stop);
8063 }
8064 }
8065 if (aep == NULL) /* did ":syntax clear" */
8066 screen_attr = 0;
8067 else
8068 screen_attr = aep->ae_attr;
8069 }
8070
8071 /*
8072 * Often all ending-codes are equal to T_ME. Avoid outputting the
8073 * same sequence several times.
8074 */
8075 if (screen_attr & HL_STANDOUT)
8076 {
8077 if (STRCMP(T_SE, T_ME) == 0)
8078 do_ME = TRUE;
8079 else
8080 out_str(T_SE);
8081 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008082 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {
8084 if (STRCMP(T_UE, T_ME) == 0)
8085 do_ME = TRUE;
8086 else
8087 out_str(T_UE);
8088 }
8089 if (screen_attr & HL_ITALIC)
8090 {
8091 if (STRCMP(T_CZR, T_ME) == 0)
8092 do_ME = TRUE;
8093 else
8094 out_str(T_CZR);
8095 }
8096 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8097 out_str(T_ME);
8098
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008099#ifdef FEAT_TERMGUICOLORS
8100 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008102 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008103 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008104 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008105 term_bg_rgb_color(cterm_normal_bg_gui_color);
8106 }
8107 else
8108#endif
8109 {
8110 if (t_colors > 1)
8111 {
8112 /* set Normal cterm colors */
8113 if (cterm_normal_fg_color != 0)
8114 term_fg_color(cterm_normal_fg_color - 1);
8115 if (cterm_normal_bg_color != 0)
8116 term_bg_color(cterm_normal_bg_color - 1);
8117 if (cterm_normal_fg_bold)
8118 out_str(T_MD);
8119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 }
8121 }
8122 }
8123 screen_attr = 0;
8124}
8125
8126/*
8127 * Reset the colors for a cterm. Used when leaving Vim.
8128 * The machine specific code may override this again.
8129 */
8130 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008131reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008133 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {
8135 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008136#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008137 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8138 || cterm_normal_bg_gui_color != INVALCOLOR)
8139 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008140#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {
8144 out_str(T_OP);
8145 screen_attr = -1;
8146 }
8147 if (cterm_normal_fg_bold)
8148 {
8149 out_str(T_ME);
8150 screen_attr = -1;
8151 }
8152 }
8153}
8154
8155/*
8156 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8157 * using the attributes from ScreenAttrs["off"].
8158 */
8159 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008160screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161{
8162 int attr;
8163
8164 /* Check for illegal values, just in case (could happen just after
8165 * resizing). */
8166 if (row >= screen_Rows || col >= screen_Columns)
8167 return;
8168
Bram Moolenaar494838a2015-02-10 19:20:37 +01008169 /* Outputting a character in the last cell on the screen may scroll the
8170 * screen up. Only do it when the "xn" termcap property is set, otherwise
8171 * mark the character invalid (update it when scrolled up). */
8172 if (*T_XN == NUL
8173 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174#ifdef FEAT_RIGHTLEFT
8175 /* account for first command-line character in rightleft mode */
8176 && !cmdmsg_rl
8177#endif
8178 )
8179 {
8180 ScreenAttrs[off] = (sattr_T)-1;
8181 return;
8182 }
8183
8184 /*
8185 * Stop highlighting first, so it's easier to move the cursor.
8186 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008187#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 if (screen_char_attr != 0)
8189 attr = screen_char_attr;
8190 else
8191#endif
8192 attr = ScreenAttrs[off];
8193 if (screen_attr != attr)
8194 screen_stop_highlight();
8195
8196 windgoto(row, col);
8197
8198 if (screen_attr != attr)
8199 screen_start_highlight(attr);
8200
8201#ifdef FEAT_MBYTE
8202 if (enc_utf8 && ScreenLinesUC[off] != 0)
8203 {
8204 char_u buf[MB_MAXBYTES + 1];
8205
8206 /* Convert UTF-8 character to bytes and write it. */
8207
8208 buf[utfc_char2bytes(off, buf)] = NUL;
8209
8210 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008211 if (utf_ambiguous_width(ScreenLinesUC[off]))
8212 screen_cur_col = 9999;
8213 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 ++screen_cur_col;
8215 }
8216 else
8217#endif
8218 {
8219#ifdef FEAT_MBYTE
8220 out_flush_check();
8221#endif
8222 out_char(ScreenLines[off]);
8223#ifdef FEAT_MBYTE
8224 /* double-byte character in single-width cell */
8225 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8226 out_char(ScreenLines2[off]);
8227#endif
8228 }
8229
8230 screen_cur_col++;
8231}
8232
8233#ifdef FEAT_MBYTE
8234
8235/*
8236 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8237 * on the screen at position 'row' and 'col'.
8238 * The attributes of the first byte is used for all. This is required to
8239 * output the two bytes of a double-byte character with nothing in between.
8240 */
8241 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008242screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243{
8244 /* Check for illegal values (could be wrong when screen was resized). */
8245 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8246 return;
8247
8248 /* Outputting the last character on the screen may scrollup the screen.
8249 * Don't to it! Mark the character invalid (update it when scrolled up) */
8250 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8251 {
8252 ScreenAttrs[off] = (sattr_T)-1;
8253 return;
8254 }
8255
8256 /* Output the first byte normally (positions the cursor), then write the
8257 * second byte directly. */
8258 screen_char(off, row, col);
8259 out_char(ScreenLines[off + 1]);
8260 ++screen_cur_col;
8261}
8262#endif
8263
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008264#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265/*
8266 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8267 * This uses the contents of ScreenLines[] and doesn't change it.
8268 */
8269 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008270screen_draw_rectangle(
8271 int row,
8272 int col,
8273 int height,
8274 int width,
8275 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276{
8277 int r, c;
8278 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008279#ifdef FEAT_MBYTE
8280 int max_off;
8281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008283 /* Can't use ScreenLines unless initialized */
8284 if (ScreenLines == NULL)
8285 return;
8286
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 if (invert)
8288 screen_char_attr = HL_INVERSE;
8289 for (r = row; r < row + height; ++r)
8290 {
8291 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008292#ifdef FEAT_MBYTE
8293 max_off = off + screen_Columns;
8294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 for (c = col; c < col + width; ++c)
8296 {
8297#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008298 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {
8300 screen_char_2(off + c, r, c);
8301 ++c;
8302 }
8303 else
8304#endif
8305 {
8306 screen_char(off + c, r, c);
8307#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008308 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 ++c;
8310#endif
8311 }
8312 }
8313 }
8314 screen_char_attr = 0;
8315}
8316#endif
8317
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008318#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319/*
8320 * Redraw the characters for a vertically split window.
8321 */
8322 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008323redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324{
8325 int col;
8326 int width;
8327
8328# ifdef FEAT_CLIPBOARD
8329 clip_may_clear_selection(row, end - 1);
8330# endif
8331
8332 if (wp == NULL)
8333 {
8334 col = 0;
8335 width = Columns;
8336 }
8337 else
8338 {
8339 col = wp->w_wincol;
8340 width = wp->w_width;
8341 }
8342 screen_draw_rectangle(row, col, end - row, width, FALSE);
8343}
8344#endif
8345
8346/*
8347 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8348 * with character 'c1' in first column followed by 'c2' in the other columns.
8349 * Use attributes 'attr'.
8350 */
8351 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008352screen_fill(
8353 int start_row,
8354 int end_row,
8355 int start_col,
8356 int end_col,
8357 int c1,
8358 int c2,
8359 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360{
8361 int row;
8362 int col;
8363 int off;
8364 int end_off;
8365 int did_delete;
8366 int c;
8367 int norm_term;
8368#if defined(FEAT_GUI) || defined(UNIX)
8369 int force_next = FALSE;
8370#endif
8371
8372 if (end_row > screen_Rows) /* safety check */
8373 end_row = screen_Rows;
8374 if (end_col > screen_Columns) /* safety check */
8375 end_col = screen_Columns;
8376 if (ScreenLines == NULL
8377 || start_row >= end_row
8378 || start_col >= end_col) /* nothing to do */
8379 return;
8380
8381 /* it's a "normal" terminal when not in a GUI or cterm */
8382 norm_term = (
8383#ifdef FEAT_GUI
8384 !gui.in_use &&
8385#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008386 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 for (row = start_row; row < end_row; ++row)
8388 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008389#ifdef FEAT_MBYTE
8390 if (has_mbyte
8391# ifdef FEAT_GUI
8392 && !gui.in_use
8393# endif
8394 )
8395 {
8396 /* When drawing over the right halve of a double-wide char clear
8397 * out the left halve. When drawing over the left halve of a
8398 * double wide-char clear out the right halve. Only needed in a
8399 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008400 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008401 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008402 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008403 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008404 }
8405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 /*
8407 * Try to use delete-line termcap code, when no attributes or in a
8408 * "normal" terminal, where a bold/italic space is just a
8409 * space.
8410 */
8411 did_delete = FALSE;
8412 if (c2 == ' '
8413 && end_col == Columns
8414 && can_clear(T_CE)
8415 && (attr == 0
8416 || (norm_term
8417 && attr <= HL_ALL
8418 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8419 {
8420 /*
8421 * check if we really need to clear something
8422 */
8423 col = start_col;
8424 if (c1 != ' ') /* don't clear first char */
8425 ++col;
8426
8427 off = LineOffset[row] + col;
8428 end_off = LineOffset[row] + end_col;
8429
8430 /* skip blanks (used often, keep it fast!) */
8431#ifdef FEAT_MBYTE
8432 if (enc_utf8)
8433 while (off < end_off && ScreenLines[off] == ' '
8434 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8435 ++off;
8436 else
8437#endif
8438 while (off < end_off && ScreenLines[off] == ' '
8439 && ScreenAttrs[off] == 0)
8440 ++off;
8441 if (off < end_off) /* something to be cleared */
8442 {
8443 col = off - LineOffset[row];
8444 screen_stop_highlight();
8445 term_windgoto(row, col);/* clear rest of this screen line */
8446 out_str(T_CE);
8447 screen_start(); /* don't know where cursor is now */
8448 col = end_col - col;
8449 while (col--) /* clear chars in ScreenLines */
8450 {
8451 ScreenLines[off] = ' ';
8452#ifdef FEAT_MBYTE
8453 if (enc_utf8)
8454 ScreenLinesUC[off] = 0;
8455#endif
8456 ScreenAttrs[off] = 0;
8457 ++off;
8458 }
8459 }
8460 did_delete = TRUE; /* the chars are cleared now */
8461 }
8462
8463 off = LineOffset[row] + start_col;
8464 c = c1;
8465 for (col = start_col; col < end_col; ++col)
8466 {
8467 if (ScreenLines[off] != c
8468#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008469 || (enc_utf8 && (int)ScreenLinesUC[off]
8470 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471#endif
8472 || ScreenAttrs[off] != attr
8473#if defined(FEAT_GUI) || defined(UNIX)
8474 || force_next
8475#endif
8476 )
8477 {
8478#if defined(FEAT_GUI) || defined(UNIX)
8479 /* The bold trick may make a single row of pixels appear in
8480 * the next character. When a bold character is removed, the
8481 * next character should be redrawn too. This happens for our
8482 * own GUI and for some xterms. */
8483 if (
8484# ifdef FEAT_GUI
8485 gui.in_use
8486# endif
8487# if defined(FEAT_GUI) && defined(UNIX)
8488 ||
8489# endif
8490# ifdef UNIX
8491 term_is_xterm
8492# endif
8493 )
8494 {
8495 if (ScreenLines[off] != ' '
8496 && (ScreenAttrs[off] > HL_ALL
8497 || ScreenAttrs[off] & HL_BOLD))
8498 force_next = TRUE;
8499 else
8500 force_next = FALSE;
8501 }
8502#endif
8503 ScreenLines[off] = c;
8504#ifdef FEAT_MBYTE
8505 if (enc_utf8)
8506 {
8507 if (c >= 0x80)
8508 {
8509 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008510 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 }
8512 else
8513 ScreenLinesUC[off] = 0;
8514 }
8515#endif
8516 ScreenAttrs[off] = attr;
8517 if (!did_delete || c != ' ')
8518 screen_char(off, row, col);
8519 }
8520 ++off;
8521 if (col == start_col)
8522 {
8523 if (did_delete)
8524 break;
8525 c = c2;
8526 }
8527 }
8528 if (end_col == Columns)
8529 LineWraps[row] = FALSE;
8530 if (row == Rows - 1) /* overwritten the command line */
8531 {
8532 redraw_cmdline = TRUE;
8533 if (c1 == ' ' && c2 == ' ')
8534 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008535 if (start_col == 0)
8536 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 }
8538 }
8539}
8540
8541/*
8542 * Check if there should be a delay. Used before clearing or redrawing the
8543 * screen or the command line.
8544 */
8545 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008546check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547{
8548 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8549 && !did_wait_return
8550 && emsg_silent == 0)
8551 {
8552 out_flush();
8553 ui_delay(1000L, TRUE);
8554 emsg_on_display = FALSE;
8555 if (check_msg_scroll)
8556 msg_scroll = FALSE;
8557 }
8558}
8559
8560/*
8561 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008562 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 * Returns TRUE if there is a valid screen to write to.
8564 * Returns FALSE when starting up and screen not initialized yet.
8565 */
8566 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008567screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008569 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 return (ScreenLines != NULL);
8571}
8572
8573/*
8574 * Resize the shell to Rows and Columns.
8575 * Allocate ScreenLines[] and associated items.
8576 *
8577 * There may be some time between setting Rows and Columns and (re)allocating
8578 * ScreenLines[]. This happens when starting up and when (manually) changing
8579 * the shell size. Always use screen_Rows and screen_Columns to access items
8580 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8581 * final size of the shell is needed.
8582 */
8583 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008584screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
8586 int new_row, old_row;
8587#ifdef FEAT_GUI
8588 int old_Rows;
8589#endif
8590 win_T *wp;
8591 int outofmem = FALSE;
8592 int len;
8593 schar_T *new_ScreenLines;
8594#ifdef FEAT_MBYTE
8595 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008596 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008598 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599#endif
8600 sattr_T *new_ScreenAttrs;
8601 unsigned *new_LineOffset;
8602 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008603#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008604 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008605 tabpage_T *tp;
8606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008608 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008609#ifdef FEAT_AUTOCMD
8610 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008612retry:
8613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 /*
8615 * Allocation of the screen buffers is done only when the size changes and
8616 * when Rows and Columns have been set and we have started doing full
8617 * screen stuff.
8618 */
8619 if ((ScreenLines != NULL
8620 && Rows == screen_Rows
8621 && Columns == screen_Columns
8622#ifdef FEAT_MBYTE
8623 && enc_utf8 == (ScreenLinesUC != NULL)
8624 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008625 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626#endif
8627 )
8628 || Rows == 0
8629 || Columns == 0
8630 || (!full_screen && ScreenLines == NULL))
8631 return;
8632
8633 /*
8634 * It's possible that we produce an out-of-memory message below, which
8635 * will cause this function to be called again. To break the loop, just
8636 * return here.
8637 */
8638 if (entered)
8639 return;
8640 entered = TRUE;
8641
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008642 /*
8643 * Note that the window sizes are updated before reallocating the arrays,
8644 * thus we must not redraw here!
8645 */
8646 ++RedrawingDisabled;
8647
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 win_new_shellsize(); /* fit the windows in the new sized shell */
8649
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 comp_col(); /* recompute columns for shown command and ruler */
8651
8652 /*
8653 * We're changing the size of the screen.
8654 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8655 * - Move lines from the old arrays into the new arrays, clear extra
8656 * lines (unless the screen is going to be cleared).
8657 * - Free the old arrays.
8658 *
8659 * If anything fails, make ScreenLines NULL, so we don't do anything!
8660 * Continuing with the old ScreenLines may result in a crash, because the
8661 * size is wrong.
8662 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008663 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008665#ifdef FEAT_AUTOCMD
8666 if (aucmd_win != NULL)
8667 win_free_lsize(aucmd_win);
8668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669
8670 new_ScreenLines = (schar_T *)lalloc((long_u)(
8671 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8672#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008673 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 if (enc_utf8)
8675 {
8676 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8677 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008678 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008679 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8681 }
8682 if (enc_dbcs == DBCS_JPNU)
8683 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8684 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8685#endif
8686 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8687 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8688 new_LineOffset = (unsigned *)lalloc((long_u)(
8689 Rows * sizeof(unsigned)), FALSE);
8690 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008691#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008692 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008695 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 {
8697 if (win_alloc_lines(wp) == FAIL)
8698 {
8699 outofmem = TRUE;
8700#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008701 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702#endif
8703 }
8704 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008705#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008706 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8707 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008708 outofmem = TRUE;
8709#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008710#ifdef FEAT_WINDOWS
8711give_up:
8712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008714#ifdef FEAT_MBYTE
8715 for (i = 0; i < p_mco; ++i)
8716 if (new_ScreenLinesC[i] == NULL)
8717 break;
8718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 if (new_ScreenLines == NULL
8720#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008721 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8723#endif
8724 || new_ScreenAttrs == NULL
8725 || new_LineOffset == NULL
8726 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008727#ifdef FEAT_WINDOWS
8728 || new_TabPageIdxs == NULL
8729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 || outofmem)
8731 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008732 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008733 {
8734 /* guess the size */
8735 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8736
8737 /* Remember we did this to avoid getting outofmem messages over
8738 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008739 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 vim_free(new_ScreenLines);
8742 new_ScreenLines = NULL;
8743#ifdef FEAT_MBYTE
8744 vim_free(new_ScreenLinesUC);
8745 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008746 for (i = 0; i < p_mco; ++i)
8747 {
8748 vim_free(new_ScreenLinesC[i]);
8749 new_ScreenLinesC[i] = NULL;
8750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 vim_free(new_ScreenLines2);
8752 new_ScreenLines2 = NULL;
8753#endif
8754 vim_free(new_ScreenAttrs);
8755 new_ScreenAttrs = NULL;
8756 vim_free(new_LineOffset);
8757 new_LineOffset = NULL;
8758 vim_free(new_LineWraps);
8759 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008760#ifdef FEAT_WINDOWS
8761 vim_free(new_TabPageIdxs);
8762 new_TabPageIdxs = NULL;
8763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 }
8765 else
8766 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008767 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008768
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 for (new_row = 0; new_row < Rows; ++new_row)
8770 {
8771 new_LineOffset[new_row] = new_row * Columns;
8772 new_LineWraps[new_row] = FALSE;
8773
8774 /*
8775 * If the screen is not going to be cleared, copy as much as
8776 * possible from the old screen to the new one and clear the rest
8777 * (used when resizing the window at the "--more--" prompt or when
8778 * executing an external command, for the GUI).
8779 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008780 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 {
8782 (void)vim_memset(new_ScreenLines + new_row * Columns,
8783 ' ', (size_t)Columns * sizeof(schar_T));
8784#ifdef FEAT_MBYTE
8785 if (enc_utf8)
8786 {
8787 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8788 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008789 for (i = 0; i < p_mco; ++i)
8790 (void)vim_memset(new_ScreenLinesC[i]
8791 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 0, (size_t)Columns * sizeof(u8char_T));
8793 }
8794 if (enc_dbcs == DBCS_JPNU)
8795 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8796 0, (size_t)Columns * sizeof(schar_T));
8797#endif
8798 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8799 0, (size_t)Columns * sizeof(sattr_T));
8800 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008801 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 {
8803 if (screen_Columns < Columns)
8804 len = screen_Columns;
8805 else
8806 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008807#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008808 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008809 * may be invalid now. Also when p_mco changes. */
8810 if (!(enc_utf8 && ScreenLinesUC == NULL)
8811 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008812#endif
8813 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8814 ScreenLines + LineOffset[old_row],
8815 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008817 if (enc_utf8 && ScreenLinesUC != NULL
8818 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 {
8820 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8821 ScreenLinesUC + LineOffset[old_row],
8822 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008823 for (i = 0; i < p_mco; ++i)
8824 mch_memmove(new_ScreenLinesC[i]
8825 + new_LineOffset[new_row],
8826 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 (size_t)len * sizeof(u8char_T));
8828 }
8829 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8830 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8831 ScreenLines2 + LineOffset[old_row],
8832 (size_t)len * sizeof(schar_T));
8833#endif
8834 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8835 ScreenAttrs + LineOffset[old_row],
8836 (size_t)len * sizeof(sattr_T));
8837 }
8838 }
8839 }
8840 /* Use the last line of the screen for the current line. */
8841 current_ScreenLine = new_ScreenLines + Rows * Columns;
8842 }
8843
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008844 free_screenlines();
8845
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 ScreenLines = new_ScreenLines;
8847#ifdef FEAT_MBYTE
8848 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008849 for (i = 0; i < p_mco; ++i)
8850 ScreenLinesC[i] = new_ScreenLinesC[i];
8851 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 ScreenLines2 = new_ScreenLines2;
8853#endif
8854 ScreenAttrs = new_ScreenAttrs;
8855 LineOffset = new_LineOffset;
8856 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008857#ifdef FEAT_WINDOWS
8858 TabPageIdxs = new_TabPageIdxs;
8859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860
8861 /* It's important that screen_Rows and screen_Columns reflect the actual
8862 * size of ScreenLines[]. Set them before calling anything. */
8863#ifdef FEAT_GUI
8864 old_Rows = screen_Rows;
8865#endif
8866 screen_Rows = Rows;
8867 screen_Columns = Columns;
8868
8869 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008870 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 screenclear2();
8872
8873#ifdef FEAT_GUI
8874 else if (gui.in_use
8875 && !gui.starting
8876 && ScreenLines != NULL
8877 && old_Rows != Rows)
8878 {
8879 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8880 /*
8881 * Adjust the position of the cursor, for when executing an external
8882 * command.
8883 */
8884 if (msg_row >= Rows) /* Rows got smaller */
8885 msg_row = Rows - 1; /* put cursor at last row */
8886 else if (Rows > old_Rows) /* Rows got bigger */
8887 msg_row += Rows - old_Rows; /* put cursor in same place */
8888 if (msg_col >= Columns) /* Columns got smaller */
8889 msg_col = Columns - 1; /* put cursor at last column */
8890 }
8891#endif
8892
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008894 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008895
8896#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008897 /*
8898 * Do not apply autocommands more than 3 times to avoid an endless loop
8899 * in case applying autocommands always changes Rows or Columns.
8900 */
8901 if (starting == 0 && ++retry_count <= 3)
8902 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008903 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008904 /* In rare cases, autocommands may have altered Rows or Columns,
8905 * jump back to check if we need to allocate the screen again. */
8906 goto retry;
8907 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909}
8910
8911 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008912free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008913{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008914#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008915 int i;
8916
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008917 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008918 for (i = 0; i < Screen_mco; ++i)
8919 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008920 vim_free(ScreenLines2);
8921#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008922 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008923 vim_free(ScreenAttrs);
8924 vim_free(LineOffset);
8925 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008926#ifdef FEAT_WINDOWS
8927 vim_free(TabPageIdxs);
8928#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008929}
8930
8931 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008932screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933{
8934 check_for_delay(FALSE);
8935 screenalloc(FALSE); /* allocate screen buffers if size changed */
8936 screenclear2(); /* clear the screen */
8937}
8938
8939 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008940screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941{
8942 int i;
8943
8944 if (starting == NO_SCREEN || ScreenLines == NULL
8945#ifdef FEAT_GUI
8946 || (gui.in_use && gui.starting)
8947#endif
8948 )
8949 return;
8950
8951#ifdef FEAT_GUI
8952 if (!gui.in_use)
8953#endif
8954 screen_attr = -1; /* force setting the Normal colors */
8955 screen_stop_highlight(); /* don't want highlighting here */
8956
8957#ifdef FEAT_CLIPBOARD
8958 /* disable selection without redrawing it */
8959 clip_scroll_selection(9999);
8960#endif
8961
8962 /* blank out ScreenLines */
8963 for (i = 0; i < Rows; ++i)
8964 {
8965 lineclear(LineOffset[i], (int)Columns);
8966 LineWraps[i] = FALSE;
8967 }
8968
8969 if (can_clear(T_CL))
8970 {
8971 out_str(T_CL); /* clear the display */
8972 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008973 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 }
8975 else
8976 {
8977 /* can't clear the screen, mark all chars with invalid attributes */
8978 for (i = 0; i < Rows; ++i)
8979 lineinvalid(LineOffset[i], (int)Columns);
8980 clear_cmdline = TRUE;
8981 }
8982
8983 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8984
8985 win_rest_invalid(firstwin);
8986 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008987#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008988 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 if (must_redraw == CLEAR) /* no need to clear again */
8991 must_redraw = NOT_VALID;
8992 compute_cmdrow();
8993 msg_row = cmdline_row; /* put cursor on last line for messages */
8994 msg_col = 0;
8995 screen_start(); /* don't know where cursor is now */
8996 msg_scrolled = 0; /* can't scroll back */
8997 msg_didany = FALSE;
8998 msg_didout = FALSE;
8999}
9000
9001/*
9002 * Clear one line in ScreenLines.
9003 */
9004 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009005lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006{
9007 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9008#ifdef FEAT_MBYTE
9009 if (enc_utf8)
9010 (void)vim_memset(ScreenLinesUC + off, 0,
9011 (size_t)width * sizeof(u8char_T));
9012#endif
9013 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
9014}
9015
9016/*
9017 * Mark one line in ScreenLines invalid by setting the attributes to an
9018 * invalid value.
9019 */
9020 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009021lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022{
9023 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9024}
9025
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009026#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027/*
9028 * Copy part of a Screenline for vertically split window "wp".
9029 */
9030 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009031linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032{
9033 unsigned off_to = LineOffset[to] + wp->w_wincol;
9034 unsigned off_from = LineOffset[from] + wp->w_wincol;
9035
9036 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9037 wp->w_width * sizeof(schar_T));
9038# ifdef FEAT_MBYTE
9039 if (enc_utf8)
9040 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009041 int i;
9042
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9044 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009045 for (i = 0; i < p_mco; ++i)
9046 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9047 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 }
9049 if (enc_dbcs == DBCS_JPNU)
9050 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9051 wp->w_width * sizeof(schar_T));
9052# endif
9053 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9054 wp->w_width * sizeof(sattr_T));
9055}
9056#endif
9057
9058/*
9059 * Return TRUE if clearing with term string "p" would work.
9060 * It can't work when the string is empty or it won't set the right background.
9061 */
9062 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009063can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
9065 return (*p != NUL && (t_colors <= 1
9066#ifdef FEAT_GUI
9067 || gui.in_use
9068#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009069#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009070 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009071 || (!p_tgc && cterm_normal_bg_color == 0)
9072#else
9073 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009074#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009075 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076}
9077
9078/*
9079 * Reset cursor position. Use whenever cursor was moved because of outputting
9080 * something directly to the screen (shell commands) or a terminal control
9081 * code.
9082 */
9083 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009084screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085{
9086 screen_cur_row = screen_cur_col = 9999;
9087}
9088
9089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 * Move the cursor to position "row","col" in the screen.
9091 * This tries to find the most efficient way to move, minimizing the number of
9092 * characters sent to the terminal.
9093 */
9094 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009095windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009097 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 int i;
9099 int plan;
9100 int cost;
9101 int wouldbe_col;
9102 int noinvcurs;
9103 char_u *bs;
9104 int goto_cost;
9105 int attr;
9106
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009107#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9109
9110#define PLAN_LE 1
9111#define PLAN_CR 2
9112#define PLAN_NL 3
9113#define PLAN_WRITE 4
9114 /* Can't use ScreenLines unless initialized */
9115 if (ScreenLines == NULL)
9116 return;
9117
9118 if (col != screen_cur_col || row != screen_cur_row)
9119 {
9120 /* Check for valid position. */
9121 if (row < 0) /* window without text lines? */
9122 row = 0;
9123 if (row >= screen_Rows)
9124 row = screen_Rows - 1;
9125 if (col >= screen_Columns)
9126 col = screen_Columns - 1;
9127
9128 /* check if no cursor movement is allowed in highlight mode */
9129 if (screen_attr && *T_MS == NUL)
9130 noinvcurs = HIGHL_COST;
9131 else
9132 noinvcurs = 0;
9133 goto_cost = GOTO_COST + noinvcurs;
9134
9135 /*
9136 * Plan how to do the positioning:
9137 * 1. Use CR to move it to column 0, same row.
9138 * 2. Use T_LE to move it a few columns to the left.
9139 * 3. Use NL to move a few lines down, column 0.
9140 * 4. Move a few columns to the right with T_ND or by writing chars.
9141 *
9142 * Don't do this if the cursor went beyond the last column, the cursor
9143 * position is unknown then (some terminals wrap, some don't )
9144 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009145 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 * characters to move the cursor to the right.
9147 */
9148 if (row >= screen_cur_row && screen_cur_col < Columns)
9149 {
9150 /*
9151 * If the cursor is in the same row, bigger col, we can use CR
9152 * or T_LE.
9153 */
9154 bs = NULL; /* init for GCC */
9155 attr = screen_attr;
9156 if (row == screen_cur_row && col < screen_cur_col)
9157 {
9158 /* "le" is preferred over "bc", because "bc" is obsolete */
9159 if (*T_LE)
9160 bs = T_LE; /* "cursor left" */
9161 else
9162 bs = T_BC; /* "backspace character (old) */
9163 if (*bs)
9164 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9165 else
9166 cost = 999;
9167 if (col + 1 < cost) /* using CR is less characters */
9168 {
9169 plan = PLAN_CR;
9170 wouldbe_col = 0;
9171 cost = 1; /* CR is just one character */
9172 }
9173 else
9174 {
9175 plan = PLAN_LE;
9176 wouldbe_col = col;
9177 }
9178 if (noinvcurs) /* will stop highlighting */
9179 {
9180 cost += noinvcurs;
9181 attr = 0;
9182 }
9183 }
9184
9185 /*
9186 * If the cursor is above where we want to be, we can use CR LF.
9187 */
9188 else if (row > screen_cur_row)
9189 {
9190 plan = PLAN_NL;
9191 wouldbe_col = 0;
9192 cost = (row - screen_cur_row) * 2; /* CR LF */
9193 if (noinvcurs) /* will stop highlighting */
9194 {
9195 cost += noinvcurs;
9196 attr = 0;
9197 }
9198 }
9199
9200 /*
9201 * If the cursor is in the same row, smaller col, just use write.
9202 */
9203 else
9204 {
9205 plan = PLAN_WRITE;
9206 wouldbe_col = screen_cur_col;
9207 cost = 0;
9208 }
9209
9210 /*
9211 * Check if any characters that need to be written have the
9212 * correct attributes. Also avoid UTF-8 characters.
9213 */
9214 i = col - wouldbe_col;
9215 if (i > 0)
9216 cost += i;
9217 if (cost < goto_cost && i > 0)
9218 {
9219 /*
9220 * Check if the attributes are correct without additionally
9221 * stopping highlighting.
9222 */
9223 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9224 while (i && *p++ == attr)
9225 --i;
9226 if (i != 0)
9227 {
9228 /*
9229 * Try if it works when highlighting is stopped here.
9230 */
9231 if (*--p == 0)
9232 {
9233 cost += noinvcurs;
9234 while (i && *p++ == 0)
9235 --i;
9236 }
9237 if (i != 0)
9238 cost = 999; /* different attributes, don't do it */
9239 }
9240#ifdef FEAT_MBYTE
9241 if (enc_utf8)
9242 {
9243 /* Don't use an UTF-8 char for positioning, it's slow. */
9244 for (i = wouldbe_col; i < col; ++i)
9245 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9246 {
9247 cost = 999;
9248 break;
9249 }
9250 }
9251#endif
9252 }
9253
9254 /*
9255 * We can do it without term_windgoto()!
9256 */
9257 if (cost < goto_cost)
9258 {
9259 if (plan == PLAN_LE)
9260 {
9261 if (noinvcurs)
9262 screen_stop_highlight();
9263 while (screen_cur_col > col)
9264 {
9265 out_str(bs);
9266 --screen_cur_col;
9267 }
9268 }
9269 else if (plan == PLAN_CR)
9270 {
9271 if (noinvcurs)
9272 screen_stop_highlight();
9273 out_char('\r');
9274 screen_cur_col = 0;
9275 }
9276 else if (plan == PLAN_NL)
9277 {
9278 if (noinvcurs)
9279 screen_stop_highlight();
9280 while (screen_cur_row < row)
9281 {
9282 out_char('\n');
9283 ++screen_cur_row;
9284 }
9285 screen_cur_col = 0;
9286 }
9287
9288 i = col - screen_cur_col;
9289 if (i > 0)
9290 {
9291 /*
9292 * Use cursor-right if it's one character only. Avoids
9293 * removing a line of pixels from the last bold char, when
9294 * using the bold trick in the GUI.
9295 */
9296 if (T_ND[0] != NUL && T_ND[1] == NUL)
9297 {
9298 while (i-- > 0)
9299 out_char(*T_ND);
9300 }
9301 else
9302 {
9303 int off;
9304
9305 off = LineOffset[row] + screen_cur_col;
9306 while (i-- > 0)
9307 {
9308 if (ScreenAttrs[off] != screen_attr)
9309 screen_stop_highlight();
9310#ifdef FEAT_MBYTE
9311 out_flush_check();
9312#endif
9313 out_char(ScreenLines[off]);
9314#ifdef FEAT_MBYTE
9315 if (enc_dbcs == DBCS_JPNU
9316 && ScreenLines[off] == 0x8e)
9317 out_char(ScreenLines2[off]);
9318#endif
9319 ++off;
9320 }
9321 }
9322 }
9323 }
9324 }
9325 else
9326 cost = 999;
9327
9328 if (cost >= goto_cost)
9329 {
9330 if (noinvcurs)
9331 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009332 if (row == screen_cur_row && (col > screen_cur_col)
9333 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 term_cursor_right(col - screen_cur_col);
9335 else
9336 term_windgoto(row, col);
9337 }
9338 screen_cur_row = row;
9339 screen_cur_col = col;
9340 }
9341}
9342
9343/*
9344 * Set cursor to its position in the current window.
9345 */
9346 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009347setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348{
9349 if (redrawing())
9350 {
9351 validate_cursor();
9352 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9353 W_WINCOL(curwin) + (
9354#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009355 /* With 'rightleft' set and the cursor on a double-wide
9356 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9358# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009359 (has_mbyte
9360 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9361 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362# endif
9363 1)) :
9364#endif
9365 curwin->w_wcol));
9366 }
9367}
9368
9369
9370/*
9371 * insert 'line_count' lines at 'row' in window 'wp'
9372 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9373 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9374 * scrolling.
9375 * Returns FAIL if the lines are not inserted, OK for success.
9376 */
9377 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009378win_ins_lines(
9379 win_T *wp,
9380 int row,
9381 int line_count,
9382 int invalid,
9383 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385 int did_delete;
9386 int nextrow;
9387 int lastrow;
9388 int retval;
9389
9390 if (invalid)
9391 wp->w_lines_valid = 0;
9392
9393 if (wp->w_height < 5)
9394 return FAIL;
9395
9396 if (line_count > wp->w_height - row)
9397 line_count = wp->w_height - row;
9398
9399 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9400 if (retval != MAYBE)
9401 return retval;
9402
9403 /*
9404 * If there is a next window or a status line, we first try to delete the
9405 * lines at the bottom to avoid messing what is after the window.
9406 * If this fails and there are following windows, don't do anything to avoid
9407 * messing up those windows, better just redraw.
9408 */
9409 did_delete = FALSE;
9410#ifdef FEAT_WINDOWS
9411 if (wp->w_next != NULL || wp->w_status_height)
9412 {
9413 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9414 line_count, (int)Rows, FALSE, NULL) == OK)
9415 did_delete = TRUE;
9416 else if (wp->w_next)
9417 return FAIL;
9418 }
9419#endif
9420 /*
9421 * if no lines deleted, blank the lines that will end up below the window
9422 */
9423 if (!did_delete)
9424 {
9425#ifdef FEAT_WINDOWS
9426 wp->w_redr_status = TRUE;
9427#endif
9428 redraw_cmdline = TRUE;
9429 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9430 lastrow = nextrow + line_count;
9431 if (lastrow > Rows)
9432 lastrow = Rows;
9433 screen_fill(nextrow - line_count, lastrow - line_count,
9434 W_WINCOL(wp), (int)W_ENDCOL(wp),
9435 ' ', ' ', 0);
9436 }
9437
9438 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9439 == FAIL)
9440 {
9441 /* deletion will have messed up other windows */
9442 if (did_delete)
9443 {
9444#ifdef FEAT_WINDOWS
9445 wp->w_redr_status = TRUE;
9446#endif
9447 win_rest_invalid(W_NEXT(wp));
9448 }
9449 return FAIL;
9450 }
9451
9452 return OK;
9453}
9454
9455/*
9456 * delete "line_count" window lines at "row" in window "wp"
9457 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9458 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9459 * scrolling
9460 * Return OK for success, FAIL if the lines are not deleted.
9461 */
9462 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009463win_del_lines(
9464 win_T *wp,
9465 int row,
9466 int line_count,
9467 int invalid,
9468 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470 int retval;
9471
9472 if (invalid)
9473 wp->w_lines_valid = 0;
9474
9475 if (line_count > wp->w_height - row)
9476 line_count = wp->w_height - row;
9477
9478 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9479 if (retval != MAYBE)
9480 return retval;
9481
9482 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9483 (int)Rows, FALSE, NULL) == FAIL)
9484 return FAIL;
9485
9486#ifdef FEAT_WINDOWS
9487 /*
9488 * If there are windows or status lines below, try to put them at the
9489 * correct place. If we can't do that, they have to be redrawn.
9490 */
9491 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9492 {
9493 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9494 line_count, (int)Rows, NULL) == FAIL)
9495 {
9496 wp->w_redr_status = TRUE;
9497 win_rest_invalid(wp->w_next);
9498 }
9499 }
9500 /*
9501 * If this is the last window and there is no status line, redraw the
9502 * command line later.
9503 */
9504 else
9505#endif
9506 redraw_cmdline = TRUE;
9507 return OK;
9508}
9509
9510/*
9511 * Common code for win_ins_lines() and win_del_lines().
9512 * Returns OK or FAIL when the work has been done.
9513 * Returns MAYBE when not finished yet.
9514 */
9515 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009516win_do_lines(
9517 win_T *wp,
9518 int row,
9519 int line_count,
9520 int mayclear,
9521 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522{
9523 int retval;
9524
9525 if (!redrawing() || line_count <= 0)
9526 return FAIL;
9527
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009528 /* When inserting lines would result in loss of command output, just redraw
9529 * the lines. */
9530 if (no_win_do_lines_ins && !del)
9531 return FAIL;
9532
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 /* only a few lines left: redraw is faster */
9534 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009535#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 && wp->w_width == Columns
9537#endif
9538 )
9539 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009540 if (!no_win_do_lines_ins)
9541 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 return FAIL;
9543 }
9544
9545 /*
9546 * Delete all remaining lines
9547 */
9548 if (row + line_count >= wp->w_height)
9549 {
9550 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9551 W_WINCOL(wp), (int)W_ENDCOL(wp),
9552 ' ', ' ', 0);
9553 return OK;
9554 }
9555
9556 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009557 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009559 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009561 if (!no_win_do_lines_ins)
9562 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563
9564 /*
9565 * If the terminal can set a scroll region, use that.
9566 * Always do this in a vertically split window. This will redraw from
9567 * ScreenLines[] when t_CV isn't defined. That's faster than using
9568 * win_line().
9569 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009570 * a character in the lower right corner of the scroll region may cause a
9571 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 */
9573 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009574#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 || W_WIDTH(wp) != Columns
9576#endif
9577 )
9578 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009579#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9581#endif
9582 scroll_region_set(wp, row);
9583 if (del)
9584 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9585 wp->w_height - row, FALSE, wp);
9586 else
9587 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9588 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009589#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9591#endif
9592 scroll_region_reset();
9593 return retval;
9594 }
9595
9596#ifdef FEAT_WINDOWS
9597 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9598 return FAIL;
9599#endif
9600
9601 return MAYBE;
9602}
9603
9604/*
9605 * window 'wp' and everything after it is messed up, mark it for redraw
9606 */
9607 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009608win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
9610#ifdef FEAT_WINDOWS
9611 while (wp != NULL)
9612#else
9613 if (wp != NULL)
9614#endif
9615 {
9616 redraw_win_later(wp, NOT_VALID);
9617#ifdef FEAT_WINDOWS
9618 wp->w_redr_status = TRUE;
9619 wp = wp->w_next;
9620#endif
9621 }
9622 redraw_cmdline = TRUE;
9623}
9624
9625/*
9626 * The rest of the routines in this file perform screen manipulations. The
9627 * given operation is performed physically on the screen. The corresponding
9628 * change is also made to the internal screen image. In this way, the editor
9629 * anticipates the effect of editing changes on the appearance of the screen.
9630 * That way, when we call screenupdate a complete redraw isn't usually
9631 * necessary. Another advantage is that we can keep adding code to anticipate
9632 * screen changes, and in the meantime, everything still works.
9633 */
9634
9635/*
9636 * types for inserting or deleting lines
9637 */
9638#define USE_T_CAL 1
9639#define USE_T_CDL 2
9640#define USE_T_AL 3
9641#define USE_T_CE 4
9642#define USE_T_DL 5
9643#define USE_T_SR 6
9644#define USE_NL 7
9645#define USE_T_CD 8
9646#define USE_REDRAW 9
9647
9648/*
9649 * insert lines on the screen and update ScreenLines[]
9650 * 'end' is the line after the scrolled part. Normally it is Rows.
9651 * When scrolling region used 'off' is the offset from the top for the region.
9652 * 'row' and 'end' are relative to the start of the region.
9653 *
9654 * return FAIL for failure, OK for success.
9655 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009656 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009657screen_ins_lines(
9658 int off,
9659 int row,
9660 int line_count,
9661 int end,
9662 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663{
9664 int i;
9665 int j;
9666 unsigned temp;
9667 int cursor_row;
9668 int type;
9669 int result_empty;
9670 int can_ce = can_clear(T_CE);
9671
9672 /*
9673 * FAIL if
9674 * - there is no valid screen
9675 * - the screen has to be redrawn completely
9676 * - the line count is less than one
9677 * - the line count is more than 'ttyscroll'
9678 */
9679 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9680 return FAIL;
9681
9682 /*
9683 * There are seven ways to insert lines:
9684 * 0. When in a vertically split window and t_CV isn't set, redraw the
9685 * characters from ScreenLines[].
9686 * 1. Use T_CD (clear to end of display) if it exists and the result of
9687 * the insert is just empty lines
9688 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9689 * present or line_count > 1. It looks better if we do all the inserts
9690 * at once.
9691 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9692 * insert is just empty lines and T_CE is not present or line_count >
9693 * 1.
9694 * 4. Use T_AL (insert line) if it exists.
9695 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9696 * just empty lines.
9697 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9698 * just empty lines.
9699 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9700 * the 'da' flag is not set or we have clear line capability.
9701 * 8. redraw the characters from ScreenLines[].
9702 *
9703 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9704 * the scrollbar for the window. It does have insert line, use that if it
9705 * exists.
9706 */
9707 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009708#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9710 type = USE_REDRAW;
9711 else
9712#endif
9713 if (can_clear(T_CD) && result_empty)
9714 type = USE_T_CD;
9715 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9716 type = USE_T_CAL;
9717 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9718 type = USE_T_CDL;
9719 else if (*T_AL != NUL)
9720 type = USE_T_AL;
9721 else if (can_ce && result_empty)
9722 type = USE_T_CE;
9723 else if (*T_DL != NUL && result_empty)
9724 type = USE_T_DL;
9725 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9726 type = USE_T_SR;
9727 else
9728 return FAIL;
9729
9730 /*
9731 * For clearing the lines screen_del_lines() is used. This will also take
9732 * care of t_db if necessary.
9733 */
9734 if (type == USE_T_CD || type == USE_T_CDL ||
9735 type == USE_T_CE || type == USE_T_DL)
9736 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9737
9738 /*
9739 * If text is retained below the screen, first clear or delete as many
9740 * lines at the bottom of the window as are about to be inserted so that
9741 * the deleted lines won't later surface during a screen_del_lines.
9742 */
9743 if (*T_DB)
9744 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9745
9746#ifdef FEAT_CLIPBOARD
9747 /* Remove a modeless selection when inserting lines halfway the screen
9748 * or not the full width of the screen. */
9749 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009750# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 || (wp != NULL && wp->w_width != Columns)
9752# endif
9753 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009754 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 else
9756 clip_scroll_selection(-line_count);
9757#endif
9758
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759#ifdef FEAT_GUI
9760 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9761 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009762 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763#endif
9764
9765 if (*T_CCS != NUL) /* cursor relative to region */
9766 cursor_row = row;
9767 else
9768 cursor_row = row + off;
9769
9770 /*
9771 * Shift LineOffset[] line_count down to reflect the inserted lines.
9772 * Clear the inserted lines in ScreenLines[].
9773 */
9774 row += off;
9775 end += off;
9776 for (i = 0; i < line_count; ++i)
9777 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009778#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 if (wp != NULL && wp->w_width != Columns)
9780 {
9781 /* need to copy part of a line */
9782 j = end - 1 - i;
9783 while ((j -= line_count) >= row)
9784 linecopy(j + line_count, j, wp);
9785 j += line_count;
9786 if (can_clear((char_u *)" "))
9787 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9788 else
9789 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9790 LineWraps[j] = FALSE;
9791 }
9792 else
9793#endif
9794 {
9795 j = end - 1 - i;
9796 temp = LineOffset[j];
9797 while ((j -= line_count) >= row)
9798 {
9799 LineOffset[j + line_count] = LineOffset[j];
9800 LineWraps[j + line_count] = LineWraps[j];
9801 }
9802 LineOffset[j + line_count] = temp;
9803 LineWraps[j + line_count] = FALSE;
9804 if (can_clear((char_u *)" "))
9805 lineclear(temp, (int)Columns);
9806 else
9807 lineinvalid(temp, (int)Columns);
9808 }
9809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810
9811 screen_stop_highlight();
9812 windgoto(cursor_row, 0);
9813
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009814#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 /* redraw the characters */
9816 if (type == USE_REDRAW)
9817 redraw_block(row, end, wp);
9818 else
9819#endif
9820 if (type == USE_T_CAL)
9821 {
9822 term_append_lines(line_count);
9823 screen_start(); /* don't know where cursor is now */
9824 }
9825 else
9826 {
9827 for (i = 0; i < line_count; i++)
9828 {
9829 if (type == USE_T_AL)
9830 {
9831 if (i && cursor_row != 0)
9832 windgoto(cursor_row, 0);
9833 out_str(T_AL);
9834 }
9835 else /* type == USE_T_SR */
9836 out_str(T_SR);
9837 screen_start(); /* don't know where cursor is now */
9838 }
9839 }
9840
9841 /*
9842 * With scroll-reverse and 'da' flag set we need to clear the lines that
9843 * have been scrolled down into the region.
9844 */
9845 if (type == USE_T_SR && *T_DA)
9846 {
9847 for (i = 0; i < line_count; ++i)
9848 {
9849 windgoto(off + i, 0);
9850 out_str(T_CE);
9851 screen_start(); /* don't know where cursor is now */
9852 }
9853 }
9854
9855#ifdef FEAT_GUI
9856 gui_can_update_cursor();
9857 if (gui.in_use)
9858 out_flush(); /* always flush after a scroll */
9859#endif
9860 return OK;
9861}
9862
9863/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009864 * Delete lines on the screen and update ScreenLines[].
9865 * "end" is the line after the scrolled part. Normally it is Rows.
9866 * When scrolling region used "off" is the offset from the top for the region.
9867 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 *
9869 * Return OK for success, FAIL if the lines are not deleted.
9870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009872screen_del_lines(
9873 int off,
9874 int row,
9875 int line_count,
9876 int end,
9877 int force, /* even when line_count > p_ttyscroll */
9878 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
9880 int j;
9881 int i;
9882 unsigned temp;
9883 int cursor_row;
9884 int cursor_end;
9885 int result_empty; /* result is empty until end of region */
9886 int can_delete; /* deleting line codes can be used */
9887 int type;
9888
9889 /*
9890 * FAIL if
9891 * - there is no valid screen
9892 * - the screen has to be redrawn completely
9893 * - the line count is less than one
9894 * - the line count is more than 'ttyscroll'
9895 */
9896 if (!screen_valid(TRUE) || line_count <= 0 ||
9897 (!force && line_count > p_ttyscroll))
9898 return FAIL;
9899
9900 /*
9901 * Check if the rest of the current region will become empty.
9902 */
9903 result_empty = row + line_count >= end;
9904
9905 /*
9906 * We can delete lines only when 'db' flag not set or when 'ce' option
9907 * available.
9908 */
9909 can_delete = (*T_DB == NUL || can_clear(T_CE));
9910
9911 /*
9912 * There are six ways to delete lines:
9913 * 0. When in a vertically split window and t_CV isn't set, redraw the
9914 * characters from ScreenLines[].
9915 * 1. Use T_CD if it exists and the result is empty.
9916 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9917 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9918 * none of the other ways work.
9919 * 4. Use T_CE (erase line) if the result is empty.
9920 * 5. Use T_DL (delete line) if it exists.
9921 * 6. redraw the characters from ScreenLines[].
9922 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009923#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9925 type = USE_REDRAW;
9926 else
9927#endif
9928 if (can_clear(T_CD) && result_empty)
9929 type = USE_T_CD;
9930#if defined(__BEOS__) && defined(BEOS_DR8)
9931 /*
9932 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9933 * its internal termcap... this works okay for tests which test *T_DB !=
9934 * NUL. It has the disadvantage that the user cannot use any :set t_*
9935 * command to get T_DB (back) to empty_option, only :set term=... will do
9936 * the trick...
9937 * Anyway, this hack will hopefully go away with the next OS release.
9938 * (Olaf Seibert)
9939 */
9940 else if (row == 0 && T_DB == empty_option
9941 && (line_count == 1 || *T_CDL == NUL))
9942#else
9943 else if (row == 0 && (
9944#ifndef AMIGA
9945 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9946 * up, so use delete-line command */
9947 line_count == 1 ||
9948#endif
9949 *T_CDL == NUL))
9950#endif
9951 type = USE_NL;
9952 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9953 type = USE_T_CDL;
9954 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009955#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 && (wp == NULL || wp->w_width == Columns)
9957#endif
9958 )
9959 type = USE_T_CE;
9960 else if (*T_DL != NUL && can_delete)
9961 type = USE_T_DL;
9962 else if (*T_CDL != NUL && can_delete)
9963 type = USE_T_CDL;
9964 else
9965 return FAIL;
9966
9967#ifdef FEAT_CLIPBOARD
9968 /* Remove a modeless selection when deleting lines halfway the screen or
9969 * not the full width of the screen. */
9970 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009971# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 || (wp != NULL && wp->w_width != Columns)
9973# endif
9974 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009975 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 else
9977 clip_scroll_selection(line_count);
9978#endif
9979
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980#ifdef FEAT_GUI
9981 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9982 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009983 gui_dont_update_cursor(gui.cursor_row >= row + off
9984 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985#endif
9986
9987 if (*T_CCS != NUL) /* cursor relative to region */
9988 {
9989 cursor_row = row;
9990 cursor_end = end;
9991 }
9992 else
9993 {
9994 cursor_row = row + off;
9995 cursor_end = end + off;
9996 }
9997
9998 /*
9999 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10000 * Clear the inserted lines in ScreenLines[].
10001 */
10002 row += off;
10003 end += off;
10004 for (i = 0; i < line_count; ++i)
10005 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010006#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 if (wp != NULL && wp->w_width != Columns)
10008 {
10009 /* need to copy part of a line */
10010 j = row + i;
10011 while ((j += line_count) <= end - 1)
10012 linecopy(j - line_count, j, wp);
10013 j -= line_count;
10014 if (can_clear((char_u *)" "))
10015 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
10016 else
10017 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10018 LineWraps[j] = FALSE;
10019 }
10020 else
10021#endif
10022 {
10023 /* whole width, moving the line pointers is faster */
10024 j = row + i;
10025 temp = LineOffset[j];
10026 while ((j += line_count) <= end - 1)
10027 {
10028 LineOffset[j - line_count] = LineOffset[j];
10029 LineWraps[j - line_count] = LineWraps[j];
10030 }
10031 LineOffset[j - line_count] = temp;
10032 LineWraps[j - line_count] = FALSE;
10033 if (can_clear((char_u *)" "))
10034 lineclear(temp, (int)Columns);
10035 else
10036 lineinvalid(temp, (int)Columns);
10037 }
10038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039
10040 screen_stop_highlight();
10041
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010042#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 /* redraw the characters */
10044 if (type == USE_REDRAW)
10045 redraw_block(row, end, wp);
10046 else
10047#endif
10048 if (type == USE_T_CD) /* delete the lines */
10049 {
10050 windgoto(cursor_row, 0);
10051 out_str(T_CD);
10052 screen_start(); /* don't know where cursor is now */
10053 }
10054 else if (type == USE_T_CDL)
10055 {
10056 windgoto(cursor_row, 0);
10057 term_delete_lines(line_count);
10058 screen_start(); /* don't know where cursor is now */
10059 }
10060 /*
10061 * Deleting lines at top of the screen or scroll region: Just scroll
10062 * the whole screen (scroll region) up by outputting newlines on the
10063 * last line.
10064 */
10065 else if (type == USE_NL)
10066 {
10067 windgoto(cursor_end - 1, 0);
10068 for (i = line_count; --i >= 0; )
10069 out_char('\n'); /* cursor will remain on same line */
10070 }
10071 else
10072 {
10073 for (i = line_count; --i >= 0; )
10074 {
10075 if (type == USE_T_DL)
10076 {
10077 windgoto(cursor_row, 0);
10078 out_str(T_DL); /* delete a line */
10079 }
10080 else /* type == USE_T_CE */
10081 {
10082 windgoto(cursor_row + i, 0);
10083 out_str(T_CE); /* erase a line */
10084 }
10085 screen_start(); /* don't know where cursor is now */
10086 }
10087 }
10088
10089 /*
10090 * If the 'db' flag is set, we need to clear the lines that have been
10091 * scrolled up at the bottom of the region.
10092 */
10093 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10094 {
10095 for (i = line_count; i > 0; --i)
10096 {
10097 windgoto(cursor_end - i, 0);
10098 out_str(T_CE); /* erase a line */
10099 screen_start(); /* don't know where cursor is now */
10100 }
10101 }
10102
10103#ifdef FEAT_GUI
10104 gui_can_update_cursor();
10105 if (gui.in_use)
10106 out_flush(); /* always flush after a scroll */
10107#endif
10108
10109 return OK;
10110}
10111
10112/*
10113 * show the current mode and ruler
10114 *
10115 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10116 * If clear_cmdline is FALSE there may be a message there that needs to be
10117 * cleared only if a mode is shown.
10118 * Return the length of the message (0 if no message).
10119 */
10120 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010121showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122{
10123 int need_clear;
10124 int length = 0;
10125 int do_mode;
10126 int attr;
10127 int nwr_save;
10128#ifdef FEAT_INS_EXPAND
10129 int sub_attr;
10130#endif
10131
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010132 do_mode = ((p_smd && msg_silent == 0)
10133 && ((State & INSERT)
10134 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010135 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 if (do_mode || Recording)
10137 {
10138 /*
10139 * Don't show mode right now, when not redrawing or inside a mapping.
10140 * Call char_avail() only when we are going to show something, because
10141 * it takes a bit of time.
10142 */
10143 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10144 {
10145 redraw_cmdline = TRUE; /* show mode later */
10146 return 0;
10147 }
10148
10149 nwr_save = need_wait_return;
10150
10151 /* wait a bit before overwriting an important message */
10152 check_for_delay(FALSE);
10153
10154 /* if the cmdline is more than one line high, erase top lines */
10155 need_clear = clear_cmdline;
10156 if (clear_cmdline && cmdline_row < Rows - 1)
10157 msg_clr_cmdline(); /* will reset clear_cmdline */
10158
10159 /* Position on the last line in the window, column 0 */
10160 msg_pos_mode();
10161 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010162 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 if (do_mode)
10164 {
10165 MSG_PUTS_ATTR("--", attr);
10166#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010167 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010168# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010169 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010170# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010171 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010172# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010173 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010174# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 MSG_PUTS_ATTR(" IM", attr);
10176# else
10177 MSG_PUTS_ATTR(" XIM", attr);
10178# endif
10179#endif
10180#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10181 if (gui.in_use)
10182 {
10183 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010184 {
10185 /* HANGUL */
10186 if (enc_utf8)
10187 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10188 else
10189 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 }
10192#endif
10193#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010194 /* CTRL-X in Insert mode */
10195 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 {
10197 /* These messages can get long, avoid a wrap in a narrow
10198 * window. Prefer showing edit_submode_extra. */
10199 length = (Rows - msg_row) * Columns - 3;
10200 if (edit_submode_extra != NULL)
10201 length -= vim_strsize(edit_submode_extra);
10202 if (length > 0)
10203 {
10204 if (edit_submode_pre != NULL)
10205 length -= vim_strsize(edit_submode_pre);
10206 if (length - vim_strsize(edit_submode) > 0)
10207 {
10208 if (edit_submode_pre != NULL)
10209 msg_puts_attr(edit_submode_pre, attr);
10210 msg_puts_attr(edit_submode, attr);
10211 }
10212 if (edit_submode_extra != NULL)
10213 {
10214 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10215 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010216 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 else
10218 sub_attr = attr;
10219 msg_puts_attr(edit_submode_extra, sub_attr);
10220 }
10221 }
10222 length = 0;
10223 }
10224 else
10225#endif
10226 {
10227#ifdef FEAT_VREPLACE
10228 if (State & VREPLACE_FLAG)
10229 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10230 else
10231#endif
10232 if (State & REPLACE_FLAG)
10233 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10234 else if (State & INSERT)
10235 {
10236#ifdef FEAT_RIGHTLEFT
10237 if (p_ri)
10238 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10239#endif
10240 MSG_PUTS_ATTR(_(" INSERT"), attr);
10241 }
10242 else if (restart_edit == 'I')
10243 MSG_PUTS_ATTR(_(" (insert)"), attr);
10244 else if (restart_edit == 'R')
10245 MSG_PUTS_ATTR(_(" (replace)"), attr);
10246 else if (restart_edit == 'V')
10247 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10248#ifdef FEAT_RIGHTLEFT
10249 if (p_hkmap)
10250 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10251# ifdef FEAT_FKMAP
10252 if (p_fkmap)
10253 MSG_PUTS_ATTR(farsi_text_5, attr);
10254# endif
10255#endif
10256#ifdef FEAT_KEYMAP
10257 if (State & LANGMAP)
10258 {
10259# ifdef FEAT_ARABIC
10260 if (curwin->w_p_arab)
10261 MSG_PUTS_ATTR(_(" Arabic"), attr);
10262 else
10263# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010264 if (get_keymap_str(curwin, (char_u *)" (%s)",
10265 NameBuff, MAXPATHL))
10266 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 }
10268#endif
10269 if ((State & INSERT) && p_paste)
10270 MSG_PUTS_ATTR(_(" (paste)"), attr);
10271
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 if (VIsual_active)
10273 {
10274 char *p;
10275
10276 /* Don't concatenate separate words to avoid translation
10277 * problems. */
10278 switch ((VIsual_select ? 4 : 0)
10279 + (VIsual_mode == Ctrl_V) * 2
10280 + (VIsual_mode == 'V'))
10281 {
10282 case 0: p = N_(" VISUAL"); break;
10283 case 1: p = N_(" VISUAL LINE"); break;
10284 case 2: p = N_(" VISUAL BLOCK"); break;
10285 case 4: p = N_(" SELECT"); break;
10286 case 5: p = N_(" SELECT LINE"); break;
10287 default: p = N_(" SELECT BLOCK"); break;
10288 }
10289 MSG_PUTS_ATTR(_(p), attr);
10290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 MSG_PUTS_ATTR(" --", attr);
10292 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010293
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 need_clear = TRUE;
10295 }
10296 if (Recording
10297#ifdef FEAT_INS_EXPAND
10298 && edit_submode == NULL /* otherwise it gets too long */
10299#endif
10300 )
10301 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010302 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 need_clear = TRUE;
10304 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010305
10306 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 if (need_clear || clear_cmdline)
10308 msg_clr_eos();
10309 msg_didout = FALSE; /* overwrite this message */
10310 length = msg_col;
10311 msg_col = 0;
10312 need_wait_return = nwr_save; /* never ask for hit-return for this */
10313 }
10314 else if (clear_cmdline && msg_silent == 0)
10315 /* Clear the whole command line. Will reset "clear_cmdline". */
10316 msg_clr_cmdline();
10317
10318#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 /* In Visual mode the size of the selected area must be redrawn. */
10320 if (VIsual_active)
10321 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322
10323 /* If the last window has no status line, the ruler is after the mode
10324 * message and must be redrawn */
10325 if (redrawing()
10326# ifdef FEAT_WINDOWS
10327 && lastwin->w_status_height == 0
10328# endif
10329 )
10330 win_redr_ruler(lastwin, TRUE);
10331#endif
10332 redraw_cmdline = FALSE;
10333 clear_cmdline = FALSE;
10334
10335 return length;
10336}
10337
10338/*
10339 * Position for a mode message.
10340 */
10341 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010342msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343{
10344 msg_col = 0;
10345 msg_row = Rows - 1;
10346}
10347
10348/*
10349 * Delete mode message. Used when ESC is typed which is expected to end
10350 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010351 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 */
10353 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010354unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355{
10356 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010357 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 */
10359 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10360 redraw_cmdline = TRUE; /* delete mode later */
10361 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010362 clearmode();
10363}
10364
10365/*
10366 * Clear the mode message.
10367 */
10368 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010369clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010370{
10371 msg_pos_mode();
10372 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010373 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010374 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375}
10376
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010377 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010378recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010379{
10380 MSG_PUTS_ATTR(_("recording"), attr);
10381 if (!shortmess(SHM_RECORDING))
10382 {
10383 char_u s[4];
10384 sprintf((char *)s, " @%c", Recording);
10385 MSG_PUTS_ATTR(s, attr);
10386 }
10387}
10388
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010389#if defined(FEAT_WINDOWS)
10390/*
10391 * Draw the tab pages line at the top of the Vim window.
10392 */
10393 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010394draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010395{
10396 int tabcount = 0;
10397 tabpage_T *tp;
10398 int tabwidth;
10399 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010400 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010401 int attr;
10402 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010403 win_T *cwp;
10404 int wincount;
10405 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010406 int c;
10407 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010408 int attr_sel = HL_ATTR(HLF_TPS);
10409 int attr_nosel = HL_ATTR(HLF_TP);
10410 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010411 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010412 int room;
10413 int use_sep_chars = (t_colors < 8
10414#ifdef FEAT_GUI
10415 && !gui.in_use
10416#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010417#ifdef FEAT_TERMGUICOLORS
10418 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010419#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010420 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010421
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010422 if (ScreenLines == NULL)
10423 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010424 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010425
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010426#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010427 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010428 if (gui_use_tabline())
10429 {
10430 gui_update_tabline();
10431 return;
10432 }
10433#endif
10434
10435 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010436 return;
10437
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010438#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010439
10440 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10441 for (scol = 0; scol < Columns; ++scol)
10442 TabPageIdxs[scol] = 0;
10443
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010444 /* Use the 'tabline' option if it's set. */
10445 if (*p_tal != NUL)
10446 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010447 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010448
10449 /* Check for an error. If there is one we would loop in redrawing the
10450 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010451 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010452 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010453 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010454 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010455 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010456 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010457 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010458 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010459#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010460 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010461 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010462 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010463
Bram Moolenaar238a5642006-02-21 22:12:05 +000010464 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10465 if (tabwidth < 6)
10466 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010467
Bram Moolenaar238a5642006-02-21 22:12:05 +000010468 attr = attr_nosel;
10469 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010470 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010471 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10472 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010473 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010474 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010475
Bram Moolenaar238a5642006-02-21 22:12:05 +000010476 if (tp->tp_topframe == topframe)
10477 attr = attr_sel;
10478 if (use_sep_chars && col > 0)
10479 screen_putchar('|', 0, col++, attr);
10480
10481 if (tp->tp_topframe != topframe)
10482 attr = attr_nosel;
10483
10484 screen_putchar(' ', 0, col++, attr);
10485
10486 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010487 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010488 cwp = curwin;
10489 wp = firstwin;
10490 }
10491 else
10492 {
10493 cwp = tp->tp_curwin;
10494 wp = tp->tp_firstwin;
10495 }
10496
10497 modified = FALSE;
10498 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10499 if (bufIsChanged(wp->w_buffer))
10500 modified = TRUE;
10501 if (modified || wincount > 1)
10502 {
10503 if (wincount > 1)
10504 {
10505 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010506 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010507 if (col + len >= Columns - 3)
10508 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010509 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010510#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010511 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010512#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010513 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010514#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010515 );
10516 col += len;
10517 }
10518 if (modified)
10519 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10520 screen_putchar(' ', 0, col++, attr);
10521 }
10522
10523 room = scol - col + tabwidth - 1;
10524 if (room > 0)
10525 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010526 /* Get buffer name in NameBuff[] */
10527 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010528 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010529 len = vim_strsize(NameBuff);
10530 p = NameBuff;
10531#ifdef FEAT_MBYTE
10532 if (has_mbyte)
10533 while (len > room)
10534 {
10535 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010536 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010537 }
10538 else
10539#endif
10540 if (len > room)
10541 {
10542 p += len - room;
10543 len = room;
10544 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010545 if (len > Columns - col - 1)
10546 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010547
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010548 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010549 col += len;
10550 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010551 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010552
10553 /* Store the tab page number in TabPageIdxs[], so that
10554 * jump_to_mouse() knows where each one is. */
10555 ++tabcount;
10556 while (scol < col)
10557 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010558 }
10559
Bram Moolenaar238a5642006-02-21 22:12:05 +000010560 if (use_sep_chars)
10561 c = '_';
10562 else
10563 c = ' ';
10564 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010565
10566 /* Put an "X" for closing the current tab if there are several. */
10567 if (first_tabpage->tp_next != NULL)
10568 {
10569 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10570 TabPageIdxs[Columns - 1] = -999;
10571 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010572 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010573
10574 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10575 * set. */
10576 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010577}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010578
10579/*
10580 * Get buffer name for "buf" into NameBuff[].
10581 * Takes care of special buffer names and translates special characters.
10582 */
10583 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010584get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010585{
10586 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010587 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010588 else
10589 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10590 trans_characters(NameBuff, MAXPATHL);
10591}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010592#endif
10593
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10595/*
10596 * Get the character to use in a status line. Get its attributes in "*attr".
10597 */
10598 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010599fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600{
10601 int fill;
10602 if (is_curwin)
10603 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010604 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 fill = fill_stl;
10606 }
10607 else
10608 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010609 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 fill = fill_stlnc;
10611 }
10612 /* Use fill when there is highlighting, and highlighting of current
10613 * window differs, or the fillchars differ, or this is not the
10614 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010615 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010616 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 || (fill_stl != fill_stlnc)))
10618 return fill;
10619 if (is_curwin)
10620 return '^';
10621 return '=';
10622}
10623#endif
10624
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010625#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626/*
10627 * Get the character to use in a separator between vertically split windows.
10628 * Get its attributes in "*attr".
10629 */
10630 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010631fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010633 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 if (*attr == 0 && fill_vert == ' ')
10635 return '|';
10636 else
10637 return fill_vert;
10638}
10639#endif
10640
10641/*
10642 * Return TRUE if redrawing should currently be done.
10643 */
10644 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010645redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010647#ifdef FEAT_EVAL
10648 if (disable_redraw_for_testing)
10649 return 0;
10650 else
10651#endif
10652 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10654}
10655
10656/*
10657 * Return TRUE if printing messages should currently be done.
10658 */
10659 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010660messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661{
10662 return (!(p_lz && char_avail() && !KeyTyped));
10663}
10664
10665/*
10666 * Show current status info in ruler and various other places
10667 * If always is FALSE, only show ruler if position has changed.
10668 */
10669 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010670showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671{
10672 if (!always && !redrawing())
10673 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010674#ifdef FEAT_INS_EXPAND
10675 if (pum_visible())
10676 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010677# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010678 /* Don't redraw right now, do it later. */
10679 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010680# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010681 return;
10682 }
10683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010685 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010686 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010687 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 else
10690#endif
10691#ifdef FEAT_CMDL_INFO
10692 win_redr_ruler(curwin, always);
10693#endif
10694
10695#ifdef FEAT_TITLE
10696 if (need_maketitle
10697# ifdef FEAT_STL_OPT
10698 || (p_icon && (stl_syntax & STL_IN_ICON))
10699 || (p_title && (stl_syntax & STL_IN_TITLE))
10700# endif
10701 )
10702 maketitle();
10703#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010704#ifdef FEAT_WINDOWS
10705 /* Redraw the tab pages line if needed. */
10706 if (redraw_tabline)
10707 draw_tabline();
10708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709}
10710
10711#ifdef FEAT_CMDL_INFO
10712 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010713win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010715#define RULER_BUF_LEN 70
10716 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 int row;
10718 int fillchar;
10719 int attr;
10720 int empty_line = FALSE;
10721 colnr_T virtcol;
10722 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010723 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010725#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 int this_ru_col;
10727 int off = 0;
10728 int width = Columns;
10729# define WITH_OFF(x) x
10730# define WITH_WIDTH(x) x
10731#else
10732# define WITH_OFF(x) 0
10733# define WITH_WIDTH(x) Columns
10734# define this_ru_col ru_col
10735#endif
10736
10737 /* If 'ruler' off or redrawing disabled, don't do anything */
10738 if (!p_ru)
10739 return;
10740
10741 /*
10742 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10743 * after deleting lines, before cursor.lnum is corrected.
10744 */
10745 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10746 return;
10747
10748#ifdef FEAT_INS_EXPAND
10749 /* Don't draw the ruler while doing insert-completion, it might overwrite
10750 * the (long) mode message. */
10751# ifdef FEAT_WINDOWS
10752 if (wp == lastwin && lastwin->w_status_height == 0)
10753# endif
10754 if (edit_submode != NULL)
10755 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010756 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10757 if (pum_visible())
10758 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759#endif
10760
10761#ifdef FEAT_STL_OPT
10762 if (*p_ruf)
10763 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010764 int save_called_emsg = called_emsg;
10765
10766 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010768 if (called_emsg)
10769 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010770 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010771 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 return;
10773 }
10774#endif
10775
10776 /*
10777 * Check if not in Insert mode and the line is empty (will show "0-1").
10778 */
10779 if (!(State & INSERT)
10780 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10781 empty_line = TRUE;
10782
10783 /*
10784 * Only draw the ruler when something changed.
10785 */
10786 validate_virtcol_win(wp);
10787 if ( redraw_cmdline
10788 || always
10789 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10790 || wp->w_cursor.col != wp->w_ru_cursor.col
10791 || wp->w_virtcol != wp->w_ru_virtcol
10792#ifdef FEAT_VIRTUALEDIT
10793 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10794#endif
10795 || wp->w_topline != wp->w_ru_topline
10796 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10797#ifdef FEAT_DIFF
10798 || wp->w_topfill != wp->w_ru_topfill
10799#endif
10800 || empty_line != wp->w_ru_empty)
10801 {
10802 cursor_off();
10803#ifdef FEAT_WINDOWS
10804 if (wp->w_status_height)
10805 {
10806 row = W_WINROW(wp) + wp->w_height;
10807 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 off = W_WINCOL(wp);
10809 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 }
10811 else
10812#endif
10813 {
10814 row = Rows - 1;
10815 fillchar = ' ';
10816 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010817#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 width = Columns;
10819 off = 0;
10820#endif
10821 }
10822
10823 /* In list mode virtcol needs to be recomputed */
10824 virtcol = wp->w_virtcol;
10825 if (wp->w_p_list && lcs_tab1 == NUL)
10826 {
10827 wp->w_p_list = FALSE;
10828 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10829 wp->w_p_list = TRUE;
10830 }
10831
10832 /*
10833 * Some sprintfs return the length, some return a pointer.
10834 * To avoid portability problems we use strlen() here.
10835 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010836 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10838 ? 0L
10839 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010840 len = STRLEN(buffer);
10841 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10843 (int)virtcol + 1);
10844
10845 /*
10846 * Add a "50%" if there is room for it.
10847 * On the last line, don't print in the last column (scrolls the
10848 * screen up on some terminals).
10849 */
10850 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010851 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 o = i + vim_strsize(buffer + i + 1);
10853#ifdef FEAT_WINDOWS
10854 if (wp->w_status_height == 0) /* can't use last char of screen */
10855#endif
10856 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010857#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 this_ru_col = ru_col - (Columns - width);
10859 if (this_ru_col < 0)
10860 this_ru_col = 0;
10861#endif
10862 /* Never use more than half the window/screen width, leave the other
10863 * half for the filename. */
10864 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10865 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10866 if (this_ru_col + o < WITH_WIDTH(width))
10867 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010868 /* need at least 3 chars left for get_rel_pos() + NUL */
10869 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 {
10871#ifdef FEAT_MBYTE
10872 if (has_mbyte)
10873 i += (*mb_char2bytes)(fillchar, buffer + i);
10874 else
10875#endif
10876 buffer[i++] = fillchar;
10877 ++o;
10878 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010879 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 }
10881 /* Truncate at window boundary. */
10882#ifdef FEAT_MBYTE
10883 if (has_mbyte)
10884 {
10885 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010886 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 {
10888 o += (*mb_ptr2cells)(buffer + i);
10889 if (this_ru_col + o > WITH_WIDTH(width))
10890 {
10891 buffer[i] = NUL;
10892 break;
10893 }
10894 }
10895 }
10896 else
10897#endif
10898 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10899 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10900
10901 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10902 i = redraw_cmdline;
10903 screen_fill(row, row + 1,
10904 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10905 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10906 fillchar, fillchar, attr);
10907 /* don't redraw the cmdline because of showing the ruler */
10908 redraw_cmdline = i;
10909 wp->w_ru_cursor = wp->w_cursor;
10910 wp->w_ru_virtcol = wp->w_virtcol;
10911 wp->w_ru_empty = empty_line;
10912 wp->w_ru_topline = wp->w_topline;
10913 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10914#ifdef FEAT_DIFF
10915 wp->w_ru_topfill = wp->w_topfill;
10916#endif
10917 }
10918}
10919#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010920
10921#if defined(FEAT_LINEBREAK) || defined(PROTO)
10922/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010923 * Return the width of the 'number' and 'relativenumber' column.
10924 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010925 * Otherwise it depends on 'numberwidth' and the line count.
10926 */
10927 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010928number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010929{
10930 int n;
10931 linenr_T lnum;
10932
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010933 if (wp->w_p_rnu && !wp->w_p_nu)
10934 /* cursor line shows "0" */
10935 lnum = wp->w_height;
10936 else
10937 /* cursor line shows absolute line number */
10938 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010939
Bram Moolenaar6b314672015-03-20 15:42:10 +010010940 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010941 return wp->w_nrwidth_width;
10942 wp->w_nrwidth_line_count = lnum;
10943
10944 n = 0;
10945 do
10946 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010947 lnum /= 10;
10948 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010949 } while (lnum > 0);
10950
10951 /* 'numberwidth' gives the minimal width plus one */
10952 if (n < wp->w_p_nuw - 1)
10953 n = wp->w_p_nuw - 1;
10954
10955 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010956 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010957 return n;
10958}
10959#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010960
10961/*
10962 * Return the current cursor column. This is the actual position on the
10963 * screen. First column is 0.
10964 */
10965 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010966screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010967{
10968 return screen_cur_col;
10969}
10970
10971/*
10972 * Return the current cursor row. This is the actual position on the screen.
10973 * First row is 0.
10974 */
10975 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010976screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010977{
10978 return screen_cur_row;
10979}