blob: c6d8f0ee4adbe0d53f2d45ba736a562c533eb8bc [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 Moolenaar21020352017-06-13 17:21:04 +02003374 line_attr = HL_ATTR(HLF_QFL);
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);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007803 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007804
Bram Moolenaarb3414592014-06-17 17:48:32 +02007805 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7806 matchcol,
7807#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007808 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007809#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007810 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007811#endif
7812 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007813 /* Copy the regprog, in case it got freed and recompiled. */
7814 if (regprog_is_copy)
7815 cur->match.regprog = cur->hl.rm.regprog;
7816
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007817 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007818 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007819 /* Error while handling regexp: stop using this regexp. */
7820 if (shl == &search_hl)
7821 {
7822 /* don't free regprog in the match list, it's a copy */
7823 vim_regfree(shl->rm.regprog);
7824 SET_NO_HLSEARCH(TRUE);
7825 }
7826 shl->rm.regprog = NULL;
7827 shl->lnum = 0;
7828 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7829 message */
7830 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007831 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007832 }
7833 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007834 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007835 else
7836 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 if (nmatched == 0)
7838 {
7839 shl->lnum = 0; /* no match found */
7840 break;
7841 }
7842 if (shl->rm.startpos[0].lnum > 0
7843 || shl->rm.startpos[0].col >= mincol
7844 || nmatched > 1
7845 || shl->rm.endpos[0].col > mincol)
7846 {
7847 shl->lnum += shl->rm.startpos[0].lnum;
7848 break; /* useful match found */
7849 }
7850 }
7851}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852
Bram Moolenaar85077472016-10-16 14:35:48 +02007853/*
7854 * If there is a match fill "shl" and return one.
7855 * Return zero otherwise.
7856 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007857 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007858next_search_hl_pos(
7859 match_T *shl, /* points to a match */
7860 linenr_T lnum,
7861 posmatch_T *posmatch, /* match positions */
7862 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007863{
7864 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007865 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007866
Bram Moolenaarb3414592014-06-17 17:48:32 +02007867 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7868 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007869 llpos_T *pos = &posmatch->pos[i];
7870
7871 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007872 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007873 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007874 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007875 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007876 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007877 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007878 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007879 /* if this match comes before the one at "found" then swap
7880 * them */
7881 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007882 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007883 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007884
Bram Moolenaar85077472016-10-16 14:35:48 +02007885 *pos = posmatch->pos[found];
7886 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007887 }
7888 }
7889 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007890 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007891 }
7892 }
7893 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007894 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007895 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007896 colnr_T start = posmatch->pos[found].col == 0
7897 ? 0 : posmatch->pos[found].col - 1;
7898 colnr_T end = posmatch->pos[found].col == 0
7899 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007900
Bram Moolenaar85077472016-10-16 14:35:48 +02007901 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007902 shl->rm.startpos[0].lnum = 0;
7903 shl->rm.startpos[0].col = start;
7904 shl->rm.endpos[0].lnum = 0;
7905 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007906 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007907 posmatch->cur = found + 1;
7908 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007909 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007910 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007911}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007912#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007913
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007915screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916{
7917 attrentry_T *aep = NULL;
7918
7919 screen_attr = attr;
7920 if (full_screen
7921#ifdef WIN3264
7922 && termcap_active
7923#endif
7924 )
7925 {
7926#ifdef FEAT_GUI
7927 if (gui.in_use)
7928 {
7929 char buf[20];
7930
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007931 /* The GUI handles this internally. */
7932 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 OUT_STR(buf);
7934 }
7935 else
7936#endif
7937 {
7938 if (attr > HL_ALL) /* special HL attr. */
7939 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007940 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 aep = syn_cterm_attr2entry(attr);
7942 else
7943 aep = syn_term_attr2entry(attr);
7944 if (aep == NULL) /* did ":syntax clear" */
7945 attr = 0;
7946 else
7947 attr = aep->ae_attr;
7948 }
7949 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7950 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007951 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007952#ifdef FEAT_TERMGUICOLORS
7953 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007954 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007955#endif
7956 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007957#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007958 )
7959#endif
7960 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007961 /* If the Normal FG color has BOLD attribute and the new HL
7962 * has a FG color defined, clear BOLD. */
7963 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7965 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007966 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7967 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 out_str(T_US);
7969 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7970 out_str(T_CZH);
7971 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7972 out_str(T_MR);
7973
7974 /*
7975 * Output the color or start string after bold etc., in case the
7976 * bold etc. override the color setting.
7977 */
7978 if (aep != NULL)
7979 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007980#ifdef FEAT_TERMGUICOLORS
7981 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007983 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007984 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007985 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007986 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 }
7988 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007991 if (t_colors > 1)
7992 {
7993 if (aep->ae_u.cterm.fg_color)
7994 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7995 if (aep->ae_u.cterm.bg_color)
7996 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7997 }
7998 else
7999 {
8000 if (aep->ae_u.term.start != NULL)
8001 out_str(aep->ae_u.term.start);
8002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 }
8004 }
8005 }
8006 }
8007}
8008
8009 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008010screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011{
8012 int do_ME = FALSE; /* output T_ME code */
8013
8014 if (screen_attr != 0
8015#ifdef WIN3264
8016 && termcap_active
8017#endif
8018 )
8019 {
8020#ifdef FEAT_GUI
8021 if (gui.in_use)
8022 {
8023 char buf[20];
8024
8025 /* use internal GUI code */
8026 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8027 OUT_STR(buf);
8028 }
8029 else
8030#endif
8031 {
8032 if (screen_attr > HL_ALL) /* special HL attr. */
8033 {
8034 attrentry_T *aep;
8035
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008036 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {
8038 /*
8039 * Assume that t_me restores the original colors!
8040 */
8041 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008042 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008043#ifdef FEAT_TERMGUICOLORS
8044 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008045 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8046 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008047#endif
8048 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008049#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008050 )
8051#endif
8052 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 do_ME = TRUE;
8054 }
8055 else
8056 {
8057 aep = syn_term_attr2entry(screen_attr);
8058 if (aep != NULL && aep->ae_u.term.stop != NULL)
8059 {
8060 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8061 do_ME = TRUE;
8062 else
8063 out_str(aep->ae_u.term.stop);
8064 }
8065 }
8066 if (aep == NULL) /* did ":syntax clear" */
8067 screen_attr = 0;
8068 else
8069 screen_attr = aep->ae_attr;
8070 }
8071
8072 /*
8073 * Often all ending-codes are equal to T_ME. Avoid outputting the
8074 * same sequence several times.
8075 */
8076 if (screen_attr & HL_STANDOUT)
8077 {
8078 if (STRCMP(T_SE, T_ME) == 0)
8079 do_ME = TRUE;
8080 else
8081 out_str(T_SE);
8082 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008083 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 {
8085 if (STRCMP(T_UE, T_ME) == 0)
8086 do_ME = TRUE;
8087 else
8088 out_str(T_UE);
8089 }
8090 if (screen_attr & HL_ITALIC)
8091 {
8092 if (STRCMP(T_CZR, T_ME) == 0)
8093 do_ME = TRUE;
8094 else
8095 out_str(T_CZR);
8096 }
8097 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8098 out_str(T_ME);
8099
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008100#ifdef FEAT_TERMGUICOLORS
8101 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008103 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008104 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008105 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008106 term_bg_rgb_color(cterm_normal_bg_gui_color);
8107 }
8108 else
8109#endif
8110 {
8111 if (t_colors > 1)
8112 {
8113 /* set Normal cterm colors */
8114 if (cterm_normal_fg_color != 0)
8115 term_fg_color(cterm_normal_fg_color - 1);
8116 if (cterm_normal_bg_color != 0)
8117 term_bg_color(cterm_normal_bg_color - 1);
8118 if (cterm_normal_fg_bold)
8119 out_str(T_MD);
8120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 }
8122 }
8123 }
8124 screen_attr = 0;
8125}
8126
8127/*
8128 * Reset the colors for a cterm. Used when leaving Vim.
8129 * The machine specific code may override this again.
8130 */
8131 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008132reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008134 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {
8136 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008137#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008138 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8139 || cterm_normal_bg_gui_color != INVALCOLOR)
8140 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008141#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {
8145 out_str(T_OP);
8146 screen_attr = -1;
8147 }
8148 if (cterm_normal_fg_bold)
8149 {
8150 out_str(T_ME);
8151 screen_attr = -1;
8152 }
8153 }
8154}
8155
8156/*
8157 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8158 * using the attributes from ScreenAttrs["off"].
8159 */
8160 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008161screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162{
8163 int attr;
8164
8165 /* Check for illegal values, just in case (could happen just after
8166 * resizing). */
8167 if (row >= screen_Rows || col >= screen_Columns)
8168 return;
8169
Bram Moolenaar494838a2015-02-10 19:20:37 +01008170 /* Outputting a character in the last cell on the screen may scroll the
8171 * screen up. Only do it when the "xn" termcap property is set, otherwise
8172 * mark the character invalid (update it when scrolled up). */
8173 if (*T_XN == NUL
8174 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175#ifdef FEAT_RIGHTLEFT
8176 /* account for first command-line character in rightleft mode */
8177 && !cmdmsg_rl
8178#endif
8179 )
8180 {
8181 ScreenAttrs[off] = (sattr_T)-1;
8182 return;
8183 }
8184
8185 /*
8186 * Stop highlighting first, so it's easier to move the cursor.
8187 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008188#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 if (screen_char_attr != 0)
8190 attr = screen_char_attr;
8191 else
8192#endif
8193 attr = ScreenAttrs[off];
8194 if (screen_attr != attr)
8195 screen_stop_highlight();
8196
8197 windgoto(row, col);
8198
8199 if (screen_attr != attr)
8200 screen_start_highlight(attr);
8201
8202#ifdef FEAT_MBYTE
8203 if (enc_utf8 && ScreenLinesUC[off] != 0)
8204 {
8205 char_u buf[MB_MAXBYTES + 1];
8206
8207 /* Convert UTF-8 character to bytes and write it. */
8208
8209 buf[utfc_char2bytes(off, buf)] = NUL;
8210
8211 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008212 if (utf_ambiguous_width(ScreenLinesUC[off]))
8213 screen_cur_col = 9999;
8214 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 ++screen_cur_col;
8216 }
8217 else
8218#endif
8219 {
8220#ifdef FEAT_MBYTE
8221 out_flush_check();
8222#endif
8223 out_char(ScreenLines[off]);
8224#ifdef FEAT_MBYTE
8225 /* double-byte character in single-width cell */
8226 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8227 out_char(ScreenLines2[off]);
8228#endif
8229 }
8230
8231 screen_cur_col++;
8232}
8233
8234#ifdef FEAT_MBYTE
8235
8236/*
8237 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8238 * on the screen at position 'row' and 'col'.
8239 * The attributes of the first byte is used for all. This is required to
8240 * output the two bytes of a double-byte character with nothing in between.
8241 */
8242 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008243screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244{
8245 /* Check for illegal values (could be wrong when screen was resized). */
8246 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8247 return;
8248
8249 /* Outputting the last character on the screen may scrollup the screen.
8250 * Don't to it! Mark the character invalid (update it when scrolled up) */
8251 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8252 {
8253 ScreenAttrs[off] = (sattr_T)-1;
8254 return;
8255 }
8256
8257 /* Output the first byte normally (positions the cursor), then write the
8258 * second byte directly. */
8259 screen_char(off, row, col);
8260 out_char(ScreenLines[off + 1]);
8261 ++screen_cur_col;
8262}
8263#endif
8264
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008265#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266/*
8267 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8268 * This uses the contents of ScreenLines[] and doesn't change it.
8269 */
8270 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008271screen_draw_rectangle(
8272 int row,
8273 int col,
8274 int height,
8275 int width,
8276 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277{
8278 int r, c;
8279 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008280#ifdef FEAT_MBYTE
8281 int max_off;
8282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008284 /* Can't use ScreenLines unless initialized */
8285 if (ScreenLines == NULL)
8286 return;
8287
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 if (invert)
8289 screen_char_attr = HL_INVERSE;
8290 for (r = row; r < row + height; ++r)
8291 {
8292 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008293#ifdef FEAT_MBYTE
8294 max_off = off + screen_Columns;
8295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 for (c = col; c < col + width; ++c)
8297 {
8298#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008299 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {
8301 screen_char_2(off + c, r, c);
8302 ++c;
8303 }
8304 else
8305#endif
8306 {
8307 screen_char(off + c, r, c);
8308#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008309 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 ++c;
8311#endif
8312 }
8313 }
8314 }
8315 screen_char_attr = 0;
8316}
8317#endif
8318
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008319#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320/*
8321 * Redraw the characters for a vertically split window.
8322 */
8323 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008324redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325{
8326 int col;
8327 int width;
8328
8329# ifdef FEAT_CLIPBOARD
8330 clip_may_clear_selection(row, end - 1);
8331# endif
8332
8333 if (wp == NULL)
8334 {
8335 col = 0;
8336 width = Columns;
8337 }
8338 else
8339 {
8340 col = wp->w_wincol;
8341 width = wp->w_width;
8342 }
8343 screen_draw_rectangle(row, col, end - row, width, FALSE);
8344}
8345#endif
8346
8347/*
8348 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8349 * with character 'c1' in first column followed by 'c2' in the other columns.
8350 * Use attributes 'attr'.
8351 */
8352 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008353screen_fill(
8354 int start_row,
8355 int end_row,
8356 int start_col,
8357 int end_col,
8358 int c1,
8359 int c2,
8360 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361{
8362 int row;
8363 int col;
8364 int off;
8365 int end_off;
8366 int did_delete;
8367 int c;
8368 int norm_term;
8369#if defined(FEAT_GUI) || defined(UNIX)
8370 int force_next = FALSE;
8371#endif
8372
8373 if (end_row > screen_Rows) /* safety check */
8374 end_row = screen_Rows;
8375 if (end_col > screen_Columns) /* safety check */
8376 end_col = screen_Columns;
8377 if (ScreenLines == NULL
8378 || start_row >= end_row
8379 || start_col >= end_col) /* nothing to do */
8380 return;
8381
8382 /* it's a "normal" terminal when not in a GUI or cterm */
8383 norm_term = (
8384#ifdef FEAT_GUI
8385 !gui.in_use &&
8386#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008387 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 for (row = start_row; row < end_row; ++row)
8389 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008390#ifdef FEAT_MBYTE
8391 if (has_mbyte
8392# ifdef FEAT_GUI
8393 && !gui.in_use
8394# endif
8395 )
8396 {
8397 /* When drawing over the right halve of a double-wide char clear
8398 * out the left halve. When drawing over the left halve of a
8399 * double wide-char clear out the right halve. Only needed in a
8400 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008401 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008402 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008403 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008404 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008405 }
8406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 /*
8408 * Try to use delete-line termcap code, when no attributes or in a
8409 * "normal" terminal, where a bold/italic space is just a
8410 * space.
8411 */
8412 did_delete = FALSE;
8413 if (c2 == ' '
8414 && end_col == Columns
8415 && can_clear(T_CE)
8416 && (attr == 0
8417 || (norm_term
8418 && attr <= HL_ALL
8419 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8420 {
8421 /*
8422 * check if we really need to clear something
8423 */
8424 col = start_col;
8425 if (c1 != ' ') /* don't clear first char */
8426 ++col;
8427
8428 off = LineOffset[row] + col;
8429 end_off = LineOffset[row] + end_col;
8430
8431 /* skip blanks (used often, keep it fast!) */
8432#ifdef FEAT_MBYTE
8433 if (enc_utf8)
8434 while (off < end_off && ScreenLines[off] == ' '
8435 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8436 ++off;
8437 else
8438#endif
8439 while (off < end_off && ScreenLines[off] == ' '
8440 && ScreenAttrs[off] == 0)
8441 ++off;
8442 if (off < end_off) /* something to be cleared */
8443 {
8444 col = off - LineOffset[row];
8445 screen_stop_highlight();
8446 term_windgoto(row, col);/* clear rest of this screen line */
8447 out_str(T_CE);
8448 screen_start(); /* don't know where cursor is now */
8449 col = end_col - col;
8450 while (col--) /* clear chars in ScreenLines */
8451 {
8452 ScreenLines[off] = ' ';
8453#ifdef FEAT_MBYTE
8454 if (enc_utf8)
8455 ScreenLinesUC[off] = 0;
8456#endif
8457 ScreenAttrs[off] = 0;
8458 ++off;
8459 }
8460 }
8461 did_delete = TRUE; /* the chars are cleared now */
8462 }
8463
8464 off = LineOffset[row] + start_col;
8465 c = c1;
8466 for (col = start_col; col < end_col; ++col)
8467 {
8468 if (ScreenLines[off] != c
8469#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008470 || (enc_utf8 && (int)ScreenLinesUC[off]
8471 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472#endif
8473 || ScreenAttrs[off] != attr
8474#if defined(FEAT_GUI) || defined(UNIX)
8475 || force_next
8476#endif
8477 )
8478 {
8479#if defined(FEAT_GUI) || defined(UNIX)
8480 /* The bold trick may make a single row of pixels appear in
8481 * the next character. When a bold character is removed, the
8482 * next character should be redrawn too. This happens for our
8483 * own GUI and for some xterms. */
8484 if (
8485# ifdef FEAT_GUI
8486 gui.in_use
8487# endif
8488# if defined(FEAT_GUI) && defined(UNIX)
8489 ||
8490# endif
8491# ifdef UNIX
8492 term_is_xterm
8493# endif
8494 )
8495 {
8496 if (ScreenLines[off] != ' '
8497 && (ScreenAttrs[off] > HL_ALL
8498 || ScreenAttrs[off] & HL_BOLD))
8499 force_next = TRUE;
8500 else
8501 force_next = FALSE;
8502 }
8503#endif
8504 ScreenLines[off] = c;
8505#ifdef FEAT_MBYTE
8506 if (enc_utf8)
8507 {
8508 if (c >= 0x80)
8509 {
8510 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008511 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 }
8513 else
8514 ScreenLinesUC[off] = 0;
8515 }
8516#endif
8517 ScreenAttrs[off] = attr;
8518 if (!did_delete || c != ' ')
8519 screen_char(off, row, col);
8520 }
8521 ++off;
8522 if (col == start_col)
8523 {
8524 if (did_delete)
8525 break;
8526 c = c2;
8527 }
8528 }
8529 if (end_col == Columns)
8530 LineWraps[row] = FALSE;
8531 if (row == Rows - 1) /* overwritten the command line */
8532 {
8533 redraw_cmdline = TRUE;
8534 if (c1 == ' ' && c2 == ' ')
8535 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008536 if (start_col == 0)
8537 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 }
8539 }
8540}
8541
8542/*
8543 * Check if there should be a delay. Used before clearing or redrawing the
8544 * screen or the command line.
8545 */
8546 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008547check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
8549 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8550 && !did_wait_return
8551 && emsg_silent == 0)
8552 {
8553 out_flush();
8554 ui_delay(1000L, TRUE);
8555 emsg_on_display = FALSE;
8556 if (check_msg_scroll)
8557 msg_scroll = FALSE;
8558 }
8559}
8560
8561/*
8562 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008563 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 * Returns TRUE if there is a valid screen to write to.
8565 * Returns FALSE when starting up and screen not initialized yet.
8566 */
8567 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008568screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008570 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 return (ScreenLines != NULL);
8572}
8573
8574/*
8575 * Resize the shell to Rows and Columns.
8576 * Allocate ScreenLines[] and associated items.
8577 *
8578 * There may be some time between setting Rows and Columns and (re)allocating
8579 * ScreenLines[]. This happens when starting up and when (manually) changing
8580 * the shell size. Always use screen_Rows and screen_Columns to access items
8581 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8582 * final size of the shell is needed.
8583 */
8584 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008585screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586{
8587 int new_row, old_row;
8588#ifdef FEAT_GUI
8589 int old_Rows;
8590#endif
8591 win_T *wp;
8592 int outofmem = FALSE;
8593 int len;
8594 schar_T *new_ScreenLines;
8595#ifdef FEAT_MBYTE
8596 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008597 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008599 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600#endif
8601 sattr_T *new_ScreenAttrs;
8602 unsigned *new_LineOffset;
8603 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008604#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008605 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008606 tabpage_T *tp;
8607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008609 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008610#ifdef FEAT_AUTOCMD
8611 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008613retry:
8614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 /*
8616 * Allocation of the screen buffers is done only when the size changes and
8617 * when Rows and Columns have been set and we have started doing full
8618 * screen stuff.
8619 */
8620 if ((ScreenLines != NULL
8621 && Rows == screen_Rows
8622 && Columns == screen_Columns
8623#ifdef FEAT_MBYTE
8624 && enc_utf8 == (ScreenLinesUC != NULL)
8625 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008626 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627#endif
8628 )
8629 || Rows == 0
8630 || Columns == 0
8631 || (!full_screen && ScreenLines == NULL))
8632 return;
8633
8634 /*
8635 * It's possible that we produce an out-of-memory message below, which
8636 * will cause this function to be called again. To break the loop, just
8637 * return here.
8638 */
8639 if (entered)
8640 return;
8641 entered = TRUE;
8642
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008643 /*
8644 * Note that the window sizes are updated before reallocating the arrays,
8645 * thus we must not redraw here!
8646 */
8647 ++RedrawingDisabled;
8648
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 win_new_shellsize(); /* fit the windows in the new sized shell */
8650
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 comp_col(); /* recompute columns for shown command and ruler */
8652
8653 /*
8654 * We're changing the size of the screen.
8655 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8656 * - Move lines from the old arrays into the new arrays, clear extra
8657 * lines (unless the screen is going to be cleared).
8658 * - Free the old arrays.
8659 *
8660 * If anything fails, make ScreenLines NULL, so we don't do anything!
8661 * Continuing with the old ScreenLines may result in a crash, because the
8662 * size is wrong.
8663 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008664 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008666#ifdef FEAT_AUTOCMD
8667 if (aucmd_win != NULL)
8668 win_free_lsize(aucmd_win);
8669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670
8671 new_ScreenLines = (schar_T *)lalloc((long_u)(
8672 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8673#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008674 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 if (enc_utf8)
8676 {
8677 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8678 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008679 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008680 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8682 }
8683 if (enc_dbcs == DBCS_JPNU)
8684 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8685 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8686#endif
8687 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8688 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8689 new_LineOffset = (unsigned *)lalloc((long_u)(
8690 Rows * sizeof(unsigned)), FALSE);
8691 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008692#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008693 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008696 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 {
8698 if (win_alloc_lines(wp) == FAIL)
8699 {
8700 outofmem = TRUE;
8701#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008702 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703#endif
8704 }
8705 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008706#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008707 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8708 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008709 outofmem = TRUE;
8710#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008711#ifdef FEAT_WINDOWS
8712give_up:
8713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008715#ifdef FEAT_MBYTE
8716 for (i = 0; i < p_mco; ++i)
8717 if (new_ScreenLinesC[i] == NULL)
8718 break;
8719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 if (new_ScreenLines == NULL
8721#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008722 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8724#endif
8725 || new_ScreenAttrs == NULL
8726 || new_LineOffset == NULL
8727 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008728#ifdef FEAT_WINDOWS
8729 || new_TabPageIdxs == NULL
8730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 || outofmem)
8732 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008733 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008734 {
8735 /* guess the size */
8736 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8737
8738 /* Remember we did this to avoid getting outofmem messages over
8739 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008740 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 vim_free(new_ScreenLines);
8743 new_ScreenLines = NULL;
8744#ifdef FEAT_MBYTE
8745 vim_free(new_ScreenLinesUC);
8746 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008747 for (i = 0; i < p_mco; ++i)
8748 {
8749 vim_free(new_ScreenLinesC[i]);
8750 new_ScreenLinesC[i] = NULL;
8751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 vim_free(new_ScreenLines2);
8753 new_ScreenLines2 = NULL;
8754#endif
8755 vim_free(new_ScreenAttrs);
8756 new_ScreenAttrs = NULL;
8757 vim_free(new_LineOffset);
8758 new_LineOffset = NULL;
8759 vim_free(new_LineWraps);
8760 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008761#ifdef FEAT_WINDOWS
8762 vim_free(new_TabPageIdxs);
8763 new_TabPageIdxs = NULL;
8764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 }
8766 else
8767 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008768 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008769
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 for (new_row = 0; new_row < Rows; ++new_row)
8771 {
8772 new_LineOffset[new_row] = new_row * Columns;
8773 new_LineWraps[new_row] = FALSE;
8774
8775 /*
8776 * If the screen is not going to be cleared, copy as much as
8777 * possible from the old screen to the new one and clear the rest
8778 * (used when resizing the window at the "--more--" prompt or when
8779 * executing an external command, for the GUI).
8780 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008781 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 {
8783 (void)vim_memset(new_ScreenLines + new_row * Columns,
8784 ' ', (size_t)Columns * sizeof(schar_T));
8785#ifdef FEAT_MBYTE
8786 if (enc_utf8)
8787 {
8788 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8789 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008790 for (i = 0; i < p_mco; ++i)
8791 (void)vim_memset(new_ScreenLinesC[i]
8792 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 0, (size_t)Columns * sizeof(u8char_T));
8794 }
8795 if (enc_dbcs == DBCS_JPNU)
8796 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8797 0, (size_t)Columns * sizeof(schar_T));
8798#endif
8799 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8800 0, (size_t)Columns * sizeof(sattr_T));
8801 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008802 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 {
8804 if (screen_Columns < Columns)
8805 len = screen_Columns;
8806 else
8807 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008808#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008809 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008810 * may be invalid now. Also when p_mco changes. */
8811 if (!(enc_utf8 && ScreenLinesUC == NULL)
8812 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008813#endif
8814 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8815 ScreenLines + LineOffset[old_row],
8816 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008818 if (enc_utf8 && ScreenLinesUC != NULL
8819 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 {
8821 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8822 ScreenLinesUC + LineOffset[old_row],
8823 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008824 for (i = 0; i < p_mco; ++i)
8825 mch_memmove(new_ScreenLinesC[i]
8826 + new_LineOffset[new_row],
8827 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 (size_t)len * sizeof(u8char_T));
8829 }
8830 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8831 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8832 ScreenLines2 + LineOffset[old_row],
8833 (size_t)len * sizeof(schar_T));
8834#endif
8835 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8836 ScreenAttrs + LineOffset[old_row],
8837 (size_t)len * sizeof(sattr_T));
8838 }
8839 }
8840 }
8841 /* Use the last line of the screen for the current line. */
8842 current_ScreenLine = new_ScreenLines + Rows * Columns;
8843 }
8844
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008845 free_screenlines();
8846
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 ScreenLines = new_ScreenLines;
8848#ifdef FEAT_MBYTE
8849 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008850 for (i = 0; i < p_mco; ++i)
8851 ScreenLinesC[i] = new_ScreenLinesC[i];
8852 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 ScreenLines2 = new_ScreenLines2;
8854#endif
8855 ScreenAttrs = new_ScreenAttrs;
8856 LineOffset = new_LineOffset;
8857 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008858#ifdef FEAT_WINDOWS
8859 TabPageIdxs = new_TabPageIdxs;
8860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861
8862 /* It's important that screen_Rows and screen_Columns reflect the actual
8863 * size of ScreenLines[]. Set them before calling anything. */
8864#ifdef FEAT_GUI
8865 old_Rows = screen_Rows;
8866#endif
8867 screen_Rows = Rows;
8868 screen_Columns = Columns;
8869
8870 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008871 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 screenclear2();
8873
8874#ifdef FEAT_GUI
8875 else if (gui.in_use
8876 && !gui.starting
8877 && ScreenLines != NULL
8878 && old_Rows != Rows)
8879 {
8880 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8881 /*
8882 * Adjust the position of the cursor, for when executing an external
8883 * command.
8884 */
8885 if (msg_row >= Rows) /* Rows got smaller */
8886 msg_row = Rows - 1; /* put cursor at last row */
8887 else if (Rows > old_Rows) /* Rows got bigger */
8888 msg_row += Rows - old_Rows; /* put cursor in same place */
8889 if (msg_col >= Columns) /* Columns got smaller */
8890 msg_col = Columns - 1; /* put cursor at last column */
8891 }
8892#endif
8893
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008895 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008896
8897#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008898 /*
8899 * Do not apply autocommands more than 3 times to avoid an endless loop
8900 * in case applying autocommands always changes Rows or Columns.
8901 */
8902 if (starting == 0 && ++retry_count <= 3)
8903 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008904 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008905 /* In rare cases, autocommands may have altered Rows or Columns,
8906 * jump back to check if we need to allocate the screen again. */
8907 goto retry;
8908 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910}
8911
8912 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008913free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008914{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008915#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008916 int i;
8917
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008918 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008919 for (i = 0; i < Screen_mco; ++i)
8920 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008921 vim_free(ScreenLines2);
8922#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008923 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008924 vim_free(ScreenAttrs);
8925 vim_free(LineOffset);
8926 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008927#ifdef FEAT_WINDOWS
8928 vim_free(TabPageIdxs);
8929#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008930}
8931
8932 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008933screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
8935 check_for_delay(FALSE);
8936 screenalloc(FALSE); /* allocate screen buffers if size changed */
8937 screenclear2(); /* clear the screen */
8938}
8939
8940 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008941screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942{
8943 int i;
8944
8945 if (starting == NO_SCREEN || ScreenLines == NULL
8946#ifdef FEAT_GUI
8947 || (gui.in_use && gui.starting)
8948#endif
8949 )
8950 return;
8951
8952#ifdef FEAT_GUI
8953 if (!gui.in_use)
8954#endif
8955 screen_attr = -1; /* force setting the Normal colors */
8956 screen_stop_highlight(); /* don't want highlighting here */
8957
8958#ifdef FEAT_CLIPBOARD
8959 /* disable selection without redrawing it */
8960 clip_scroll_selection(9999);
8961#endif
8962
8963 /* blank out ScreenLines */
8964 for (i = 0; i < Rows; ++i)
8965 {
8966 lineclear(LineOffset[i], (int)Columns);
8967 LineWraps[i] = FALSE;
8968 }
8969
8970 if (can_clear(T_CL))
8971 {
8972 out_str(T_CL); /* clear the display */
8973 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008974 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 }
8976 else
8977 {
8978 /* can't clear the screen, mark all chars with invalid attributes */
8979 for (i = 0; i < Rows; ++i)
8980 lineinvalid(LineOffset[i], (int)Columns);
8981 clear_cmdline = TRUE;
8982 }
8983
8984 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8985
8986 win_rest_invalid(firstwin);
8987 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008988#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008989 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 if (must_redraw == CLEAR) /* no need to clear again */
8992 must_redraw = NOT_VALID;
8993 compute_cmdrow();
8994 msg_row = cmdline_row; /* put cursor on last line for messages */
8995 msg_col = 0;
8996 screen_start(); /* don't know where cursor is now */
8997 msg_scrolled = 0; /* can't scroll back */
8998 msg_didany = FALSE;
8999 msg_didout = FALSE;
9000}
9001
9002/*
9003 * Clear one line in ScreenLines.
9004 */
9005 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009006lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9009#ifdef FEAT_MBYTE
9010 if (enc_utf8)
9011 (void)vim_memset(ScreenLinesUC + off, 0,
9012 (size_t)width * sizeof(u8char_T));
9013#endif
9014 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
9015}
9016
9017/*
9018 * Mark one line in ScreenLines invalid by setting the attributes to an
9019 * invalid value.
9020 */
9021 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009022lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023{
9024 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9025}
9026
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009027#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028/*
9029 * Copy part of a Screenline for vertically split window "wp".
9030 */
9031 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009032linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033{
9034 unsigned off_to = LineOffset[to] + wp->w_wincol;
9035 unsigned off_from = LineOffset[from] + wp->w_wincol;
9036
9037 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9038 wp->w_width * sizeof(schar_T));
9039# ifdef FEAT_MBYTE
9040 if (enc_utf8)
9041 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009042 int i;
9043
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9045 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009046 for (i = 0; i < p_mco; ++i)
9047 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9048 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 }
9050 if (enc_dbcs == DBCS_JPNU)
9051 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9052 wp->w_width * sizeof(schar_T));
9053# endif
9054 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9055 wp->w_width * sizeof(sattr_T));
9056}
9057#endif
9058
9059/*
9060 * Return TRUE if clearing with term string "p" would work.
9061 * It can't work when the string is empty or it won't set the right background.
9062 */
9063 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009064can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065{
9066 return (*p != NUL && (t_colors <= 1
9067#ifdef FEAT_GUI
9068 || gui.in_use
9069#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009070#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009071 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009072 || (!p_tgc && cterm_normal_bg_color == 0)
9073#else
9074 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009075#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009076 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077}
9078
9079/*
9080 * Reset cursor position. Use whenever cursor was moved because of outputting
9081 * something directly to the screen (shell commands) or a terminal control
9082 * code.
9083 */
9084 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009085screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086{
9087 screen_cur_row = screen_cur_col = 9999;
9088}
9089
9090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 * Move the cursor to position "row","col" in the screen.
9092 * This tries to find the most efficient way to move, minimizing the number of
9093 * characters sent to the terminal.
9094 */
9095 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009096windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009098 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 int i;
9100 int plan;
9101 int cost;
9102 int wouldbe_col;
9103 int noinvcurs;
9104 char_u *bs;
9105 int goto_cost;
9106 int attr;
9107
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009108#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9110
9111#define PLAN_LE 1
9112#define PLAN_CR 2
9113#define PLAN_NL 3
9114#define PLAN_WRITE 4
9115 /* Can't use ScreenLines unless initialized */
9116 if (ScreenLines == NULL)
9117 return;
9118
9119 if (col != screen_cur_col || row != screen_cur_row)
9120 {
9121 /* Check for valid position. */
9122 if (row < 0) /* window without text lines? */
9123 row = 0;
9124 if (row >= screen_Rows)
9125 row = screen_Rows - 1;
9126 if (col >= screen_Columns)
9127 col = screen_Columns - 1;
9128
9129 /* check if no cursor movement is allowed in highlight mode */
9130 if (screen_attr && *T_MS == NUL)
9131 noinvcurs = HIGHL_COST;
9132 else
9133 noinvcurs = 0;
9134 goto_cost = GOTO_COST + noinvcurs;
9135
9136 /*
9137 * Plan how to do the positioning:
9138 * 1. Use CR to move it to column 0, same row.
9139 * 2. Use T_LE to move it a few columns to the left.
9140 * 3. Use NL to move a few lines down, column 0.
9141 * 4. Move a few columns to the right with T_ND or by writing chars.
9142 *
9143 * Don't do this if the cursor went beyond the last column, the cursor
9144 * position is unknown then (some terminals wrap, some don't )
9145 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009146 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 * characters to move the cursor to the right.
9148 */
9149 if (row >= screen_cur_row && screen_cur_col < Columns)
9150 {
9151 /*
9152 * If the cursor is in the same row, bigger col, we can use CR
9153 * or T_LE.
9154 */
9155 bs = NULL; /* init for GCC */
9156 attr = screen_attr;
9157 if (row == screen_cur_row && col < screen_cur_col)
9158 {
9159 /* "le" is preferred over "bc", because "bc" is obsolete */
9160 if (*T_LE)
9161 bs = T_LE; /* "cursor left" */
9162 else
9163 bs = T_BC; /* "backspace character (old) */
9164 if (*bs)
9165 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9166 else
9167 cost = 999;
9168 if (col + 1 < cost) /* using CR is less characters */
9169 {
9170 plan = PLAN_CR;
9171 wouldbe_col = 0;
9172 cost = 1; /* CR is just one character */
9173 }
9174 else
9175 {
9176 plan = PLAN_LE;
9177 wouldbe_col = col;
9178 }
9179 if (noinvcurs) /* will stop highlighting */
9180 {
9181 cost += noinvcurs;
9182 attr = 0;
9183 }
9184 }
9185
9186 /*
9187 * If the cursor is above where we want to be, we can use CR LF.
9188 */
9189 else if (row > screen_cur_row)
9190 {
9191 plan = PLAN_NL;
9192 wouldbe_col = 0;
9193 cost = (row - screen_cur_row) * 2; /* CR LF */
9194 if (noinvcurs) /* will stop highlighting */
9195 {
9196 cost += noinvcurs;
9197 attr = 0;
9198 }
9199 }
9200
9201 /*
9202 * If the cursor is in the same row, smaller col, just use write.
9203 */
9204 else
9205 {
9206 plan = PLAN_WRITE;
9207 wouldbe_col = screen_cur_col;
9208 cost = 0;
9209 }
9210
9211 /*
9212 * Check if any characters that need to be written have the
9213 * correct attributes. Also avoid UTF-8 characters.
9214 */
9215 i = col - wouldbe_col;
9216 if (i > 0)
9217 cost += i;
9218 if (cost < goto_cost && i > 0)
9219 {
9220 /*
9221 * Check if the attributes are correct without additionally
9222 * stopping highlighting.
9223 */
9224 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9225 while (i && *p++ == attr)
9226 --i;
9227 if (i != 0)
9228 {
9229 /*
9230 * Try if it works when highlighting is stopped here.
9231 */
9232 if (*--p == 0)
9233 {
9234 cost += noinvcurs;
9235 while (i && *p++ == 0)
9236 --i;
9237 }
9238 if (i != 0)
9239 cost = 999; /* different attributes, don't do it */
9240 }
9241#ifdef FEAT_MBYTE
9242 if (enc_utf8)
9243 {
9244 /* Don't use an UTF-8 char for positioning, it's slow. */
9245 for (i = wouldbe_col; i < col; ++i)
9246 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9247 {
9248 cost = 999;
9249 break;
9250 }
9251 }
9252#endif
9253 }
9254
9255 /*
9256 * We can do it without term_windgoto()!
9257 */
9258 if (cost < goto_cost)
9259 {
9260 if (plan == PLAN_LE)
9261 {
9262 if (noinvcurs)
9263 screen_stop_highlight();
9264 while (screen_cur_col > col)
9265 {
9266 out_str(bs);
9267 --screen_cur_col;
9268 }
9269 }
9270 else if (plan == PLAN_CR)
9271 {
9272 if (noinvcurs)
9273 screen_stop_highlight();
9274 out_char('\r');
9275 screen_cur_col = 0;
9276 }
9277 else if (plan == PLAN_NL)
9278 {
9279 if (noinvcurs)
9280 screen_stop_highlight();
9281 while (screen_cur_row < row)
9282 {
9283 out_char('\n');
9284 ++screen_cur_row;
9285 }
9286 screen_cur_col = 0;
9287 }
9288
9289 i = col - screen_cur_col;
9290 if (i > 0)
9291 {
9292 /*
9293 * Use cursor-right if it's one character only. Avoids
9294 * removing a line of pixels from the last bold char, when
9295 * using the bold trick in the GUI.
9296 */
9297 if (T_ND[0] != NUL && T_ND[1] == NUL)
9298 {
9299 while (i-- > 0)
9300 out_char(*T_ND);
9301 }
9302 else
9303 {
9304 int off;
9305
9306 off = LineOffset[row] + screen_cur_col;
9307 while (i-- > 0)
9308 {
9309 if (ScreenAttrs[off] != screen_attr)
9310 screen_stop_highlight();
9311#ifdef FEAT_MBYTE
9312 out_flush_check();
9313#endif
9314 out_char(ScreenLines[off]);
9315#ifdef FEAT_MBYTE
9316 if (enc_dbcs == DBCS_JPNU
9317 && ScreenLines[off] == 0x8e)
9318 out_char(ScreenLines2[off]);
9319#endif
9320 ++off;
9321 }
9322 }
9323 }
9324 }
9325 }
9326 else
9327 cost = 999;
9328
9329 if (cost >= goto_cost)
9330 {
9331 if (noinvcurs)
9332 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009333 if (row == screen_cur_row && (col > screen_cur_col)
9334 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 term_cursor_right(col - screen_cur_col);
9336 else
9337 term_windgoto(row, col);
9338 }
9339 screen_cur_row = row;
9340 screen_cur_col = col;
9341 }
9342}
9343
9344/*
9345 * Set cursor to its position in the current window.
9346 */
9347 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009348setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349{
9350 if (redrawing())
9351 {
9352 validate_cursor();
9353 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9354 W_WINCOL(curwin) + (
9355#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009356 /* With 'rightleft' set and the cursor on a double-wide
9357 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9359# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009360 (has_mbyte
9361 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9362 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363# endif
9364 1)) :
9365#endif
9366 curwin->w_wcol));
9367 }
9368}
9369
9370
9371/*
9372 * insert 'line_count' lines at 'row' in window 'wp'
9373 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9374 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9375 * scrolling.
9376 * Returns FAIL if the lines are not inserted, OK for success.
9377 */
9378 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009379win_ins_lines(
9380 win_T *wp,
9381 int row,
9382 int line_count,
9383 int invalid,
9384 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
9386 int did_delete;
9387 int nextrow;
9388 int lastrow;
9389 int retval;
9390
9391 if (invalid)
9392 wp->w_lines_valid = 0;
9393
9394 if (wp->w_height < 5)
9395 return FAIL;
9396
9397 if (line_count > wp->w_height - row)
9398 line_count = wp->w_height - row;
9399
9400 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9401 if (retval != MAYBE)
9402 return retval;
9403
9404 /*
9405 * If there is a next window or a status line, we first try to delete the
9406 * lines at the bottom to avoid messing what is after the window.
9407 * If this fails and there are following windows, don't do anything to avoid
9408 * messing up those windows, better just redraw.
9409 */
9410 did_delete = FALSE;
9411#ifdef FEAT_WINDOWS
9412 if (wp->w_next != NULL || wp->w_status_height)
9413 {
9414 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9415 line_count, (int)Rows, FALSE, NULL) == OK)
9416 did_delete = TRUE;
9417 else if (wp->w_next)
9418 return FAIL;
9419 }
9420#endif
9421 /*
9422 * if no lines deleted, blank the lines that will end up below the window
9423 */
9424 if (!did_delete)
9425 {
9426#ifdef FEAT_WINDOWS
9427 wp->w_redr_status = TRUE;
9428#endif
9429 redraw_cmdline = TRUE;
9430 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9431 lastrow = nextrow + line_count;
9432 if (lastrow > Rows)
9433 lastrow = Rows;
9434 screen_fill(nextrow - line_count, lastrow - line_count,
9435 W_WINCOL(wp), (int)W_ENDCOL(wp),
9436 ' ', ' ', 0);
9437 }
9438
9439 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9440 == FAIL)
9441 {
9442 /* deletion will have messed up other windows */
9443 if (did_delete)
9444 {
9445#ifdef FEAT_WINDOWS
9446 wp->w_redr_status = TRUE;
9447#endif
9448 win_rest_invalid(W_NEXT(wp));
9449 }
9450 return FAIL;
9451 }
9452
9453 return OK;
9454}
9455
9456/*
9457 * delete "line_count" window lines at "row" in window "wp"
9458 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9459 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9460 * scrolling
9461 * Return OK for success, FAIL if the lines are not deleted.
9462 */
9463 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009464win_del_lines(
9465 win_T *wp,
9466 int row,
9467 int line_count,
9468 int invalid,
9469 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 int retval;
9472
9473 if (invalid)
9474 wp->w_lines_valid = 0;
9475
9476 if (line_count > wp->w_height - row)
9477 line_count = wp->w_height - row;
9478
9479 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9480 if (retval != MAYBE)
9481 return retval;
9482
9483 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9484 (int)Rows, FALSE, NULL) == FAIL)
9485 return FAIL;
9486
9487#ifdef FEAT_WINDOWS
9488 /*
9489 * If there are windows or status lines below, try to put them at the
9490 * correct place. If we can't do that, they have to be redrawn.
9491 */
9492 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9493 {
9494 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9495 line_count, (int)Rows, NULL) == FAIL)
9496 {
9497 wp->w_redr_status = TRUE;
9498 win_rest_invalid(wp->w_next);
9499 }
9500 }
9501 /*
9502 * If this is the last window and there is no status line, redraw the
9503 * command line later.
9504 */
9505 else
9506#endif
9507 redraw_cmdline = TRUE;
9508 return OK;
9509}
9510
9511/*
9512 * Common code for win_ins_lines() and win_del_lines().
9513 * Returns OK or FAIL when the work has been done.
9514 * Returns MAYBE when not finished yet.
9515 */
9516 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009517win_do_lines(
9518 win_T *wp,
9519 int row,
9520 int line_count,
9521 int mayclear,
9522 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523{
9524 int retval;
9525
9526 if (!redrawing() || line_count <= 0)
9527 return FAIL;
9528
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009529 /* When inserting lines would result in loss of command output, just redraw
9530 * the lines. */
9531 if (no_win_do_lines_ins && !del)
9532 return FAIL;
9533
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 /* only a few lines left: redraw is faster */
9535 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009536#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 && wp->w_width == Columns
9538#endif
9539 )
9540 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009541 if (!no_win_do_lines_ins)
9542 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 return FAIL;
9544 }
9545
9546 /*
9547 * Delete all remaining lines
9548 */
9549 if (row + line_count >= wp->w_height)
9550 {
9551 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9552 W_WINCOL(wp), (int)W_ENDCOL(wp),
9553 ' ', ' ', 0);
9554 return OK;
9555 }
9556
9557 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009558 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009560 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009562 if (!no_win_do_lines_ins)
9563 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564
9565 /*
9566 * If the terminal can set a scroll region, use that.
9567 * Always do this in a vertically split window. This will redraw from
9568 * ScreenLines[] when t_CV isn't defined. That's faster than using
9569 * win_line().
9570 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009571 * a character in the lower right corner of the scroll region may cause a
9572 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 */
9574 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009575#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 || W_WIDTH(wp) != Columns
9577#endif
9578 )
9579 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009580#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9582#endif
9583 scroll_region_set(wp, row);
9584 if (del)
9585 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9586 wp->w_height - row, FALSE, wp);
9587 else
9588 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9589 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009590#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9592#endif
9593 scroll_region_reset();
9594 return retval;
9595 }
9596
9597#ifdef FEAT_WINDOWS
9598 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9599 return FAIL;
9600#endif
9601
9602 return MAYBE;
9603}
9604
9605/*
9606 * window 'wp' and everything after it is messed up, mark it for redraw
9607 */
9608 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009609win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
9611#ifdef FEAT_WINDOWS
9612 while (wp != NULL)
9613#else
9614 if (wp != NULL)
9615#endif
9616 {
9617 redraw_win_later(wp, NOT_VALID);
9618#ifdef FEAT_WINDOWS
9619 wp->w_redr_status = TRUE;
9620 wp = wp->w_next;
9621#endif
9622 }
9623 redraw_cmdline = TRUE;
9624}
9625
9626/*
9627 * The rest of the routines in this file perform screen manipulations. The
9628 * given operation is performed physically on the screen. The corresponding
9629 * change is also made to the internal screen image. In this way, the editor
9630 * anticipates the effect of editing changes on the appearance of the screen.
9631 * That way, when we call screenupdate a complete redraw isn't usually
9632 * necessary. Another advantage is that we can keep adding code to anticipate
9633 * screen changes, and in the meantime, everything still works.
9634 */
9635
9636/*
9637 * types for inserting or deleting lines
9638 */
9639#define USE_T_CAL 1
9640#define USE_T_CDL 2
9641#define USE_T_AL 3
9642#define USE_T_CE 4
9643#define USE_T_DL 5
9644#define USE_T_SR 6
9645#define USE_NL 7
9646#define USE_T_CD 8
9647#define USE_REDRAW 9
9648
9649/*
9650 * insert lines on the screen and update ScreenLines[]
9651 * 'end' is the line after the scrolled part. Normally it is Rows.
9652 * When scrolling region used 'off' is the offset from the top for the region.
9653 * 'row' and 'end' are relative to the start of the region.
9654 *
9655 * return FAIL for failure, OK for success.
9656 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009657 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009658screen_ins_lines(
9659 int off,
9660 int row,
9661 int line_count,
9662 int end,
9663 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665 int i;
9666 int j;
9667 unsigned temp;
9668 int cursor_row;
9669 int type;
9670 int result_empty;
9671 int can_ce = can_clear(T_CE);
9672
9673 /*
9674 * FAIL if
9675 * - there is no valid screen
9676 * - the screen has to be redrawn completely
9677 * - the line count is less than one
9678 * - the line count is more than 'ttyscroll'
9679 */
9680 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9681 return FAIL;
9682
9683 /*
9684 * There are seven ways to insert lines:
9685 * 0. When in a vertically split window and t_CV isn't set, redraw the
9686 * characters from ScreenLines[].
9687 * 1. Use T_CD (clear to end of display) if it exists and the result of
9688 * the insert is just empty lines
9689 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9690 * present or line_count > 1. It looks better if we do all the inserts
9691 * at once.
9692 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9693 * insert is just empty lines and T_CE is not present or line_count >
9694 * 1.
9695 * 4. Use T_AL (insert line) if it exists.
9696 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9697 * just empty lines.
9698 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9699 * just empty lines.
9700 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9701 * the 'da' flag is not set or we have clear line capability.
9702 * 8. redraw the characters from ScreenLines[].
9703 *
9704 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9705 * the scrollbar for the window. It does have insert line, use that if it
9706 * exists.
9707 */
9708 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009709#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9711 type = USE_REDRAW;
9712 else
9713#endif
9714 if (can_clear(T_CD) && result_empty)
9715 type = USE_T_CD;
9716 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9717 type = USE_T_CAL;
9718 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9719 type = USE_T_CDL;
9720 else if (*T_AL != NUL)
9721 type = USE_T_AL;
9722 else if (can_ce && result_empty)
9723 type = USE_T_CE;
9724 else if (*T_DL != NUL && result_empty)
9725 type = USE_T_DL;
9726 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9727 type = USE_T_SR;
9728 else
9729 return FAIL;
9730
9731 /*
9732 * For clearing the lines screen_del_lines() is used. This will also take
9733 * care of t_db if necessary.
9734 */
9735 if (type == USE_T_CD || type == USE_T_CDL ||
9736 type == USE_T_CE || type == USE_T_DL)
9737 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9738
9739 /*
9740 * If text is retained below the screen, first clear or delete as many
9741 * lines at the bottom of the window as are about to be inserted so that
9742 * the deleted lines won't later surface during a screen_del_lines.
9743 */
9744 if (*T_DB)
9745 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9746
9747#ifdef FEAT_CLIPBOARD
9748 /* Remove a modeless selection when inserting lines halfway the screen
9749 * or not the full width of the screen. */
9750 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009751# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 || (wp != NULL && wp->w_width != Columns)
9753# endif
9754 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009755 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 else
9757 clip_scroll_selection(-line_count);
9758#endif
9759
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760#ifdef FEAT_GUI
9761 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9762 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009763 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764#endif
9765
9766 if (*T_CCS != NUL) /* cursor relative to region */
9767 cursor_row = row;
9768 else
9769 cursor_row = row + off;
9770
9771 /*
9772 * Shift LineOffset[] line_count down to reflect the inserted lines.
9773 * Clear the inserted lines in ScreenLines[].
9774 */
9775 row += off;
9776 end += off;
9777 for (i = 0; i < line_count; ++i)
9778 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009779#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 if (wp != NULL && wp->w_width != Columns)
9781 {
9782 /* need to copy part of a line */
9783 j = end - 1 - i;
9784 while ((j -= line_count) >= row)
9785 linecopy(j + line_count, j, wp);
9786 j += line_count;
9787 if (can_clear((char_u *)" "))
9788 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9789 else
9790 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9791 LineWraps[j] = FALSE;
9792 }
9793 else
9794#endif
9795 {
9796 j = end - 1 - i;
9797 temp = LineOffset[j];
9798 while ((j -= line_count) >= row)
9799 {
9800 LineOffset[j + line_count] = LineOffset[j];
9801 LineWraps[j + line_count] = LineWraps[j];
9802 }
9803 LineOffset[j + line_count] = temp;
9804 LineWraps[j + line_count] = FALSE;
9805 if (can_clear((char_u *)" "))
9806 lineclear(temp, (int)Columns);
9807 else
9808 lineinvalid(temp, (int)Columns);
9809 }
9810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811
9812 screen_stop_highlight();
9813 windgoto(cursor_row, 0);
9814
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009815#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 /* redraw the characters */
9817 if (type == USE_REDRAW)
9818 redraw_block(row, end, wp);
9819 else
9820#endif
9821 if (type == USE_T_CAL)
9822 {
9823 term_append_lines(line_count);
9824 screen_start(); /* don't know where cursor is now */
9825 }
9826 else
9827 {
9828 for (i = 0; i < line_count; i++)
9829 {
9830 if (type == USE_T_AL)
9831 {
9832 if (i && cursor_row != 0)
9833 windgoto(cursor_row, 0);
9834 out_str(T_AL);
9835 }
9836 else /* type == USE_T_SR */
9837 out_str(T_SR);
9838 screen_start(); /* don't know where cursor is now */
9839 }
9840 }
9841
9842 /*
9843 * With scroll-reverse and 'da' flag set we need to clear the lines that
9844 * have been scrolled down into the region.
9845 */
9846 if (type == USE_T_SR && *T_DA)
9847 {
9848 for (i = 0; i < line_count; ++i)
9849 {
9850 windgoto(off + i, 0);
9851 out_str(T_CE);
9852 screen_start(); /* don't know where cursor is now */
9853 }
9854 }
9855
9856#ifdef FEAT_GUI
9857 gui_can_update_cursor();
9858 if (gui.in_use)
9859 out_flush(); /* always flush after a scroll */
9860#endif
9861 return OK;
9862}
9863
9864/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009865 * Delete lines on the screen and update ScreenLines[].
9866 * "end" is the line after the scrolled part. Normally it is Rows.
9867 * When scrolling region used "off" is the offset from the top for the region.
9868 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 *
9870 * Return OK for success, FAIL if the lines are not deleted.
9871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009873screen_del_lines(
9874 int off,
9875 int row,
9876 int line_count,
9877 int end,
9878 int force, /* even when line_count > p_ttyscroll */
9879 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880{
9881 int j;
9882 int i;
9883 unsigned temp;
9884 int cursor_row;
9885 int cursor_end;
9886 int result_empty; /* result is empty until end of region */
9887 int can_delete; /* deleting line codes can be used */
9888 int type;
9889
9890 /*
9891 * FAIL if
9892 * - there is no valid screen
9893 * - the screen has to be redrawn completely
9894 * - the line count is less than one
9895 * - the line count is more than 'ttyscroll'
9896 */
9897 if (!screen_valid(TRUE) || line_count <= 0 ||
9898 (!force && line_count > p_ttyscroll))
9899 return FAIL;
9900
9901 /*
9902 * Check if the rest of the current region will become empty.
9903 */
9904 result_empty = row + line_count >= end;
9905
9906 /*
9907 * We can delete lines only when 'db' flag not set or when 'ce' option
9908 * available.
9909 */
9910 can_delete = (*T_DB == NUL || can_clear(T_CE));
9911
9912 /*
9913 * There are six ways to delete lines:
9914 * 0. When in a vertically split window and t_CV isn't set, redraw the
9915 * characters from ScreenLines[].
9916 * 1. Use T_CD if it exists and the result is empty.
9917 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9918 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9919 * none of the other ways work.
9920 * 4. Use T_CE (erase line) if the result is empty.
9921 * 5. Use T_DL (delete line) if it exists.
9922 * 6. redraw the characters from ScreenLines[].
9923 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009924#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9926 type = USE_REDRAW;
9927 else
9928#endif
9929 if (can_clear(T_CD) && result_empty)
9930 type = USE_T_CD;
9931#if defined(__BEOS__) && defined(BEOS_DR8)
9932 /*
9933 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9934 * its internal termcap... this works okay for tests which test *T_DB !=
9935 * NUL. It has the disadvantage that the user cannot use any :set t_*
9936 * command to get T_DB (back) to empty_option, only :set term=... will do
9937 * the trick...
9938 * Anyway, this hack will hopefully go away with the next OS release.
9939 * (Olaf Seibert)
9940 */
9941 else if (row == 0 && T_DB == empty_option
9942 && (line_count == 1 || *T_CDL == NUL))
9943#else
9944 else if (row == 0 && (
9945#ifndef AMIGA
9946 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9947 * up, so use delete-line command */
9948 line_count == 1 ||
9949#endif
9950 *T_CDL == NUL))
9951#endif
9952 type = USE_NL;
9953 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9954 type = USE_T_CDL;
9955 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009956#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 && (wp == NULL || wp->w_width == Columns)
9958#endif
9959 )
9960 type = USE_T_CE;
9961 else if (*T_DL != NUL && can_delete)
9962 type = USE_T_DL;
9963 else if (*T_CDL != NUL && can_delete)
9964 type = USE_T_CDL;
9965 else
9966 return FAIL;
9967
9968#ifdef FEAT_CLIPBOARD
9969 /* Remove a modeless selection when deleting lines halfway the screen or
9970 * not the full width of the screen. */
9971 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009972# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 || (wp != NULL && wp->w_width != Columns)
9974# endif
9975 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009976 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 else
9978 clip_scroll_selection(line_count);
9979#endif
9980
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981#ifdef FEAT_GUI
9982 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9983 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009984 gui_dont_update_cursor(gui.cursor_row >= row + off
9985 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986#endif
9987
9988 if (*T_CCS != NUL) /* cursor relative to region */
9989 {
9990 cursor_row = row;
9991 cursor_end = end;
9992 }
9993 else
9994 {
9995 cursor_row = row + off;
9996 cursor_end = end + off;
9997 }
9998
9999 /*
10000 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10001 * Clear the inserted lines in ScreenLines[].
10002 */
10003 row += off;
10004 end += off;
10005 for (i = 0; i < line_count; ++i)
10006 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010007#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 if (wp != NULL && wp->w_width != Columns)
10009 {
10010 /* need to copy part of a line */
10011 j = row + i;
10012 while ((j += line_count) <= end - 1)
10013 linecopy(j - line_count, j, wp);
10014 j -= line_count;
10015 if (can_clear((char_u *)" "))
10016 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
10017 else
10018 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10019 LineWraps[j] = FALSE;
10020 }
10021 else
10022#endif
10023 {
10024 /* whole width, moving the line pointers is faster */
10025 j = row + i;
10026 temp = LineOffset[j];
10027 while ((j += line_count) <= end - 1)
10028 {
10029 LineOffset[j - line_count] = LineOffset[j];
10030 LineWraps[j - line_count] = LineWraps[j];
10031 }
10032 LineOffset[j - line_count] = temp;
10033 LineWraps[j - line_count] = FALSE;
10034 if (can_clear((char_u *)" "))
10035 lineclear(temp, (int)Columns);
10036 else
10037 lineinvalid(temp, (int)Columns);
10038 }
10039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040
10041 screen_stop_highlight();
10042
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010043#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 /* redraw the characters */
10045 if (type == USE_REDRAW)
10046 redraw_block(row, end, wp);
10047 else
10048#endif
10049 if (type == USE_T_CD) /* delete the lines */
10050 {
10051 windgoto(cursor_row, 0);
10052 out_str(T_CD);
10053 screen_start(); /* don't know where cursor is now */
10054 }
10055 else if (type == USE_T_CDL)
10056 {
10057 windgoto(cursor_row, 0);
10058 term_delete_lines(line_count);
10059 screen_start(); /* don't know where cursor is now */
10060 }
10061 /*
10062 * Deleting lines at top of the screen or scroll region: Just scroll
10063 * the whole screen (scroll region) up by outputting newlines on the
10064 * last line.
10065 */
10066 else if (type == USE_NL)
10067 {
10068 windgoto(cursor_end - 1, 0);
10069 for (i = line_count; --i >= 0; )
10070 out_char('\n'); /* cursor will remain on same line */
10071 }
10072 else
10073 {
10074 for (i = line_count; --i >= 0; )
10075 {
10076 if (type == USE_T_DL)
10077 {
10078 windgoto(cursor_row, 0);
10079 out_str(T_DL); /* delete a line */
10080 }
10081 else /* type == USE_T_CE */
10082 {
10083 windgoto(cursor_row + i, 0);
10084 out_str(T_CE); /* erase a line */
10085 }
10086 screen_start(); /* don't know where cursor is now */
10087 }
10088 }
10089
10090 /*
10091 * If the 'db' flag is set, we need to clear the lines that have been
10092 * scrolled up at the bottom of the region.
10093 */
10094 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10095 {
10096 for (i = line_count; i > 0; --i)
10097 {
10098 windgoto(cursor_end - i, 0);
10099 out_str(T_CE); /* erase a line */
10100 screen_start(); /* don't know where cursor is now */
10101 }
10102 }
10103
10104#ifdef FEAT_GUI
10105 gui_can_update_cursor();
10106 if (gui.in_use)
10107 out_flush(); /* always flush after a scroll */
10108#endif
10109
10110 return OK;
10111}
10112
10113/*
10114 * show the current mode and ruler
10115 *
10116 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10117 * If clear_cmdline is FALSE there may be a message there that needs to be
10118 * cleared only if a mode is shown.
10119 * Return the length of the message (0 if no message).
10120 */
10121 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010122showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123{
10124 int need_clear;
10125 int length = 0;
10126 int do_mode;
10127 int attr;
10128 int nwr_save;
10129#ifdef FEAT_INS_EXPAND
10130 int sub_attr;
10131#endif
10132
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010133 do_mode = ((p_smd && msg_silent == 0)
10134 && ((State & INSERT)
10135 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010136 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 if (do_mode || Recording)
10138 {
10139 /*
10140 * Don't show mode right now, when not redrawing or inside a mapping.
10141 * Call char_avail() only when we are going to show something, because
10142 * it takes a bit of time.
10143 */
10144 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10145 {
10146 redraw_cmdline = TRUE; /* show mode later */
10147 return 0;
10148 }
10149
10150 nwr_save = need_wait_return;
10151
10152 /* wait a bit before overwriting an important message */
10153 check_for_delay(FALSE);
10154
10155 /* if the cmdline is more than one line high, erase top lines */
10156 need_clear = clear_cmdline;
10157 if (clear_cmdline && cmdline_row < Rows - 1)
10158 msg_clr_cmdline(); /* will reset clear_cmdline */
10159
10160 /* Position on the last line in the window, column 0 */
10161 msg_pos_mode();
10162 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010163 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 if (do_mode)
10165 {
10166 MSG_PUTS_ATTR("--", attr);
10167#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010168 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010169# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010170 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010171# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010172 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010173# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010174 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010175# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 MSG_PUTS_ATTR(" IM", attr);
10177# else
10178 MSG_PUTS_ATTR(" XIM", attr);
10179# endif
10180#endif
10181#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10182 if (gui.in_use)
10183 {
10184 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010185 {
10186 /* HANGUL */
10187 if (enc_utf8)
10188 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10189 else
10190 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 }
10193#endif
10194#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010195 /* CTRL-X in Insert mode */
10196 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 {
10198 /* These messages can get long, avoid a wrap in a narrow
10199 * window. Prefer showing edit_submode_extra. */
10200 length = (Rows - msg_row) * Columns - 3;
10201 if (edit_submode_extra != NULL)
10202 length -= vim_strsize(edit_submode_extra);
10203 if (length > 0)
10204 {
10205 if (edit_submode_pre != NULL)
10206 length -= vim_strsize(edit_submode_pre);
10207 if (length - vim_strsize(edit_submode) > 0)
10208 {
10209 if (edit_submode_pre != NULL)
10210 msg_puts_attr(edit_submode_pre, attr);
10211 msg_puts_attr(edit_submode, attr);
10212 }
10213 if (edit_submode_extra != NULL)
10214 {
10215 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10216 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010217 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 else
10219 sub_attr = attr;
10220 msg_puts_attr(edit_submode_extra, sub_attr);
10221 }
10222 }
10223 length = 0;
10224 }
10225 else
10226#endif
10227 {
10228#ifdef FEAT_VREPLACE
10229 if (State & VREPLACE_FLAG)
10230 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10231 else
10232#endif
10233 if (State & REPLACE_FLAG)
10234 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10235 else if (State & INSERT)
10236 {
10237#ifdef FEAT_RIGHTLEFT
10238 if (p_ri)
10239 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10240#endif
10241 MSG_PUTS_ATTR(_(" INSERT"), attr);
10242 }
10243 else if (restart_edit == 'I')
10244 MSG_PUTS_ATTR(_(" (insert)"), attr);
10245 else if (restart_edit == 'R')
10246 MSG_PUTS_ATTR(_(" (replace)"), attr);
10247 else if (restart_edit == 'V')
10248 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10249#ifdef FEAT_RIGHTLEFT
10250 if (p_hkmap)
10251 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10252# ifdef FEAT_FKMAP
10253 if (p_fkmap)
10254 MSG_PUTS_ATTR(farsi_text_5, attr);
10255# endif
10256#endif
10257#ifdef FEAT_KEYMAP
10258 if (State & LANGMAP)
10259 {
10260# ifdef FEAT_ARABIC
10261 if (curwin->w_p_arab)
10262 MSG_PUTS_ATTR(_(" Arabic"), attr);
10263 else
10264# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010265 if (get_keymap_str(curwin, (char_u *)" (%s)",
10266 NameBuff, MAXPATHL))
10267 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 }
10269#endif
10270 if ((State & INSERT) && p_paste)
10271 MSG_PUTS_ATTR(_(" (paste)"), attr);
10272
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 if (VIsual_active)
10274 {
10275 char *p;
10276
10277 /* Don't concatenate separate words to avoid translation
10278 * problems. */
10279 switch ((VIsual_select ? 4 : 0)
10280 + (VIsual_mode == Ctrl_V) * 2
10281 + (VIsual_mode == 'V'))
10282 {
10283 case 0: p = N_(" VISUAL"); break;
10284 case 1: p = N_(" VISUAL LINE"); break;
10285 case 2: p = N_(" VISUAL BLOCK"); break;
10286 case 4: p = N_(" SELECT"); break;
10287 case 5: p = N_(" SELECT LINE"); break;
10288 default: p = N_(" SELECT BLOCK"); break;
10289 }
10290 MSG_PUTS_ATTR(_(p), attr);
10291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 MSG_PUTS_ATTR(" --", attr);
10293 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010294
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 need_clear = TRUE;
10296 }
10297 if (Recording
10298#ifdef FEAT_INS_EXPAND
10299 && edit_submode == NULL /* otherwise it gets too long */
10300#endif
10301 )
10302 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010303 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 need_clear = TRUE;
10305 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010306
10307 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 if (need_clear || clear_cmdline)
10309 msg_clr_eos();
10310 msg_didout = FALSE; /* overwrite this message */
10311 length = msg_col;
10312 msg_col = 0;
10313 need_wait_return = nwr_save; /* never ask for hit-return for this */
10314 }
10315 else if (clear_cmdline && msg_silent == 0)
10316 /* Clear the whole command line. Will reset "clear_cmdline". */
10317 msg_clr_cmdline();
10318
10319#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 /* In Visual mode the size of the selected area must be redrawn. */
10321 if (VIsual_active)
10322 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323
10324 /* If the last window has no status line, the ruler is after the mode
10325 * message and must be redrawn */
10326 if (redrawing()
10327# ifdef FEAT_WINDOWS
10328 && lastwin->w_status_height == 0
10329# endif
10330 )
10331 win_redr_ruler(lastwin, TRUE);
10332#endif
10333 redraw_cmdline = FALSE;
10334 clear_cmdline = FALSE;
10335
10336 return length;
10337}
10338
10339/*
10340 * Position for a mode message.
10341 */
10342 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010343msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344{
10345 msg_col = 0;
10346 msg_row = Rows - 1;
10347}
10348
10349/*
10350 * Delete mode message. Used when ESC is typed which is expected to end
10351 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010352 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 */
10354 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010355unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356{
10357 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010358 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 */
10360 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10361 redraw_cmdline = TRUE; /* delete mode later */
10362 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010363 clearmode();
10364}
10365
10366/*
10367 * Clear the mode message.
10368 */
10369 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010370clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010371{
10372 msg_pos_mode();
10373 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010374 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010375 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376}
10377
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010378 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010379recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010380{
10381 MSG_PUTS_ATTR(_("recording"), attr);
10382 if (!shortmess(SHM_RECORDING))
10383 {
10384 char_u s[4];
10385 sprintf((char *)s, " @%c", Recording);
10386 MSG_PUTS_ATTR(s, attr);
10387 }
10388}
10389
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010390#if defined(FEAT_WINDOWS)
10391/*
10392 * Draw the tab pages line at the top of the Vim window.
10393 */
10394 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010395draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010396{
10397 int tabcount = 0;
10398 tabpage_T *tp;
10399 int tabwidth;
10400 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010401 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010402 int attr;
10403 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010404 win_T *cwp;
10405 int wincount;
10406 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010407 int c;
10408 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010409 int attr_sel = HL_ATTR(HLF_TPS);
10410 int attr_nosel = HL_ATTR(HLF_TP);
10411 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010412 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010413 int room;
10414 int use_sep_chars = (t_colors < 8
10415#ifdef FEAT_GUI
10416 && !gui.in_use
10417#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010418#ifdef FEAT_TERMGUICOLORS
10419 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010420#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010421 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010422
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010423 if (ScreenLines == NULL)
10424 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010425 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010426
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010427#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010428 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010429 if (gui_use_tabline())
10430 {
10431 gui_update_tabline();
10432 return;
10433 }
10434#endif
10435
10436 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010437 return;
10438
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010439#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010440
10441 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10442 for (scol = 0; scol < Columns; ++scol)
10443 TabPageIdxs[scol] = 0;
10444
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010445 /* Use the 'tabline' option if it's set. */
10446 if (*p_tal != NUL)
10447 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010448 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010449
10450 /* Check for an error. If there is one we would loop in redrawing the
10451 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010452 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010453 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010454 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010455 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010456 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010457 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010458 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010459 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010460#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010461 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010462 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010463 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010464
Bram Moolenaar238a5642006-02-21 22:12:05 +000010465 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10466 if (tabwidth < 6)
10467 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010468
Bram Moolenaar238a5642006-02-21 22:12:05 +000010469 attr = attr_nosel;
10470 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010471 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010472 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10473 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010474 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010475 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010476
Bram Moolenaar238a5642006-02-21 22:12:05 +000010477 if (tp->tp_topframe == topframe)
10478 attr = attr_sel;
10479 if (use_sep_chars && col > 0)
10480 screen_putchar('|', 0, col++, attr);
10481
10482 if (tp->tp_topframe != topframe)
10483 attr = attr_nosel;
10484
10485 screen_putchar(' ', 0, col++, attr);
10486
10487 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010488 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010489 cwp = curwin;
10490 wp = firstwin;
10491 }
10492 else
10493 {
10494 cwp = tp->tp_curwin;
10495 wp = tp->tp_firstwin;
10496 }
10497
10498 modified = FALSE;
10499 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10500 if (bufIsChanged(wp->w_buffer))
10501 modified = TRUE;
10502 if (modified || wincount > 1)
10503 {
10504 if (wincount > 1)
10505 {
10506 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010507 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010508 if (col + len >= Columns - 3)
10509 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010510 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010511#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010512 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010513#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010514 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010515#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010516 );
10517 col += len;
10518 }
10519 if (modified)
10520 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10521 screen_putchar(' ', 0, col++, attr);
10522 }
10523
10524 room = scol - col + tabwidth - 1;
10525 if (room > 0)
10526 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010527 /* Get buffer name in NameBuff[] */
10528 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010529 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010530 len = vim_strsize(NameBuff);
10531 p = NameBuff;
10532#ifdef FEAT_MBYTE
10533 if (has_mbyte)
10534 while (len > room)
10535 {
10536 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010537 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010538 }
10539 else
10540#endif
10541 if (len > room)
10542 {
10543 p += len - room;
10544 len = room;
10545 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010546 if (len > Columns - col - 1)
10547 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010548
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010549 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010550 col += len;
10551 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010552 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010553
10554 /* Store the tab page number in TabPageIdxs[], so that
10555 * jump_to_mouse() knows where each one is. */
10556 ++tabcount;
10557 while (scol < col)
10558 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010559 }
10560
Bram Moolenaar238a5642006-02-21 22:12:05 +000010561 if (use_sep_chars)
10562 c = '_';
10563 else
10564 c = ' ';
10565 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010566
10567 /* Put an "X" for closing the current tab if there are several. */
10568 if (first_tabpage->tp_next != NULL)
10569 {
10570 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10571 TabPageIdxs[Columns - 1] = -999;
10572 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010573 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010574
10575 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10576 * set. */
10577 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010578}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010579
10580/*
10581 * Get buffer name for "buf" into NameBuff[].
10582 * Takes care of special buffer names and translates special characters.
10583 */
10584 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010585get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010586{
10587 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010588 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010589 else
10590 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10591 trans_characters(NameBuff, MAXPATHL);
10592}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010593#endif
10594
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10596/*
10597 * Get the character to use in a status line. Get its attributes in "*attr".
10598 */
10599 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010600fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601{
10602 int fill;
10603 if (is_curwin)
10604 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010605 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 fill = fill_stl;
10607 }
10608 else
10609 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010610 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 fill = fill_stlnc;
10612 }
10613 /* Use fill when there is highlighting, and highlighting of current
10614 * window differs, or the fillchars differ, or this is not the
10615 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010616 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010617 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 || (fill_stl != fill_stlnc)))
10619 return fill;
10620 if (is_curwin)
10621 return '^';
10622 return '=';
10623}
10624#endif
10625
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010626#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627/*
10628 * Get the character to use in a separator between vertically split windows.
10629 * Get its attributes in "*attr".
10630 */
10631 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010632fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010634 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 if (*attr == 0 && fill_vert == ' ')
10636 return '|';
10637 else
10638 return fill_vert;
10639}
10640#endif
10641
10642/*
10643 * Return TRUE if redrawing should currently be done.
10644 */
10645 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010646redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010648#ifdef FEAT_EVAL
10649 if (disable_redraw_for_testing)
10650 return 0;
10651 else
10652#endif
10653 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10655}
10656
10657/*
10658 * Return TRUE if printing messages should currently be done.
10659 */
10660 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010661messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662{
10663 return (!(p_lz && char_avail() && !KeyTyped));
10664}
10665
10666/*
10667 * Show current status info in ruler and various other places
10668 * If always is FALSE, only show ruler if position has changed.
10669 */
10670 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010671showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672{
10673 if (!always && !redrawing())
10674 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010675#ifdef FEAT_INS_EXPAND
10676 if (pum_visible())
10677 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010678# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010679 /* Don't redraw right now, do it later. */
10680 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010681# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010682 return;
10683 }
10684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010686 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010687 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010688 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 else
10691#endif
10692#ifdef FEAT_CMDL_INFO
10693 win_redr_ruler(curwin, always);
10694#endif
10695
10696#ifdef FEAT_TITLE
10697 if (need_maketitle
10698# ifdef FEAT_STL_OPT
10699 || (p_icon && (stl_syntax & STL_IN_ICON))
10700 || (p_title && (stl_syntax & STL_IN_TITLE))
10701# endif
10702 )
10703 maketitle();
10704#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010705#ifdef FEAT_WINDOWS
10706 /* Redraw the tab pages line if needed. */
10707 if (redraw_tabline)
10708 draw_tabline();
10709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710}
10711
10712#ifdef FEAT_CMDL_INFO
10713 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010714win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010716#define RULER_BUF_LEN 70
10717 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 int row;
10719 int fillchar;
10720 int attr;
10721 int empty_line = FALSE;
10722 colnr_T virtcol;
10723 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010724 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010726#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 int this_ru_col;
10728 int off = 0;
10729 int width = Columns;
10730# define WITH_OFF(x) x
10731# define WITH_WIDTH(x) x
10732#else
10733# define WITH_OFF(x) 0
10734# define WITH_WIDTH(x) Columns
10735# define this_ru_col ru_col
10736#endif
10737
10738 /* If 'ruler' off or redrawing disabled, don't do anything */
10739 if (!p_ru)
10740 return;
10741
10742 /*
10743 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10744 * after deleting lines, before cursor.lnum is corrected.
10745 */
10746 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10747 return;
10748
10749#ifdef FEAT_INS_EXPAND
10750 /* Don't draw the ruler while doing insert-completion, it might overwrite
10751 * the (long) mode message. */
10752# ifdef FEAT_WINDOWS
10753 if (wp == lastwin && lastwin->w_status_height == 0)
10754# endif
10755 if (edit_submode != NULL)
10756 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010757 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10758 if (pum_visible())
10759 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760#endif
10761
10762#ifdef FEAT_STL_OPT
10763 if (*p_ruf)
10764 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010765 int save_called_emsg = called_emsg;
10766
10767 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010769 if (called_emsg)
10770 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010771 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010772 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 return;
10774 }
10775#endif
10776
10777 /*
10778 * Check if not in Insert mode and the line is empty (will show "0-1").
10779 */
10780 if (!(State & INSERT)
10781 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10782 empty_line = TRUE;
10783
10784 /*
10785 * Only draw the ruler when something changed.
10786 */
10787 validate_virtcol_win(wp);
10788 if ( redraw_cmdline
10789 || always
10790 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10791 || wp->w_cursor.col != wp->w_ru_cursor.col
10792 || wp->w_virtcol != wp->w_ru_virtcol
10793#ifdef FEAT_VIRTUALEDIT
10794 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10795#endif
10796 || wp->w_topline != wp->w_ru_topline
10797 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10798#ifdef FEAT_DIFF
10799 || wp->w_topfill != wp->w_ru_topfill
10800#endif
10801 || empty_line != wp->w_ru_empty)
10802 {
10803 cursor_off();
10804#ifdef FEAT_WINDOWS
10805 if (wp->w_status_height)
10806 {
10807 row = W_WINROW(wp) + wp->w_height;
10808 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 off = W_WINCOL(wp);
10810 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 }
10812 else
10813#endif
10814 {
10815 row = Rows - 1;
10816 fillchar = ' ';
10817 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010818#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 width = Columns;
10820 off = 0;
10821#endif
10822 }
10823
10824 /* In list mode virtcol needs to be recomputed */
10825 virtcol = wp->w_virtcol;
10826 if (wp->w_p_list && lcs_tab1 == NUL)
10827 {
10828 wp->w_p_list = FALSE;
10829 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10830 wp->w_p_list = TRUE;
10831 }
10832
10833 /*
10834 * Some sprintfs return the length, some return a pointer.
10835 * To avoid portability problems we use strlen() here.
10836 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010837 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10839 ? 0L
10840 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010841 len = STRLEN(buffer);
10842 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10844 (int)virtcol + 1);
10845
10846 /*
10847 * Add a "50%" if there is room for it.
10848 * On the last line, don't print in the last column (scrolls the
10849 * screen up on some terminals).
10850 */
10851 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010852 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 o = i + vim_strsize(buffer + i + 1);
10854#ifdef FEAT_WINDOWS
10855 if (wp->w_status_height == 0) /* can't use last char of screen */
10856#endif
10857 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010858#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 this_ru_col = ru_col - (Columns - width);
10860 if (this_ru_col < 0)
10861 this_ru_col = 0;
10862#endif
10863 /* Never use more than half the window/screen width, leave the other
10864 * half for the filename. */
10865 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10866 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10867 if (this_ru_col + o < WITH_WIDTH(width))
10868 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010869 /* need at least 3 chars left for get_rel_pos() + NUL */
10870 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 {
10872#ifdef FEAT_MBYTE
10873 if (has_mbyte)
10874 i += (*mb_char2bytes)(fillchar, buffer + i);
10875 else
10876#endif
10877 buffer[i++] = fillchar;
10878 ++o;
10879 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010880 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 }
10882 /* Truncate at window boundary. */
10883#ifdef FEAT_MBYTE
10884 if (has_mbyte)
10885 {
10886 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010887 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 {
10889 o += (*mb_ptr2cells)(buffer + i);
10890 if (this_ru_col + o > WITH_WIDTH(width))
10891 {
10892 buffer[i] = NUL;
10893 break;
10894 }
10895 }
10896 }
10897 else
10898#endif
10899 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10900 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10901
10902 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10903 i = redraw_cmdline;
10904 screen_fill(row, row + 1,
10905 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10906 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10907 fillchar, fillchar, attr);
10908 /* don't redraw the cmdline because of showing the ruler */
10909 redraw_cmdline = i;
10910 wp->w_ru_cursor = wp->w_cursor;
10911 wp->w_ru_virtcol = wp->w_virtcol;
10912 wp->w_ru_empty = empty_line;
10913 wp->w_ru_topline = wp->w_topline;
10914 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10915#ifdef FEAT_DIFF
10916 wp->w_ru_topfill = wp->w_topfill;
10917#endif
10918 }
10919}
10920#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010921
10922#if defined(FEAT_LINEBREAK) || defined(PROTO)
10923/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010924 * Return the width of the 'number' and 'relativenumber' column.
10925 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010926 * Otherwise it depends on 'numberwidth' and the line count.
10927 */
10928 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010929number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010930{
10931 int n;
10932 linenr_T lnum;
10933
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010934 if (wp->w_p_rnu && !wp->w_p_nu)
10935 /* cursor line shows "0" */
10936 lnum = wp->w_height;
10937 else
10938 /* cursor line shows absolute line number */
10939 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010940
Bram Moolenaar6b314672015-03-20 15:42:10 +010010941 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010942 return wp->w_nrwidth_width;
10943 wp->w_nrwidth_line_count = lnum;
10944
10945 n = 0;
10946 do
10947 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010948 lnum /= 10;
10949 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010950 } while (lnum > 0);
10951
10952 /* 'numberwidth' gives the minimal width plus one */
10953 if (n < wp->w_p_nuw - 1)
10954 n = wp->w_p_nuw - 1;
10955
10956 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010957 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010958 return n;
10959}
10960#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010961
10962/*
10963 * Return the current cursor column. This is the actual position on the
10964 * screen. First column is 0.
10965 */
10966 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010967screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010968{
10969 return screen_cur_col;
10970}
10971
10972/*
10973 * Return the current cursor row. This is the actual position on the screen.
10974 * First row is 0.
10975 */
10976 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010977screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010978{
10979 return screen_cur_row;
10980}