blob: d93ce50f3cbc1a0594a9fbe16eebbc78a58ad89f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
125static 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 +0000126#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
128static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
129static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200131static int win_line(win_T *, linenr_T, int, int, int nochange, proftime_T *syntax_tm);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200138# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void start_search_hl(void);
140static void end_search_hl(void);
141static void init_search_hl(win_T *wp);
142static void prepare_search_hl(win_T *wp, linenr_T lnum);
143static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
144static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void screen_start_highlight(int attr);
147static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200152static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void lineinvalid(unsigned off, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void linecopy(int to, int from, win_T *wp);
155static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200156static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void win_rest_invalid(win_T *wp);
158static void msg_pos_mode(void);
159static void recording_mode(int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void draw_tabline(void);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200161static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100162static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100167static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/* Ugly global: overrule attribute used by screen_char() */
171static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200173#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
174/* Can limit syntax highlight time to 'redrawtime'. */
175# define SYN_TIME_LIMIT 1
176#endif
177
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200178#ifdef FEAT_RIGHTLEFT
179# define HAS_RIGHTLEFT(x) x
180#else
181# define HAS_RIGHTLEFT(x) FALSE
182#endif
183
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * Redraw the current window later, with update_screen(type).
186 * Set must_redraw only if not already set to a higher value.
187 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
188 */
189 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100190redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191{
192 redraw_win_later(curwin, type);
193}
194
195 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100196redraw_win_later(
197 win_T *wp,
198 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
200 if (wp->w_redr_type < type)
201 {
202 wp->w_redr_type = type;
203 if (type >= NOT_VALID)
204 wp->w_lines_valid = 0;
205 if (must_redraw < type) /* must_redraw is the maximum of all windows */
206 must_redraw = type;
207 }
208}
209
210/*
211 * Force a complete redraw later. Also resets the highlighting. To be used
212 * after executing a shell command that messes up the screen.
213 */
214 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100215redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216{
217 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000218#ifdef FEAT_GUI
219 if (gui.in_use)
220 /* Use a code that will reset gui.highlight_mask in
221 * gui_stop_highlight(). */
222 screen_attr = HL_ALL + 1;
223 else
224#endif
225 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200226 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227}
228
229/*
230 * Mark all windows to be redrawn later.
231 */
232 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100233redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234{
235 win_T *wp;
236
237 FOR_ALL_WINDOWS(wp)
238 {
239 redraw_win_later(wp, type);
240 }
241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100247redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
249 redraw_buf_later(curbuf, type);
250}
251
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 win_T *wp;
256
257 FOR_ALL_WINDOWS(wp)
258 {
259 if (wp->w_buffer == buf)
260 redraw_win_later(wp, type);
261 }
262}
263
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200264 void
265redraw_buf_and_status_later(buf_T *buf, int type)
266{
267 win_T *wp;
268
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200269#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200270 if (wild_menu_showing != 0)
271 /* Don't redraw while the command line completion is displayed, it
272 * would disappear. */
273 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200274#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200275 FOR_ALL_WINDOWS(wp)
276 {
277 if (wp->w_buffer == buf)
278 {
279 redraw_win_later(wp, type);
280 wp->w_redr_status = TRUE;
281 }
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 Moolenaarcb8bbe92017-07-16 13:48:22 +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.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200434 * If "call_update_screen" is FALSE don't call update_screen() when at the
435 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100436 */
437 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200438redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200440 ++redrawing_for_callback;
441
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100442 if (State == HITRETURN || State == ASKMORE)
443 ; /* do nothing */
444 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200445 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200446 /* Redrawing only works when the screen didn't scroll. Don't clear
447 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200448 if (msg_scrolled == 0
449#ifdef FEAT_WILDMENU
450 && wild_menu_showing == 0
451#endif
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200452 && call_update_screen)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200453 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200454 /* Redraw in the same position, so that the user can continue
455 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200456 redrawcmdline_ex(FALSE);
457 }
Bram Moolenaar144445d2016-07-08 21:41:54 +0200458 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100459 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200460 /* keep the command line if possible */
461 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100462 setcursor();
463 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100464 cursor_on();
465 out_flush();
466#ifdef FEAT_GUI
467 if (gui.in_use)
468 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200469 /* Don't update the cursor when it is blinking and off to avoid
470 * flicker. */
471 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200472 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100473 gui_mch_flush();
474 }
475#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200476
477 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100478}
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.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200527 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200529 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200530update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200532 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 win_T *wp;
534 static int did_intro = FALSE;
535#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
536 int did_one;
537#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200538#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200539 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200540 int gui_cursor_col;
541 int gui_cursor_row;
542#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200543 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000545 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200547 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200549 if (type == VALID_NO_UPDATE)
550 {
551 no_update = TRUE;
552 type = 0;
553 }
554
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 if (must_redraw)
556 {
557 if (type < must_redraw) /* use maximal type */
558 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000559
560 /* must_redraw is reset here, so that when we run into some weird
561 * reason to redraw while busy redrawing (e.g., asynchronous
562 * scrolling), or update_topline() in win_update() will cause a
563 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 must_redraw = 0;
565 }
566
567 /* Need to update w_lines[]. */
568 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
569 type = NOT_VALID;
570
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000571 /* Postpone the redrawing when it's not needed and when being called
572 * recursively. */
573 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 {
575 redraw_later(type); /* remember type for next time */
576 must_redraw = type;
577 if (type > INVERTED_ALL)
578 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200579 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
581
582 updating_screen = TRUE;
583#ifdef FEAT_SYN_HL
584 ++display_tick; /* let syntax code know we're in a next round of
585 * display updating */
586#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200587 if (no_update)
588 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590 /*
591 * if the screen was scrolled up when displaying a message, scroll it down
592 */
593 if (msg_scrolled)
594 {
595 clear_cmdline = TRUE;
596 if (msg_scrolled > Rows - 5) /* clearing is faster */
597 type = CLEAR;
598 else if (type != CLEAR)
599 {
600 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200601 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
602 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 type = CLEAR;
604 FOR_ALL_WINDOWS(wp)
605 {
606 if (W_WINROW(wp) < msg_scrolled)
607 {
608 if (W_WINROW(wp) + wp->w_height > msg_scrolled
609 && wp->w_redr_type < REDRAW_TOP
610 && wp->w_lines_valid > 0
611 && wp->w_topline == wp->w_lines[0].wl_lnum)
612 {
613 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
614 wp->w_redr_type = REDRAW_TOP;
615 }
616 else
617 {
618 wp->w_redr_type = NOT_VALID;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
620 <= msg_scrolled)
621 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 }
623 }
624 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200625 if (!no_update)
626 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000627 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
629 msg_scrolled = 0;
630 need_wait_return = FALSE;
631 }
632
633 /* reset cmdline_row now (may have been changed temporarily) */
634 compute_cmdrow();
635
636 /* Check for changed highlighting */
637 if (need_highlight_changed)
638 highlight_changed();
639
640 if (type == CLEAR) /* first clear screen */
641 {
642 screenclear(); /* will reset clear_cmdline */
643 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200644 /* must_redraw may be set indirectly, avoid another redraw later */
645 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
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 /* Redraw the tab pages line if needed. */
683 if (redraw_tabline || type >= NOT_VALID)
684 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686#ifdef FEAT_SYN_HL
687 /*
688 * Correct stored syntax highlighting info for changes in each displayed
689 * buffer. Each buffer must only be done once.
690 */
691 FOR_ALL_WINDOWS(wp)
692 {
693 if (wp->w_buffer->b_mod_set)
694 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 win_T *wwp;
696
697 /* Check if we already did this buffer. */
698 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
699 if (wwp->w_buffer == wp->w_buffer)
700 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200701 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 syn_stack_apply_changes(wp->w_buffer);
703 }
704 }
705#endif
706
707 /*
708 * Go from top to bottom through the windows, redrawing the ones that need
709 * it.
710 */
711#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
712 did_one = FALSE;
713#endif
714#ifdef FEAT_SEARCH_EXTRA
715 search_hl.rm.regprog = NULL;
716#endif
717 FOR_ALL_WINDOWS(wp)
718 {
719 if (wp->w_redr_type != 0)
720 {
721 cursor_off();
722#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
723 if (!did_one)
724 {
725 did_one = TRUE;
726# ifdef FEAT_SEARCH_EXTRA
727 start_search_hl();
728# endif
729# ifdef FEAT_CLIPBOARD
730 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200731 if (clip_star.available && clip_isautosel_star())
732 clip_update_selection(&clip_star);
733 if (clip_plus.available && clip_isautosel_plus())
734 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735# endif
736#ifdef FEAT_GUI
737 /* Remove the cursor before starting to do anything, because
738 * scrolling may make it difficult to redraw the text under
739 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200740 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200741 {
742 gui_cursor_col = gui.cursor_col;
743 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200745 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747#endif
748 }
749#endif
750 win_update(wp);
751 }
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 /* redraw status line after the window to minimize cursor movement */
754 if (wp->w_redr_status)
755 {
756 cursor_off();
757 win_redr_status(wp);
758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 }
760#if defined(FEAT_SEARCH_EXTRA)
761 end_search_hl();
762#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100763#ifdef FEAT_INS_EXPAND
764 /* May need to redraw the popup menu. */
765 if (pum_visible())
766 pum_redraw();
767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 /* Reset b_mod_set flags. Going through all windows is probably faster
770 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200771 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
774 updating_screen = FALSE;
775#ifdef FEAT_GUI
776 gui_may_resize_shell();
777#endif
778
779 /* Clear or redraw the command line. Done last, because scrolling may
780 * mess up the command line. */
781 if (clear_cmdline || redraw_cmdline)
782 showmode();
783
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200784 if (no_update)
785 --no_win_do_lines_ins;
786
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200788 if (!did_intro)
789 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 did_intro = TRUE;
791
792#ifdef FEAT_GUI
793 /* Redraw the cursor and update the scrollbars when all screen updating is
794 * done. */
795 if (gui.in_use)
796 {
797 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200798 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200799 {
800 /* Put the GUI position where the cursor was, gui_update_cursor()
801 * uses that. */
802 gui.col = gui_cursor_col;
803 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200804# ifdef FEAT_MBYTE
805 gui.col = mb_fix_col(gui.col, gui.row);
806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200808 screen_cur_col = gui.col;
809 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 gui_update_scrollbars(FALSE);
812 }
813#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200814 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815}
816
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100817#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
818/*
819 * Prepare for updating one or more windows.
820 * Caller must check for "updating_screen" already set to avoid recursiveness.
821 */
822 static void
823update_prepare(void)
824{
825 cursor_off();
826 updating_screen = TRUE;
827#ifdef FEAT_GUI
828 /* Remove the cursor before starting to do anything, because scrolling may
829 * make it difficult to redraw the text under it. */
830 if (gui.in_use)
831 gui_undraw_cursor();
832#endif
833#ifdef FEAT_SEARCH_EXTRA
834 start_search_hl();
835#endif
836}
837
838/*
839 * Finish updating one or more windows.
840 */
841 static void
842update_finish(void)
843{
844 if (redraw_cmdline)
845 showmode();
846
847# ifdef FEAT_SEARCH_EXTRA
848 end_search_hl();
849# endif
850
851 updating_screen = FALSE;
852
853# ifdef FEAT_GUI
854 gui_may_resize_shell();
855
856 /* Redraw the cursor and update the scrollbars when all screen updating is
857 * done. */
858 if (gui.in_use)
859 {
860 out_flush(); /* required before updating the cursor */
861 gui_update_cursor(FALSE, FALSE);
862 gui_update_scrollbars(FALSE);
863 }
864# endif
865}
866#endif
867
Bram Moolenaar860cae12010-06-05 23:22:07 +0200868#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200869/*
870 * Return TRUE if the cursor line in window "wp" may be concealed, according
871 * to the 'concealcursor' option.
872 */
873 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100874conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200875{
876 int c;
877
878 if (*wp->w_p_cocu == NUL)
879 return FALSE;
880 if (get_real_state() & VISUAL)
881 c = 'v';
882 else if (State & INSERT)
883 c = 'i';
884 else if (State & NORMAL)
885 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200886 else if (State & CMDLINE)
887 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200888 else
889 return FALSE;
890 return vim_strchr(wp->w_p_cocu, c) != NULL;
891}
892
893/*
894 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
895 */
896 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100897conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200898{
899 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
900 {
901 need_cursor_line_redraw = TRUE;
902 /* Need to recompute cursor column, e.g., when starting Visual mode
903 * without concealing. */
904 curs_columns(TRUE);
905 }
906}
907
Bram Moolenaar860cae12010-06-05 23:22:07 +0200908 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100909update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200910{
911 int row;
912 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200913#ifdef SYN_TIME_LIMIT
914 proftime_T syntax_tm;
915#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200916
Bram Moolenaar908be432016-05-24 10:51:30 +0200917 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100918 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200919 return;
920
Bram Moolenaar860cae12010-06-05 23:22:07 +0200921 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200922 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200923 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200924#ifdef SYN_TIME_LIMIT
925 /* Set the time limit to 'redrawtime'. */
926 profile_setlimit(p_rdt, &syntax_tm);
927#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100928 update_prepare();
929
Bram Moolenaar860cae12010-06-05 23:22:07 +0200930 row = 0;
931 for (j = 0; j < wp->w_lines_valid; ++j)
932 {
933 if (lnum == wp->w_lines[j].wl_lnum)
934 {
935 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200936# ifdef FEAT_SEARCH_EXTRA
937 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200938 start_search_hl();
939 prepare_search_hl(wp, lnum);
940# endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200941 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE,
942#ifdef SYN_TIME_LIMIT
943 &syntax_tm
944#else
945 NULL
946#endif
947 );
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948# 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
Bram Moolenaar07920482017-08-01 20:53:30 +0200995 * happening (recursive call), messages on the screen or still starting up.
996 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100997 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200998 || State == ASKMORE || State == HITRETURN
999 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001000#ifdef FEAT_GUI
1001 || gui.starting
1002#endif
1003 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 return;
1005
1006 /* update all windows that need updating */
1007 update_prepare();
1008
Bram Moolenaar29323592016-07-24 22:04:11 +02001009 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {
1011 if (wp->w_redr_type != 0)
1012 win_update(wp);
1013 if (wp->w_redr_status)
1014 win_redr_status(wp);
1015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 update_finish();
1018}
1019#endif
1020
1021
1022#if defined(FEAT_GUI) || defined(PROTO)
1023/*
1024 * Update a single window, its status line and maybe the command line msg.
1025 * Used for the GUI scrollbar.
1026 */
1027 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001028updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001030 /* return if already busy updating */
1031 if (updating_screen)
1032 return;
1033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 update_prepare();
1035
1036#ifdef FEAT_CLIPBOARD
1037 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001038 if (clip_star.available && clip_isautosel_star())
1039 clip_update_selection(&clip_star);
1040 if (clip_plus.available && clip_isautosel_plus())
1041 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001043
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001045
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001046 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001047 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001048 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (wp->w_redr_status
1051# ifdef FEAT_CMDL_INFO
1052 || p_ru
1053# endif
1054# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001055 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056# endif
1057 )
1058 win_redr_status(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
1060 update_finish();
1061}
1062#endif
1063
1064/*
1065 * Update a single window.
1066 *
1067 * This may cause the windows below it also to be redrawn (when clearing the
1068 * screen or scrolling lines).
1069 *
1070 * How the window is redrawn depends on wp->w_redr_type. Each type also
1071 * implies the one below it.
1072 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001073 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1075 * INVERTED redraw the changed part of the Visual area
1076 * INVERTED_ALL redraw the whole Visual area
1077 * VALID 1. scroll up/down to adjust for a changed w_topline
1078 * 2. update lines at the top when scrolled down
1079 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001080 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 * b_mod_top and b_mod_bot.
1082 * - if wp->w_redraw_top non-zero, redraw lines between
1083 * wp->w_redraw_top and wp->w_redr_bot.
1084 * - continue redrawing when syntax status is invalid.
1085 * 4. if scrolled up, update lines at the bottom.
1086 * This results in three areas that may need updating:
1087 * top: from first row to top_end (when scrolled down)
1088 * mid: from mid_start to mid_end (update inversion or changed text)
1089 * bot: from bot_start to last row (when scrolled up)
1090 */
1091 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001092win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093{
1094 buf_T *buf = wp->w_buffer;
1095 int type;
1096 int top_end = 0; /* Below last row of the top area that needs
1097 updating. 0 when no top area updating. */
1098 int mid_start = 999;/* first row of the mid area that needs
1099 updating. 999 when no mid area updating. */
1100 int mid_end = 0; /* Below last row of the mid area that needs
1101 updating. 0 when no mid area updating. */
1102 int bot_start = 999;/* first row of the bot area that needs
1103 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 int scrolled_down = FALSE; /* TRUE when scrolled down when
1105 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001107 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 int top_to_mod = FALSE; /* redraw above mod_top */
1109#endif
1110
1111 int row; /* current window row to display */
1112 linenr_T lnum; /* current buffer lnum to display */
1113 int idx; /* current index in w_lines[] */
1114 int srow; /* starting row of the current line */
1115
1116 int eof = FALSE; /* if TRUE, we hit the end of the file */
1117 int didline = FALSE; /* if TRUE, we finished the last line */
1118 int i;
1119 long j;
1120 static int recursive = FALSE; /* being called recursively */
1121 int old_botline = wp->w_botline;
1122#ifdef FEAT_FOLDING
1123 long fold_count;
1124#endif
1125#ifdef FEAT_SYN_HL
1126 /* remember what happened to the previous line, to know if
1127 * check_visual_highlight() can be used */
1128#define DID_NONE 1 /* didn't update a line */
1129#define DID_LINE 2 /* updated a normal line */
1130#define DID_FOLD 3 /* updated a folded line */
1131 int did_update = DID_NONE;
1132 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1133#endif
1134 linenr_T mod_top = 0;
1135 linenr_T mod_bot = 0;
1136#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1137 int save_got_int;
1138#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001139#ifdef SYN_TIME_LIMIT
1140 proftime_T syntax_tm;
1141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
1143 type = wp->w_redr_type;
1144
1145 if (type == NOT_VALID)
1146 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 wp->w_lines_valid = 0;
1149 }
1150
1151 /* Window is zero-height: nothing to draw. */
1152 if (wp->w_height == 0)
1153 {
1154 wp->w_redr_type = 0;
1155 return;
1156 }
1157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 /* Window is zero-width: Only need to draw the separator. */
1159 if (wp->w_width == 0)
1160 {
1161 /* draw the vertical separator right of this window */
1162 draw_vsep_win(wp, 0);
1163 wp->w_redr_type = 0;
1164 return;
1165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001167#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001168 /* If this window contains a terminal, redraw works completely differently.
1169 */
1170 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001171 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001172 wp->w_redr_type = 0;
1173 return;
1174 }
1175#endif
1176
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001178 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#endif
1180
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001181#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001182 /* Force redraw when width of 'number' or 'relativenumber' column
1183 * changes. */
1184 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001185 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001186 {
1187 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001188 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001189 }
1190 else
1191#endif
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1194 {
1195 /*
1196 * When there are both inserted/deleted lines and specific lines to be
1197 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1198 * everything (only happens when redrawing is off for while).
1199 */
1200 type = NOT_VALID;
1201 }
1202 else
1203 {
1204 /*
1205 * Set mod_top to the first line that needs displaying because of
1206 * changes. Set mod_bot to the first line after the changes.
1207 */
1208 mod_top = wp->w_redraw_top;
1209 if (wp->w_redraw_bot != 0)
1210 mod_bot = wp->w_redraw_bot + 1;
1211 else
1212 mod_bot = 0;
1213 wp->w_redraw_top = 0; /* reset for next time */
1214 wp->w_redraw_bot = 0;
1215 if (buf->b_mod_set)
1216 {
1217 if (mod_top == 0 || mod_top > buf->b_mod_top)
1218 {
1219 mod_top = buf->b_mod_top;
1220#ifdef FEAT_SYN_HL
1221 /* Need to redraw lines above the change that may be included
1222 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001223 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001225 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (mod_top < 1)
1227 mod_top = 1;
1228 }
1229#endif
1230 }
1231 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1232 mod_bot = buf->b_mod_bot;
1233
1234#ifdef FEAT_SEARCH_EXTRA
1235 /* When 'hlsearch' is on and using a multi-line search pattern, a
1236 * change in one line may make the Search highlighting in a
1237 * previous line invalid. Simple solution: redraw all visible
1238 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001239 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001241 if (search_hl.rm.regprog != NULL
1242 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001244 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001245 {
1246 cur = wp->w_match_head;
1247 while (cur != NULL)
1248 {
1249 if (cur->match.regprog != NULL
1250 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001251 {
1252 top_to_mod = TRUE;
1253 break;
1254 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001255 cur = cur->next;
1256 }
1257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#endif
1259 }
1260#ifdef FEAT_FOLDING
1261 if (mod_top != 0 && hasAnyFolding(wp))
1262 {
1263 linenr_T lnumt, lnumb;
1264
1265 /*
1266 * A change in a line can cause lines above it to become folded or
1267 * unfolded. Find the top most buffer line that may be affected.
1268 * If the line was previously folded and displayed, get the first
1269 * line of that fold. If the line is folded now, get the first
1270 * folded line. Use the minimum of these two.
1271 */
1272
1273 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1274 * the line below it. If there is no valid entry, use w_topline.
1275 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1276 * to this line. If there is no valid entry, use MAXLNUM. */
1277 lnumt = wp->w_topline;
1278 lnumb = MAXLNUM;
1279 for (i = 0; i < wp->w_lines_valid; ++i)
1280 if (wp->w_lines[i].wl_valid)
1281 {
1282 if (wp->w_lines[i].wl_lastlnum < mod_top)
1283 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1284 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1285 {
1286 lnumb = wp->w_lines[i].wl_lnum;
1287 /* When there is a fold column it might need updating
1288 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001289 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 ++lnumb;
1291 }
1292 }
1293
1294 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1295 if (mod_top > lnumt)
1296 mod_top = lnumt;
1297
1298 /* Now do the same for the bottom line (one above mod_bot). */
1299 --mod_bot;
1300 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1301 ++mod_bot;
1302 if (mod_bot < lnumb)
1303 mod_bot = lnumb;
1304 }
1305#endif
1306
1307 /* When a change starts above w_topline and the end is below
1308 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001309 * If the end of the change is above w_topline: do like no change was
1310 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (mod_top != 0 && mod_top < wp->w_topline)
1312 {
1313 if (mod_bot > wp->w_topline)
1314 mod_top = wp->w_topline;
1315#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001316 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 top_end = 1;
1318#endif
1319 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001320
1321 /* When line numbers are displayed need to redraw all lines below
1322 * inserted/deleted lines. */
1323 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1324 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
1326
1327 /*
1328 * When only displaying the lines at the top, set top_end. Used when
1329 * window has scrolled down for msg_scrolled.
1330 */
1331 if (type == REDRAW_TOP)
1332 {
1333 j = 0;
1334 for (i = 0; i < wp->w_lines_valid; ++i)
1335 {
1336 j += wp->w_lines[i].wl_size;
1337 if (j >= wp->w_upd_rows)
1338 {
1339 top_end = j;
1340 break;
1341 }
1342 }
1343 if (top_end == 0)
1344 /* not found (cannot happen?): redraw everything */
1345 type = NOT_VALID;
1346 else
1347 /* top area defined, the rest is VALID */
1348 type = VALID;
1349 }
1350
Bram Moolenaar367329b2007-08-30 11:53:22 +00001351 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001352 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1353 * non-zero and thus not FALSE) will indicate that screenclear() was not
1354 * called. */
1355 if (screen_cleared)
1356 screen_cleared = MAYBE;
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 /*
1359 * If there are no changes on the screen that require a complete redraw,
1360 * handle three cases:
1361 * 1: we are off the top of the screen by a few lines: scroll down
1362 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1363 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1364 * w_lines[] that needs updating.
1365 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001366 if ((type == VALID || type == SOME_VALID
1367 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368#ifdef FEAT_DIFF
1369 && !wp->w_botfill && !wp->w_old_botfill
1370#endif
1371 )
1372 {
1373 if (mod_top != 0 && wp->w_topline == mod_top)
1374 {
1375 /*
1376 * w_topline is the first changed line, the scrolling will be done
1377 * further down.
1378 */
1379 }
1380 else if (wp->w_lines[0].wl_valid
1381 && (wp->w_topline < wp->w_lines[0].wl_lnum
1382#ifdef FEAT_DIFF
1383 || (wp->w_topline == wp->w_lines[0].wl_lnum
1384 && wp->w_topfill > wp->w_old_topfill)
1385#endif
1386 ))
1387 {
1388 /*
1389 * New topline is above old topline: May scroll down.
1390 */
1391#ifdef FEAT_FOLDING
1392 if (hasAnyFolding(wp))
1393 {
1394 linenr_T ln;
1395
1396 /* count the number of lines we are off, counting a sequence
1397 * of folded lines as one */
1398 j = 0;
1399 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1400 {
1401 ++j;
1402 if (j >= wp->w_height - 2)
1403 break;
1404 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1405 }
1406 }
1407 else
1408#endif
1409 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1410 if (j < wp->w_height - 2) /* not too far off */
1411 {
1412 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1413#ifdef FEAT_DIFF
1414 /* insert extra lines for previously invisible filler lines */
1415 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1416 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1417 - wp->w_old_topfill;
1418#endif
1419 if (i < wp->w_height - 2) /* less than a screen off */
1420 {
1421 /*
1422 * Try to insert the correct number of lines.
1423 * If not the last window, delete the lines at the bottom.
1424 * win_ins_lines may fail when the terminal can't do it.
1425 */
1426 if (i > 0)
1427 check_for_delay(FALSE);
1428 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1429 {
1430 if (wp->w_lines_valid != 0)
1431 {
1432 /* Need to update rows that are new, stop at the
1433 * first one that scrolled down. */
1434 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 /* Move the entries that were scrolled, disable
1438 * the entries for the lines to be redrawn. */
1439 if ((wp->w_lines_valid += j) > wp->w_height)
1440 wp->w_lines_valid = wp->w_height;
1441 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1442 wp->w_lines[idx] = wp->w_lines[idx - j];
1443 while (idx >= 0)
1444 wp->w_lines[idx--].wl_valid = FALSE;
1445 }
1446 }
1447 else
1448 mid_start = 0; /* redraw all lines */
1449 }
1450 else
1451 mid_start = 0; /* redraw all lines */
1452 }
1453 else
1454 mid_start = 0; /* redraw all lines */
1455 }
1456 else
1457 {
1458 /*
1459 * New topline is at or below old topline: May scroll up.
1460 * When topline didn't change, find first entry in w_lines[] that
1461 * needs updating.
1462 */
1463
1464 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1465 j = -1;
1466 row = 0;
1467 for (i = 0; i < wp->w_lines_valid; i++)
1468 {
1469 if (wp->w_lines[i].wl_valid
1470 && wp->w_lines[i].wl_lnum == wp->w_topline)
1471 {
1472 j = i;
1473 break;
1474 }
1475 row += wp->w_lines[i].wl_size;
1476 }
1477 if (j == -1)
1478 {
1479 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1480 * lines */
1481 mid_start = 0;
1482 }
1483 else
1484 {
1485 /*
1486 * Try to delete the correct number of lines.
1487 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1488 */
1489#ifdef FEAT_DIFF
1490 /* If the topline didn't change, delete old filler lines,
1491 * otherwise delete filler lines of the new topline... */
1492 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1493 row += wp->w_old_topfill;
1494 else
1495 row += diff_check_fill(wp, wp->w_topline);
1496 /* ... but don't delete new filler lines. */
1497 row -= wp->w_topfill;
1498#endif
1499 if (row > 0)
1500 {
1501 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001502 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1503 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 bot_start = wp->w_height - row;
1505 else
1506 mid_start = 0; /* redraw all lines */
1507 }
1508 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1509 {
1510 /*
1511 * Skip the lines (below the deleted lines) that are still
1512 * valid and don't need redrawing. Copy their info
1513 * upwards, to compensate for the deleted lines. Set
1514 * bot_start to the first row that needs redrawing.
1515 */
1516 bot_start = 0;
1517 idx = 0;
1518 for (;;)
1519 {
1520 wp->w_lines[idx] = wp->w_lines[j];
1521 /* stop at line that didn't fit, unless it is still
1522 * valid (no lines deleted) */
1523 if (row > 0 && bot_start + row
1524 + (int)wp->w_lines[j].wl_size > wp->w_height)
1525 {
1526 wp->w_lines_valid = idx + 1;
1527 break;
1528 }
1529 bot_start += wp->w_lines[idx++].wl_size;
1530
1531 /* stop at the last valid entry in w_lines[].wl_size */
1532 if (++j >= wp->w_lines_valid)
1533 {
1534 wp->w_lines_valid = idx;
1535 break;
1536 }
1537 }
1538#ifdef FEAT_DIFF
1539 /* Correct the first entry for filler lines at the top
1540 * when it won't get updated below. */
1541 if (wp->w_p_diff && bot_start > 0)
1542 wp->w_lines[0].wl_size =
1543 plines_win_nofill(wp, wp->w_topline, TRUE)
1544 + wp->w_topfill;
1545#endif
1546 }
1547 }
1548 }
1549
1550 /* When starting redraw in the first line, redraw all lines. When
1551 * there is only one window it's probably faster to clear the screen
1552 * first. */
1553 if (mid_start == 0)
1554 {
1555 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001556 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001557 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001558 /* Clear the screen when it was not done by win_del_lines() or
1559 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1560 * then. */
1561 if (screen_cleared != TRUE)
1562 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001563 /* The screen was cleared, redraw the tab pages line. */
1564 if (redraw_tabline)
1565 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001568
1569 /* When win_del_lines() or win_ins_lines() caused the screen to be
1570 * cleared (only happens for the first window) or when screenclear()
1571 * was called directly above, "must_redraw" will have been set to
1572 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1573 if (screen_cleared == TRUE)
1574 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 }
1576 else
1577 {
1578 /* Not VALID or INVERTED: redraw all lines. */
1579 mid_start = 0;
1580 mid_end = wp->w_height;
1581 }
1582
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001583 if (type == SOME_VALID)
1584 {
1585 /* SOME_VALID: redraw all lines. */
1586 mid_start = 0;
1587 mid_end = wp->w_height;
1588 type = NOT_VALID;
1589 }
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /* check if we are updating or removing the inverted part */
1592 if ((VIsual_active && buf == curwin->w_buffer)
1593 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1594 {
1595 linenr_T from, to;
1596
1597 if (VIsual_active)
1598 {
1599 if (VIsual_active
1600 && (VIsual_mode != wp->w_old_visual_mode
1601 || type == INVERTED_ALL))
1602 {
1603 /*
1604 * If the type of Visual selection changed, redraw the whole
1605 * selection. Also when the ownership of the X selection is
1606 * gained or lost.
1607 */
1608 if (curwin->w_cursor.lnum < VIsual.lnum)
1609 {
1610 from = curwin->w_cursor.lnum;
1611 to = VIsual.lnum;
1612 }
1613 else
1614 {
1615 from = VIsual.lnum;
1616 to = curwin->w_cursor.lnum;
1617 }
1618 /* redraw more when the cursor moved as well */
1619 if (wp->w_old_cursor_lnum < from)
1620 from = wp->w_old_cursor_lnum;
1621 if (wp->w_old_cursor_lnum > to)
1622 to = wp->w_old_cursor_lnum;
1623 if (wp->w_old_visual_lnum < from)
1624 from = wp->w_old_visual_lnum;
1625 if (wp->w_old_visual_lnum > to)
1626 to = wp->w_old_visual_lnum;
1627 }
1628 else
1629 {
1630 /*
1631 * Find the line numbers that need to be updated: The lines
1632 * between the old cursor position and the current cursor
1633 * position. Also check if the Visual position changed.
1634 */
1635 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1636 {
1637 from = curwin->w_cursor.lnum;
1638 to = wp->w_old_cursor_lnum;
1639 }
1640 else
1641 {
1642 from = wp->w_old_cursor_lnum;
1643 to = curwin->w_cursor.lnum;
1644 if (from == 0) /* Visual mode just started */
1645 from = to;
1646 }
1647
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001648 if (VIsual.lnum != wp->w_old_visual_lnum
1649 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
1651 if (wp->w_old_visual_lnum < from
1652 && wp->w_old_visual_lnum != 0)
1653 from = wp->w_old_visual_lnum;
1654 if (wp->w_old_visual_lnum > to)
1655 to = wp->w_old_visual_lnum;
1656 if (VIsual.lnum < from)
1657 from = VIsual.lnum;
1658 if (VIsual.lnum > to)
1659 to = VIsual.lnum;
1660 }
1661 }
1662
1663 /*
1664 * If in block mode and changed column or curwin->w_curswant:
1665 * update all lines.
1666 * First compute the actual start and end column.
1667 */
1668 if (VIsual_mode == Ctrl_V)
1669 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001670 colnr_T fromc, toc;
1671#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1672 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaar404406a2014-10-09 13:24:43 +02001674 if (curwin->w_p_lbr)
1675 ve_flags = VE_ALL;
1676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001678#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1679 ve_flags = save_ve_flags;
1680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 ++toc;
1682 if (curwin->w_curswant == MAXCOL)
1683 toc = MAXCOL;
1684
1685 if (fromc != wp->w_old_cursor_fcol
1686 || toc != wp->w_old_cursor_lcol)
1687 {
1688 if (from > VIsual.lnum)
1689 from = VIsual.lnum;
1690 if (to < VIsual.lnum)
1691 to = VIsual.lnum;
1692 }
1693 wp->w_old_cursor_fcol = fromc;
1694 wp->w_old_cursor_lcol = toc;
1695 }
1696 }
1697 else
1698 {
1699 /* Use the line numbers of the old Visual area. */
1700 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1701 {
1702 from = wp->w_old_cursor_lnum;
1703 to = wp->w_old_visual_lnum;
1704 }
1705 else
1706 {
1707 from = wp->w_old_visual_lnum;
1708 to = wp->w_old_cursor_lnum;
1709 }
1710 }
1711
1712 /*
1713 * There is no need to update lines above the top of the window.
1714 */
1715 if (from < wp->w_topline)
1716 from = wp->w_topline;
1717
1718 /*
1719 * If we know the value of w_botline, use it to restrict the update to
1720 * the lines that are visible in the window.
1721 */
1722 if (wp->w_valid & VALID_BOTLINE)
1723 {
1724 if (from >= wp->w_botline)
1725 from = wp->w_botline - 1;
1726 if (to >= wp->w_botline)
1727 to = wp->w_botline - 1;
1728 }
1729
1730 /*
1731 * Find the minimal part to be updated.
1732 * Watch out for scrolling that made entries in w_lines[] invalid.
1733 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1734 * top_end; need to redraw from top_end to the "to" line.
1735 * A middle mouse click with a Visual selection may change the text
1736 * above the Visual area and reset wl_valid, do count these for
1737 * mid_end (in srow).
1738 */
1739 if (mid_start > 0)
1740 {
1741 lnum = wp->w_topline;
1742 idx = 0;
1743 srow = 0;
1744 if (scrolled_down)
1745 mid_start = top_end;
1746 else
1747 mid_start = 0;
1748 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1749 {
1750 if (wp->w_lines[idx].wl_valid)
1751 mid_start += wp->w_lines[idx].wl_size;
1752 else if (!scrolled_down)
1753 srow += wp->w_lines[idx].wl_size;
1754 ++idx;
1755# ifdef FEAT_FOLDING
1756 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1757 lnum = wp->w_lines[idx].wl_lnum;
1758 else
1759# endif
1760 ++lnum;
1761 }
1762 srow += mid_start;
1763 mid_end = wp->w_height;
1764 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1765 {
1766 if (wp->w_lines[idx].wl_valid
1767 && wp->w_lines[idx].wl_lnum >= to + 1)
1768 {
1769 /* Only update until first row of this line */
1770 mid_end = srow;
1771 break;
1772 }
1773 srow += wp->w_lines[idx].wl_size;
1774 }
1775 }
1776 }
1777
1778 if (VIsual_active && buf == curwin->w_buffer)
1779 {
1780 wp->w_old_visual_mode = VIsual_mode;
1781 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1782 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001783 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 wp->w_old_curswant = curwin->w_curswant;
1785 }
1786 else
1787 {
1788 wp->w_old_visual_mode = 0;
1789 wp->w_old_cursor_lnum = 0;
1790 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001791 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1795 /* reset got_int, otherwise regexp won't work */
1796 save_got_int = got_int;
1797 got_int = 0;
1798#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001799#ifdef SYN_TIME_LIMIT
1800 /* Set the time limit to 'redrawtime'. */
1801 profile_setlimit(p_rdt, &syntax_tm);
1802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803#ifdef FEAT_FOLDING
1804 win_foldinfo.fi_level = 0;
1805#endif
1806
1807 /*
1808 * Update all the window rows.
1809 */
1810 idx = 0; /* first entry in w_lines[].wl_size */
1811 row = 0;
1812 srow = 0;
1813 lnum = wp->w_topline; /* first line shown in window */
1814 for (;;)
1815 {
1816 /* stop updating when reached the end of the window (check for _past_
1817 * the end of the window is at the end of the loop) */
1818 if (row == wp->w_height)
1819 {
1820 didline = TRUE;
1821 break;
1822 }
1823
1824 /* stop updating when hit the end of the file */
1825 if (lnum > buf->b_ml.ml_line_count)
1826 {
1827 eof = TRUE;
1828 break;
1829 }
1830
1831 /* Remember the starting row of the line that is going to be dealt
1832 * with. It is used further down when the line doesn't fit. */
1833 srow = row;
1834
1835 /*
1836 * Update a line when it is in an area that needs updating, when it
1837 * has changes or w_lines[idx] is invalid.
1838 * bot_start may be halfway a wrapped line after using
1839 * win_del_lines(), check if the current line includes it.
1840 * When syntax folding is being used, the saved syntax states will
1841 * already have been updated, we can't see where the syntax state is
1842 * the same again, just update until the end of the window.
1843 */
1844 if (row < top_end
1845 || (row >= mid_start && row < mid_end)
1846#ifdef FEAT_SEARCH_EXTRA
1847 || top_to_mod
1848#endif
1849 || idx >= wp->w_lines_valid
1850 || (row + wp->w_lines[idx].wl_size > bot_start)
1851 || (mod_top != 0
1852 && (lnum == mod_top
1853 || (lnum >= mod_top
1854 && (lnum < mod_bot
1855#ifdef FEAT_SYN_HL
1856 || did_update == DID_FOLD
1857 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001858 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 && (
1860# ifdef FEAT_FOLDING
1861 (foldmethodIsSyntax(wp)
1862 && hasAnyFolding(wp)) ||
1863# endif
1864 syntax_check_changed(lnum)))
1865#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001866#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001867 /* match in fixed position might need redraw
1868 * if lines were inserted or deleted */
1869 || (wp->w_match_head != NULL
1870 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 )))))
1873 {
1874#ifdef FEAT_SEARCH_EXTRA
1875 if (lnum == mod_top)
1876 top_to_mod = FALSE;
1877#endif
1878
1879 /*
1880 * When at start of changed lines: May scroll following lines
1881 * up or down to minimize redrawing.
1882 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001883 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 */
1885 if (lnum == mod_top
1886 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001887 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
1889 int old_rows = 0;
1890 int new_rows = 0;
1891 int xtra_rows;
1892 linenr_T l;
1893
1894 /* Count the old number of window rows, using w_lines[], which
1895 * should still contain the sizes for the lines as they are
1896 * currently displayed. */
1897 for (i = idx; i < wp->w_lines_valid; ++i)
1898 {
1899 /* Only valid lines have a meaningful wl_lnum. Invalid
1900 * lines are part of the changed area. */
1901 if (wp->w_lines[i].wl_valid
1902 && wp->w_lines[i].wl_lnum == mod_bot)
1903 break;
1904 old_rows += wp->w_lines[i].wl_size;
1905#ifdef FEAT_FOLDING
1906 if (wp->w_lines[i].wl_valid
1907 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1908 {
1909 /* Must have found the last valid entry above mod_bot.
1910 * Add following invalid entries. */
1911 ++i;
1912 while (i < wp->w_lines_valid
1913 && !wp->w_lines[i].wl_valid)
1914 old_rows += wp->w_lines[i++].wl_size;
1915 break;
1916 }
1917#endif
1918 }
1919
1920 if (i >= wp->w_lines_valid)
1921 {
1922 /* We can't find a valid line below the changed lines,
1923 * need to redraw until the end of the window.
1924 * Inserting/deleting lines has no use. */
1925 bot_start = 0;
1926 }
1927 else
1928 {
1929 /* Able to count old number of rows: Count new window
1930 * rows, and may insert/delete lines */
1931 j = idx;
1932 for (l = lnum; l < mod_bot; ++l)
1933 {
1934#ifdef FEAT_FOLDING
1935 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1936 ++new_rows;
1937 else
1938#endif
1939#ifdef FEAT_DIFF
1940 if (l == wp->w_topline)
1941 new_rows += plines_win_nofill(wp, l, TRUE)
1942 + wp->w_topfill;
1943 else
1944#endif
1945 new_rows += plines_win(wp, l, TRUE);
1946 ++j;
1947 if (new_rows > wp->w_height - row - 2)
1948 {
1949 /* it's getting too much, must redraw the rest */
1950 new_rows = 9999;
1951 break;
1952 }
1953 }
1954 xtra_rows = new_rows - old_rows;
1955 if (xtra_rows < 0)
1956 {
1957 /* May scroll text up. If there is not enough
1958 * remaining text or scrolling fails, must redraw the
1959 * rest. If scrolling works, must redraw the text
1960 * below the scrolled text. */
1961 if (row - xtra_rows >= wp->w_height - 2)
1962 mod_bot = MAXLNUM;
1963 else
1964 {
1965 check_for_delay(FALSE);
1966 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001967 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 mod_bot = MAXLNUM;
1969 else
1970 bot_start = wp->w_height + xtra_rows;
1971 }
1972 }
1973 else if (xtra_rows > 0)
1974 {
1975 /* May scroll text down. If there is not enough
1976 * remaining text of scrolling fails, must redraw the
1977 * rest. */
1978 if (row + xtra_rows >= wp->w_height - 2)
1979 mod_bot = MAXLNUM;
1980 else
1981 {
1982 check_for_delay(FALSE);
1983 if (win_ins_lines(wp, row + old_rows,
1984 xtra_rows, FALSE, FALSE) == FAIL)
1985 mod_bot = MAXLNUM;
1986 else if (top_end > row + old_rows)
1987 /* Scrolled the part at the top that requires
1988 * updating down. */
1989 top_end += xtra_rows;
1990 }
1991 }
1992
1993 /* When not updating the rest, may need to move w_lines[]
1994 * entries. */
1995 if (mod_bot != MAXLNUM && i != j)
1996 {
1997 if (j < i)
1998 {
1999 int x = row + new_rows;
2000
2001 /* move entries in w_lines[] upwards */
2002 for (;;)
2003 {
2004 /* stop at last valid entry in w_lines[] */
2005 if (i >= wp->w_lines_valid)
2006 {
2007 wp->w_lines_valid = j;
2008 break;
2009 }
2010 wp->w_lines[j] = wp->w_lines[i];
2011 /* stop at a line that won't fit */
2012 if (x + (int)wp->w_lines[j].wl_size
2013 > wp->w_height)
2014 {
2015 wp->w_lines_valid = j + 1;
2016 break;
2017 }
2018 x += wp->w_lines[j++].wl_size;
2019 ++i;
2020 }
2021 if (bot_start > x)
2022 bot_start = x;
2023 }
2024 else /* j > i */
2025 {
2026 /* move entries in w_lines[] downwards */
2027 j -= i;
2028 wp->w_lines_valid += j;
2029 if (wp->w_lines_valid > wp->w_height)
2030 wp->w_lines_valid = wp->w_height;
2031 for (i = wp->w_lines_valid; i - j >= idx; --i)
2032 wp->w_lines[i] = wp->w_lines[i - j];
2033
2034 /* The w_lines[] entries for inserted lines are
2035 * now invalid, but wl_size may be used above.
2036 * Reset to zero. */
2037 while (i >= idx)
2038 {
2039 wp->w_lines[i].wl_size = 0;
2040 wp->w_lines[i--].wl_valid = FALSE;
2041 }
2042 }
2043 }
2044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
2046
2047#ifdef FEAT_FOLDING
2048 /*
2049 * When lines are folded, display one line for all of them.
2050 * Otherwise, display normally (can be several display lines when
2051 * 'wrap' is on).
2052 */
2053 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2054 if (fold_count != 0)
2055 {
2056 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2057 ++row;
2058 --fold_count;
2059 wp->w_lines[idx].wl_folded = TRUE;
2060 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2061# ifdef FEAT_SYN_HL
2062 did_update = DID_FOLD;
2063# endif
2064 }
2065 else
2066#endif
2067 if (idx < wp->w_lines_valid
2068 && wp->w_lines[idx].wl_valid
2069 && wp->w_lines[idx].wl_lnum == lnum
2070 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002071 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 && srow + wp->w_lines[idx].wl_size > wp->w_height
2073#ifdef FEAT_DIFF
2074 && diff_check_fill(wp, lnum) == 0
2075#endif
2076 )
2077 {
2078 /* This line is not going to fit. Don't draw anything here,
2079 * will draw "@ " lines below. */
2080 row = wp->w_height + 1;
2081 }
2082 else
2083 {
2084#ifdef FEAT_SEARCH_EXTRA
2085 prepare_search_hl(wp, lnum);
2086#endif
2087#ifdef FEAT_SYN_HL
2088 /* Let the syntax stuff know we skipped a few lines. */
2089 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002090 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 syntax_end_parsing(syntax_last_parsed + 1);
2092#endif
2093
2094 /*
2095 * Display one line.
2096 */
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002097 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0,
2098#ifdef SYN_TIME_LIMIT
2099 &syntax_tm
2100#else
2101 NULL
2102#endif
2103 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105#ifdef FEAT_FOLDING
2106 wp->w_lines[idx].wl_folded = FALSE;
2107 wp->w_lines[idx].wl_lastlnum = lnum;
2108#endif
2109#ifdef FEAT_SYN_HL
2110 did_update = DID_LINE;
2111 syntax_last_parsed = lnum;
2112#endif
2113 }
2114
2115 wp->w_lines[idx].wl_lnum = lnum;
2116 wp->w_lines[idx].wl_valid = TRUE;
2117 if (row > wp->w_height) /* past end of screen */
2118 {
2119 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002120 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2122 ++idx;
2123 break;
2124 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002125 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 wp->w_lines[idx].wl_size = row - srow;
2127 ++idx;
2128#ifdef FEAT_FOLDING
2129 lnum += fold_count + 1;
2130#else
2131 ++lnum;
2132#endif
2133 }
2134 else
2135 {
2136 /* This line does not need updating, advance to the next one */
2137 row += wp->w_lines[idx++].wl_size;
2138 if (row > wp->w_height) /* past end of screen */
2139 break;
2140#ifdef FEAT_FOLDING
2141 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2142#else
2143 ++lnum;
2144#endif
2145#ifdef FEAT_SYN_HL
2146 did_update = DID_NONE;
2147#endif
2148 }
2149
2150 if (lnum > buf->b_ml.ml_line_count)
2151 {
2152 eof = TRUE;
2153 break;
2154 }
2155 }
2156 /*
2157 * End of loop over all window lines.
2158 */
2159
2160
2161 if (idx > wp->w_lines_valid)
2162 wp->w_lines_valid = idx;
2163
2164#ifdef FEAT_SYN_HL
2165 /*
2166 * Let the syntax stuff know we stop parsing here.
2167 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002168 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 syntax_end_parsing(syntax_last_parsed + 1);
2170#endif
2171
2172 /*
2173 * If we didn't hit the end of the file, and we didn't finish the last
2174 * line we were working on, then the line didn't fit.
2175 */
2176 wp->w_empty_rows = 0;
2177#ifdef FEAT_DIFF
2178 wp->w_filler_rows = 0;
2179#endif
2180 if (!eof && !didline)
2181 {
2182 if (lnum == wp->w_topline)
2183 {
2184 /*
2185 * Single line that does not fit!
2186 * Don't overwrite it, it can be edited.
2187 */
2188 wp->w_botline = lnum + 1;
2189 }
2190#ifdef FEAT_DIFF
2191 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2192 {
2193 /* Window ends in filler lines. */
2194 wp->w_botline = lnum;
2195 wp->w_filler_rows = wp->w_height - srow;
2196 }
2197#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002198 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2199 {
2200 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2201
2202 /*
2203 * Last line isn't finished: Display "@@@" in the last screen line.
2204 */
2205 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002206 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002207 screen_fill(scr_row, scr_row + 1,
2208 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002209 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002210 set_empty_rows(wp, srow);
2211 wp->w_botline = lnum;
2212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2214 {
2215 /*
2216 * Last line isn't finished: Display "@@@" at the end.
2217 */
2218 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2219 W_WINROW(wp) + wp->w_height,
2220 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002221 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 set_empty_rows(wp, srow);
2223 wp->w_botline = lnum;
2224 }
2225 else
2226 {
2227 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2228 wp->w_botline = lnum;
2229 }
2230 }
2231 else
2232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 if (eof) /* we hit the end of the file */
2235 {
2236 wp->w_botline = buf->b_ml.ml_line_count + 1;
2237#ifdef FEAT_DIFF
2238 j = diff_check_fill(wp, wp->w_botline);
2239 if (j > 0 && !wp->w_botfill)
2240 {
2241 /*
2242 * Display filler lines at the end of the file
2243 */
2244 if (char2cells(fill_diff) > 1)
2245 i = '-';
2246 else
2247 i = fill_diff;
2248 if (row + j > wp->w_height)
2249 j = wp->w_height - row;
2250 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2251 row += j;
2252 }
2253#endif
2254 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002255 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 wp->w_botline = lnum;
2257
2258 /* make sure the rest of the screen is blank */
2259 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002260 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
2262
2263 /* Reset the type of redrawing required, the window has been updated. */
2264 wp->w_redr_type = 0;
2265#ifdef FEAT_DIFF
2266 wp->w_old_topfill = wp->w_topfill;
2267 wp->w_old_botfill = wp->w_botfill;
2268#endif
2269
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002270 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
2272 /*
2273 * There is a trick with w_botline. If we invalidate it on each
2274 * change that might modify it, this will cause a lot of expensive
2275 * calls to plines() in update_topline() each time. Therefore the
2276 * value of w_botline is often approximated, and this value is used to
2277 * compute the value of w_topline. If the value of w_botline was
2278 * wrong, check that the value of w_topline is correct (cursor is on
2279 * the visible part of the text). If it's not, we need to redraw
2280 * again. Mostly this just means scrolling up a few lines, so it
2281 * doesn't look too bad. Only do this for the current window (where
2282 * changes are relevant).
2283 */
2284 wp->w_valid |= VALID_BOTLINE;
2285 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2286 {
2287 recursive = TRUE;
2288 curwin->w_valid &= ~VALID_TOPLINE;
2289 update_topline(); /* may invalidate w_botline again */
2290 if (must_redraw != 0)
2291 {
2292 /* Don't update for changes in buffer again. */
2293 i = curbuf->b_mod_set;
2294 curbuf->b_mod_set = FALSE;
2295 win_update(curwin);
2296 must_redraw = 0;
2297 curbuf->b_mod_set = i;
2298 }
2299 recursive = FALSE;
2300 }
2301 }
2302
2303#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2304 /* restore got_int, unless CTRL-C was hit while redrawing */
2305 if (!got_int)
2306 got_int = save_got_int;
2307#endif
2308}
2309
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310/*
2311 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2312 * as the filler character.
2313 */
2314 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002315win_draw_end(
2316 win_T *wp,
2317 int c1,
2318 int c2,
2319 int row,
2320 int endrow,
2321 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
2323#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2324 int n = 0;
2325# define FDC_OFF n
2326#else
2327# define FDC_OFF 0
2328#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002329#ifdef FEAT_FOLDING
2330 int fdc = compute_foldcolumn(wp, 0);
2331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332
2333#ifdef FEAT_RIGHTLEFT
2334 if (wp->w_p_rl)
2335 {
2336 /* No check for cmdline window: should never be right-left. */
2337# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002338 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
2340 if (n > 0)
2341 {
2342 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002343 if (n > W_WIDTH(wp))
2344 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2346 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002347 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 }
2349# endif
2350# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002351 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
2353 int nn = n + 2;
2354
2355 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002356 if (nn > W_WIDTH(wp))
2357 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2359 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002360 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 n = nn;
2362 }
2363# endif
2364 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2365 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002366 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2368 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002369 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
2371 else
2372#endif
2373 {
2374#ifdef FEAT_CMDWIN
2375 if (cmdwin_type != 0 && wp == curwin)
2376 {
2377 /* draw the cmdline character in the leftmost column */
2378 n = 1;
2379 if (n > wp->w_width)
2380 n = wp->w_width;
2381 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2382 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002383 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 }
2385#endif
2386#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002387 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002389 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
2391 /* draw the fold column at the left */
2392 if (nn > W_WIDTH(wp))
2393 nn = W_WIDTH(wp);
2394 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2395 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002396 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 n = nn;
2398 }
2399#endif
2400#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002401 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
2403 int nn = n + 2;
2404
2405 /* draw the sign column after the fold column */
2406 if (nn > W_WIDTH(wp))
2407 nn = W_WIDTH(wp);
2408 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2409 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002410 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 n = nn;
2412 }
2413#endif
2414 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2415 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002416 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
2418 set_empty_rows(wp, row);
2419}
2420
Bram Moolenaar1a384422010-07-14 19:53:30 +02002421#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002422static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002423
2424/*
2425 * Advance **color_cols and return TRUE when there are columns to draw.
2426 */
2427 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002428advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002429{
2430 while (**color_cols >= 0 && vcol > **color_cols)
2431 ++*color_cols;
2432 return (**color_cols >= 0);
2433}
2434#endif
2435
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#ifdef FEAT_FOLDING
2437/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002438 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2439 * space is available for window "wp", minus "col".
2440 */
2441 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002442compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002443{
2444 int fdc = wp->w_p_fdc;
2445 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2446 int wwidth = W_WIDTH(wp);
2447
2448 if (fdc > wwidth - (col + wmw))
2449 fdc = wwidth - (col + wmw);
2450 return fdc;
2451}
2452
2453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 * Display one folded line.
2455 */
2456 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002457fold_line(
2458 win_T *wp,
2459 long fold_count,
2460 foldinfo_T *foldinfo,
2461 linenr_T lnum,
2462 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002464 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 pos_T *top, *bot;
2466 linenr_T lnume = lnum + fold_count - 1;
2467 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002468 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 int col;
2471 int txtcol;
2472 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002473 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 /* Build the fold line:
2476 * 1. Add the cmdwin_type for the command-line window
2477 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002478 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 * 4. Compose the text
2480 * 5. Add the text
2481 * 6. set highlighting for the Visual area an other text
2482 */
2483 col = 0;
2484
2485 /*
2486 * 1. Add the cmdwin_type for the command-line window
2487 * Ignores 'rightleft', this window is never right-left.
2488 */
2489#ifdef FEAT_CMDWIN
2490 if (cmdwin_type != 0 && wp == curwin)
2491 {
2492 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002493 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494#ifdef FEAT_MBYTE
2495 if (enc_utf8)
2496 ScreenLinesUC[off] = 0;
2497#endif
2498 ++col;
2499 }
2500#endif
2501
2502 /*
2503 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002504 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002506 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 if (fdc > 0)
2508 {
2509 fill_foldcolumn(buf, wp, TRUE, lnum);
2510#ifdef FEAT_RIGHTLEFT
2511 if (wp->w_p_rl)
2512 {
2513 int i;
2514
2515 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002516 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 /* reverse the fold column */
2518 for (i = 0; i < fdc; ++i)
2519 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2520 }
2521 else
2522#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002523 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 col += fdc;
2525 }
2526
2527#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002528# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2529 for (ri = 0; ri < l; ++ri) \
2530 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2531 else \
2532 for (ri = 0; ri < l; ++ri) \
2533 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002535# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2536 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537#endif
2538
Bram Moolenaar64486672010-05-16 15:46:46 +02002539 /* Set all attributes of the 'number' or 'relativenumber' column and the
2540 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002541 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
2543#ifdef FEAT_SIGNS
2544 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002545 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
2547 len = W_WIDTH(wp) - col;
2548 if (len > 0)
2549 {
2550 if (len > 2)
2551 len = 2;
2552# ifdef FEAT_RIGHTLEFT
2553 if (wp->w_p_rl)
2554 /* the line number isn't reversed */
2555 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002556 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 else
2558# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002559 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 col += len;
2561 }
2562 }
2563#endif
2564
2565 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002566 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002568 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
2570 len = W_WIDTH(wp) - col;
2571 if (len > 0)
2572 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002573 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002574 long num;
2575 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002576
2577 if (len > w + 1)
2578 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002579
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002580 if (wp->w_p_nu && !wp->w_p_rnu)
2581 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002582 num = (long)lnum;
2583 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002584 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002585 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002586 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002587 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002588 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002589 /* 'number' + 'relativenumber': cursor line shows absolute
2590 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002591 num = lnum;
2592 fmt = "%-*ld ";
2593 }
2594 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002595
Bram Moolenaar700e7342013-01-30 12:31:36 +01002596 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597#ifdef FEAT_RIGHTLEFT
2598 if (wp->w_p_rl)
2599 /* the line number isn't reversed */
2600 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002601 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 else
2603#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002604 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 col += len;
2606 }
2607 }
2608
2609 /*
2610 * 4. Compose the folded-line string with 'foldtext', if set.
2611 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002612 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613
2614 txtcol = col; /* remember where text starts */
2615
2616 /*
2617 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2618 * Right-left text is put in columns 0 - number-col, normal text is put
2619 * in columns number-col - window-width.
2620 */
2621#ifdef FEAT_MBYTE
2622 if (has_mbyte)
2623 {
2624 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002625 int u8c, u8cc[MAX_MCO];
2626 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 int idx;
2628 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002629 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630# ifdef FEAT_ARABIC
2631 int prev_c = 0; /* previous Arabic character */
2632 int prev_c1 = 0; /* first composing char for prev_c */
2633# endif
2634
2635# ifdef FEAT_RIGHTLEFT
2636 if (wp->w_p_rl)
2637 idx = off;
2638 else
2639# endif
2640 idx = off + col;
2641
2642 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2643 for (p = text; *p != NUL; )
2644 {
2645 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002646 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 if (col + cells > W_WIDTH(wp)
2648# ifdef FEAT_RIGHTLEFT
2649 - (wp->w_p_rl ? col : 0)
2650# endif
2651 )
2652 break;
2653 ScreenLines[idx] = *p;
2654 if (enc_utf8)
2655 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002656 u8c = utfc_ptr2char(p, u8cc);
2657 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
2659 ScreenLinesUC[idx] = 0;
2660#ifdef FEAT_ARABIC
2661 prev_c = u8c;
2662#endif
2663 }
2664 else
2665 {
2666#ifdef FEAT_ARABIC
2667 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2668 {
2669 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002670 int pc, pc1, nc;
2671 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 int firstbyte = *p;
2673
2674 /* The idea of what is the previous and next
2675 * character depends on 'rightleft'. */
2676 if (wp->w_p_rl)
2677 {
2678 pc = prev_c;
2679 pc1 = prev_c1;
2680 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002681 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 }
2683 else
2684 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002685 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002687 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689 prev_c = u8c;
2690
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002691 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 pc, pc1, nc);
2693 ScreenLines[idx] = firstbyte;
2694 }
2695 else
2696 prev_c = u8c;
2697#endif
2698 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002699#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 if (u8c >= 0x10000)
2701 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2702 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002705 for (i = 0; i < Screen_mco; ++i)
2706 {
2707 ScreenLinesC[i][idx] = u8cc[i];
2708 if (u8cc[i] == 0)
2709 break;
2710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
2712 if (cells > 1)
2713 ScreenLines[idx + 1] = 0;
2714 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002715 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2716 /* double-byte single width character */
2717 ScreenLines2[idx] = p[1];
2718 else if (cells > 1)
2719 /* double-width character */
2720 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 col += cells;
2722 idx += cells;
2723 p += c_len;
2724 }
2725 }
2726 else
2727#endif
2728 {
2729 len = (int)STRLEN(text);
2730 if (len > W_WIDTH(wp) - col)
2731 len = W_WIDTH(wp) - col;
2732 if (len > 0)
2733 {
2734#ifdef FEAT_RIGHTLEFT
2735 if (wp->w_p_rl)
2736 STRNCPY(current_ScreenLine, text, len);
2737 else
2738#endif
2739 STRNCPY(current_ScreenLine + col, text, len);
2740 col += len;
2741 }
2742 }
2743
2744 /* Fill the rest of the line with the fold filler */
2745#ifdef FEAT_RIGHTLEFT
2746 if (wp->w_p_rl)
2747 col -= txtcol;
2748#endif
2749 while (col < W_WIDTH(wp)
2750#ifdef FEAT_RIGHTLEFT
2751 - (wp->w_p_rl ? txtcol : 0)
2752#endif
2753 )
2754 {
2755#ifdef FEAT_MBYTE
2756 if (enc_utf8)
2757 {
2758 if (fill_fold >= 0x80)
2759 {
2760 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002761 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002762 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002765 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002767 ScreenLines[off + col] = fill_fold;
2768 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002769 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002771 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002773 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775
2776 if (text != buf)
2777 vim_free(text);
2778
2779 /*
2780 * 6. set highlighting for the Visual area an other text.
2781 * If all folded lines are in the Visual area, highlight the line.
2782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2784 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002785 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
2787 /* Visual is after curwin->w_cursor */
2788 top = &curwin->w_cursor;
2789 bot = &VIsual;
2790 }
2791 else
2792 {
2793 /* Visual is before curwin->w_cursor */
2794 top = &VIsual;
2795 bot = &curwin->w_cursor;
2796 }
2797 if (lnum >= top->lnum
2798 && lnume <= bot->lnum
2799 && (VIsual_mode != 'v'
2800 || ((lnum > top->lnum
2801 || (lnum == top->lnum
2802 && top->col == 0))
2803 && (lnume < bot->lnum
2804 || (lnume == bot->lnum
2805 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002806 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 {
2808 if (VIsual_mode == Ctrl_V)
2809 {
2810 /* Visual block mode: highlight the chars part of the block */
2811 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2812 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002813 if (wp->w_old_cursor_lcol != MAXCOL
2814 && wp->w_old_cursor_lcol + txtcol
2815 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 len = wp->w_old_cursor_lcol;
2817 else
2818 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002819 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002820 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
2822 }
2823 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002826 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
2829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002831#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002832 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2833 if (wp->w_p_cc_cols)
2834 {
2835 int i = 0;
2836 int j = wp->w_p_cc_cols[i];
2837 int old_txtcol = txtcol;
2838
2839 while (j > -1)
2840 {
2841 txtcol += j;
2842 if (wp->w_p_wrap)
2843 txtcol -= wp->w_skipcol;
2844 else
2845 txtcol -= wp->w_leftcol;
2846 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2847 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002848 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002849 txtcol = old_txtcol;
2850 j = wp->w_p_cc_cols[++i];
2851 }
2852 }
2853
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002854 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002855 if (wp->w_p_cuc)
2856 {
2857 txtcol += wp->w_virtcol;
2858 if (wp->w_p_wrap)
2859 txtcol -= wp->w_skipcol;
2860 else
2861 txtcol -= wp->w_leftcol;
2862 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2863 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002864 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002865 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002868 screen_line(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 (int)W_WIDTH(wp), FALSE);
2870
2871 /*
2872 * Update w_cline_height and w_cline_folded if the cursor line was
2873 * updated (saves a call to plines() later).
2874 */
2875 if (wp == curwin
2876 && lnum <= curwin->w_cursor.lnum
2877 && lnume >= curwin->w_cursor.lnum)
2878 {
2879 curwin->w_cline_row = row;
2880 curwin->w_cline_height = 1;
2881 curwin->w_cline_folded = TRUE;
2882 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2883 }
2884}
2885
2886/*
2887 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2888 */
2889 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002890copy_text_attr(
2891 int off,
2892 char_u *buf,
2893 int len,
2894 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002896 int i;
2897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 mch_memmove(ScreenLines + off, buf, (size_t)len);
2899# ifdef FEAT_MBYTE
2900 if (enc_utf8)
2901 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2902# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002903 for (i = 0; i < len; ++i)
2904 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905}
2906
2907/*
2908 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002909 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 */
2911 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002912fill_foldcolumn(
2913 char_u *p,
2914 win_T *wp,
2915 int closed, /* TRUE of FALSE */
2916 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917{
2918 int i = 0;
2919 int level;
2920 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002921 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002922 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923
2924 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002925 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
2927 level = win_foldinfo.fi_level;
2928 if (level > 0)
2929 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002930 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002931 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002932
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 /* If the column is too narrow, we start at the lowest level that
2934 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002935 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 if (first_level < 1)
2937 first_level = 1;
2938
Bram Moolenaar1c934292015-01-27 16:39:29 +01002939 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
2941 if (win_foldinfo.fi_lnum == lnum
2942 && first_level + i >= win_foldinfo.fi_low_level)
2943 p[i] = '-';
2944 else if (first_level == 1)
2945 p[i] = '|';
2946 else if (first_level + i <= 9)
2947 p[i] = '0' + first_level + i;
2948 else
2949 p[i] = '>';
2950 if (first_level + i == level)
2951 break;
2952 }
2953 }
2954 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002955 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956}
2957#endif /* FEAT_FOLDING */
2958
2959/*
2960 * Display line "lnum" of window 'wp' on the screen.
2961 * Start at row "startrow", stop when "endrow" is reached.
2962 * wp->w_virtcol needs to be valid.
2963 *
2964 * Return the number of last row the line occupies.
2965 */
2966 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002967win_line(
2968 win_T *wp,
2969 linenr_T lnum,
2970 int startrow,
2971 int endrow,
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002972 int nochange UNUSED, /* not updating for changed text */
Bram Moolenaar02e177d2017-08-26 23:43:28 +02002973 proftime_T *syntax_tm UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01002975 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2977 int c = 0; /* init for GCC */
2978 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002979#ifdef FEAT_LINEBREAK
2980 long vcol_sbr = -1; /* virtual column after showbreak */
2981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 long vcol_prev = -1; /* "vcol" of previous character */
2983 char_u *line; /* current line */
2984 char_u *ptr; /* current position in "line" */
2985 int row; /* row in the window, excl w_winrow */
2986 int screen_row; /* row on the screen, incl w_winrow */
2987
2988 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2989 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002990 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002991 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 int c_extra = NUL; /* extra chars, all the same */
2993 int extra_attr = 0; /* attributes when n_extra != 0 */
2994 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2995 displaying lcs_eol at end-of-line */
2996 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2997 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2998
2999 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3000 int saved_n_extra = 0;
3001 char_u *saved_p_extra = NULL;
3002 int saved_c_extra = 0;
3003 int saved_char_attr = 0;
3004
3005 int n_attr = 0; /* chars with special attr */
3006 int saved_attr2 = 0; /* char_attr saved for n_attr */
3007 int n_attr3 = 0; /* chars with overruling special attr */
3008 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3009
3010 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3011
3012 int fromcol, tocol; /* start/end of inverting */
3013 int fromcol_prev = -2; /* start of inverting after cursor */
3014 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003016 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 pos_T pos;
3018 long v;
3019
3020 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003021 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3023 in this line */
3024 int attr = 0; /* attributes for area highlighting */
3025 int area_attr = 0; /* attributes desired by highlighting */
3026 int search_attr = 0; /* attributes desired by 'hlsearch' */
3027#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003028 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 int syntax_attr = 0; /* attributes desired by syntax */
3030 int has_syntax = FALSE; /* this buffer has syntax highl. */
3031 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003032 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003033 int draw_color_col = FALSE; /* highlight colorcolumn */
3034 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003035#endif
3036#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003037 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003038# define SPWORDLEN 150
3039 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003040 int nextlinecol = 0; /* column where nextline[] starts */
3041 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003042 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003043 int spell_attr = 0; /* attributes desired by spelling */
3044 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003045 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3046 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003047 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003048 static int cap_col = -1; /* column to check for Cap word */
3049 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003050 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051#endif
3052 int extra_check; /* has syntax or linebreak */
3053#ifdef FEAT_MBYTE
3054 int multi_attr = 0; /* attributes desired by multibyte */
3055 int mb_l = 1; /* multi-byte byte length */
3056 int mb_c = 0; /* decoded multi-byte character */
3057 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003058 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059#endif
3060#ifdef FEAT_DIFF
3061 int filler_lines; /* nr of filler lines to be drawn */
3062 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003063 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 int change_start = MAXCOL; /* first col of changed area */
3065 int change_end = -1; /* last col of changed area */
3066#endif
3067 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3068#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003069 int need_showbreak = FALSE; /* overlong line, skipping first x
3070 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003072#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003073 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003075 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076#endif
3077#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003078 matchitem_T *cur; /* points to the match list */
3079 match_T *shl; /* points to search_hl or a match */
3080 int shl_flag; /* flag to indicate whether search_hl
3081 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003082 int pos_inprogress; /* marks that position match search is
3083 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003084 int prevcol_hl_flag; /* flag to indicate whether prevcol
3085 equals startcol of search_hl or one
3086 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087#endif
3088#ifdef FEAT_ARABIC
3089 int prev_c = 0; /* previous Arabic character */
3090 int prev_c1 = 0; /* first composing char for prev_c */
3091#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003092#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003093 int did_line_attr = 0;
3094#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003095#ifdef FEAT_TERMINAL
3096 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003097 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
3100 /* draw_state: items that are drawn in sequence: */
3101#define WL_START 0 /* nothing done yet */
3102#ifdef FEAT_CMDWIN
3103# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3104#else
3105# define WL_CMDLINE WL_START
3106#endif
3107#ifdef FEAT_FOLDING
3108# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3109#else
3110# define WL_FOLD WL_CMDLINE
3111#endif
3112#ifdef FEAT_SIGNS
3113# define WL_SIGN WL_FOLD + 1 /* column for signs */
3114#else
3115# define WL_SIGN WL_FOLD /* column for signs */
3116#endif
3117#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003118#ifdef FEAT_LINEBREAK
3119# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003121# define WL_BRI WL_NR
3122#endif
3123#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3124# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3125#else
3126# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#endif
3128#define WL_LINE WL_SBR + 1 /* text in the line */
3129 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003130#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 int feedback_col = 0;
3132 int feedback_old_attr = -1;
3133#endif
3134
Bram Moolenaar860cae12010-06-05 23:22:07 +02003135#ifdef FEAT_CONCEAL
3136 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003137 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003138 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003139 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003140 int is_concealing = FALSE;
3141 int boguscols = 0; /* nonexistent columns added to force
3142 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003143 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003144 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003145 int match_conc = 0; /* cchar for match functions */
3146 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003147 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003148# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003149# define FIX_FOR_BOGUSCOLS \
3150 { \
3151 n_extra += vcol_off; \
3152 vcol -= vcol_off; \
3153 vcol_off = 0; \
3154 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003155 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003156 boguscols = 0; \
3157 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003158#else
3159# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
3162 if (startrow > endrow) /* past the end already! */
3163 return startrow;
3164
3165 row = startrow;
3166 screen_row = row + W_WINROW(wp);
3167
3168 /*
3169 * To speed up the loop below, set extra_check when there is linebreak,
3170 * trailing white space and/or syntax processing to be done.
3171 */
3172#ifdef FEAT_LINEBREAK
3173 extra_check = wp->w_p_lbr;
3174#else
3175 extra_check = 0;
3176#endif
3177#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003178 if (syntax_present(wp) && !wp->w_s->b_syn_error
3179# ifdef SYN_TIME_LIMIT
3180 && !wp->w_s->b_syn_slow
3181# endif
3182 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
3184 /* Prepare for syntax highlighting in this line. When there is an
3185 * error, stop syntax highlighting. */
3186 save_did_emsg = did_emsg;
3187 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003188 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003190 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 else
3192 {
3193 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003194#ifdef SYN_TIME_LIMIT
3195 if (!wp->w_s->b_syn_slow)
3196#endif
3197 {
3198 has_syntax = TRUE;
3199 extra_check = TRUE;
3200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 }
3202 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003203
3204 /* Check for columns to display for 'colorcolumn'. */
3205 color_cols = wp->w_p_cc_cols;
3206 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003207 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003208#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003209
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003210#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003211 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003212 {
3213 extra_check = TRUE;
3214 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003215 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003216 }
3217#endif
3218
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003219#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003220 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003221 && *wp->w_s->b_p_spl != NUL
3222 && wp->w_s->b_langp.ga_len > 0
3223 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003224 {
3225 /* Prepare for spell checking. */
3226 has_spell = TRUE;
3227 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003228
3229 /* Get the start of the next line, so that words that wrap to the next
3230 * line are found too: "et<line-break>al.".
3231 * Trick: skip a few chars for C/shell/Vim comments */
3232 nextline[SPWORDLEN] = NUL;
3233 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3234 {
3235 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3236 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3237 }
3238
3239 /* When a word wrapped from the previous line the start of the current
3240 * line is valid. */
3241 if (lnum == checked_lnum)
3242 cur_checked_col = checked_col;
3243 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003244
3245 /* When there was a sentence end in the previous line may require a
3246 * word starting with capital in this line. In line 1 always check
3247 * the first word. */
3248 if (lnum != capcol_lnum)
3249 cap_col = -1;
3250 if (lnum == 1)
3251 cap_col = 0;
3252 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254#endif
3255
3256 /*
3257 * handle visual active in this window
3258 */
3259 fromcol = -10;
3260 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3262 {
3263 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003264 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
3266 top = &curwin->w_cursor;
3267 bot = &VIsual;
3268 }
3269 else /* Visual is before curwin->w_cursor */
3270 {
3271 top = &VIsual;
3272 bot = &curwin->w_cursor;
3273 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003274 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 if (VIsual_mode == Ctrl_V) /* block mode */
3276 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003277 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
3279 fromcol = wp->w_old_cursor_fcol;
3280 tocol = wp->w_old_cursor_lcol;
3281 }
3282 }
3283 else /* non-block mode */
3284 {
3285 if (lnum > top->lnum && lnum <= bot->lnum)
3286 fromcol = 0;
3287 else if (lnum == top->lnum)
3288 {
3289 if (VIsual_mode == 'V') /* linewise */
3290 fromcol = 0;
3291 else
3292 {
3293 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3294 if (gchar_pos(top) == NUL)
3295 tocol = fromcol + 1;
3296 }
3297 }
3298 if (VIsual_mode != 'V' && lnum == bot->lnum)
3299 {
3300 if (*p_sel == 'e' && bot->col == 0
3301#ifdef FEAT_VIRTUALEDIT
3302 && bot->coladd == 0
3303#endif
3304 )
3305 {
3306 fromcol = -10;
3307 tocol = MAXCOL;
3308 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003309 else if (bot->col == MAXCOL)
3310 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 else
3312 {
3313 pos = *bot;
3314 if (*p_sel == 'e')
3315 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3316 else
3317 {
3318 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3319 ++tocol;
3320 }
3321 }
3322 }
3323 }
3324
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 /* Check if the character under the cursor should not be inverted */
3326 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003327#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 )
3331 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
3333 /* if inverting in this line set area_highlighting */
3334 if (fromcol >= 0)
3335 {
3336 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003337 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003339 if ((clip_star.available && !clip_star.owned
3340 && clip_isautosel_star())
3341 || (clip_plus.available && !clip_plus.owned
3342 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003343 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344#endif
3345 }
3346 }
3347
3348 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003349 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003351 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 && wp == curwin
3353 && lnum >= curwin->w_cursor.lnum
3354 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3355 {
3356 if (lnum == curwin->w_cursor.lnum)
3357 getvcol(curwin, &(curwin->w_cursor),
3358 (colnr_T *)&fromcol, NULL, NULL);
3359 else
3360 fromcol = 0;
3361 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3362 {
3363 pos.lnum = lnum;
3364 pos.col = search_match_endcol;
3365 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3366 }
3367 else
3368 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003369 /* do at least one character; happens when past end of line */
3370 if (fromcol == tocol)
3371 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003373 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
3375
3376#ifdef FEAT_DIFF
3377 filler_lines = diff_check(wp, lnum);
3378 if (filler_lines < 0)
3379 {
3380 if (filler_lines == -1)
3381 {
3382 if (diff_find_change(wp, lnum, &change_start, &change_end))
3383 diff_hlf = HLF_ADD; /* added line */
3384 else if (change_start == 0)
3385 diff_hlf = HLF_TXD; /* changed text */
3386 else
3387 diff_hlf = HLF_CHD; /* changed line */
3388 }
3389 else
3390 diff_hlf = HLF_ADD; /* added line */
3391 filler_lines = 0;
3392 area_highlighting = TRUE;
3393 }
3394 if (lnum == wp->w_topline)
3395 filler_lines = wp->w_topfill;
3396 filler_todo = filler_lines;
3397#endif
3398
3399#ifdef LINE_ATTR
3400# ifdef FEAT_SIGNS
3401 /* If this line has a sign with line highlighting set line_attr. */
3402 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3403 if (v != 0)
3404 line_attr = sign_get_attr((int)v, TRUE);
3405# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003406# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003408 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003409 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410# endif
3411 if (line_attr != 0)
3412 area_highlighting = TRUE;
3413#endif
3414
3415 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3416 ptr = line;
3417
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003418#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003419 if (has_spell)
3420 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003421 /* For checking first word with a capital skip white space. */
3422 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003423 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003424
Bram Moolenaar30abd282005-06-22 22:35:10 +00003425 /* To be able to spell-check over line boundaries copy the end of the
3426 * current line into nextline[]. Above the start of the next line was
3427 * copied to nextline[SPWORDLEN]. */
3428 if (nextline[SPWORDLEN] == NUL)
3429 {
3430 /* No next line or it is empty. */
3431 nextlinecol = MAXCOL;
3432 nextline_idx = 0;
3433 }
3434 else
3435 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003436 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003437 if (v < SPWORDLEN)
3438 {
3439 /* Short line, use it completely and append the start of the
3440 * next line. */
3441 nextlinecol = 0;
3442 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003443 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003444 nextline_idx = v + 1;
3445 }
3446 else
3447 {
3448 /* Long line, use only the last SPWORDLEN bytes. */
3449 nextlinecol = v - SPWORDLEN;
3450 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3451 nextline_idx = SPWORDLEN + 1;
3452 }
3453 }
3454 }
3455#endif
3456
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003457 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003459 if (lcs_space || lcs_trail)
3460 extra_check = TRUE;
3461 /* find start of trailing whitespace */
3462 if (lcs_trail)
3463 {
3464 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003465 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003466 --trailcol;
3467 trailcol += (colnr_T) (ptr - line);
3468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470
3471 /*
3472 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3473 * first character to be displayed.
3474 */
3475 if (wp->w_p_wrap)
3476 v = wp->w_skipcol;
3477 else
3478 v = wp->w_leftcol;
3479 if (v > 0)
3480 {
3481#ifdef FEAT_MBYTE
3482 char_u *prev_ptr = ptr;
3483#endif
3484 while (vcol < v && *ptr != NUL)
3485 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003486 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 vcol += c;
3488#ifdef FEAT_MBYTE
3489 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003491 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003494 /* When:
3495 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003496 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003497 * - 'virtualedit' is set, or
3498 * - the visual mode is active,
3499 * the end of the line may be before the start of the displayed part.
3500 */
3501 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003502#ifdef FEAT_SYN_HL
3503 wp->w_p_cuc || draw_color_col ||
3504#endif
3505#ifdef FEAT_VIRTUALEDIT
3506 virtual_active() ||
3507#endif
3508 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
3513 /* Handle a character that's not completely on the screen: Put ptr at
3514 * that character but skip the first few screen characters. */
3515 if (vcol > v)
3516 {
3517 vcol -= c;
3518#ifdef FEAT_MBYTE
3519 ptr = prev_ptr;
3520#else
3521 --ptr;
3522#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003523 /* If the character fits on the screen, don't need to skip it.
3524 * Except for a TAB. */
3525 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003526#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003527 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003528#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003529 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003530 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532
3533 /*
3534 * Adjust for when the inverted text is before the screen,
3535 * and when the start of the inverted text is before the screen.
3536 */
3537 if (tocol <= vcol)
3538 fromcol = 0;
3539 else if (fromcol >= 0 && fromcol < vcol)
3540 fromcol = vcol;
3541
3542#ifdef FEAT_LINEBREAK
3543 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3544 if (wp->w_p_wrap)
3545 need_showbreak = TRUE;
3546#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003547#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003548 /* When spell checking a word we need to figure out the start of the
3549 * word and if it's badly spelled or not. */
3550 if (has_spell)
3551 {
3552 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003553 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003554 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003555
3556 pos = wp->w_cursor;
3557 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003558 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003559 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003560
3561 /* spell_move_to() may call ml_get() and make "line" invalid */
3562 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3563 ptr = line + linecol;
3564
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003565 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003566 {
3567 /* no bad word found at line start, don't check until end of a
3568 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003569 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003570 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003571 }
3572 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003573 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003574 /* bad word found, use attributes until end of word */
3575 word_end = wp->w_cursor.col + len + 1;
3576
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003577 /* Turn index into actual attributes. */
3578 if (spell_hlf != HLF_COUNT)
3579 spell_attr = highlight_attr[spell_hlf];
3580 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003581 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003582
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003583# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003584 /* Need to restart syntax highlighting for this line. */
3585 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003586 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003587# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003588 }
3589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
3591
3592 /*
3593 * Correct highlighting for cursor that can't be disabled.
3594 * Avoids having to check this for each character.
3595 */
3596 if (fromcol >= 0)
3597 {
3598 if (noinvcur)
3599 {
3600 if ((colnr_T)fromcol == wp->w_virtcol)
3601 {
3602 /* highlighting starts at cursor, let it start just after the
3603 * cursor */
3604 fromcol_prev = fromcol;
3605 fromcol = -1;
3606 }
3607 else if ((colnr_T)fromcol < wp->w_virtcol)
3608 /* restart highlighting after the cursor */
3609 fromcol_prev = wp->w_virtcol;
3610 }
3611 if (fromcol >= tocol)
3612 fromcol = -1;
3613 }
3614
3615#ifdef FEAT_SEARCH_EXTRA
3616 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003617 * Handle highlighting the last used search pattern and matches.
3618 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003620 cur = wp->w_match_head;
3621 shl_flag = FALSE;
3622 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003624 if (shl_flag == FALSE)
3625 {
3626 shl = &search_hl;
3627 shl_flag = TRUE;
3628 }
3629 else
3630 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003631 shl->startcol = MAXCOL;
3632 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003634 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003635 v = (long)(ptr - line);
3636 if (cur != NULL)
3637 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003638 next_search_hl(wp, shl, lnum, (colnr_T)v,
3639 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003640
3641 /* Need to get the line again, a multi-line regexp may have made it
3642 * invalid. */
3643 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3644 ptr = line + v;
3645
3646 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003648 if (shl->lnum == lnum)
3649 shl->startcol = shl->rm.startpos[0].col;
3650 else
3651 shl->startcol = 0;
3652 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3653 - shl->rm.startpos[0].lnum)
3654 shl->endcol = shl->rm.endpos[0].col;
3655 else
3656 shl->endcol = MAXCOL;
3657 /* Highlight one character for an empty match. */
3658 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003661 if (has_mbyte && line[shl->endcol] != NUL)
3662 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3663 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003665 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003667 if ((long)shl->startcol < v) /* match at leftcol */
3668 {
3669 shl->attr_cur = shl->attr;
3670 search_attr = shl->attr;
3671 }
3672 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003674 if (shl != &search_hl && cur != NULL)
3675 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
3677#endif
3678
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003679#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003680 /* Cursor line highlighting for 'cursorline' in the current window. Not
3681 * when Visual mode is active, because it's not clear what is selected
3682 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003683 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003684 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003685 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003686 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003687 area_highlighting = TRUE;
3688 }
3689#endif
3690
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003691 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 col = 0;
3693#ifdef FEAT_RIGHTLEFT
3694 if (wp->w_p_rl)
3695 {
3696 /* Rightleft window: process the text in the normal direction, but put
3697 * it in current_ScreenLine[] from right to left. Start at the
3698 * rightmost column of the window. */
3699 col = W_WIDTH(wp) - 1;
3700 off += col;
3701 }
3702#endif
3703
3704 /*
3705 * Repeat for the whole displayed line.
3706 */
3707 for (;;)
3708 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003709#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003710 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 /* Skip this quickly when working on the text. */
3713 if (draw_state != WL_LINE)
3714 {
3715#ifdef FEAT_CMDWIN
3716 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3717 {
3718 draw_state = WL_CMDLINE;
3719 if (cmdwin_type != 0 && wp == curwin)
3720 {
3721 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003723 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003724 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
3726 }
3727#endif
3728
3729#ifdef FEAT_FOLDING
3730 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3731 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003732 int fdc = compute_foldcolumn(wp, 0);
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003735 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003737 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003738 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003739 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003740 p_extra_free = alloc(12 + 1);
3741
3742 if (p_extra_free != NULL)
3743 {
3744 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3745 n_extra = fdc;
3746 p_extra_free[n_extra] = NUL;
3747 p_extra = p_extra_free;
3748 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003749 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
3752 }
3753#endif
3754
3755#ifdef FEAT_SIGNS
3756 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3757 {
3758 draw_state = WL_SIGN;
3759 /* Show the sign column when there are any signs in this
3760 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003761 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003763 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003765 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766# endif
3767
3768 /* Draw two cells with the sign value or blank. */
3769 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003770 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 n_extra = 2;
3772
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003773 if (row == startrow
3774#ifdef FEAT_DIFF
3775 + filler_lines && filler_todo <= 0
3776#endif
3777 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 {
3779 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3780 SIGN_TEXT);
3781# ifdef FEAT_SIGN_ICONS
3782 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3783 SIGN_ICON);
3784 if (gui.in_use && icon_sign != 0)
3785 {
3786 /* Use the image in this position. */
3787 c_extra = SIGN_BYTE;
3788# ifdef FEAT_NETBEANS_INTG
3789 if (buf_signcount(wp->w_buffer, lnum) > 1)
3790 c_extra = MULTISIGN_BYTE;
3791# endif
3792 char_attr = icon_sign;
3793 }
3794 else
3795# endif
3796 if (text_sign != 0)
3797 {
3798 p_extra = sign_get_text(text_sign);
3799 if (p_extra != NULL)
3800 {
3801 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003802 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804 char_attr = sign_get_attr(text_sign, FALSE);
3805 }
3806 }
3807 }
3808 }
3809#endif
3810
3811 if (draw_state == WL_NR - 1 && n_extra == 0)
3812 {
3813 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003814 /* Display the absolute or relative line number. After the
3815 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3816 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 && (row == startrow
3818#ifdef FEAT_DIFF
3819 + filler_lines
3820#endif
3821 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3822 {
3823 /* Draw the line number (empty space after wrapping). */
3824 if (row == startrow
3825#ifdef FEAT_DIFF
3826 + filler_lines
3827#endif
3828 )
3829 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003830 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003831 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003832
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003833 if (wp->w_p_nu && !wp->w_p_rnu)
3834 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003835 num = (long)lnum;
3836 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003837 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003838 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003839 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003840 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003841 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003842 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003843 num = lnum;
3844 fmt = "%-*ld ";
3845 }
3846 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003847
Bram Moolenaar700e7342013-01-30 12:31:36 +01003848 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003849 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 if (wp->w_skipcol > 0)
3851 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3852 *p_extra = '-';
3853#ifdef FEAT_RIGHTLEFT
3854 if (wp->w_p_rl) /* reverse line numbers */
3855 rl_mirror(extra);
3856#endif
3857 p_extra = extra;
3858 c_extra = NUL;
3859 }
3860 else
3861 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003862 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003863 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003864#ifdef FEAT_SYN_HL
3865 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003866 * the current line differently.
3867 * TODO: Can we use CursorLine instead of CursorLineNr
3868 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003869 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003870 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003871 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 }
3874 }
3875
Bram Moolenaar597a4222014-06-25 14:39:50 +02003876#ifdef FEAT_LINEBREAK
3877 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3878 && n_extra == 0 && *p_sbr != NUL)
3879 /* draw indent after showbreak value */
3880 draw_state = WL_BRI;
3881 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3882 /* After the showbreak, draw the breakindent */
3883 draw_state = WL_BRI - 1;
3884
3885 /* draw 'breakindent': indent wrapped text accordingly */
3886 if (draw_state == WL_BRI - 1 && n_extra == 0)
3887 {
3888 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003889 /* if need_showbreak is set, breakindent also applies */
3890 if (wp->w_p_bri && n_extra == 0
3891 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003892# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003893 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003894# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003895 )
3896 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003897 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003898# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003899 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003900 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003901 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003902# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003903 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3904 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003905 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003906# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003907 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003908# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003909 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003910 c_extra = ' ';
3911 n_extra = get_breakindent_win(wp,
3912 ml_get_buf(wp->w_buffer, lnum, FALSE));
3913 /* Correct end of highlighted area for 'breakindent',
3914 * required when 'linebreak' is also set. */
3915 if (tocol == vcol)
3916 tocol += n_extra;
3917 }
3918 }
3919#endif
3920
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3922 if (draw_state == WL_SBR - 1 && n_extra == 0)
3923 {
3924 draw_state = WL_SBR;
3925# ifdef FEAT_DIFF
3926 if (filler_todo > 0)
3927 {
3928 /* Draw "deleted" diff line(s). */
3929 if (char2cells(fill_diff) > 1)
3930 c_extra = '-';
3931 else
3932 c_extra = fill_diff;
3933# ifdef FEAT_RIGHTLEFT
3934 if (wp->w_p_rl)
3935 n_extra = col + 1;
3936 else
3937# endif
3938 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003939 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
3941# endif
3942# ifdef FEAT_LINEBREAK
3943 if (*p_sbr != NUL && need_showbreak)
3944 {
3945 /* Draw 'showbreak' at the start of each broken line. */
3946 p_extra = p_sbr;
3947 c_extra = NUL;
3948 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003949 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003951 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 /* Correct end of highlighted area for 'showbreak',
3953 * required when 'linebreak' is also set. */
3954 if (tocol == vcol)
3955 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003956#ifdef FEAT_SYN_HL
3957 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003958 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003959 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003960 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
3963# endif
3964 }
3965#endif
3966
3967 if (draw_state == WL_LINE - 1 && n_extra == 0)
3968 {
3969 draw_state = WL_LINE;
3970 if (saved_n_extra)
3971 {
3972 /* Continue item from end of wrapped line. */
3973 n_extra = saved_n_extra;
3974 c_extra = saved_c_extra;
3975 p_extra = saved_p_extra;
3976 char_attr = saved_char_attr;
3977 }
3978 else
3979 char_attr = 0;
3980 }
3981 }
3982
3983 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003984 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003985 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986#ifdef FEAT_DIFF
3987 && filler_todo <= 0
3988#endif
3989 )
3990 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02003991 screen_line(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02003992 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003993 /* Pretend we have finished updating the window. Except when
3994 * 'cursorcolumn' is set. */
3995#ifdef FEAT_SYN_HL
3996 if (wp->w_p_cuc)
3997 row = wp->w_cline_row + wp->w_cline_height;
3998 else
3999#endif
4000 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 break;
4002 }
4003
4004 if (draw_state == WL_LINE && area_highlighting)
4005 {
4006 /* handle Visual or match highlighting in this line */
4007 if (vcol == fromcol
4008#ifdef FEAT_MBYTE
4009 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4010 && (*mb_ptr2cells)(ptr) > 1)
4011#endif
4012 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004013 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 && vcol < tocol))
4015 area_attr = attr; /* start highlighting */
4016 else if (area_attr != 0
4017 && (vcol == tocol
4018 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020
4021#ifdef FEAT_SEARCH_EXTRA
4022 if (!n_extra)
4023 {
4024 /*
4025 * Check for start/end of search pattern match.
4026 * After end, check for start/end of next match.
4027 * When another match, have to check for start again.
4028 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004029 * Do this for 'search_hl' and the match list (ordered by
4030 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004032 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004033 cur = wp->w_match_head;
4034 shl_flag = FALSE;
4035 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004037 if (shl_flag == FALSE
4038 && ((cur != NULL
4039 && cur->priority > SEARCH_HL_PRIORITY)
4040 || cur == NULL))
4041 {
4042 shl = &search_hl;
4043 shl_flag = TRUE;
4044 }
4045 else
4046 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004047 if (cur != NULL)
4048 cur->pos.cur = 0;
4049 pos_inprogress = TRUE;
4050 while (shl->rm.regprog != NULL
4051 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004053 if (shl->startcol != MAXCOL
4054 && v >= (long)shl->startcol
4055 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004057#ifdef FEAT_MBYTE
4058 int tmp_col = v + MB_PTR2LEN(ptr);
4059
4060 if (shl->endcol < tmp_col)
4061 shl->endcol = tmp_col;
4062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004064#ifdef FEAT_CONCEAL
4065 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4066 == cur->hlg_id)
4067 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004068 has_match_conc =
4069 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004070 match_conc = cur->conceal_char;
4071 }
4072 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004073 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004076 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
4078 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004079 next_search_hl(wp, shl, lnum, (colnr_T)v,
4080 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004081 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004082 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 /* Need to get the line again, a multi-line regexp
4085 * may have made it invalid. */
4086 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4087 ptr = line + v;
4088
4089 if (shl->lnum == lnum)
4090 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004091 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004093 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004095 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004097 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 {
4099 /* highlight empty match, try again after
4100 * it */
4101#ifdef FEAT_MBYTE
4102 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004103 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004104 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 else
4106#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004107 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109
4110 /* Loop to check if the match starts at the
4111 * current position */
4112 continue;
4113 }
4114 }
4115 break;
4116 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004117 if (shl != &search_hl && cur != NULL)
4118 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004120
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004121 /* Use attributes from match with highest priority among
4122 * 'search_hl' and the match list. */
4123 search_attr = search_hl.attr_cur;
4124 cur = wp->w_match_head;
4125 shl_flag = FALSE;
4126 while (cur != NULL || shl_flag == FALSE)
4127 {
4128 if (shl_flag == FALSE
4129 && ((cur != NULL
4130 && cur->priority > SEARCH_HL_PRIORITY)
4131 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004132 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004133 shl = &search_hl;
4134 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004135 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004136 else
4137 shl = &cur->hl;
4138 if (shl->attr_cur != 0)
4139 search_attr = shl->attr_cur;
4140 if (shl != &search_hl && cur != NULL)
4141 cur = cur->next;
4142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144#endif
4145
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004147 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004149 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4150 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004152 if (diff_hlf == HLF_TXD && ptr - line > change_end
4153 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004155 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004156 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004157 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
4159#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004160
4161 /* Decide which of the highlight attributes to use. */
4162 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004163#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004164 if (area_attr != 0)
4165 char_attr = hl_combine_attr(line_attr, area_attr);
4166 else if (search_attr != 0)
4167 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004168 /* Use line_attr when not in the Visual or 'incsearch' area
4169 * (area_attr may be 0 when "noinvcur" is set). */
4170 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004171 || vcol < fromcol || vcol_prev < fromcol_prev
4172 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004173 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004174#else
4175 if (area_attr != 0)
4176 char_attr = area_attr;
4177 else if (search_attr != 0)
4178 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004179#endif
4180 else
4181 {
4182 attr_pri = FALSE;
4183#ifdef FEAT_SYN_HL
4184 if (has_syntax)
4185 char_attr = syntax_attr;
4186 else
4187#endif
4188 char_attr = 0;
4189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191
4192 /*
4193 * Get the next character to put on the screen.
4194 */
4195 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004196 * The "p_extra" points to the extra stuff that is inserted to
4197 * represent special characters (non-printable stuff) and other
4198 * things. When all characters are the same, c_extra is used.
4199 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4200 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4202 */
4203 if (n_extra > 0)
4204 {
4205 if (c_extra != NUL)
4206 {
4207 c = c_extra;
4208#ifdef FEAT_MBYTE
4209 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004210 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
4212 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004213 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004214 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 }
4216 else
4217 mb_utf8 = FALSE;
4218#endif
4219 }
4220 else
4221 {
4222 c = *p_extra;
4223#ifdef FEAT_MBYTE
4224 if (has_mbyte)
4225 {
4226 mb_c = c;
4227 if (enc_utf8)
4228 {
4229 /* If the UTF-8 character is more than one byte:
4230 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004231 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 mb_utf8 = FALSE;
4233 if (mb_l > n_extra)
4234 mb_l = 1;
4235 else if (mb_l > 1)
4236 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004237 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004239 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241 }
4242 else
4243 {
4244 /* if this is a DBCS character, put it in "mb_c" */
4245 mb_l = MB_BYTE2LEN(c);
4246 if (mb_l >= n_extra)
4247 mb_l = 1;
4248 else if (mb_l > 1)
4249 mb_c = (c << 8) + p_extra[1];
4250 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004251 if (mb_l == 0) /* at the NUL at end-of-line */
4252 mb_l = 1;
4253
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 /* If a double-width char doesn't fit display a '>' in the
4255 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004256 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257# ifdef FEAT_RIGHTLEFT
4258 wp->w_p_rl ? (col <= 0) :
4259# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004260 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 && (*mb_char2cells)(mb_c) == 2)
4262 {
4263 c = '>';
4264 mb_c = c;
4265 mb_l = 1;
4266 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004267 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 /* put the pointer back to output the double-width
4269 * character at the start of the next line. */
4270 ++n_extra;
4271 --p_extra;
4272 }
4273 else
4274 {
4275 n_extra -= mb_l - 1;
4276 p_extra += mb_l - 1;
4277 }
4278 }
4279#endif
4280 ++p_extra;
4281 }
4282 --n_extra;
4283 }
4284 else
4285 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004286#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004287 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004288#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004289
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004290 if (p_extra_free != NULL)
4291 {
4292 vim_free(p_extra_free);
4293 p_extra_free = NULL;
4294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 /*
4296 * Get a character from the line itself.
4297 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004298 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004299#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004300 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302#ifdef FEAT_MBYTE
4303 if (has_mbyte)
4304 {
4305 mb_c = c;
4306 if (enc_utf8)
4307 {
4308 /* If the UTF-8 character is more than one byte: Decode it
4309 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004310 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 mb_utf8 = FALSE;
4312 if (mb_l > 1)
4313 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004314 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 /* Overlong encoded ASCII or ASCII with composing char
4316 * is displayed normally, except a NUL. */
4317 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004318 {
4319 c = mb_c;
4320# ifdef FEAT_LINEBREAK
4321 c0 = mb_c;
4322# endif
4323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004325
4326 /* At start of the line we can have a composing char.
4327 * Draw it as a space with a composing char. */
4328 if (utf_iscomposing(mb_c))
4329 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004330 int i;
4331
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004332 for (i = Screen_mco - 1; i > 0; --i)
4333 u8cc[i] = u8cc[i - 1];
4334 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004335 mb_c = ' ';
4336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 }
4338
4339 if ((mb_l == 1 && c >= 0x80)
4340 || (mb_l >= 1 && mb_c == 0)
4341 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004342# ifdef UNICODE16
4343 || mb_c >= 0x10000
4344# endif
4345 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 {
4347 /*
4348 * Illegal UTF-8 byte: display as <xx>.
4349 * Non-BMP character : display as ? or fullwidth ?.
4350 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004351# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
4355 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004356# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 if (wp->w_p_rl) /* reverse */
4358 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004361# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 else if (utf_char2cells(mb_c) != 2)
4363 STRCPY(extra, "?");
4364 else
4365 /* 0xff1f in UTF-8: full-width '?' */
4366 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368
4369 p_extra = extra;
4370 c = *p_extra;
4371 mb_c = mb_ptr2char_adv(&p_extra);
4372 mb_utf8 = (c >= 0x80);
4373 n_extra = (int)STRLEN(p_extra);
4374 c_extra = NUL;
4375 if (area_attr == 0 && search_attr == 0)
4376 {
4377 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004378 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 saved_attr2 = char_attr; /* save current attr */
4380 }
4381 }
4382 else if (mb_l == 0) /* at the NUL at end-of-line */
4383 mb_l = 1;
4384#ifdef FEAT_ARABIC
4385 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4386 {
4387 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004388 int pc, pc1, nc;
4389 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 /* The idea of what is the previous and next
4392 * character depends on 'rightleft'. */
4393 if (wp->w_p_rl)
4394 {
4395 pc = prev_c;
4396 pc1 = prev_c1;
4397 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004398 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 }
4400 else
4401 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004402 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004404 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406 prev_c = mb_c;
4407
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004408 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
4410 else
4411 prev_c = mb_c;
4412#endif
4413 }
4414 else /* enc_dbcs */
4415 {
4416 mb_l = MB_BYTE2LEN(c);
4417 if (mb_l == 0) /* at the NUL at end-of-line */
4418 mb_l = 1;
4419 else if (mb_l > 1)
4420 {
4421 /* We assume a second byte below 32 is illegal.
4422 * Hopefully this is OK for all double-byte encodings!
4423 */
4424 if (ptr[1] >= 32)
4425 mb_c = (c << 8) + ptr[1];
4426 else
4427 {
4428 if (ptr[1] == NUL)
4429 {
4430 /* head byte at end of line */
4431 mb_l = 1;
4432 transchar_nonprint(extra, c);
4433 }
4434 else
4435 {
4436 /* illegal tail byte */
4437 mb_l = 2;
4438 STRCPY(extra, "XX");
4439 }
4440 p_extra = extra;
4441 n_extra = (int)STRLEN(extra) - 1;
4442 c_extra = NUL;
4443 c = *p_extra++;
4444 if (area_attr == 0 && search_attr == 0)
4445 {
4446 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004447 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 saved_attr2 = char_attr; /* save current attr */
4449 }
4450 mb_c = c;
4451 }
4452 }
4453 }
4454 /* If a double-width char doesn't fit display a '>' in the
4455 * last column; the character is displayed at the start of the
4456 * next line. */
4457 if ((
4458# ifdef FEAT_RIGHTLEFT
4459 wp->w_p_rl ? (col <= 0) :
4460# endif
4461 (col >= W_WIDTH(wp) - 1))
4462 && (*mb_char2cells)(mb_c) == 2)
4463 {
4464 c = '>';
4465 mb_c = c;
4466 mb_utf8 = FALSE;
4467 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004468 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 /* Put pointer back so that the character will be
4470 * displayed at the start of the next line. */
4471 --ptr;
4472 }
4473 else if (*ptr != NUL)
4474 ptr += mb_l - 1;
4475
4476 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004477 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004478 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004479 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004482 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 c = ' ';
4484 if (area_attr == 0 && search_attr == 0)
4485 {
4486 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004487 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 saved_attr2 = char_attr; /* save current attr */
4489 }
4490 mb_c = c;
4491 mb_utf8 = FALSE;
4492 mb_l = 1;
4493 }
4494
4495 }
4496#endif
4497 ++ptr;
4498
4499 if (extra_check)
4500 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004501#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004502 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004503#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004504
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004505#ifdef FEAT_TERMINAL
4506 if (get_term_attr)
4507 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004508 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004509
4510 if (!attr_pri)
4511 char_attr = syntax_attr;
4512 else
4513 char_attr = hl_combine_attr(syntax_attr, char_attr);
4514 }
4515#endif
4516
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004517#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 /* Get syntax attribute, unless still at the start of the line
4519 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004520 v = (long)(ptr - line);
4521 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 {
4523 /* Get the syntax attribute for the character. If there
4524 * is an error, disable syntax highlighting. */
4525 save_did_emsg = did_emsg;
4526 did_emsg = FALSE;
4527
Bram Moolenaar217ad922005-03-20 22:37:15 +00004528 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004529# ifdef FEAT_SPELL
4530 has_spell ? &can_spell :
4531# endif
4532 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004535 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004536 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004537 has_syntax = FALSE;
4538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 else
4540 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004541#ifdef SYN_TIME_LIMIT
4542 if (wp->w_s->b_syn_slow)
4543 has_syntax = FALSE;
4544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545
4546 /* Need to get the line again, a multi-line regexp may
4547 * have made it invalid. */
4548 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4549 ptr = line + v;
4550
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004551 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004553 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004554 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004555# ifdef FEAT_CONCEAL
4556 /* no concealing past the end of the line, it interferes
4557 * with line highlighting */
4558 if (c == NUL)
4559 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004560 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004561 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004562# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004564#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004565
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004566#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004567 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004568 * Only do this when there is no syntax highlighting, the
4569 * @Spell cluster is not used or the current syntax item
4570 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004571 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004572 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004573 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004574# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004575 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004576 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004577# endif
4578 if (c != 0 && (
4579# ifdef FEAT_SYN_HL
4580 !has_syntax ||
4581# endif
4582 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004583 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004584 char_u *prev_ptr, *p;
4585 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004586 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004587# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004588 if (has_mbyte)
4589 {
4590 prev_ptr = ptr - mb_l;
4591 v -= mb_l - 1;
4592 }
4593 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004594# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004595 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004596
4597 /* Use nextline[] if possible, it has the start of the
4598 * next line concatenated. */
4599 if ((prev_ptr - line) - nextlinecol >= 0)
4600 p = nextline + (prev_ptr - line) - nextlinecol;
4601 else
4602 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004603 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004604 len = spell_check(wp, p, &spell_hlf, &cap_col,
4605 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004606 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004607
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004608 /* In Insert mode only highlight a word that
4609 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004610 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004611 && (State & INSERT) != 0
4612 && wp->w_cursor.lnum == lnum
4613 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004614 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004615 && wp->w_cursor.col < (colnr_T)word_end)
4616 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004617 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004618 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004619 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004620
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004621 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004622 && (p - nextline) + len > nextline_idx)
4623 {
4624 /* Remember that the good word continues at the
4625 * start of the next line. */
4626 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004627 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004628 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004629
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004630 /* Turn index into actual attributes. */
4631 if (spell_hlf != HLF_COUNT)
4632 spell_attr = highlight_attr[spell_hlf];
4633
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004634 if (cap_col > 0)
4635 {
4636 if (p != prev_ptr
4637 && (p - nextline) + cap_col >= nextline_idx)
4638 {
4639 /* Remember that the word in the next line
4640 * must start with a capital. */
4641 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004642 cap_col = (int)((p - nextline) + cap_col
4643 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004644 }
4645 else
4646 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004647 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004648 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004649 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004650 }
4651 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004652 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004653 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004654 char_attr = hl_combine_attr(char_attr, spell_attr);
4655 else
4656 char_attr = hl_combine_attr(spell_attr, char_attr);
4657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#endif
4659#ifdef FEAT_LINEBREAK
4660 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004661 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004663 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004664 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004666# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004667 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004668# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004669 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004671 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004673 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004674
Bram Moolenaar597a4222014-06-25 14:39:50 +02004675 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004676 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004677 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004678 if (c == TAB && n_extra + col > W_WIDTH(wp))
4679 n_extra = (int)wp->w_buffer->b_p_ts
4680 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4681
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004682# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004683 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004684# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004686# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004687 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004688 {
4689#ifdef FEAT_CONCEAL
4690 if (c == TAB)
4691 /* See "Tab alignment" below. */
4692 FIX_FOR_BOGUSCOLS;
4693#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004694 if (!wp->w_p_list)
4695 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
4698#endif
4699
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004700 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4701 */
4702 if (wp->w_p_list
4703 && (((c == 160
4704#ifdef FEAT_MBYTE
4705 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4706#endif
4707 ) && lcs_nbsp)
4708 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4709 {
4710 c = (c == ' ') ? lcs_space : lcs_nbsp;
4711 if (area_attr == 0 && search_attr == 0)
4712 {
4713 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004714 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004715 saved_attr2 = char_attr; /* save current attr */
4716 }
4717#ifdef FEAT_MBYTE
4718 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004719 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004720 {
4721 mb_utf8 = TRUE;
4722 u8cc[0] = 0;
4723 c = 0xc0;
4724 }
4725 else
4726 mb_utf8 = FALSE;
4727#endif
4728 }
4729
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4731 {
4732 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004733 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
4735 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004736 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 saved_attr2 = char_attr; /* save current attr */
4738 }
4739#ifdef FEAT_MBYTE
4740 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004741 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
4743 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004744 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004745 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 }
4747 else
4748 mb_utf8 = FALSE;
4749#endif
4750 }
4751 }
4752
4753 /*
4754 * Handling of non-printable characters.
4755 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004756 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
4758 /*
4759 * when getting a character from the file, we may have to
4760 * turn it into something else on the way to putting it
4761 * into "ScreenLines".
4762 */
4763 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4764 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004765 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004766 long vcol_adjusted = vcol; /* removed showbreak length */
4767#ifdef FEAT_LINEBREAK
4768 /* only adjust the tab_len, when at the first column
4769 * after the showbreak value was drawn */
4770 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4771 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004774 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004775 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4776
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004777#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004778 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004779#endif
4780 /* tab amount depends on current column */
4781 n_extra = tab_len;
4782#ifdef FEAT_LINEBREAK
4783 else
4784 {
4785 char_u *p;
4786 int len = n_extra;
4787 int i;
4788 int saved_nextra = n_extra;
4789
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004790#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004791 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004792 /* there are characters to conceal */
4793 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004794 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4795 */
4796 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4797 && n_extra > tab_len)
4798 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004799#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004800
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004801 /* if n_extra > 0, it gives the number of chars, to
4802 * use for a tab, else we need to calculate the width
4803 * for a tab */
4804#ifdef FEAT_MBYTE
4805 len = (tab_len * mb_char2len(lcs_tab2));
4806 if (n_extra > 0)
4807 len += n_extra - tab_len;
4808#endif
4809 c = lcs_tab1;
4810 p = alloc((unsigned)(len + 1));
4811 vim_memset(p, ' ', len);
4812 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004813 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004814 p_extra_free = p;
4815 for (i = 0; i < tab_len; i++)
4816 {
4817#ifdef FEAT_MBYTE
4818 mb_char2bytes(lcs_tab2, p);
4819 p += mb_char2len(lcs_tab2);
4820 n_extra += mb_char2len(lcs_tab2)
4821 - (saved_nextra > 0 ? 1 : 0);
4822#else
4823 p[i] = lcs_tab2;
4824#endif
4825 }
4826 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004827#ifdef FEAT_CONCEAL
4828 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4829 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004830 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004831 n_extra -= vcol_off;
4832#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004833 }
4834#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004835#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004836 {
4837 int vc_saved = vcol_off;
4838
4839 /* Tab alignment should be identical regardless of
4840 * 'conceallevel' value. So tab compensates of all
4841 * previous concealed characters, and thus resets
4842 * vcol_off and boguscols accumulated so far in the
4843 * line. Note that the tab can be longer than
4844 * 'tabstop' when there are concealed characters. */
4845 FIX_FOR_BOGUSCOLS;
4846
4847 /* Make sure, the highlighting for the tab char will be
4848 * correctly set further below (effectively reverts the
4849 * FIX_FOR_BOGSUCOLS macro */
4850 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004851 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004852 tab_len += vc_saved;
4853 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855#ifdef FEAT_MBYTE
4856 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4857#endif
4858 if (wp->w_p_list)
4859 {
4860 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004861#ifdef FEAT_LINEBREAK
4862 if (wp->w_p_lbr)
4863 c_extra = NUL; /* using p_extra from above */
4864 else
4865#endif
4866 c_extra = lcs_tab2;
4867 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004868 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 saved_attr2 = char_attr; /* save current attr */
4870#ifdef FEAT_MBYTE
4871 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004872 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
4874 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004875 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004876 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878#endif
4879 }
4880 else
4881 {
4882 c_extra = ' ';
4883 c = ' ';
4884 }
4885 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004886 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004887 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004888 || ((fromcol >= 0 || fromcol_prev >= 0)
4889 && tocol > vcol
4890 && VIsual_mode != Ctrl_V
4891 && (
4892# ifdef FEAT_RIGHTLEFT
4893 wp->w_p_rl ? (col >= 0) :
4894# endif
4895 (col < W_WIDTH(wp)))
4896 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004897 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004898 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004899 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004901 /* Display a '$' after the line or highlight an extra
4902 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4904 /* For a diff line the highlighting continues after the
4905 * "$". */
4906 if (
4907# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004908 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909# ifdef LINE_ATTR
4910 &&
4911# endif
4912# endif
4913# ifdef LINE_ATTR
4914 line_attr == 0
4915# endif
4916 )
4917#endif
4918 {
4919#ifdef FEAT_VIRTUALEDIT
4920 /* In virtualedit, visual selections may extend
4921 * beyond end of line. */
4922 if (area_highlighting && virtual_active()
4923 && tocol != MAXCOL && vcol < tocol)
4924 n_extra = 0;
4925 else
4926#endif
4927 {
4928 p_extra = at_end_str;
4929 n_extra = 1;
4930 c_extra = NUL;
4931 }
4932 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004933 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004934 c = lcs_eol;
4935 else
4936 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 lcs_eol_one = -1;
4938 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004939 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004941 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 n_attr = 1;
4943 }
4944#ifdef FEAT_MBYTE
4945 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004946 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
4948 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004949 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004950 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952 else
4953 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4954#endif
4955 }
4956 else if (c != NUL)
4957 {
4958 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004959 if (n_extra == 0)
4960 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961#ifdef FEAT_RIGHTLEFT
4962 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4963 rl_mirror(p_extra); /* reverse "<12>" */
4964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004966#ifdef FEAT_LINEBREAK
4967 if (wp->w_p_lbr)
4968 {
4969 char_u *p;
4970
4971 c = *p_extra;
4972 p = alloc((unsigned)n_extra + 1);
4973 vim_memset(p, ' ', n_extra);
4974 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4975 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004976 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004977 p_extra_free = p_extra = p;
4978 }
4979 else
4980#endif
4981 {
4982 n_extra = byte2cells(c) - 1;
4983 c = *p_extra++;
4984 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004985 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 {
4987 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004988 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 saved_attr2 = char_attr; /* save current attr */
4990 }
4991#ifdef FEAT_MBYTE
4992 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4993#endif
4994 }
4995#ifdef FEAT_VIRTUALEDIT
4996 else if (VIsual_active
4997 && (VIsual_mode == Ctrl_V
4998 || VIsual_mode == 'v')
4999 && virtual_active()
5000 && tocol != MAXCOL
5001 && vcol < tocol
5002 && (
5003# ifdef FEAT_RIGHTLEFT
5004 wp->w_p_rl ? (col >= 0) :
5005# endif
5006 (col < W_WIDTH(wp))))
5007 {
5008 c = ' ';
5009 --ptr; /* put it back at the NUL */
5010 }
5011#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005012#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 else if ((
5014# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005015 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005017# ifdef FEAT_TERMINAL
5018 term_attr != 0 ||
5019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 ) && (
5022# ifdef FEAT_RIGHTLEFT
5023 wp->w_p_rl ? (col >= 0) :
5024# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005025 (col
5026# ifdef FEAT_CONCEAL
5027 - boguscols
5028# endif
5029 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
5031 /* Highlight until the right side of the window */
5032 c = ' ';
5033 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005034
5035 /* Remember we do the char for line highlighting. */
5036 ++did_line_attr;
5037
5038 /* don't do search HL for the rest of the line */
5039 if (line_attr != 0 && char_attr == search_attr && col > 0)
5040 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041# ifdef FEAT_DIFF
5042 if (diff_hlf == HLF_TXD)
5043 {
5044 diff_hlf = HLF_CHD;
5045 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005046 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005047 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005048 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5049 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005050 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
5053# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005054# ifdef FEAT_TERMINAL
5055 if (term_attr != 0)
5056 {
5057 char_attr = term_attr;
5058 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5059 char_attr = hl_combine_attr(char_attr,
5060 HL_ATTR(HLF_CUL));
5061 }
5062# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
5064#endif
5065 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005066
5067#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005068 if ( wp->w_p_cole > 0
5069 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005070 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005071 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005072 && !(lnum_in_visual_area
5073 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005074 {
5075 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005076 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005077 && (syn_get_sub_char() != NUL || match_conc
5078 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005079 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005080 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005081 /* First time at this concealed item: display one
5082 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005083 if (match_conc)
5084 c = match_conc;
5085 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005086 c = syn_get_sub_char();
5087 else if (lcs_conceal != NUL)
5088 c = lcs_conceal;
5089 else
5090 c = ' ';
5091
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005092 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005093
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005094 if (n_extra > 0)
5095 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005096 vcol += n_extra;
5097 if (wp->w_p_wrap && n_extra > 0)
5098 {
5099# ifdef FEAT_RIGHTLEFT
5100 if (wp->w_p_rl)
5101 {
5102 col -= n_extra;
5103 boguscols -= n_extra;
5104 }
5105 else
5106# endif
5107 {
5108 boguscols += n_extra;
5109 col += n_extra;
5110 }
5111 }
5112 n_extra = 0;
5113 n_attr = 0;
5114 }
5115 else if (n_skip == 0)
5116 {
5117 is_concealing = TRUE;
5118 n_skip = 1;
5119 }
5120# ifdef FEAT_MBYTE
5121 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005122 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005123 {
5124 mb_utf8 = TRUE;
5125 u8cc[0] = 0;
5126 c = 0xc0;
5127 }
5128 else
5129 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5130# endif
5131 }
5132 else
5133 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005134 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005135 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005136 }
5137#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
5139
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005140#ifdef FEAT_CONCEAL
5141 /* In the cursor line and we may be concealing characters: correct
5142 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005143 if (!did_wcol && draw_state == WL_LINE
5144 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005145 && conceal_cursor_line(wp)
5146 && (int)wp->w_virtcol <= vcol + n_skip)
5147 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005148# ifdef FEAT_RIGHTLEFT
5149 if (wp->w_p_rl)
5150 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5151 else
5152# endif
5153 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005154 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005155 did_wcol = TRUE;
5156 }
5157#endif
5158
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 /* Don't override visual selection highlighting. */
5160 if (n_attr > 0
5161 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005162 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 char_attr = extra_attr;
5164
Bram Moolenaar81695252004-12-29 20:58:21 +00005165#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 /* XIM don't send preedit_start and preedit_end, but they send
5167 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5168 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005169 if (p_imst == IM_ON_THE_SPOT
5170 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005171 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 && (State & INSERT)
5173 && !p_imdisable
5174 && im_is_preediting()
5175 && draw_state == WL_LINE)
5176 {
5177 colnr_T tcol;
5178
5179 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005180 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 else
5182 tcol = preedit_end_col;
5183 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5184 {
5185 if (feedback_old_attr < 0)
5186 {
5187 feedback_col = 0;
5188 feedback_old_attr = char_attr;
5189 }
5190 char_attr = im_get_feedback_attr(feedback_col);
5191 if (char_attr < 0)
5192 char_attr = feedback_old_attr;
5193 feedback_col++;
5194 }
5195 else if (feedback_old_attr >= 0)
5196 {
5197 char_attr = feedback_old_attr;
5198 feedback_old_attr = -1;
5199 feedback_col = 0;
5200 }
5201 }
5202#endif
5203 /*
5204 * Handle the case where we are in column 0 but not on the first
5205 * character of the line and the user wants us to show us a
5206 * special character (via 'listchars' option "precedes:<char>".
5207 */
5208 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005209 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5211#ifdef FEAT_DIFF
5212 && filler_todo <= 0
5213#endif
5214 && draw_state > WL_NR
5215 && c != NUL)
5216 {
5217 c = lcs_prec;
5218 lcs_prec_todo = NUL;
5219#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005220 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5221 {
5222 /* Double-width character being overwritten by the "precedes"
5223 * character, need to fill up half the character. */
5224 c_extra = MB_FILLER_CHAR;
5225 n_extra = 1;
5226 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005227 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005230 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
5232 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005233 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005234 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 }
5236 else
5237 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5238#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005239 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
5241 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005242 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 n_attr3 = 1;
5244 }
5245 }
5246
5247 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005248 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005250 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005251#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005252 || did_line_attr == 1
5253#endif
5254 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005256#ifdef FEAT_SEARCH_EXTRA
5257 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005258
5259 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005260 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005261 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005262#endif
5263
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005264 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 * highlight match at end of line. If it's beyond the last
5266 * char on the screen, just overwrite that one (tricky!) Not
5267 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005268#ifdef FEAT_SEARCH_EXTRA
5269 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005270 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005271 prevcol_hl_flag = TRUE;
5272 else
5273 {
5274 cur = wp->w_match_head;
5275 while (cur != NULL)
5276 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005277 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005278 {
5279 prevcol_hl_flag = TRUE;
5280 break;
5281 }
5282 cur = cur->next;
5283 }
5284 }
5285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005287 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005288 && (VIsual_mode != Ctrl_V
5289 || lnum == VIsual.lnum
5290 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005291 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292#ifdef FEAT_SEARCH_EXTRA
5293 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005294 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005295# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005296 && did_line_attr <= 1
5297# endif
5298 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299#endif
5300 ))
5301 {
5302 int n = 0;
5303
5304#ifdef FEAT_RIGHTLEFT
5305 if (wp->w_p_rl)
5306 {
5307 if (col < 0)
5308 n = 1;
5309 }
5310 else
5311#endif
5312 {
5313 if (col >= W_WIDTH(wp))
5314 n = -1;
5315 }
5316 if (n != 0)
5317 {
5318 /* At the window boundary, highlight the last character
5319 * instead (better than nothing). */
5320 off += n;
5321 col += n;
5322 }
5323 else
5324 {
5325 /* Add a blank character to highlight. */
5326 ScreenLines[off] = ' ';
5327#ifdef FEAT_MBYTE
5328 if (enc_utf8)
5329 ScreenLinesUC[off] = 0;
5330#endif
5331 }
5332#ifdef FEAT_SEARCH_EXTRA
5333 if (area_attr == 0)
5334 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005335 /* Use attributes from match with highest priority among
5336 * 'search_hl' and the match list. */
5337 char_attr = search_hl.attr;
5338 cur = wp->w_match_head;
5339 shl_flag = FALSE;
5340 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005341 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005342 if (shl_flag == FALSE
5343 && ((cur != NULL
5344 && cur->priority > SEARCH_HL_PRIORITY)
5345 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005346 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005347 shl = &search_hl;
5348 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005349 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005350 else
5351 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005352 if ((ptr - line) - 1 == (long)shl->startcol
5353 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005354 char_attr = shl->attr;
5355 if (shl != &search_hl && cur != NULL)
5356 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
5359#endif
5360 ScreenAttrs[off] = char_attr;
5361#ifdef FEAT_RIGHTLEFT
5362 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005363 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005365 --off;
5366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 else
5368#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005369 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005371 ++off;
5372 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005373 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005374#ifdef FEAT_SYN_HL
5375 eol_hl_off = 1;
5376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
Bram Moolenaar91170f82006-05-05 21:15:17 +00005380 /*
5381 * At end of the text line.
5382 */
5383 if (c == NUL)
5384 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005385#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005386 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5387 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005388 {
5389 /* highlight last char after line */
5390 --col;
5391 --off;
5392 --vcol;
5393 }
5394
Bram Moolenaar1a384422010-07-14 19:53:30 +02005395 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005396 if (wp->w_p_wrap)
5397 v = wp->w_skipcol;
5398 else
5399 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005400
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005401 /* check if line ends before left margin */
5402 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005403 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005404#ifdef FEAT_CONCEAL
5405 /* Get rid of the boguscols now, we want to draw until the right
5406 * edge for 'cursorcolumn'. */
5407 col -= boguscols;
5408 boguscols = 0;
5409#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005410
5411 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005412 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005413
5414 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005415 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5416 && (int)wp->w_virtcol <
5417 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005418 && lnum != wp->w_cursor.lnum)
5419 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005420# ifdef FEAT_RIGHTLEFT
5421 && !wp->w_p_rl
5422# endif
5423 )
5424 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005425 int rightmost_vcol = 0;
5426 int i;
5427
5428 if (wp->w_p_cuc)
5429 rightmost_vcol = wp->w_virtcol;
5430 if (draw_color_col)
5431 /* determine rightmost colorcolumn to possibly draw */
5432 for (i = 0; color_cols[i] >= 0; ++i)
5433 if (rightmost_vcol < color_cols[i])
5434 rightmost_vcol = color_cols[i];
5435
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005436 while (col < W_WIDTH(wp))
5437 {
5438 ScreenLines[off] = ' ';
5439#ifdef FEAT_MBYTE
5440 if (enc_utf8)
5441 ScreenLinesUC[off] = 0;
5442#endif
5443 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005444 if (draw_color_col)
5445 draw_color_col = advance_color_col(VCOL_HLC,
5446 &color_cols);
5447
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005448 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005449 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005450 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005451 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005452 else
5453 ScreenAttrs[off++] = 0;
5454
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005455 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005456 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005457
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005458 ++vcol;
5459 }
5460 }
5461#endif
5462
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005463 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005464 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 row++;
5466
5467 /*
5468 * Update w_cline_height and w_cline_folded if the cursor line was
5469 * updated (saves a call to plines() later).
5470 */
5471 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5472 {
5473 curwin->w_cline_row = startrow;
5474 curwin->w_cline_height = row - startrow;
5475#ifdef FEAT_FOLDING
5476 curwin->w_cline_folded = FALSE;
5477#endif
5478 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5479 }
5480
5481 break;
5482 }
5483
5484 /* line continues beyond line end */
5485 if (lcs_ext
5486 && !wp->w_p_wrap
5487#ifdef FEAT_DIFF
5488 && filler_todo <= 0
5489#endif
5490 && (
5491#ifdef FEAT_RIGHTLEFT
5492 wp->w_p_rl ? col == 0 :
5493#endif
5494 col == W_WIDTH(wp) - 1)
5495 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005496 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5498 {
5499 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005500 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501#ifdef FEAT_MBYTE
5502 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005503 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
5505 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005506 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005507 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 }
5509 else
5510 mb_utf8 = FALSE;
5511#endif
5512 }
5513
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005514#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005515 /* advance to the next 'colorcolumn' */
5516 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005517 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005518
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005519 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005520 * highlight the cursor position itself.
5521 * Also highlight the 'colorcolumn' if it is different than
5522 * 'cursorcolumn' */
5523 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005524 if (draw_state == WL_LINE && !lnum_in_visual_area
5525 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005526 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005527 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005528 && lnum != wp->w_cursor.lnum)
5529 {
5530 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005531 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005532 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005533 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005534 {
5535 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005536 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005537 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005538 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005539#endif
5540
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 /*
5542 * Store character to be displayed.
5543 * Skip characters that are left of the screen for 'nowrap'.
5544 */
5545 vcol_prev = vcol;
5546 if (draw_state < WL_LINE || n_skip <= 0)
5547 {
5548 /*
5549 * Store the character.
5550 */
5551#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5552 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5553 {
5554 /* A double-wide character is: put first halve in left cell. */
5555 --off;
5556 --col;
5557 }
5558#endif
5559 ScreenLines[off] = c;
5560#ifdef FEAT_MBYTE
5561 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005562 {
5563 if ((mb_c & 0xff00) == 0x8e00)
5564 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 else if (enc_utf8)
5568 {
5569 if (mb_utf8)
5570 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005571 int i;
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005574 if ((c & 0xff) == 0)
5575 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005576 for (i = 0; i < Screen_mco; ++i)
5577 {
5578 ScreenLinesC[i][off] = u8cc[i];
5579 if (u8cc[i] == 0)
5580 break;
5581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583 else
5584 ScreenLinesUC[off] = 0;
5585 }
5586 if (multi_attr)
5587 {
5588 ScreenAttrs[off] = multi_attr;
5589 multi_attr = 0;
5590 }
5591 else
5592#endif
5593 ScreenAttrs[off] = char_attr;
5594
5595#ifdef FEAT_MBYTE
5596 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5597 {
5598 /* Need to fill two screen columns. */
5599 ++off;
5600 ++col;
5601 if (enc_utf8)
5602 /* UTF-8: Put a 0 in the second screen char. */
5603 ScreenLines[off] = 0;
5604 else
5605 /* DBCS: Put second byte in the second screen char. */
5606 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005607 if (draw_state > WL_NR
5608#ifdef FEAT_DIFF
5609 && filler_todo <= 0
5610#endif
5611 )
5612 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 /* When "tocol" is halfway a character, set it to the end of
5614 * the character, otherwise highlighting won't stop. */
5615 if (tocol == vcol)
5616 ++tocol;
5617#ifdef FEAT_RIGHTLEFT
5618 if (wp->w_p_rl)
5619 {
5620 /* now it's time to backup one cell */
5621 --off;
5622 --col;
5623 }
5624#endif
5625 }
5626#endif
5627#ifdef FEAT_RIGHTLEFT
5628 if (wp->w_p_rl)
5629 {
5630 --off;
5631 --col;
5632 }
5633 else
5634#endif
5635 {
5636 ++off;
5637 ++col;
5638 }
5639 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005640#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005641 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005642 {
5643 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005644 ++vcol_off;
5645 if (n_extra > 0)
5646 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005647 if (wp->w_p_wrap)
5648 {
5649 /*
5650 * Special voodoo required if 'wrap' is on.
5651 *
5652 * Advance the column indicator to force the line
5653 * drawing to wrap early. This will make the line
5654 * take up the same screen space when parts are concealed,
5655 * so that cursor line computations aren't messed up.
5656 *
5657 * To avoid the fictitious advance of 'col' causing
5658 * trailing junk to be written out of the screen line
5659 * we are building, 'boguscols' keeps track of the number
5660 * of bad columns we have advanced.
5661 */
5662 if (n_extra > 0)
5663 {
5664 vcol += n_extra;
5665# ifdef FEAT_RIGHTLEFT
5666 if (wp->w_p_rl)
5667 {
5668 col -= n_extra;
5669 boguscols -= n_extra;
5670 }
5671 else
5672# endif
5673 {
5674 col += n_extra;
5675 boguscols += n_extra;
5676 }
5677 n_extra = 0;
5678 n_attr = 0;
5679 }
5680
5681
5682# ifdef FEAT_MBYTE
5683 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5684 {
5685 /* Need to fill two screen columns. */
5686# ifdef FEAT_RIGHTLEFT
5687 if (wp->w_p_rl)
5688 {
5689 --boguscols;
5690 --col;
5691 }
5692 else
5693# endif
5694 {
5695 ++boguscols;
5696 ++col;
5697 }
5698 }
5699# endif
5700
5701# ifdef FEAT_RIGHTLEFT
5702 if (wp->w_p_rl)
5703 {
5704 --boguscols;
5705 --col;
5706 }
5707 else
5708# endif
5709 {
5710 ++boguscols;
5711 ++col;
5712 }
5713 }
5714 else
5715 {
5716 if (n_extra > 0)
5717 {
5718 vcol += n_extra;
5719 n_extra = 0;
5720 n_attr = 0;
5721 }
5722 }
5723
5724 }
5725#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 else
5727 --n_skip;
5728
Bram Moolenaar64486672010-05-16 15:46:46 +02005729 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5730 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005731 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732#ifdef FEAT_DIFF
5733 && filler_todo <= 0
5734#endif
5735 )
5736 ++vcol;
5737
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005738#ifdef FEAT_SYN_HL
5739 if (vcol_save_attr >= 0)
5740 char_attr = vcol_save_attr;
5741#endif
5742
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 /* restore attributes after "predeces" in 'listchars' */
5744 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5745 char_attr = saved_attr3;
5746
5747 /* restore attributes after last 'listchars' or 'number' char */
5748 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5749 char_attr = saved_attr2;
5750
5751 /*
5752 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005753 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 */
5755 if ((
5756#ifdef FEAT_RIGHTLEFT
5757 wp->w_p_rl ? (col < 0) :
5758#endif
5759 (col >= W_WIDTH(wp)))
5760 && (*ptr != NUL
5761#ifdef FEAT_DIFF
5762 || filler_todo > 0
5763#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005764 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5766 )
5767 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005768#ifdef FEAT_CONCEAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005769 screen_line(screen_row, W_WINCOL(wp), col - boguscols,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005770 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005771 boguscols = 0;
5772#else
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005773 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005774 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 ++row;
5777 ++screen_row;
5778
5779 /* When not wrapping and finished diff lines, or when displayed
5780 * '$' and highlighting until last column, break here. */
5781 if ((!wp->w_p_wrap
5782#ifdef FEAT_DIFF
5783 && filler_todo <= 0
5784#endif
5785 ) || lcs_eol_one == -1)
5786 break;
5787
5788 /* When the window is too narrow draw all "@" lines. */
5789 if (draw_state != WL_LINE
5790#ifdef FEAT_DIFF
5791 && filler_todo <= 0
5792#endif
5793 )
5794 {
5795 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 row = endrow;
5798 }
5799
5800 /* When line got too long for screen break here. */
5801 if (row == endrow)
5802 {
5803 ++row;
5804 break;
5805 }
5806
5807 if (screen_cur_row == screen_row - 1
5808#ifdef FEAT_DIFF
5809 && filler_todo <= 0
5810#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005811 && W_WIDTH(wp) == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 {
5813 /* Remember that the line wraps, used for modeless copy. */
5814 LineWraps[screen_row - 1] = TRUE;
5815
5816 /*
5817 * Special trick to make copy/paste of wrapped lines work with
5818 * xterm/screen: write an extra character beyond the end of
5819 * the line. This will work with all terminal types
5820 * (regardless of the xn,am settings).
5821 * Only do this on a fast tty.
5822 * Only do this if the cursor is on the current line
5823 * (something has been written in it).
5824 * Don't do this for the GUI.
5825 * Don't do this for double-width characters.
5826 * Don't do this for a window not at the right screen border.
5827 */
5828 if (p_tf
5829#ifdef FEAT_GUI
5830 && !gui.in_use
5831#endif
5832#ifdef FEAT_MBYTE
5833 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005834 && ((*mb_off2cells)(LineOffset[screen_row],
5835 LineOffset[screen_row] + screen_Columns)
5836 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005838 + (int)Columns - 2,
5839 LineOffset[screen_row] + screen_Columns)
5840 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841#endif
5842 )
5843 {
5844 /* First make sure we are at the end of the screen line,
5845 * then output the same character again to let the
5846 * terminal know about the wrap. If the terminal doesn't
5847 * auto-wrap, we overwrite the character. */
5848 if (screen_cur_col != W_WIDTH(wp))
5849 screen_char(LineOffset[screen_row - 1]
5850 + (unsigned)Columns - 1,
5851 screen_row - 1, (int)(Columns - 1));
5852
5853#ifdef FEAT_MBYTE
5854 /* When there is a multi-byte character, just output a
5855 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005856 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5857 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 out_char(' ');
5859 else
5860#endif
5861 out_char(ScreenLines[LineOffset[screen_row - 1]
5862 + (Columns - 1)]);
5863 /* force a redraw of the first char on the next line */
5864 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5865 screen_start(); /* don't know where cursor is now */
5866 }
5867 }
5868
5869 col = 0;
5870 off = (unsigned)(current_ScreenLine - ScreenLines);
5871#ifdef FEAT_RIGHTLEFT
5872 if (wp->w_p_rl)
5873 {
5874 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5875 off += col;
5876 }
5877#endif
5878
5879 /* reset the drawing state for the start of a wrapped line */
5880 draw_state = WL_START;
5881 saved_n_extra = n_extra;
5882 saved_p_extra = p_extra;
5883 saved_c_extra = c_extra;
5884 saved_char_attr = char_attr;
5885 n_extra = 0;
5886 lcs_prec_todo = lcs_prec;
5887#ifdef FEAT_LINEBREAK
5888# ifdef FEAT_DIFF
5889 if (filler_todo <= 0)
5890# endif
5891 need_showbreak = TRUE;
5892#endif
5893#ifdef FEAT_DIFF
5894 --filler_todo;
5895 /* When the filler lines are actually below the last line of the
5896 * file, don't draw the line itself, break here. */
5897 if (filler_todo == 0 && wp->w_botfill)
5898 break;
5899#endif
5900 }
5901
5902 } /* for every character in the line */
5903
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005904#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005905 /* After an empty line check first word for capital. */
5906 if (*skipwhite(line) == NUL)
5907 {
5908 capcol_lnum = lnum + 1;
5909 cap_col = 0;
5910 }
5911#endif
5912
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005913 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 return row;
5915}
5916
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005917#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005918static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005919
5920/*
5921 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005922 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005923 */
5924 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005925comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005926{
5927 int i;
5928
5929 for (i = 0; i < Screen_mco; ++i)
5930 {
5931 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5932 return TRUE;
5933 if (ScreenLinesC[i][off_from] == 0)
5934 break;
5935 }
5936 return FALSE;
5937}
5938#endif
5939
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940/*
5941 * Check whether the given character needs redrawing:
5942 * - the (first byte of the) character is different
5943 * - the attributes are different
5944 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005945 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 */
5947 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005948char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949{
5950 if (cols > 0
5951 && ((ScreenLines[off_from] != ScreenLines[off_to]
5952 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5953
5954#ifdef FEAT_MBYTE
5955 || (enc_dbcs != 0
5956 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5957 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5958 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5959 : (cols > 1 && ScreenLines[off_from + 1]
5960 != ScreenLines[off_to + 1])))
5961 || (enc_utf8
5962 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5963 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005964 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005965 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5966 && ScreenLines[off_from + 1]
5967 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968#endif
5969 ))
5970 return TRUE;
5971 return FALSE;
5972}
5973
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005974#if defined(FEAT_TERMINAL) || defined(PROTO)
5975/*
5976 * Return the index in ScreenLines[] for the current screen line.
5977 */
5978 int
5979screen_get_current_line_off()
5980{
5981 return (int)(current_ScreenLine - ScreenLines);
5982}
5983#endif
5984
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985/*
5986 * Move one "cooked" screen line to the screen, but only the characters that
5987 * have actually changed. Handle insert/delete character.
5988 * "coloff" gives the first column on the screen for this line.
5989 * "endcol" gives the columns where valid characters are.
5990 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5991 * needs to be cleared, negative otherwise.
5992 * "rlflag" is TRUE in a rightleft window:
5993 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5994 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5995 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005996 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005997screen_line(
5998 int row,
5999 int coloff,
6000 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006001 int clear_width,
6002 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003{
6004 unsigned off_from;
6005 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006006#ifdef FEAT_MBYTE
6007 unsigned max_off_from;
6008 unsigned max_off_to;
6009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 int force = FALSE; /* force update rest of the line */
6013 int redraw_this /* bool: does character need redraw? */
6014#ifdef FEAT_GUI
6015 = TRUE /* For GUI when while-loop empty */
6016#endif
6017 ;
6018 int redraw_next; /* redraw_this for next character */
6019#ifdef FEAT_MBYTE
6020 int clear_next = FALSE;
6021 int char_cells; /* 1: normal char */
6022 /* 2: occupies two display cells */
6023# define CHAR_CELLS char_cells
6024#else
6025# define CHAR_CELLS 1
6026#endif
6027
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006028 /* Check for illegal row and col, just in case. */
6029 if (row >= Rows)
6030 row = Rows - 1;
6031 if (endcol > Columns)
6032 endcol = Columns;
6033
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034# ifdef FEAT_CLIPBOARD
6035 clip_may_clear_selection(row, row);
6036# endif
6037
6038 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6039 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006040#ifdef FEAT_MBYTE
6041 max_off_from = off_from + screen_Columns;
6042 max_off_to = LineOffset[row] + screen_Columns;
6043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044
6045#ifdef FEAT_RIGHTLEFT
6046 if (rlflag)
6047 {
6048 /* Clear rest first, because it's left of the text. */
6049 if (clear_width > 0)
6050 {
6051 while (col <= endcol && ScreenLines[off_to] == ' '
6052 && ScreenAttrs[off_to] == 0
6053# ifdef FEAT_MBYTE
6054 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6055# endif
6056 )
6057 {
6058 ++off_to;
6059 ++col;
6060 }
6061 if (col <= endcol)
6062 screen_fill(row, row + 1, col + coloff,
6063 endcol + coloff + 1, ' ', ' ', 0);
6064 }
6065 col = endcol + 1;
6066 off_to = LineOffset[row] + col + coloff;
6067 off_from += col;
6068 endcol = (clear_width > 0 ? clear_width : -clear_width);
6069 }
6070#endif /* FEAT_RIGHTLEFT */
6071
6072 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6073
6074 while (col < endcol)
6075 {
6076#ifdef FEAT_MBYTE
6077 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006078 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 else
6080 char_cells = 1;
6081#endif
6082
6083 redraw_this = redraw_next;
6084 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6085 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6086
6087#ifdef FEAT_GUI
6088 /* If the next character was bold, then redraw the current character to
6089 * remove any pixels that might have spilt over into us. This only
6090 * happens in the GUI.
6091 */
6092 if (redraw_next && gui.in_use)
6093 {
6094 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006095 if (hl > HL_ALL)
6096 hl = syn_attr2attr(hl);
6097 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 redraw_this = TRUE;
6099 }
6100#endif
6101
6102 if (redraw_this)
6103 {
6104 /*
6105 * Special handling when 'xs' termcap flag set (hpterm):
6106 * Attributes for characters are stored at the position where the
6107 * cursor is when writing the highlighting code. The
6108 * start-highlighting code must be written with the cursor on the
6109 * first highlighted character. The stop-highlighting code must
6110 * be written with the cursor just after the last highlighted
6111 * character.
6112 * Overwriting a character doesn't remove it's highlighting. Need
6113 * to clear the rest of the line, and force redrawing it
6114 * completely.
6115 */
6116 if ( p_wiv
6117 && !force
6118#ifdef FEAT_GUI
6119 && !gui.in_use
6120#endif
6121 && ScreenAttrs[off_to] != 0
6122 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6123 {
6124 /*
6125 * Need to remove highlighting attributes here.
6126 */
6127 windgoto(row, col + coloff);
6128 out_str(T_CE); /* clear rest of this screen line */
6129 screen_start(); /* don't know where cursor is now */
6130 force = TRUE; /* force redraw of rest of the line */
6131 redraw_next = TRUE; /* or else next char would miss out */
6132
6133 /*
6134 * If the previous character was highlighted, need to stop
6135 * highlighting at this character.
6136 */
6137 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6138 {
6139 screen_attr = ScreenAttrs[off_to - 1];
6140 term_windgoto(row, col + coloff);
6141 screen_stop_highlight();
6142 }
6143 else
6144 screen_attr = 0; /* highlighting has stopped */
6145 }
6146#ifdef FEAT_MBYTE
6147 if (enc_dbcs != 0)
6148 {
6149 /* Check if overwriting a double-byte with a single-byte or
6150 * the other way around requires another character to be
6151 * redrawn. For UTF-8 this isn't needed, because comparing
6152 * ScreenLinesUC[] is sufficient. */
6153 if (char_cells == 1
6154 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006155 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 {
6157 /* Writing a single-cell character over a double-cell
6158 * character: need to redraw the next cell. */
6159 ScreenLines[off_to + 1] = 0;
6160 redraw_next = TRUE;
6161 }
6162 else if (char_cells == 2
6163 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006164 && (*mb_off2cells)(off_to, max_off_to) == 1
6165 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 {
6167 /* Writing the second half of a double-cell character over
6168 * a double-cell character: need to redraw the second
6169 * cell. */
6170 ScreenLines[off_to + 2] = 0;
6171 redraw_next = TRUE;
6172 }
6173
6174 if (enc_dbcs == DBCS_JPNU)
6175 ScreenLines2[off_to] = ScreenLines2[off_from];
6176 }
6177 /* When writing a single-width character over a double-width
6178 * character and at the end of the redrawn text, need to clear out
6179 * the right halve of the old character.
6180 * Also required when writing the right halve of a double-width
6181 * char over the left halve of an existing one. */
6182 if (has_mbyte && col + char_cells == endcol
6183 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006184 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006186 && (*mb_off2cells)(off_to, max_off_to) == 1
6187 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 clear_next = TRUE;
6189#endif
6190
6191 ScreenLines[off_to] = ScreenLines[off_from];
6192#ifdef FEAT_MBYTE
6193 if (enc_utf8)
6194 {
6195 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6196 if (ScreenLinesUC[off_from] != 0)
6197 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006198 int i;
6199
6200 for (i = 0; i < Screen_mco; ++i)
6201 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 }
6203 }
6204 if (char_cells == 2)
6205 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6206#endif
6207
6208#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006209 /* The bold trick makes a single column of pixels appear in the
6210 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 * character should be redrawn too. This happens for our own GUI
6212 * and for some xterms. */
6213 if (
6214# ifdef FEAT_GUI
6215 gui.in_use
6216# endif
6217# if defined(FEAT_GUI) && defined(UNIX)
6218 ||
6219# endif
6220# ifdef UNIX
6221 term_is_xterm
6222# endif
6223 )
6224 {
6225 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006226 if (hl > HL_ALL)
6227 hl = syn_attr2attr(hl);
6228 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 redraw_next = TRUE;
6230 }
6231#endif
6232 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6233#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006234 /* For simplicity set the attributes of second half of a
6235 * double-wide character equal to the first half. */
6236 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006238
6239 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 else
6242#endif
6243 screen_char(off_to, row, col + coloff);
6244 }
6245 else if ( p_wiv
6246#ifdef FEAT_GUI
6247 && !gui.in_use
6248#endif
6249 && col + coloff > 0)
6250 {
6251 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6252 {
6253 /*
6254 * Don't output stop-highlight when moving the cursor, it will
6255 * stop the highlighting when it should continue.
6256 */
6257 screen_attr = 0;
6258 }
6259 else if (screen_attr != 0)
6260 screen_stop_highlight();
6261 }
6262
6263 off_to += CHAR_CELLS;
6264 off_from += CHAR_CELLS;
6265 col += CHAR_CELLS;
6266 }
6267
6268#ifdef FEAT_MBYTE
6269 if (clear_next)
6270 {
6271 /* Clear the second half of a double-wide character of which the left
6272 * half was overwritten with a single-wide character. */
6273 ScreenLines[off_to] = ' ';
6274 if (enc_utf8)
6275 ScreenLinesUC[off_to] = 0;
6276 screen_char(off_to, row, col + coloff);
6277 }
6278#endif
6279
6280 if (clear_width > 0
6281#ifdef FEAT_RIGHTLEFT
6282 && !rlflag
6283#endif
6284 )
6285 {
6286#ifdef FEAT_GUI
6287 int startCol = col;
6288#endif
6289
6290 /* blank out the rest of the line */
6291 while (col < clear_width && ScreenLines[off_to] == ' '
6292 && ScreenAttrs[off_to] == 0
6293#ifdef FEAT_MBYTE
6294 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6295#endif
6296 )
6297 {
6298 ++off_to;
6299 ++col;
6300 }
6301 if (col < clear_width)
6302 {
6303#ifdef FEAT_GUI
6304 /*
6305 * In the GUI, clearing the rest of the line may leave pixels
6306 * behind if the first character cleared was bold. Some bold
6307 * fonts spill over the left. In this case we redraw the previous
6308 * character too. If we didn't skip any blanks above, then we
6309 * only redraw if the character wasn't already redrawn anyway.
6310 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006311 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 {
6313 hl = ScreenAttrs[off_to];
6314 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006315 {
6316 int prev_cells = 1;
6317# ifdef FEAT_MBYTE
6318 if (enc_utf8)
6319 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6320 * that its width is 2. */
6321 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6322 else if (enc_dbcs != 0)
6323 {
6324 /* find previous character by counting from first
6325 * column and get its width. */
6326 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006327 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006328
6329 while (off < off_to)
6330 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006331 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006332 off += prev_cells;
6333 }
6334 }
6335
6336 if (enc_dbcs != 0 && prev_cells > 1)
6337 screen_char_2(off_to - prev_cells, row,
6338 col + coloff - prev_cells);
6339 else
6340# endif
6341 screen_char(off_to - prev_cells, row,
6342 col + coloff - prev_cells);
6343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 }
6345#endif
6346 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6347 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 off_to += clear_width - col;
6349 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 }
6351 }
6352
6353 if (clear_width > 0)
6354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 /* For a window that's left of another, draw the separator char. */
6356 if (col + coloff < Columns)
6357 {
6358 int c;
6359
6360 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006361 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006362#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006363 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6364 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 || ScreenAttrs[off_to] != hl)
6367 {
6368 ScreenLines[off_to] = c;
6369 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006370#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 if (enc_utf8)
6372 {
6373 if (c >= 0x80)
6374 {
6375 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006376 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 }
6378 else
6379 ScreenLinesUC[off_to] = 0;
6380 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 screen_char(off_to, row, col + coloff);
6383 }
6384 }
6385 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 LineWraps[row] = FALSE;
6387 }
6388}
6389
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006390#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006392 * Mirror text "str" for right-left displaying.
6393 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006395 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006396rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397{
6398 char_u *p1, *p2;
6399 int t;
6400
6401 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6402 {
6403 t = *p1;
6404 *p1 = *p2;
6405 *p2 = t;
6406 }
6407}
6408#endif
6409
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410/*
6411 * mark all status lines for redraw; used after first :cd
6412 */
6413 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006414status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
6416 win_T *wp;
6417
Bram Moolenaar29323592016-07-24 22:04:11 +02006418 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 if (wp->w_status_height)
6420 {
6421 wp->w_redr_status = TRUE;
6422 redraw_later(VALID);
6423 }
6424}
6425
6426/*
6427 * mark all status lines of the current buffer for redraw
6428 */
6429 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006430status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431{
6432 win_T *wp;
6433
Bram Moolenaar29323592016-07-24 22:04:11 +02006434 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6436 {
6437 wp->w_redr_status = TRUE;
6438 redraw_later(VALID);
6439 }
6440}
6441
6442/*
6443 * Redraw all status lines that need to be redrawn.
6444 */
6445 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006446redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
6448 win_T *wp;
6449
Bram Moolenaar29323592016-07-24 22:04:11 +02006450 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 if (wp->w_redr_status)
6452 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006453 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006454 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456
Bram Moolenaar4033c552017-09-16 20:54:51 +02006457#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458/*
6459 * Redraw all status lines at the bottom of frame "frp".
6460 */
6461 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006462win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463{
6464 if (frp->fr_layout == FR_LEAF)
6465 frp->fr_win->w_redr_status = TRUE;
6466 else if (frp->fr_layout == FR_ROW)
6467 {
6468 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6469 win_redraw_last_status(frp);
6470 }
6471 else /* frp->fr_layout == FR_COL */
6472 {
6473 frp = frp->fr_child;
6474 while (frp->fr_next != NULL)
6475 frp = frp->fr_next;
6476 win_redraw_last_status(frp);
6477 }
6478}
6479#endif
6480
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481/*
6482 * Draw the verticap separator right of window "wp" starting with line "row".
6483 */
6484 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006485draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486{
6487 int hl;
6488 int c;
6489
6490 if (wp->w_vsep_width)
6491 {
6492 /* draw the vertical separator right of this window */
6493 c = fillchar_vsep(&hl);
6494 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6495 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6496 c, ' ', hl);
6497 }
6498}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
6500#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006501static int status_match_len(expand_T *xp, char_u *s);
6502static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
6504/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006505 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 */
6507 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006508status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
6510 int len = 0;
6511
6512#ifdef FEAT_MENU
6513 int emenu = (xp->xp_context == EXPAND_MENUS
6514 || xp->xp_context == EXPAND_MENUNAMES);
6515
6516 /* Check for menu separators - replace with '|'. */
6517 if (emenu && menu_is_separator(s))
6518 return 1;
6519#endif
6520
6521 while (*s != NUL)
6522 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006523 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006524 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006525 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 }
6527
6528 return len;
6529}
6530
6531/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006532 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006533 * These are backslashes used for escaping. Do show backslashes in help tags.
6534 */
6535 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006536skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006537{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006538 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006539#ifdef FEAT_MENU
6540 || ((xp->xp_context == EXPAND_MENUS
6541 || xp->xp_context == EXPAND_MENUNAMES)
6542 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6543#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006544 )
6545 {
6546#ifndef BACKSLASH_IN_FILENAME
6547 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6548 return 2;
6549#endif
6550 return 1;
6551 }
6552 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006553}
6554
6555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 * Show wildchar matches in the status line.
6557 * Show at least the "match" item.
6558 * We start at item 'first_match' in the list and show all matches that fit.
6559 *
6560 * If inversion is possible we use it. Else '=' characters are used.
6561 */
6562 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006563win_redr_status_matches(
6564 expand_T *xp,
6565 int num_matches,
6566 char_u **matches, /* list of matches */
6567 int match,
6568 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
6570#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6571 int row;
6572 char_u *buf;
6573 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006574 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 int fillchar;
6576 int attr;
6577 int i;
6578 int highlight = TRUE;
6579 char_u *selstart = NULL;
6580 int selstart_col = 0;
6581 char_u *selend = NULL;
6582 static int first_match = 0;
6583 int add_left = FALSE;
6584 char_u *s;
6585#ifdef FEAT_MENU
6586 int emenu;
6587#endif
6588#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6589 int l;
6590#endif
6591
6592 if (matches == NULL) /* interrupted completion? */
6593 return;
6594
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006595#ifdef FEAT_MBYTE
6596 if (has_mbyte)
6597 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6598 else
6599#endif
6600 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 if (buf == NULL)
6602 return;
6603
6604 if (match == -1) /* don't show match but original text */
6605 {
6606 match = 0;
6607 highlight = FALSE;
6608 }
6609 /* count 1 for the ending ">" */
6610 clen = status_match_len(xp, L_MATCH(match)) + 3;
6611 if (match == 0)
6612 first_match = 0;
6613 else if (match < first_match)
6614 {
6615 /* jumping left, as far as we can go */
6616 first_match = match;
6617 add_left = TRUE;
6618 }
6619 else
6620 {
6621 /* check if match fits on the screen */
6622 for (i = first_match; i < match; ++i)
6623 clen += status_match_len(xp, L_MATCH(i)) + 2;
6624 if (first_match > 0)
6625 clen += 2;
6626 /* jumping right, put match at the left */
6627 if ((long)clen > Columns)
6628 {
6629 first_match = match;
6630 /* if showing the last match, we can add some on the left */
6631 clen = 2;
6632 for (i = match; i < num_matches; ++i)
6633 {
6634 clen += status_match_len(xp, L_MATCH(i)) + 2;
6635 if ((long)clen >= Columns)
6636 break;
6637 }
6638 if (i == num_matches)
6639 add_left = TRUE;
6640 }
6641 }
6642 if (add_left)
6643 while (first_match > 0)
6644 {
6645 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6646 if ((long)clen >= Columns)
6647 break;
6648 --first_match;
6649 }
6650
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006651 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653 if (first_match == 0)
6654 {
6655 *buf = NUL;
6656 len = 0;
6657 }
6658 else
6659 {
6660 STRCPY(buf, "< ");
6661 len = 2;
6662 }
6663 clen = len;
6664
6665 i = first_match;
6666 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6667 {
6668 if (i == match)
6669 {
6670 selstart = buf + len;
6671 selstart_col = clen;
6672 }
6673
6674 s = L_MATCH(i);
6675 /* Check for menu separators - replace with '|' */
6676#ifdef FEAT_MENU
6677 emenu = (xp->xp_context == EXPAND_MENUS
6678 || xp->xp_context == EXPAND_MENUNAMES);
6679 if (emenu && menu_is_separator(s))
6680 {
6681 STRCPY(buf + len, transchar('|'));
6682 l = (int)STRLEN(buf + len);
6683 len += l;
6684 clen += l;
6685 }
6686 else
6687#endif
6688 for ( ; *s != NUL; ++s)
6689 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006690 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 clen += ptr2cells(s);
6692#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006693 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 {
6695 STRNCPY(buf + len, s, l);
6696 s += l - 1;
6697 len += l;
6698 }
6699 else
6700#endif
6701 {
6702 STRCPY(buf + len, transchar_byte(*s));
6703 len += (int)STRLEN(buf + len);
6704 }
6705 }
6706 if (i == match)
6707 selend = buf + len;
6708
6709 *(buf + len++) = ' ';
6710 *(buf + len++) = ' ';
6711 clen += 2;
6712 if (++i == num_matches)
6713 break;
6714 }
6715
6716 if (i != num_matches)
6717 {
6718 *(buf + len++) = '>';
6719 ++clen;
6720 }
6721
6722 buf[len] = NUL;
6723
6724 row = cmdline_row - 1;
6725 if (row >= 0)
6726 {
6727 if (wild_menu_showing == 0)
6728 {
6729 if (msg_scrolled > 0)
6730 {
6731 /* Put the wildmenu just above the command line. If there is
6732 * no room, scroll the screen one line up. */
6733 if (cmdline_row == Rows - 1)
6734 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006735 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 ++msg_scrolled;
6737 }
6738 else
6739 {
6740 ++cmdline_row;
6741 ++row;
6742 }
6743 wild_menu_showing = WM_SCROLLED;
6744 }
6745 else
6746 {
6747 /* Create status line if needed by setting 'laststatus' to 2.
6748 * Set 'winminheight' to zero to avoid that the window is
6749 * resized. */
6750 if (lastwin->w_status_height == 0)
6751 {
6752 save_p_ls = p_ls;
6753 save_p_wmh = p_wmh;
6754 p_ls = 2;
6755 p_wmh = 0;
6756 last_status(FALSE);
6757 }
6758 wild_menu_showing = WM_SHOWN;
6759 }
6760 }
6761
6762 screen_puts(buf, row, 0, attr);
6763 if (selstart != NULL && highlight)
6764 {
6765 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006766 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 }
6768
6769 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6770 }
6771
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 vim_free(buf);
6774}
6775#endif
6776
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777/*
6778 * Redraw the status line of window wp.
6779 *
6780 * If inversion is possible we use it. Else '=' characters are used.
6781 */
6782 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006783win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 int row;
6786 char_u *p;
6787 int len;
6788 int fillchar;
6789 int attr;
6790 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006791 static int busy = FALSE;
6792
6793 /* It's possible to get here recursively when 'statusline' (indirectly)
6794 * invokes ":redrawstatus". Simply ignore the call then. */
6795 if (busy)
6796 return;
6797 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
6799 wp->w_redr_status = FALSE;
6800 if (wp->w_status_height == 0)
6801 {
6802 /* no status line, can only be last window */
6803 redraw_cmdline = TRUE;
6804 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006805 else if (!redrawing()
6806#ifdef FEAT_INS_EXPAND
6807 /* don't update status line when popup menu is visible and may be
6808 * drawn over it */
6809 || pum_visible()
6810#endif
6811 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
6813 /* Don't redraw right now, do it later. */
6814 wp->w_redr_status = TRUE;
6815 }
6816#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006817 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 {
6819 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006820 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 }
6822#endif
6823 else
6824 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006825 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006827 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 p = NameBuff;
6829 len = (int)STRLEN(p);
6830
Bram Moolenaard85f2712017-07-28 21:51:57 +02006831 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832#ifdef FEAT_QUICKFIX
6833 || wp->w_p_pvw
6834#endif
6835 || bufIsChanged(wp->w_buffer)
6836 || wp->w_buffer->b_p_ro)
6837 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006838 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006840 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 len += (int)STRLEN(p + len);
6842 }
6843#ifdef FEAT_QUICKFIX
6844 if (wp->w_p_pvw)
6845 {
6846 STRCPY(p + len, _("[Preview]"));
6847 len += (int)STRLEN(p + len);
6848 }
6849#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006850 if (bufIsChanged(wp->w_buffer)
6851#ifdef FEAT_TERMINAL
6852 && !bt_terminal(wp->w_buffer)
6853#endif
6854 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {
6856 STRCPY(p + len, "[+]");
6857 len += 3;
6858 }
6859 if (wp->w_buffer->b_p_ro)
6860 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006861 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006862 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 }
6864
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6866 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6867 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6868 if (this_ru_col <= 1)
6869 {
6870 p = (char_u *)"<"; /* No room for file name! */
6871 len = 1;
6872 }
6873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874#ifdef FEAT_MBYTE
6875 if (has_mbyte)
6876 {
6877 int clen = 0, i;
6878
6879 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006880 clen = mb_string2cells(p, -1);
6881
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 /* Find first character that will fit.
6883 * Going from start to end is much faster for DBCS. */
6884 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006885 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 clen -= (*mb_ptr2cells)(p + i);
6887 len = clen;
6888 if (i > 0)
6889 {
6890 p = p + i - 1;
6891 *p = '<';
6892 ++len;
6893 }
6894
6895 }
6896 else
6897#endif
6898 if (len > this_ru_col - 1)
6899 {
6900 p += len - (this_ru_col - 1);
6901 *p = '<';
6902 len = this_ru_col - 1;
6903 }
6904
6905 row = W_WINROW(wp) + wp->w_height;
6906 screen_puts(p, row, W_WINCOL(wp), attr);
6907 screen_fill(row, row + 1, len + W_WINCOL(wp),
6908 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6909
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006910 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6912 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6913 - 1 + W_WINCOL(wp)), attr);
6914
6915#ifdef FEAT_CMDL_INFO
6916 win_redr_ruler(wp, TRUE);
6917#endif
6918 }
6919
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 /*
6921 * May need to draw the character below the vertical separator.
6922 */
6923 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6924 {
6925 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006926 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 else
6928 fillchar = fillchar_vsep(&attr);
6929 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6930 attr);
6931 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006932 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933}
6934
Bram Moolenaar238a5642006-02-21 22:12:05 +00006935#ifdef FEAT_STL_OPT
6936/*
6937 * Redraw the status line according to 'statusline' and take care of any
6938 * errors encountered.
6939 */
6940 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006941redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006942{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006943 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006944 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006945
6946 /* When called recursively return. This can happen when the statusline
6947 * contains an expression that triggers a redraw. */
6948 if (entered)
6949 return;
6950 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006951
Bram Moolenaara742e082016-04-05 21:10:38 +02006952 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006953 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006954 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006955 {
6956 /* When there is an error disable the statusline, otherwise the
6957 * display is messed up with errors and a redraw triggers the problem
6958 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006959 set_string_option_direct((char_u *)"statusline", -1,
6960 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006961 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006962 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006963 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006964 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006965}
6966#endif
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968/*
6969 * Return TRUE if the status line of window "wp" is connected to the status
6970 * line of the window right of it. If not, then it's a vertical separator.
6971 * Only call if (wp->w_vsep_width != 0).
6972 */
6973 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006974stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 frame_T *fr;
6977
6978 fr = wp->w_frame;
6979 while (fr->fr_parent != NULL)
6980 {
6981 if (fr->fr_parent->fr_layout == FR_COL)
6982 {
6983 if (fr->fr_next != NULL)
6984 break;
6985 }
6986 else
6987 {
6988 if (fr->fr_next != NULL)
6989 return TRUE;
6990 }
6991 fr = fr->fr_parent;
6992 }
6993 return FALSE;
6994}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997/*
6998 * Get the value to show for the language mappings, active 'keymap'.
6999 */
7000 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007001get_keymap_str(
7002 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007003 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007004 char_u *buf, /* buffer for the result */
7005 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007 char_u *p;
7008
7009 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7010 return FALSE;
7011
7012 {
7013#ifdef FEAT_EVAL
7014 buf_T *old_curbuf = curbuf;
7015 win_T *old_curwin = curwin;
7016 char_u *s;
7017
7018 curbuf = wp->w_buffer;
7019 curwin = wp;
7020 STRCPY(buf, "b:keymap_name"); /* must be writable */
7021 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007022 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 --emsg_skip;
7024 curbuf = old_curbuf;
7025 curwin = old_curwin;
7026 if (p == NULL || *p == NUL)
7027#endif
7028 {
7029#ifdef FEAT_KEYMAP
7030 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7031 p = wp->w_buffer->b_p_keymap;
7032 else
7033#endif
7034 p = (char_u *)"lang";
7035 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007036 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 buf[0] = NUL;
7038#ifdef FEAT_EVAL
7039 vim_free(s);
7040#endif
7041 }
7042 return buf[0] != NUL;
7043}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
7045#if defined(FEAT_STL_OPT) || defined(PROTO)
7046/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007047 * Redraw the status line or ruler of window "wp".
7048 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 */
7050 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007051win_redr_custom(
7052 win_T *wp,
7053 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007055 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 int attr;
7057 int curattr;
7058 int row;
7059 int col = 0;
7060 int maxwidth;
7061 int width;
7062 int n;
7063 int len;
7064 int fillchar;
7065 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007066 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007068 struct stl_hlrec hltab[STL_MAX_ITEM];
7069 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007070 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007071 win_T *ewp;
7072 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
Bram Moolenaar1d633412013-12-11 15:52:01 +01007074 /* There is a tiny chance that this gets called recursively: When
7075 * redrawing a status line triggers redrawing the ruler or tabline.
7076 * Avoid trouble by not allowing recursion. */
7077 if (entered)
7078 return;
7079 entered = TRUE;
7080
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007082 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007084 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007085 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007086 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007087 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007088 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007089 maxwidth = Columns;
7090# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007091 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007094 else
7095 {
7096 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007097 fillchar = fillchar_status(&attr, wp);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007098 maxwidth = W_WIDTH(wp);
7099
7100 if (draw_ruler)
7101 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007102 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007103 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007104 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007105 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007106 if (*++stl == '-')
7107 stl++;
7108 if (atoi((char *)stl))
7109 while (VIM_ISDIGIT(*stl))
7110 stl++;
7111 if (*stl++ != '(')
7112 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007113 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007114 col = ru_col - (Columns - W_WIDTH(wp));
7115 if (col < (W_WIDTH(wp) + 1) / 2)
7116 col = (W_WIDTH(wp) + 1) / 2;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007117 maxwidth = W_WIDTH(wp) - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007118 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007119 {
7120 row = Rows - 1;
7121 --maxwidth; /* writing in last column may cause scrolling */
7122 fillchar = ' ';
7123 attr = 0;
7124 }
7125
7126# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007127 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007128# endif
7129 }
7130 else
7131 {
7132 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007133 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007134 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007135 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007136# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007137 use_sandbox = was_set_insecurely((char_u *)"statusline",
7138 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007139# endif
7140 }
7141
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007142 col += W_WINCOL(wp);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007143 }
7144
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007146 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
Bram Moolenaar61452852011-02-01 18:01:11 +01007148 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7149 * the cursor away and back. */
7150 ewp = wp == NULL ? curwin : wp;
7151 p_crb_save = ewp->w_p_crb;
7152 ewp->w_p_crb = FALSE;
7153
Bram Moolenaar362f3562009-11-03 16:20:34 +00007154 /* Make a copy, because the statusline may include a function call that
7155 * might change the option value and free the memory. */
7156 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007157 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007158 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007159 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007160 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007161 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007163 /* Make all characters printable. */
7164 p = transstr(buf);
7165 if (p != NULL)
7166 {
7167 vim_strncpy(buf, p, sizeof(buf) - 1);
7168 vim_free(p);
7169 }
7170
7171 /* fill up with "fillchar" */
7172 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007173 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 {
7175#ifdef FEAT_MBYTE
7176 len += (*mb_char2bytes)(fillchar, buf + len);
7177#else
7178 buf[len++] = fillchar;
7179#endif
7180 ++width;
7181 }
7182 buf[len] = NUL;
7183
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007184 /*
7185 * Draw each snippet with the specified highlighting.
7186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 curattr = attr;
7188 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007189 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007191 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 screen_puts_len(p, len, row, col, curattr);
7193 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007194 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007196 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007198 else if (hltab[n].userhl < 0)
7199 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007200#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007201 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7202 && wp->w_status_height != 0)
7203 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007204 else if (wp != NULL && bt_terminal(wp->w_buffer)
7205 && wp->w_status_height != 0)
7206 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007207#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007208 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007209 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007211 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 }
7213 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007214
7215 if (wp == NULL)
7216 {
7217 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7218 col = 0;
7219 len = 0;
7220 p = buf;
7221 fillchar = 0;
7222 for (n = 0; tabtab[n].start != NULL; n++)
7223 {
7224 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7225 while (col < len)
7226 TabPageIdxs[col++] = fillchar;
7227 p = tabtab[n].start;
7228 fillchar = tabtab[n].userhl;
7229 }
7230 while (col < Columns)
7231 TabPageIdxs[col++] = fillchar;
7232 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007233
7234theend:
7235 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236}
7237
7238#endif /* FEAT_STL_OPT */
7239
7240/*
7241 * Output a single character directly to the screen and update ScreenLines.
7242 */
7243 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007244screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 char_u buf[MB_MAXBYTES + 1];
7247
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007248#ifdef FEAT_MBYTE
7249 if (has_mbyte)
7250 buf[(*mb_char2bytes)(c, buf)] = NUL;
7251 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007253 {
7254 buf[0] = c;
7255 buf[1] = NUL;
7256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 screen_puts(buf, row, col, attr);
7258}
7259
7260/*
7261 * Get a single character directly from ScreenLines into "bytes[]".
7262 * Also return its attribute in *attrp;
7263 */
7264 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007265screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266{
7267 unsigned off;
7268
7269 /* safety check */
7270 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7271 {
7272 off = LineOffset[row] + col;
7273 *attrp = ScreenAttrs[off];
7274 bytes[0] = ScreenLines[off];
7275 bytes[1] = NUL;
7276
7277#ifdef FEAT_MBYTE
7278 if (enc_utf8 && ScreenLinesUC[off] != 0)
7279 bytes[utfc_char2bytes(off, bytes)] = NUL;
7280 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7281 {
7282 bytes[0] = ScreenLines[off];
7283 bytes[1] = ScreenLines2[off];
7284 bytes[2] = NUL;
7285 }
7286 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7287 {
7288 bytes[1] = ScreenLines[off + 1];
7289 bytes[2] = NUL;
7290 }
7291#endif
7292 }
7293}
7294
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007295#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007296static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007297
7298/*
7299 * Return TRUE if composing characters for screen posn "off" differs from
7300 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007301 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007302 */
7303 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007304screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007305{
7306 int i;
7307
7308 for (i = 0; i < Screen_mco; ++i)
7309 {
7310 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7311 return TRUE;
7312 if (u8cc[i] == 0)
7313 break;
7314 }
7315 return FALSE;
7316}
7317#endif
7318
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319/*
7320 * Put string '*text' on the screen at position 'row' and 'col', with
7321 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7322 * Note: only outputs within one row, message is truncated at screen boundary!
7323 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7324 */
7325 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007326screen_puts(
7327 char_u *text,
7328 int row,
7329 int col,
7330 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332 screen_puts_len(text, -1, row, col, attr);
7333}
7334
7335/*
7336 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7337 * a NUL.
7338 */
7339 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007340screen_puts_len(
7341 char_u *text,
7342 int textlen,
7343 int row,
7344 int col,
7345 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346{
7347 unsigned off;
7348 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007349 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 int c;
7351#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007352 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 int mbyte_blen = 1;
7354 int mbyte_cells = 1;
7355 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007356 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 int clear_next_cell = FALSE;
7358# ifdef FEAT_ARABIC
7359 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007360 int pc, nc, nc1;
7361 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362# endif
7363#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007364#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7365 int force_redraw_this;
7366 int force_redraw_next = FALSE;
7367#endif
7368 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369
7370 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7371 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007372 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373
Bram Moolenaarc236c162008-07-13 17:41:49 +00007374#ifdef FEAT_MBYTE
7375 /* When drawing over the right halve of a double-wide char clear out the
7376 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007377 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007378# ifdef FEAT_GUI
7379 && !gui.in_use
7380# endif
7381 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007382 {
7383 ScreenLines[off - 1] = ' ';
7384 ScreenAttrs[off - 1] = 0;
7385 if (enc_utf8)
7386 {
7387 ScreenLinesUC[off - 1] = 0;
7388 ScreenLinesC[0][off - 1] = 0;
7389 }
7390 /* redraw the previous cell, make it empty */
7391 screen_char(off - 1, row, col - 1);
7392 /* force the cell at "col" to be redrawn */
7393 force_redraw_next = TRUE;
7394 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007395#endif
7396
Bram Moolenaar367329b2007-08-30 11:53:22 +00007397#ifdef FEAT_MBYTE
7398 max_off = LineOffset[row] + screen_Columns;
7399#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007400 while (col < screen_Columns
7401 && (len < 0 || (int)(ptr - text) < len)
7402 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 {
7404 c = *ptr;
7405#ifdef FEAT_MBYTE
7406 /* check if this is the first byte of a multibyte */
7407 if (has_mbyte)
7408 {
7409 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007410 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007412 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7414 mbyte_cells = 1;
7415 else if (enc_dbcs != 0)
7416 mbyte_cells = mbyte_blen;
7417 else /* enc_utf8 */
7418 {
7419 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007420 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 (int)((text + len) - ptr));
7422 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007423 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007425# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 /* Non-BMP character: display as ? or fullwidth ?. */
7427 if (u8c >= 0x10000)
7428 {
7429 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7430 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007431 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434# ifdef FEAT_ARABIC
7435 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7436 {
7437 /* Do Arabic shaping. */
7438 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7439 {
7440 /* Past end of string to be displayed. */
7441 nc = NUL;
7442 nc1 = NUL;
7443 }
7444 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007445 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007446 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7447 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007448 nc1 = pcc[0];
7449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 pc = prev_c;
7451 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007452 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 }
7454 else
7455 prev_c = u8c;
7456# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007457 if (col + mbyte_cells > screen_Columns)
7458 {
7459 /* Only 1 cell left, but character requires 2 cells:
7460 * display a '>' in the last column to avoid wrapping. */
7461 c = '>';
7462 mbyte_cells = 1;
7463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 }
7465 }
7466#endif
7467
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007468#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7469 force_redraw_this = force_redraw_next;
7470 force_redraw_next = FALSE;
7471#endif
7472
7473 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474#ifdef FEAT_MBYTE
7475 || (mbyte_cells == 2
7476 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7477 || (enc_dbcs == DBCS_JPNU
7478 && c == 0x8e
7479 && ScreenLines2[off] != ptr[1])
7480 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007481 && (ScreenLinesUC[off] !=
7482 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7483 || (ScreenLinesUC[off] != 0
7484 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485#endif
7486 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007487 || exmode_active;
7488
7489 if (need_redraw
7490#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7491 || force_redraw_this
7492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 )
7494 {
7495#if defined(FEAT_GUI) || defined(UNIX)
7496 /* The bold trick makes a single row of pixels appear in the next
7497 * character. When a bold character is removed, the next
7498 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007499 * and for some xterms. */
7500 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501# ifdef FEAT_GUI
7502 gui.in_use
7503# endif
7504# if defined(FEAT_GUI) && defined(UNIX)
7505 ||
7506# endif
7507# ifdef UNIX
7508 term_is_xterm
7509# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007510 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007512 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007514 if (n > HL_ALL)
7515 n = syn_attr2attr(n);
7516 if (n & HL_BOLD)
7517 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 }
7519#endif
7520#ifdef FEAT_MBYTE
7521 /* When at the end of the text and overwriting a two-cell
7522 * character with a one-cell character, need to clear the next
7523 * cell. Also when overwriting the left halve of a two-cell char
7524 * with the right halve of a two-cell char. Do this only once
7525 * (mb_off2cells() may return 2 on the right halve). */
7526 if (clear_next_cell)
7527 clear_next_cell = FALSE;
7528 else if (has_mbyte
7529 && (len < 0 ? ptr[mbyte_blen] == NUL
7530 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007531 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007533 && (*mb_off2cells)(off, max_off) == 1
7534 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 clear_next_cell = TRUE;
7536
7537 /* Make sure we never leave a second byte of a double-byte behind,
7538 * it confuses mb_off2cells(). */
7539 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007540 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007542 && (*mb_off2cells)(off, max_off) == 1
7543 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 ScreenLines[off + mbyte_blen] = 0;
7545#endif
7546 ScreenLines[off] = c;
7547 ScreenAttrs[off] = attr;
7548#ifdef FEAT_MBYTE
7549 if (enc_utf8)
7550 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007551 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 ScreenLinesUC[off] = 0;
7553 else
7554 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007555 int i;
7556
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007558 for (i = 0; i < Screen_mco; ++i)
7559 {
7560 ScreenLinesC[i][off] = u8cc[i];
7561 if (u8cc[i] == 0)
7562 break;
7563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 }
7565 if (mbyte_cells == 2)
7566 {
7567 ScreenLines[off + 1] = 0;
7568 ScreenAttrs[off + 1] = attr;
7569 }
7570 screen_char(off, row, col);
7571 }
7572 else if (mbyte_cells == 2)
7573 {
7574 ScreenLines[off + 1] = ptr[1];
7575 ScreenAttrs[off + 1] = attr;
7576 screen_char_2(off, row, col);
7577 }
7578 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7579 {
7580 ScreenLines2[off] = ptr[1];
7581 screen_char(off, row, col);
7582 }
7583 else
7584#endif
7585 screen_char(off, row, col);
7586 }
7587#ifdef FEAT_MBYTE
7588 if (has_mbyte)
7589 {
7590 off += mbyte_cells;
7591 col += mbyte_cells;
7592 ptr += mbyte_blen;
7593 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007594 {
7595 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007597 len = -1;
7598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 }
7600 else
7601#endif
7602 {
7603 ++off;
7604 ++col;
7605 ++ptr;
7606 }
7607 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007608
7609#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7610 /* If we detected the next character needs to be redrawn, but the text
7611 * doesn't extend up to there, update the character here. */
7612 if (force_redraw_next && col < screen_Columns)
7613 {
7614# ifdef FEAT_MBYTE
7615 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7616 screen_char_2(off, row, col);
7617 else
7618# endif
7619 screen_char(off, row, col);
7620 }
7621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622}
7623
7624#ifdef FEAT_SEARCH_EXTRA
7625/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007626 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 */
7628 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007629start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630{
7631 if (p_hls && !no_hlsearch)
7632 {
7633 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007634 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007635# ifdef FEAT_RELTIME
7636 /* Set the time limit to 'redrawtime'. */
7637 profile_setlimit(p_rdt, &search_hl.tm);
7638# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 }
7640}
7641
7642/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007643 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 */
7645 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007646end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
7648 if (search_hl.rm.regprog != NULL)
7649 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007650 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 search_hl.rm.regprog = NULL;
7652 }
7653}
7654
7655/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007656 * Init for calling prepare_search_hl().
7657 */
7658 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007659init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007660{
7661 matchitem_T *cur;
7662
7663 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7664 * match */
7665 cur = wp->w_match_head;
7666 while (cur != NULL)
7667 {
7668 cur->hl.rm = cur->match;
7669 if (cur->hlg_id == 0)
7670 cur->hl.attr = 0;
7671 else
7672 cur->hl.attr = syn_id2attr(cur->hlg_id);
7673 cur->hl.buf = wp->w_buffer;
7674 cur->hl.lnum = 0;
7675 cur->hl.first_lnum = 0;
7676# ifdef FEAT_RELTIME
7677 /* Set the time limit to 'redrawtime'. */
7678 profile_setlimit(p_rdt, &(cur->hl.tm));
7679# endif
7680 cur = cur->next;
7681 }
7682 search_hl.buf = wp->w_buffer;
7683 search_hl.lnum = 0;
7684 search_hl.first_lnum = 0;
7685 /* time limit is set at the toplevel, for all windows */
7686}
7687
7688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 * Advance to the match in window "wp" line "lnum" or past it.
7690 */
7691 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007692prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007694 matchitem_T *cur; /* points to the match list */
7695 match_T *shl; /* points to search_hl or a match */
7696 int shl_flag; /* flag to indicate whether search_hl
7697 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007698 int pos_inprogress; /* marks that position match search is
7699 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 int n;
7701
7702 /*
7703 * When using a multi-line pattern, start searching at the top
7704 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007705 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007707 cur = wp->w_match_head;
7708 shl_flag = FALSE;
7709 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007711 if (shl_flag == FALSE)
7712 {
7713 shl = &search_hl;
7714 shl_flag = TRUE;
7715 }
7716 else
7717 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 if (shl->rm.regprog != NULL
7719 && shl->lnum == 0
7720 && re_multiline(shl->rm.regprog))
7721 {
7722 if (shl->first_lnum == 0)
7723 {
7724# ifdef FEAT_FOLDING
7725 for (shl->first_lnum = lnum;
7726 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7727 if (hasFoldingWin(wp, shl->first_lnum - 1,
7728 NULL, NULL, TRUE, NULL))
7729 break;
7730# else
7731 shl->first_lnum = wp->w_topline;
7732# endif
7733 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007734 if (cur != NULL)
7735 cur->pos.cur = 0;
7736 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007738 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7739 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007741 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7742 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007743 pos_inprogress = cur == NULL || cur->pos.cur == 0
7744 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 if (shl->lnum != 0)
7746 {
7747 shl->first_lnum = shl->lnum
7748 + shl->rm.endpos[0].lnum
7749 - shl->rm.startpos[0].lnum;
7750 n = shl->rm.endpos[0].col;
7751 }
7752 else
7753 {
7754 ++shl->first_lnum;
7755 n = 0;
7756 }
7757 }
7758 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007759 if (shl != &search_hl && cur != NULL)
7760 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 }
7762}
7763
7764/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007765 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 * Uses shl->buf.
7767 * Sets shl->lnum and shl->rm contents.
7768 * Note: Assumes a previous match is always before "lnum", unless
7769 * shl->lnum is zero.
7770 * Careful: Any pointers for buffer lines will become invalid.
7771 */
7772 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007773next_search_hl(
7774 win_T *win,
7775 match_T *shl, /* points to search_hl or a match */
7776 linenr_T lnum,
7777 colnr_T mincol, /* minimal column for a match */
7778 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780 linenr_T l;
7781 colnr_T matchcol;
7782 long nmatched;
7783
7784 if (shl->lnum != 0)
7785 {
7786 /* Check for three situations:
7787 * 1. If the "lnum" is below a previous match, start a new search.
7788 * 2. If the previous match includes "mincol", use it.
7789 * 3. Continue after the previous match.
7790 */
7791 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7792 if (lnum > l)
7793 shl->lnum = 0;
7794 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7795 return;
7796 }
7797
7798 /*
7799 * Repeat searching for a match until one is found that includes "mincol"
7800 * or none is found in this line.
7801 */
7802 called_emsg = FALSE;
7803 for (;;)
7804 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007805#ifdef FEAT_RELTIME
7806 /* Stop searching after passing the time limit. */
7807 if (profile_passed_limit(&(shl->tm)))
7808 {
7809 shl->lnum = 0; /* no match found in time */
7810 break;
7811 }
7812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 /* Three situations:
7814 * 1. No useful previous match: search from start of line.
7815 * 2. Not Vi compatible or empty match: continue at next character.
7816 * Break the loop if this is beyond the end of the line.
7817 * 3. Vi compatible searching: continue at end of previous match.
7818 */
7819 if (shl->lnum == 0)
7820 matchcol = 0;
7821 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7822 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007823 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007825 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007826
7827 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007828 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007829 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007831 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 shl->lnum = 0;
7833 break;
7834 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007835#ifdef FEAT_MBYTE
7836 if (has_mbyte)
7837 matchcol += mb_ptr2len(ml);
7838 else
7839#endif
7840 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 }
7842 else
7843 matchcol = shl->rm.endpos[0].col;
7844
7845 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007846 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007848 /* Remember whether shl->rm is using a copy of the regprog in
7849 * cur->match. */
7850 int regprog_is_copy = (shl != &search_hl && cur != NULL
7851 && shl == &cur->hl
7852 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007853 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007854
Bram Moolenaarb3414592014-06-17 17:48:32 +02007855 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7856 matchcol,
7857#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007858 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007859#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007860 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007861#endif
7862 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007863 /* Copy the regprog, in case it got freed and recompiled. */
7864 if (regprog_is_copy)
7865 cur->match.regprog = cur->hl.rm.regprog;
7866
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007867 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007868 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007869 /* Error while handling regexp: stop using this regexp. */
7870 if (shl == &search_hl)
7871 {
7872 /* don't free regprog in the match list, it's a copy */
7873 vim_regfree(shl->rm.regprog);
7874 SET_NO_HLSEARCH(TRUE);
7875 }
7876 shl->rm.regprog = NULL;
7877 shl->lnum = 0;
7878 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7879 message */
7880 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007881 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007882 }
7883 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007884 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007885 else
7886 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 if (nmatched == 0)
7888 {
7889 shl->lnum = 0; /* no match found */
7890 break;
7891 }
7892 if (shl->rm.startpos[0].lnum > 0
7893 || shl->rm.startpos[0].col >= mincol
7894 || nmatched > 1
7895 || shl->rm.endpos[0].col > mincol)
7896 {
7897 shl->lnum += shl->rm.startpos[0].lnum;
7898 break; /* useful match found */
7899 }
7900 }
7901}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902
Bram Moolenaar85077472016-10-16 14:35:48 +02007903/*
7904 * If there is a match fill "shl" and return one.
7905 * Return zero otherwise.
7906 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007907 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007908next_search_hl_pos(
7909 match_T *shl, /* points to a match */
7910 linenr_T lnum,
7911 posmatch_T *posmatch, /* match positions */
7912 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007913{
7914 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007915 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007916
Bram Moolenaarb3414592014-06-17 17:48:32 +02007917 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7918 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007919 llpos_T *pos = &posmatch->pos[i];
7920
7921 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007922 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007923 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007924 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007925 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007926 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007927 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007928 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007929 /* if this match comes before the one at "found" then swap
7930 * them */
7931 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007932 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007933 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007934
Bram Moolenaar85077472016-10-16 14:35:48 +02007935 *pos = posmatch->pos[found];
7936 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007937 }
7938 }
7939 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007940 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007941 }
7942 }
7943 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007944 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007945 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007946 colnr_T start = posmatch->pos[found].col == 0
7947 ? 0 : posmatch->pos[found].col - 1;
7948 colnr_T end = posmatch->pos[found].col == 0
7949 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007950
Bram Moolenaar85077472016-10-16 14:35:48 +02007951 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007952 shl->rm.startpos[0].lnum = 0;
7953 shl->rm.startpos[0].col = start;
7954 shl->rm.endpos[0].lnum = 0;
7955 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007956 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007957 posmatch->cur = found + 1;
7958 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007959 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007960 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007962#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007963
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007965screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966{
7967 attrentry_T *aep = NULL;
7968
7969 screen_attr = attr;
7970 if (full_screen
7971#ifdef WIN3264
7972 && termcap_active
7973#endif
7974 )
7975 {
7976#ifdef FEAT_GUI
7977 if (gui.in_use)
7978 {
7979 char buf[20];
7980
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007981 /* The GUI handles this internally. */
7982 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 OUT_STR(buf);
7984 }
7985 else
7986#endif
7987 {
7988 if (attr > HL_ALL) /* special HL attr. */
7989 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007990 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 aep = syn_cterm_attr2entry(attr);
7992 else
7993 aep = syn_term_attr2entry(attr);
7994 if (aep == NULL) /* did ":syntax clear" */
7995 attr = 0;
7996 else
7997 attr = aep->ae_attr;
7998 }
7999 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8000 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008001 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008002#ifdef FEAT_TERMGUICOLORS
8003 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008004 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008005#endif
8006 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008007#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008008 )
8009#endif
8010 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008011 /* If the Normal FG color has BOLD attribute and the new HL
8012 * has a FG color defined, clear BOLD. */
8013 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8015 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008016 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8017 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 out_str(T_US);
8019 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8020 out_str(T_CZH);
8021 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8022 out_str(T_MR);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008023 if ((attr & HL_STRIKETHROUGH) && T_STS != NULL) /* strike */
8024 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025
8026 /*
8027 * Output the color or start string after bold etc., in case the
8028 * bold etc. override the color setting.
8029 */
8030 if (aep != NULL)
8031 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008032#ifdef FEAT_TERMGUICOLORS
8033 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008035 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008036 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008037 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008038 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 }
8040 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008043 if (t_colors > 1)
8044 {
8045 if (aep->ae_u.cterm.fg_color)
8046 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8047 if (aep->ae_u.cterm.bg_color)
8048 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8049 }
8050 else
8051 {
8052 if (aep->ae_u.term.start != NULL)
8053 out_str(aep->ae_u.term.start);
8054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 }
8056 }
8057 }
8058 }
8059}
8060
8061 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008062screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063{
8064 int do_ME = FALSE; /* output T_ME code */
8065
8066 if (screen_attr != 0
8067#ifdef WIN3264
8068 && termcap_active
8069#endif
8070 )
8071 {
8072#ifdef FEAT_GUI
8073 if (gui.in_use)
8074 {
8075 char buf[20];
8076
8077 /* use internal GUI code */
8078 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8079 OUT_STR(buf);
8080 }
8081 else
8082#endif
8083 {
8084 if (screen_attr > HL_ALL) /* special HL attr. */
8085 {
8086 attrentry_T *aep;
8087
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008088 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {
8090 /*
8091 * Assume that t_me restores the original colors!
8092 */
8093 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008094 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008095#ifdef FEAT_TERMGUICOLORS
8096 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008097 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8098 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008099#endif
8100 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008101#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008102 )
8103#endif
8104 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 do_ME = TRUE;
8106 }
8107 else
8108 {
8109 aep = syn_term_attr2entry(screen_attr);
8110 if (aep != NULL && aep->ae_u.term.stop != NULL)
8111 {
8112 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8113 do_ME = TRUE;
8114 else
8115 out_str(aep->ae_u.term.stop);
8116 }
8117 }
8118 if (aep == NULL) /* did ":syntax clear" */
8119 screen_attr = 0;
8120 else
8121 screen_attr = aep->ae_attr;
8122 }
8123
8124 /*
8125 * Often all ending-codes are equal to T_ME. Avoid outputting the
8126 * same sequence several times.
8127 */
8128 if (screen_attr & HL_STANDOUT)
8129 {
8130 if (STRCMP(T_SE, T_ME) == 0)
8131 do_ME = TRUE;
8132 else
8133 out_str(T_SE);
8134 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008135 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {
8137 if (STRCMP(T_UE, T_ME) == 0)
8138 do_ME = TRUE;
8139 else
8140 out_str(T_UE);
8141 }
8142 if (screen_attr & HL_ITALIC)
8143 {
8144 if (STRCMP(T_CZR, T_ME) == 0)
8145 do_ME = TRUE;
8146 else
8147 out_str(T_CZR);
8148 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008149 if (screen_attr & HL_STRIKETHROUGH)
8150 {
8151 if (STRCMP(T_STE, T_ME) == 0)
8152 do_ME = TRUE;
8153 else
8154 out_str(T_STE);
8155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8157 out_str(T_ME);
8158
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008159#ifdef FEAT_TERMGUICOLORS
8160 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008162 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008163 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008164 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008165 term_bg_rgb_color(cterm_normal_bg_gui_color);
8166 }
8167 else
8168#endif
8169 {
8170 if (t_colors > 1)
8171 {
8172 /* set Normal cterm colors */
8173 if (cterm_normal_fg_color != 0)
8174 term_fg_color(cterm_normal_fg_color - 1);
8175 if (cterm_normal_bg_color != 0)
8176 term_bg_color(cterm_normal_bg_color - 1);
8177 if (cterm_normal_fg_bold)
8178 out_str(T_MD);
8179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 }
8181 }
8182 }
8183 screen_attr = 0;
8184}
8185
8186/*
8187 * Reset the colors for a cterm. Used when leaving Vim.
8188 * The machine specific code may override this again.
8189 */
8190 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008191reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008193 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {
8195 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008196#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008197 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8198 || cterm_normal_bg_gui_color != INVALCOLOR)
8199 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008200#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {
8204 out_str(T_OP);
8205 screen_attr = -1;
8206 }
8207 if (cterm_normal_fg_bold)
8208 {
8209 out_str(T_ME);
8210 screen_attr = -1;
8211 }
8212 }
8213}
8214
8215/*
8216 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8217 * using the attributes from ScreenAttrs["off"].
8218 */
8219 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008220screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221{
8222 int attr;
8223
8224 /* Check for illegal values, just in case (could happen just after
8225 * resizing). */
8226 if (row >= screen_Rows || col >= screen_Columns)
8227 return;
8228
Bram Moolenaar494838a2015-02-10 19:20:37 +01008229 /* Outputting a character in the last cell on the screen may scroll the
8230 * screen up. Only do it when the "xn" termcap property is set, otherwise
8231 * mark the character invalid (update it when scrolled up). */
8232 if (*T_XN == NUL
8233 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234#ifdef FEAT_RIGHTLEFT
8235 /* account for first command-line character in rightleft mode */
8236 && !cmdmsg_rl
8237#endif
8238 )
8239 {
8240 ScreenAttrs[off] = (sattr_T)-1;
8241 return;
8242 }
8243
8244 /*
8245 * Stop highlighting first, so it's easier to move the cursor.
8246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 if (screen_char_attr != 0)
8248 attr = screen_char_attr;
8249 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 attr = ScreenAttrs[off];
8251 if (screen_attr != attr)
8252 screen_stop_highlight();
8253
8254 windgoto(row, col);
8255
8256 if (screen_attr != attr)
8257 screen_start_highlight(attr);
8258
8259#ifdef FEAT_MBYTE
8260 if (enc_utf8 && ScreenLinesUC[off] != 0)
8261 {
8262 char_u buf[MB_MAXBYTES + 1];
8263
8264 /* Convert UTF-8 character to bytes and write it. */
8265
8266 buf[utfc_char2bytes(off, buf)] = NUL;
8267
8268 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008269 if (utf_ambiguous_width(ScreenLinesUC[off]))
8270 screen_cur_col = 9999;
8271 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 ++screen_cur_col;
8273 }
8274 else
8275#endif
8276 {
8277#ifdef FEAT_MBYTE
8278 out_flush_check();
8279#endif
8280 out_char(ScreenLines[off]);
8281#ifdef FEAT_MBYTE
8282 /* double-byte character in single-width cell */
8283 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8284 out_char(ScreenLines2[off]);
8285#endif
8286 }
8287
8288 screen_cur_col++;
8289}
8290
8291#ifdef FEAT_MBYTE
8292
8293/*
8294 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8295 * on the screen at position 'row' and 'col'.
8296 * The attributes of the first byte is used for all. This is required to
8297 * output the two bytes of a double-byte character with nothing in between.
8298 */
8299 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008300screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301{
8302 /* Check for illegal values (could be wrong when screen was resized). */
8303 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8304 return;
8305
8306 /* Outputting the last character on the screen may scrollup the screen.
8307 * Don't to it! Mark the character invalid (update it when scrolled up) */
8308 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8309 {
8310 ScreenAttrs[off] = (sattr_T)-1;
8311 return;
8312 }
8313
8314 /* Output the first byte normally (positions the cursor), then write the
8315 * second byte directly. */
8316 screen_char(off, row, col);
8317 out_char(ScreenLines[off + 1]);
8318 ++screen_cur_col;
8319}
8320#endif
8321
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322/*
8323 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8324 * This uses the contents of ScreenLines[] and doesn't change it.
8325 */
8326 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008327screen_draw_rectangle(
8328 int row,
8329 int col,
8330 int height,
8331 int width,
8332 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333{
8334 int r, c;
8335 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008336#ifdef FEAT_MBYTE
8337 int max_off;
8338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008340 /* Can't use ScreenLines unless initialized */
8341 if (ScreenLines == NULL)
8342 return;
8343
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 if (invert)
8345 screen_char_attr = HL_INVERSE;
8346 for (r = row; r < row + height; ++r)
8347 {
8348 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008349#ifdef FEAT_MBYTE
8350 max_off = off + screen_Columns;
8351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 for (c = col; c < col + width; ++c)
8353 {
8354#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008355 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {
8357 screen_char_2(off + c, r, c);
8358 ++c;
8359 }
8360 else
8361#endif
8362 {
8363 screen_char(off + c, r, c);
8364#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008365 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 ++c;
8367#endif
8368 }
8369 }
8370 }
8371 screen_char_attr = 0;
8372}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374/*
8375 * Redraw the characters for a vertically split window.
8376 */
8377 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008378redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379{
8380 int col;
8381 int width;
8382
8383# ifdef FEAT_CLIPBOARD
8384 clip_may_clear_selection(row, end - 1);
8385# endif
8386
8387 if (wp == NULL)
8388 {
8389 col = 0;
8390 width = Columns;
8391 }
8392 else
8393 {
8394 col = wp->w_wincol;
8395 width = wp->w_width;
8396 }
8397 screen_draw_rectangle(row, col, end - row, width, FALSE);
8398}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399
8400/*
8401 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8402 * with character 'c1' in first column followed by 'c2' in the other columns.
8403 * Use attributes 'attr'.
8404 */
8405 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008406screen_fill(
8407 int start_row,
8408 int end_row,
8409 int start_col,
8410 int end_col,
8411 int c1,
8412 int c2,
8413 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
8415 int row;
8416 int col;
8417 int off;
8418 int end_off;
8419 int did_delete;
8420 int c;
8421 int norm_term;
8422#if defined(FEAT_GUI) || defined(UNIX)
8423 int force_next = FALSE;
8424#endif
8425
8426 if (end_row > screen_Rows) /* safety check */
8427 end_row = screen_Rows;
8428 if (end_col > screen_Columns) /* safety check */
8429 end_col = screen_Columns;
8430 if (ScreenLines == NULL
8431 || start_row >= end_row
8432 || start_col >= end_col) /* nothing to do */
8433 return;
8434
8435 /* it's a "normal" terminal when not in a GUI or cterm */
8436 norm_term = (
8437#ifdef FEAT_GUI
8438 !gui.in_use &&
8439#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008440 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 for (row = start_row; row < end_row; ++row)
8442 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008443#ifdef FEAT_MBYTE
8444 if (has_mbyte
8445# ifdef FEAT_GUI
8446 && !gui.in_use
8447# endif
8448 )
8449 {
8450 /* When drawing over the right halve of a double-wide char clear
8451 * out the left halve. When drawing over the left halve of a
8452 * double wide-char clear out the right halve. Only needed in a
8453 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008454 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008455 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008456 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008457 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008458 }
8459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 /*
8461 * Try to use delete-line termcap code, when no attributes or in a
8462 * "normal" terminal, where a bold/italic space is just a
8463 * space.
8464 */
8465 did_delete = FALSE;
8466 if (c2 == ' '
8467 && end_col == Columns
8468 && can_clear(T_CE)
8469 && (attr == 0
8470 || (norm_term
8471 && attr <= HL_ALL
8472 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8473 {
8474 /*
8475 * check if we really need to clear something
8476 */
8477 col = start_col;
8478 if (c1 != ' ') /* don't clear first char */
8479 ++col;
8480
8481 off = LineOffset[row] + col;
8482 end_off = LineOffset[row] + end_col;
8483
8484 /* skip blanks (used often, keep it fast!) */
8485#ifdef FEAT_MBYTE
8486 if (enc_utf8)
8487 while (off < end_off && ScreenLines[off] == ' '
8488 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8489 ++off;
8490 else
8491#endif
8492 while (off < end_off && ScreenLines[off] == ' '
8493 && ScreenAttrs[off] == 0)
8494 ++off;
8495 if (off < end_off) /* something to be cleared */
8496 {
8497 col = off - LineOffset[row];
8498 screen_stop_highlight();
8499 term_windgoto(row, col);/* clear rest of this screen line */
8500 out_str(T_CE);
8501 screen_start(); /* don't know where cursor is now */
8502 col = end_col - col;
8503 while (col--) /* clear chars in ScreenLines */
8504 {
8505 ScreenLines[off] = ' ';
8506#ifdef FEAT_MBYTE
8507 if (enc_utf8)
8508 ScreenLinesUC[off] = 0;
8509#endif
8510 ScreenAttrs[off] = 0;
8511 ++off;
8512 }
8513 }
8514 did_delete = TRUE; /* the chars are cleared now */
8515 }
8516
8517 off = LineOffset[row] + start_col;
8518 c = c1;
8519 for (col = start_col; col < end_col; ++col)
8520 {
8521 if (ScreenLines[off] != c
8522#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008523 || (enc_utf8 && (int)ScreenLinesUC[off]
8524 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525#endif
8526 || ScreenAttrs[off] != attr
8527#if defined(FEAT_GUI) || defined(UNIX)
8528 || force_next
8529#endif
8530 )
8531 {
8532#if defined(FEAT_GUI) || defined(UNIX)
8533 /* The bold trick may make a single row of pixels appear in
8534 * the next character. When a bold character is removed, the
8535 * next character should be redrawn too. This happens for our
8536 * own GUI and for some xterms. */
8537 if (
8538# ifdef FEAT_GUI
8539 gui.in_use
8540# endif
8541# if defined(FEAT_GUI) && defined(UNIX)
8542 ||
8543# endif
8544# ifdef UNIX
8545 term_is_xterm
8546# endif
8547 )
8548 {
8549 if (ScreenLines[off] != ' '
8550 && (ScreenAttrs[off] > HL_ALL
8551 || ScreenAttrs[off] & HL_BOLD))
8552 force_next = TRUE;
8553 else
8554 force_next = FALSE;
8555 }
8556#endif
8557 ScreenLines[off] = c;
8558#ifdef FEAT_MBYTE
8559 if (enc_utf8)
8560 {
8561 if (c >= 0x80)
8562 {
8563 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008564 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 }
8566 else
8567 ScreenLinesUC[off] = 0;
8568 }
8569#endif
8570 ScreenAttrs[off] = attr;
8571 if (!did_delete || c != ' ')
8572 screen_char(off, row, col);
8573 }
8574 ++off;
8575 if (col == start_col)
8576 {
8577 if (did_delete)
8578 break;
8579 c = c2;
8580 }
8581 }
8582 if (end_col == Columns)
8583 LineWraps[row] = FALSE;
8584 if (row == Rows - 1) /* overwritten the command line */
8585 {
8586 redraw_cmdline = TRUE;
8587 if (c1 == ' ' && c2 == ' ')
8588 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008589 if (start_col == 0)
8590 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 }
8592 }
8593}
8594
8595/*
8596 * Check if there should be a delay. Used before clearing or redrawing the
8597 * screen or the command line.
8598 */
8599 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008600check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601{
8602 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8603 && !did_wait_return
8604 && emsg_silent == 0)
8605 {
8606 out_flush();
8607 ui_delay(1000L, TRUE);
8608 emsg_on_display = FALSE;
8609 if (check_msg_scroll)
8610 msg_scroll = FALSE;
8611 }
8612}
8613
8614/*
8615 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008616 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 * Returns TRUE if there is a valid screen to write to.
8618 * Returns FALSE when starting up and screen not initialized yet.
8619 */
8620 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008621screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008623 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 return (ScreenLines != NULL);
8625}
8626
8627/*
8628 * Resize the shell to Rows and Columns.
8629 * Allocate ScreenLines[] and associated items.
8630 *
8631 * There may be some time between setting Rows and Columns and (re)allocating
8632 * ScreenLines[]. This happens when starting up and when (manually) changing
8633 * the shell size. Always use screen_Rows and screen_Columns to access items
8634 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8635 * final size of the shell is needed.
8636 */
8637 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008638screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639{
8640 int new_row, old_row;
8641#ifdef FEAT_GUI
8642 int old_Rows;
8643#endif
8644 win_T *wp;
8645 int outofmem = FALSE;
8646 int len;
8647 schar_T *new_ScreenLines;
8648#ifdef FEAT_MBYTE
8649 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008650 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008652 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653#endif
8654 sattr_T *new_ScreenAttrs;
8655 unsigned *new_LineOffset;
8656 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008657 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008658 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008660 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008661#ifdef FEAT_AUTOCMD
8662 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008664retry:
8665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 /*
8667 * Allocation of the screen buffers is done only when the size changes and
8668 * when Rows and Columns have been set and we have started doing full
8669 * screen stuff.
8670 */
8671 if ((ScreenLines != NULL
8672 && Rows == screen_Rows
8673 && Columns == screen_Columns
8674#ifdef FEAT_MBYTE
8675 && enc_utf8 == (ScreenLinesUC != NULL)
8676 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008677 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678#endif
8679 )
8680 || Rows == 0
8681 || Columns == 0
8682 || (!full_screen && ScreenLines == NULL))
8683 return;
8684
8685 /*
8686 * It's possible that we produce an out-of-memory message below, which
8687 * will cause this function to be called again. To break the loop, just
8688 * return here.
8689 */
8690 if (entered)
8691 return;
8692 entered = TRUE;
8693
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008694 /*
8695 * Note that the window sizes are updated before reallocating the arrays,
8696 * thus we must not redraw here!
8697 */
8698 ++RedrawingDisabled;
8699
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 win_new_shellsize(); /* fit the windows in the new sized shell */
8701
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 comp_col(); /* recompute columns for shown command and ruler */
8703
8704 /*
8705 * We're changing the size of the screen.
8706 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8707 * - Move lines from the old arrays into the new arrays, clear extra
8708 * lines (unless the screen is going to be cleared).
8709 * - Free the old arrays.
8710 *
8711 * If anything fails, make ScreenLines NULL, so we don't do anything!
8712 * Continuing with the old ScreenLines may result in a crash, because the
8713 * size is wrong.
8714 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008715 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008717#ifdef FEAT_AUTOCMD
8718 if (aucmd_win != NULL)
8719 win_free_lsize(aucmd_win);
8720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721
8722 new_ScreenLines = (schar_T *)lalloc((long_u)(
8723 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8724#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008725 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 if (enc_utf8)
8727 {
8728 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8729 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008730 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008731 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8733 }
8734 if (enc_dbcs == DBCS_JPNU)
8735 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8736 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8737#endif
8738 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8739 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8740 new_LineOffset = (unsigned *)lalloc((long_u)(
8741 Rows * sizeof(unsigned)), FALSE);
8742 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008743 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008745 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 {
8747 if (win_alloc_lines(wp) == FAIL)
8748 {
8749 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008750 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 }
8752 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008753#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008754 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8755 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008756 outofmem = TRUE;
8757#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008758give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008760#ifdef FEAT_MBYTE
8761 for (i = 0; i < p_mco; ++i)
8762 if (new_ScreenLinesC[i] == NULL)
8763 break;
8764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 if (new_ScreenLines == NULL
8766#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008767 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8769#endif
8770 || new_ScreenAttrs == NULL
8771 || new_LineOffset == NULL
8772 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008773 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 || outofmem)
8775 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008776 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008777 {
8778 /* guess the size */
8779 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8780
8781 /* Remember we did this to avoid getting outofmem messages over
8782 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008783 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 vim_free(new_ScreenLines);
8786 new_ScreenLines = NULL;
8787#ifdef FEAT_MBYTE
8788 vim_free(new_ScreenLinesUC);
8789 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008790 for (i = 0; i < p_mco; ++i)
8791 {
8792 vim_free(new_ScreenLinesC[i]);
8793 new_ScreenLinesC[i] = NULL;
8794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 vim_free(new_ScreenLines2);
8796 new_ScreenLines2 = NULL;
8797#endif
8798 vim_free(new_ScreenAttrs);
8799 new_ScreenAttrs = NULL;
8800 vim_free(new_LineOffset);
8801 new_LineOffset = NULL;
8802 vim_free(new_LineWraps);
8803 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008804 vim_free(new_TabPageIdxs);
8805 new_TabPageIdxs = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 }
8807 else
8808 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008809 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008810
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 for (new_row = 0; new_row < Rows; ++new_row)
8812 {
8813 new_LineOffset[new_row] = new_row * Columns;
8814 new_LineWraps[new_row] = FALSE;
8815
8816 /*
8817 * If the screen is not going to be cleared, copy as much as
8818 * possible from the old screen to the new one and clear the rest
8819 * (used when resizing the window at the "--more--" prompt or when
8820 * executing an external command, for the GUI).
8821 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008822 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 {
8824 (void)vim_memset(new_ScreenLines + new_row * Columns,
8825 ' ', (size_t)Columns * sizeof(schar_T));
8826#ifdef FEAT_MBYTE
8827 if (enc_utf8)
8828 {
8829 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8830 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008831 for (i = 0; i < p_mco; ++i)
8832 (void)vim_memset(new_ScreenLinesC[i]
8833 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 0, (size_t)Columns * sizeof(u8char_T));
8835 }
8836 if (enc_dbcs == DBCS_JPNU)
8837 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8838 0, (size_t)Columns * sizeof(schar_T));
8839#endif
8840 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8841 0, (size_t)Columns * sizeof(sattr_T));
8842 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008843 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 {
8845 if (screen_Columns < Columns)
8846 len = screen_Columns;
8847 else
8848 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008849#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008850 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008851 * may be invalid now. Also when p_mco changes. */
8852 if (!(enc_utf8 && ScreenLinesUC == NULL)
8853 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008854#endif
8855 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8856 ScreenLines + LineOffset[old_row],
8857 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008859 if (enc_utf8 && ScreenLinesUC != NULL
8860 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 {
8862 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8863 ScreenLinesUC + LineOffset[old_row],
8864 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008865 for (i = 0; i < p_mco; ++i)
8866 mch_memmove(new_ScreenLinesC[i]
8867 + new_LineOffset[new_row],
8868 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 (size_t)len * sizeof(u8char_T));
8870 }
8871 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8872 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8873 ScreenLines2 + LineOffset[old_row],
8874 (size_t)len * sizeof(schar_T));
8875#endif
8876 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8877 ScreenAttrs + LineOffset[old_row],
8878 (size_t)len * sizeof(sattr_T));
8879 }
8880 }
8881 }
8882 /* Use the last line of the screen for the current line. */
8883 current_ScreenLine = new_ScreenLines + Rows * Columns;
8884 }
8885
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008886 free_screenlines();
8887
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 ScreenLines = new_ScreenLines;
8889#ifdef FEAT_MBYTE
8890 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008891 for (i = 0; i < p_mco; ++i)
8892 ScreenLinesC[i] = new_ScreenLinesC[i];
8893 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 ScreenLines2 = new_ScreenLines2;
8895#endif
8896 ScreenAttrs = new_ScreenAttrs;
8897 LineOffset = new_LineOffset;
8898 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008899 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
8901 /* It's important that screen_Rows and screen_Columns reflect the actual
8902 * size of ScreenLines[]. Set them before calling anything. */
8903#ifdef FEAT_GUI
8904 old_Rows = screen_Rows;
8905#endif
8906 screen_Rows = Rows;
8907 screen_Columns = Columns;
8908
8909 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008910 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 screenclear2();
8912
8913#ifdef FEAT_GUI
8914 else if (gui.in_use
8915 && !gui.starting
8916 && ScreenLines != NULL
8917 && old_Rows != Rows)
8918 {
8919 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8920 /*
8921 * Adjust the position of the cursor, for when executing an external
8922 * command.
8923 */
8924 if (msg_row >= Rows) /* Rows got smaller */
8925 msg_row = Rows - 1; /* put cursor at last row */
8926 else if (Rows > old_Rows) /* Rows got bigger */
8927 msg_row += Rows - old_Rows; /* put cursor in same place */
8928 if (msg_col >= Columns) /* Columns got smaller */
8929 msg_col = Columns - 1; /* put cursor at last column */
8930 }
8931#endif
8932
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008934 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008935
8936#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008937 /*
8938 * Do not apply autocommands more than 3 times to avoid an endless loop
8939 * in case applying autocommands always changes Rows or Columns.
8940 */
8941 if (starting == 0 && ++retry_count <= 3)
8942 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008943 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008944 /* In rare cases, autocommands may have altered Rows or Columns,
8945 * jump back to check if we need to allocate the screen again. */
8946 goto retry;
8947 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949}
8950
8951 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008952free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008953{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008954#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008955 int i;
8956
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008957 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008958 for (i = 0; i < Screen_mco; ++i)
8959 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008960 vim_free(ScreenLines2);
8961#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008962 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008963 vim_free(ScreenAttrs);
8964 vim_free(LineOffset);
8965 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008966 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008967}
8968
8969 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008970screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971{
8972 check_for_delay(FALSE);
8973 screenalloc(FALSE); /* allocate screen buffers if size changed */
8974 screenclear2(); /* clear the screen */
8975}
8976
8977 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008978screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
8980 int i;
8981
8982 if (starting == NO_SCREEN || ScreenLines == NULL
8983#ifdef FEAT_GUI
8984 || (gui.in_use && gui.starting)
8985#endif
8986 )
8987 return;
8988
8989#ifdef FEAT_GUI
8990 if (!gui.in_use)
8991#endif
8992 screen_attr = -1; /* force setting the Normal colors */
8993 screen_stop_highlight(); /* don't want highlighting here */
8994
8995#ifdef FEAT_CLIPBOARD
8996 /* disable selection without redrawing it */
8997 clip_scroll_selection(9999);
8998#endif
8999
9000 /* blank out ScreenLines */
9001 for (i = 0; i < Rows; ++i)
9002 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009003 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 LineWraps[i] = FALSE;
9005 }
9006
9007 if (can_clear(T_CL))
9008 {
9009 out_str(T_CL); /* clear the display */
9010 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009011 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 }
9013 else
9014 {
9015 /* can't clear the screen, mark all chars with invalid attributes */
9016 for (i = 0; i < Rows; ++i)
9017 lineinvalid(LineOffset[i], (int)Columns);
9018 clear_cmdline = TRUE;
9019 }
9020
9021 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9022
9023 win_rest_invalid(firstwin);
9024 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009025 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 if (must_redraw == CLEAR) /* no need to clear again */
9027 must_redraw = NOT_VALID;
9028 compute_cmdrow();
9029 msg_row = cmdline_row; /* put cursor on last line for messages */
9030 msg_col = 0;
9031 screen_start(); /* don't know where cursor is now */
9032 msg_scrolled = 0; /* can't scroll back */
9033 msg_didany = FALSE;
9034 msg_didout = FALSE;
9035}
9036
9037/*
9038 * Clear one line in ScreenLines.
9039 */
9040 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009041lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042{
9043 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9044#ifdef FEAT_MBYTE
9045 if (enc_utf8)
9046 (void)vim_memset(ScreenLinesUC + off, 0,
9047 (size_t)width * sizeof(u8char_T));
9048#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009049 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050}
9051
9052/*
9053 * Mark one line in ScreenLines invalid by setting the attributes to an
9054 * invalid value.
9055 */
9056 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009057lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058{
9059 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9060}
9061
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062/*
9063 * Copy part of a Screenline for vertically split window "wp".
9064 */
9065 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009066linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067{
9068 unsigned off_to = LineOffset[to] + wp->w_wincol;
9069 unsigned off_from = LineOffset[from] + wp->w_wincol;
9070
9071 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9072 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009073#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 if (enc_utf8)
9075 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009076 int i;
9077
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9079 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009080 for (i = 0; i < p_mco; ++i)
9081 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9082 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 }
9084 if (enc_dbcs == DBCS_JPNU)
9085 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9086 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9089 wp->w_width * sizeof(sattr_T));
9090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
9092/*
9093 * Return TRUE if clearing with term string "p" would work.
9094 * It can't work when the string is empty or it won't set the right background.
9095 */
9096 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009097can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098{
9099 return (*p != NUL && (t_colors <= 1
9100#ifdef FEAT_GUI
9101 || gui.in_use
9102#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009103#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009104 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009105 || (!p_tgc && cterm_normal_bg_color == 0)
9106#else
9107 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009108#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009109 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110}
9111
9112/*
9113 * Reset cursor position. Use whenever cursor was moved because of outputting
9114 * something directly to the screen (shell commands) or a terminal control
9115 * code.
9116 */
9117 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009118screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119{
9120 screen_cur_row = screen_cur_col = 9999;
9121}
9122
9123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 * Move the cursor to position "row","col" in the screen.
9125 * This tries to find the most efficient way to move, minimizing the number of
9126 * characters sent to the terminal.
9127 */
9128 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009129windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009131 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 int i;
9133 int plan;
9134 int cost;
9135 int wouldbe_col;
9136 int noinvcurs;
9137 char_u *bs;
9138 int goto_cost;
9139 int attr;
9140
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009141#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9143
9144#define PLAN_LE 1
9145#define PLAN_CR 2
9146#define PLAN_NL 3
9147#define PLAN_WRITE 4
9148 /* Can't use ScreenLines unless initialized */
9149 if (ScreenLines == NULL)
9150 return;
9151
9152 if (col != screen_cur_col || row != screen_cur_row)
9153 {
9154 /* Check for valid position. */
9155 if (row < 0) /* window without text lines? */
9156 row = 0;
9157 if (row >= screen_Rows)
9158 row = screen_Rows - 1;
9159 if (col >= screen_Columns)
9160 col = screen_Columns - 1;
9161
9162 /* check if no cursor movement is allowed in highlight mode */
9163 if (screen_attr && *T_MS == NUL)
9164 noinvcurs = HIGHL_COST;
9165 else
9166 noinvcurs = 0;
9167 goto_cost = GOTO_COST + noinvcurs;
9168
9169 /*
9170 * Plan how to do the positioning:
9171 * 1. Use CR to move it to column 0, same row.
9172 * 2. Use T_LE to move it a few columns to the left.
9173 * 3. Use NL to move a few lines down, column 0.
9174 * 4. Move a few columns to the right with T_ND or by writing chars.
9175 *
9176 * Don't do this if the cursor went beyond the last column, the cursor
9177 * position is unknown then (some terminals wrap, some don't )
9178 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009179 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 * characters to move the cursor to the right.
9181 */
9182 if (row >= screen_cur_row && screen_cur_col < Columns)
9183 {
9184 /*
9185 * If the cursor is in the same row, bigger col, we can use CR
9186 * or T_LE.
9187 */
9188 bs = NULL; /* init for GCC */
9189 attr = screen_attr;
9190 if (row == screen_cur_row && col < screen_cur_col)
9191 {
9192 /* "le" is preferred over "bc", because "bc" is obsolete */
9193 if (*T_LE)
9194 bs = T_LE; /* "cursor left" */
9195 else
9196 bs = T_BC; /* "backspace character (old) */
9197 if (*bs)
9198 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9199 else
9200 cost = 999;
9201 if (col + 1 < cost) /* using CR is less characters */
9202 {
9203 plan = PLAN_CR;
9204 wouldbe_col = 0;
9205 cost = 1; /* CR is just one character */
9206 }
9207 else
9208 {
9209 plan = PLAN_LE;
9210 wouldbe_col = col;
9211 }
9212 if (noinvcurs) /* will stop highlighting */
9213 {
9214 cost += noinvcurs;
9215 attr = 0;
9216 }
9217 }
9218
9219 /*
9220 * If the cursor is above where we want to be, we can use CR LF.
9221 */
9222 else if (row > screen_cur_row)
9223 {
9224 plan = PLAN_NL;
9225 wouldbe_col = 0;
9226 cost = (row - screen_cur_row) * 2; /* CR LF */
9227 if (noinvcurs) /* will stop highlighting */
9228 {
9229 cost += noinvcurs;
9230 attr = 0;
9231 }
9232 }
9233
9234 /*
9235 * If the cursor is in the same row, smaller col, just use write.
9236 */
9237 else
9238 {
9239 plan = PLAN_WRITE;
9240 wouldbe_col = screen_cur_col;
9241 cost = 0;
9242 }
9243
9244 /*
9245 * Check if any characters that need to be written have the
9246 * correct attributes. Also avoid UTF-8 characters.
9247 */
9248 i = col - wouldbe_col;
9249 if (i > 0)
9250 cost += i;
9251 if (cost < goto_cost && i > 0)
9252 {
9253 /*
9254 * Check if the attributes are correct without additionally
9255 * stopping highlighting.
9256 */
9257 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9258 while (i && *p++ == attr)
9259 --i;
9260 if (i != 0)
9261 {
9262 /*
9263 * Try if it works when highlighting is stopped here.
9264 */
9265 if (*--p == 0)
9266 {
9267 cost += noinvcurs;
9268 while (i && *p++ == 0)
9269 --i;
9270 }
9271 if (i != 0)
9272 cost = 999; /* different attributes, don't do it */
9273 }
9274#ifdef FEAT_MBYTE
9275 if (enc_utf8)
9276 {
9277 /* Don't use an UTF-8 char for positioning, it's slow. */
9278 for (i = wouldbe_col; i < col; ++i)
9279 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9280 {
9281 cost = 999;
9282 break;
9283 }
9284 }
9285#endif
9286 }
9287
9288 /*
9289 * We can do it without term_windgoto()!
9290 */
9291 if (cost < goto_cost)
9292 {
9293 if (plan == PLAN_LE)
9294 {
9295 if (noinvcurs)
9296 screen_stop_highlight();
9297 while (screen_cur_col > col)
9298 {
9299 out_str(bs);
9300 --screen_cur_col;
9301 }
9302 }
9303 else if (plan == PLAN_CR)
9304 {
9305 if (noinvcurs)
9306 screen_stop_highlight();
9307 out_char('\r');
9308 screen_cur_col = 0;
9309 }
9310 else if (plan == PLAN_NL)
9311 {
9312 if (noinvcurs)
9313 screen_stop_highlight();
9314 while (screen_cur_row < row)
9315 {
9316 out_char('\n');
9317 ++screen_cur_row;
9318 }
9319 screen_cur_col = 0;
9320 }
9321
9322 i = col - screen_cur_col;
9323 if (i > 0)
9324 {
9325 /*
9326 * Use cursor-right if it's one character only. Avoids
9327 * removing a line of pixels from the last bold char, when
9328 * using the bold trick in the GUI.
9329 */
9330 if (T_ND[0] != NUL && T_ND[1] == NUL)
9331 {
9332 while (i-- > 0)
9333 out_char(*T_ND);
9334 }
9335 else
9336 {
9337 int off;
9338
9339 off = LineOffset[row] + screen_cur_col;
9340 while (i-- > 0)
9341 {
9342 if (ScreenAttrs[off] != screen_attr)
9343 screen_stop_highlight();
9344#ifdef FEAT_MBYTE
9345 out_flush_check();
9346#endif
9347 out_char(ScreenLines[off]);
9348#ifdef FEAT_MBYTE
9349 if (enc_dbcs == DBCS_JPNU
9350 && ScreenLines[off] == 0x8e)
9351 out_char(ScreenLines2[off]);
9352#endif
9353 ++off;
9354 }
9355 }
9356 }
9357 }
9358 }
9359 else
9360 cost = 999;
9361
9362 if (cost >= goto_cost)
9363 {
9364 if (noinvcurs)
9365 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009366 if (row == screen_cur_row && (col > screen_cur_col)
9367 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 term_cursor_right(col - screen_cur_col);
9369 else
9370 term_windgoto(row, col);
9371 }
9372 screen_cur_row = row;
9373 screen_cur_col = col;
9374 }
9375}
9376
9377/*
9378 * Set cursor to its position in the current window.
9379 */
9380 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009381setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
9383 if (redrawing())
9384 {
9385 validate_cursor();
9386 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9387 W_WINCOL(curwin) + (
9388#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009389 /* With 'rightleft' set and the cursor on a double-wide
9390 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9392# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009393 (has_mbyte
9394 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9395 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396# endif
9397 1)) :
9398#endif
9399 curwin->w_wcol));
9400 }
9401}
9402
9403
9404/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009405 * Insert 'line_count' lines at 'row' in window 'wp'.
9406 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9407 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 * scrolling.
9409 * Returns FAIL if the lines are not inserted, OK for success.
9410 */
9411 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009412win_ins_lines(
9413 win_T *wp,
9414 int row,
9415 int line_count,
9416 int invalid,
9417 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418{
9419 int did_delete;
9420 int nextrow;
9421 int lastrow;
9422 int retval;
9423
9424 if (invalid)
9425 wp->w_lines_valid = 0;
9426
9427 if (wp->w_height < 5)
9428 return FAIL;
9429
9430 if (line_count > wp->w_height - row)
9431 line_count = wp->w_height - row;
9432
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009433 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 if (retval != MAYBE)
9435 return retval;
9436
9437 /*
9438 * If there is a next window or a status line, we first try to delete the
9439 * lines at the bottom to avoid messing what is after the window.
9440 * If this fails and there are following windows, don't do anything to avoid
9441 * messing up those windows, better just redraw.
9442 */
9443 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 if (wp->w_next != NULL || wp->w_status_height)
9445 {
9446 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009447 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 did_delete = TRUE;
9449 else if (wp->w_next)
9450 return FAIL;
9451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 /*
9453 * if no lines deleted, blank the lines that will end up below the window
9454 */
9455 if (!did_delete)
9456 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 redraw_cmdline = TRUE;
9459 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9460 lastrow = nextrow + line_count;
9461 if (lastrow > Rows)
9462 lastrow = Rows;
9463 screen_fill(nextrow - line_count, lastrow - line_count,
9464 W_WINCOL(wp), (int)W_ENDCOL(wp),
9465 ' ', ' ', 0);
9466 }
9467
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009468 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 == FAIL)
9470 {
9471 /* deletion will have messed up other windows */
9472 if (did_delete)
9473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 win_rest_invalid(W_NEXT(wp));
9476 }
9477 return FAIL;
9478 }
9479
9480 return OK;
9481}
9482
9483/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009484 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9486 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9487 * scrolling
9488 * Return OK for success, FAIL if the lines are not deleted.
9489 */
9490 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009491win_del_lines(
9492 win_T *wp,
9493 int row,
9494 int line_count,
9495 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009496 int mayclear,
9497 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
9499 int retval;
9500
9501 if (invalid)
9502 wp->w_lines_valid = 0;
9503
9504 if (line_count > wp->w_height - row)
9505 line_count = wp->w_height - row;
9506
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009507 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 if (retval != MAYBE)
9509 return retval;
9510
9511 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009512 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 return FAIL;
9514
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 /*
9516 * If there are windows or status lines below, try to put them at the
9517 * correct place. If we can't do that, they have to be redrawn.
9518 */
9519 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9520 {
9521 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009522 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 {
9524 wp->w_redr_status = TRUE;
9525 win_rest_invalid(wp->w_next);
9526 }
9527 }
9528 /*
9529 * If this is the last window and there is no status line, redraw the
9530 * command line later.
9531 */
9532 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 redraw_cmdline = TRUE;
9534 return OK;
9535}
9536
9537/*
9538 * Common code for win_ins_lines() and win_del_lines().
9539 * Returns OK or FAIL when the work has been done.
9540 * Returns MAYBE when not finished yet.
9541 */
9542 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009543win_do_lines(
9544 win_T *wp,
9545 int row,
9546 int line_count,
9547 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009548 int del,
9549 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550{
9551 int retval;
9552
9553 if (!redrawing() || line_count <= 0)
9554 return FAIL;
9555
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009556 /* When inserting lines would result in loss of command output, just redraw
9557 * the lines. */
9558 if (no_win_do_lines_ins && !del)
9559 return FAIL;
9560
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009562 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009564 if (!no_win_do_lines_ins)
9565 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 return FAIL;
9567 }
9568
9569 /*
9570 * Delete all remaining lines
9571 */
9572 if (row + line_count >= wp->w_height)
9573 {
9574 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9575 W_WINCOL(wp), (int)W_ENDCOL(wp),
9576 ' ', ' ', 0);
9577 return OK;
9578 }
9579
9580 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009581 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009583 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009585 if (!no_win_do_lines_ins)
9586 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587
9588 /*
9589 * If the terminal can set a scroll region, use that.
9590 * Always do this in a vertically split window. This will redraw from
9591 * ScreenLines[] when t_CV isn't defined. That's faster than using
9592 * win_line().
9593 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009594 * a character in the lower right corner of the scroll region may cause a
9595 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009597 if (scroll_region || W_WIDTH(wp) != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 scroll_region_set(wp, row);
9601 if (del)
9602 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009603 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 else
9605 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009606 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 scroll_region_reset();
9609 return retval;
9610 }
9611
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9613 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614
9615 return MAYBE;
9616}
9617
9618/*
9619 * window 'wp' and everything after it is messed up, mark it for redraw
9620 */
9621 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009622win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 {
9626 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 wp->w_redr_status = TRUE;
9628 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 }
9630 redraw_cmdline = TRUE;
9631}
9632
9633/*
9634 * The rest of the routines in this file perform screen manipulations. The
9635 * given operation is performed physically on the screen. The corresponding
9636 * change is also made to the internal screen image. In this way, the editor
9637 * anticipates the effect of editing changes on the appearance of the screen.
9638 * That way, when we call screenupdate a complete redraw isn't usually
9639 * necessary. Another advantage is that we can keep adding code to anticipate
9640 * screen changes, and in the meantime, everything still works.
9641 */
9642
9643/*
9644 * types for inserting or deleting lines
9645 */
9646#define USE_T_CAL 1
9647#define USE_T_CDL 2
9648#define USE_T_AL 3
9649#define USE_T_CE 4
9650#define USE_T_DL 5
9651#define USE_T_SR 6
9652#define USE_NL 7
9653#define USE_T_CD 8
9654#define USE_REDRAW 9
9655
9656/*
9657 * insert lines on the screen and update ScreenLines[]
9658 * 'end' is the line after the scrolled part. Normally it is Rows.
9659 * When scrolling region used 'off' is the offset from the top for the region.
9660 * 'row' and 'end' are relative to the start of the region.
9661 *
9662 * return FAIL for failure, OK for success.
9663 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009664 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009665screen_ins_lines(
9666 int off,
9667 int row,
9668 int line_count,
9669 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009670 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009671 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672{
9673 int i;
9674 int j;
9675 unsigned temp;
9676 int cursor_row;
9677 int type;
9678 int result_empty;
9679 int can_ce = can_clear(T_CE);
9680
9681 /*
9682 * FAIL if
9683 * - there is no valid screen
9684 * - the screen has to be redrawn completely
9685 * - the line count is less than one
9686 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009687 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009689 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9690#ifdef FEAT_CLIPBOARD
9691 || (clip_star.state != SELECT_CLEARED
9692 && redrawing_for_callback > 0)
9693#endif
9694 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 return FAIL;
9696
9697 /*
9698 * There are seven ways to insert lines:
9699 * 0. When in a vertically split window and t_CV isn't set, redraw the
9700 * characters from ScreenLines[].
9701 * 1. Use T_CD (clear to end of display) if it exists and the result of
9702 * the insert is just empty lines
9703 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9704 * present or line_count > 1. It looks better if we do all the inserts
9705 * at once.
9706 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9707 * insert is just empty lines and T_CE is not present or line_count >
9708 * 1.
9709 * 4. Use T_AL (insert line) if it exists.
9710 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9711 * just empty lines.
9712 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9713 * just empty lines.
9714 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9715 * the 'da' flag is not set or we have clear line capability.
9716 * 8. redraw the characters from ScreenLines[].
9717 *
9718 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9719 * the scrollbar for the window. It does have insert line, use that if it
9720 * exists.
9721 */
9722 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9724 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009725 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 type = USE_T_CD;
9727 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9728 type = USE_T_CAL;
9729 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9730 type = USE_T_CDL;
9731 else if (*T_AL != NUL)
9732 type = USE_T_AL;
9733 else if (can_ce && result_empty)
9734 type = USE_T_CE;
9735 else if (*T_DL != NUL && result_empty)
9736 type = USE_T_DL;
9737 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9738 type = USE_T_SR;
9739 else
9740 return FAIL;
9741
9742 /*
9743 * For clearing the lines screen_del_lines() is used. This will also take
9744 * care of t_db if necessary.
9745 */
9746 if (type == USE_T_CD || type == USE_T_CDL ||
9747 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009748 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749
9750 /*
9751 * If text is retained below the screen, first clear or delete as many
9752 * lines at the bottom of the window as are about to be inserted so that
9753 * the deleted lines won't later surface during a screen_del_lines.
9754 */
9755 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009756 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757
9758#ifdef FEAT_CLIPBOARD
9759 /* Remove a modeless selection when inserting lines halfway the screen
9760 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009761 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009762 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 else
9764 clip_scroll_selection(-line_count);
9765#endif
9766
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767#ifdef FEAT_GUI
9768 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9769 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009770 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771#endif
9772
9773 if (*T_CCS != NUL) /* cursor relative to region */
9774 cursor_row = row;
9775 else
9776 cursor_row = row + off;
9777
9778 /*
9779 * Shift LineOffset[] line_count down to reflect the inserted lines.
9780 * Clear the inserted lines in ScreenLines[].
9781 */
9782 row += off;
9783 end += off;
9784 for (i = 0; i < line_count; ++i)
9785 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 if (wp != NULL && wp->w_width != Columns)
9787 {
9788 /* need to copy part of a line */
9789 j = end - 1 - i;
9790 while ((j -= line_count) >= row)
9791 linecopy(j + line_count, j, wp);
9792 j += line_count;
9793 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009794 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9795 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 else
9797 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9798 LineWraps[j] = FALSE;
9799 }
9800 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 {
9802 j = end - 1 - i;
9803 temp = LineOffset[j];
9804 while ((j -= line_count) >= row)
9805 {
9806 LineOffset[j + line_count] = LineOffset[j];
9807 LineWraps[j + line_count] = LineWraps[j];
9808 }
9809 LineOffset[j + line_count] = temp;
9810 LineWraps[j + line_count] = FALSE;
9811 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009812 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 else
9814 lineinvalid(temp, (int)Columns);
9815 }
9816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817
9818 screen_stop_highlight();
9819 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009820 if (clear_attr != 0)
9821 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 /* redraw the characters */
9824 if (type == USE_REDRAW)
9825 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009826 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 {
9828 term_append_lines(line_count);
9829 screen_start(); /* don't know where cursor is now */
9830 }
9831 else
9832 {
9833 for (i = 0; i < line_count; i++)
9834 {
9835 if (type == USE_T_AL)
9836 {
9837 if (i && cursor_row != 0)
9838 windgoto(cursor_row, 0);
9839 out_str(T_AL);
9840 }
9841 else /* type == USE_T_SR */
9842 out_str(T_SR);
9843 screen_start(); /* don't know where cursor is now */
9844 }
9845 }
9846
9847 /*
9848 * With scroll-reverse and 'da' flag set we need to clear the lines that
9849 * have been scrolled down into the region.
9850 */
9851 if (type == USE_T_SR && *T_DA)
9852 {
9853 for (i = 0; i < line_count; ++i)
9854 {
9855 windgoto(off + i, 0);
9856 out_str(T_CE);
9857 screen_start(); /* don't know where cursor is now */
9858 }
9859 }
9860
9861#ifdef FEAT_GUI
9862 gui_can_update_cursor();
9863 if (gui.in_use)
9864 out_flush(); /* always flush after a scroll */
9865#endif
9866 return OK;
9867}
9868
9869/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009870 * Delete lines on the screen and update ScreenLines[].
9871 * "end" is the line after the scrolled part. Normally it is Rows.
9872 * When scrolling region used "off" is the offset from the top for the region.
9873 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 *
9875 * Return OK for success, FAIL if the lines are not deleted.
9876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009878screen_del_lines(
9879 int off,
9880 int row,
9881 int line_count,
9882 int end,
9883 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009884 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009885 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886{
9887 int j;
9888 int i;
9889 unsigned temp;
9890 int cursor_row;
9891 int cursor_end;
9892 int result_empty; /* result is empty until end of region */
9893 int can_delete; /* deleting line codes can be used */
9894 int type;
9895
9896 /*
9897 * FAIL if
9898 * - there is no valid screen
9899 * - the screen has to be redrawn completely
9900 * - the line count is less than one
9901 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009902 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009904 if (!screen_valid(TRUE) || line_count <= 0
9905 || (!force && line_count > p_ttyscroll)
9906#ifdef FEAT_CLIPBOARD
9907 || (clip_star.state != SELECT_CLEARED
9908 && redrawing_for_callback > 0)
9909#endif
9910 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 return FAIL;
9912
9913 /*
9914 * Check if the rest of the current region will become empty.
9915 */
9916 result_empty = row + line_count >= end;
9917
9918 /*
9919 * We can delete lines only when 'db' flag not set or when 'ce' option
9920 * available.
9921 */
9922 can_delete = (*T_DB == NUL || can_clear(T_CE));
9923
9924 /*
9925 * There are six ways to delete lines:
9926 * 0. When in a vertically split window and t_CV isn't set, redraw the
9927 * characters from ScreenLines[].
9928 * 1. Use T_CD if it exists and the result is empty.
9929 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9930 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9931 * none of the other ways work.
9932 * 4. Use T_CE (erase line) if the result is empty.
9933 * 5. Use T_DL (delete line) if it exists.
9934 * 6. redraw the characters from ScreenLines[].
9935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9937 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009938 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 type = USE_T_CD;
9940#if defined(__BEOS__) && defined(BEOS_DR8)
9941 /*
9942 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9943 * its internal termcap... this works okay for tests which test *T_DB !=
9944 * NUL. It has the disadvantage that the user cannot use any :set t_*
9945 * command to get T_DB (back) to empty_option, only :set term=... will do
9946 * the trick...
9947 * Anyway, this hack will hopefully go away with the next OS release.
9948 * (Olaf Seibert)
9949 */
9950 else if (row == 0 && T_DB == empty_option
9951 && (line_count == 1 || *T_CDL == NUL))
9952#else
9953 else if (row == 0 && (
9954#ifndef AMIGA
9955 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9956 * up, so use delete-line command */
9957 line_count == 1 ||
9958#endif
9959 *T_CDL == NUL))
9960#endif
9961 type = USE_NL;
9962 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9963 type = USE_T_CDL;
9964 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009965 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 type = USE_T_CE;
9967 else if (*T_DL != NUL && can_delete)
9968 type = USE_T_DL;
9969 else if (*T_CDL != NUL && can_delete)
9970 type = USE_T_CDL;
9971 else
9972 return FAIL;
9973
9974#ifdef FEAT_CLIPBOARD
9975 /* Remove a modeless selection when deleting lines halfway the screen or
9976 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009977 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009978 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 else
9980 clip_scroll_selection(line_count);
9981#endif
9982
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983#ifdef FEAT_GUI
9984 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9985 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009986 gui_dont_update_cursor(gui.cursor_row >= row + off
9987 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988#endif
9989
9990 if (*T_CCS != NUL) /* cursor relative to region */
9991 {
9992 cursor_row = row;
9993 cursor_end = end;
9994 }
9995 else
9996 {
9997 cursor_row = row + off;
9998 cursor_end = end + off;
9999 }
10000
10001 /*
10002 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10003 * Clear the inserted lines in ScreenLines[].
10004 */
10005 row += off;
10006 end += off;
10007 for (i = 0; i < line_count; ++i)
10008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 if (wp != NULL && wp->w_width != Columns)
10010 {
10011 /* need to copy part of a line */
10012 j = row + i;
10013 while ((j += line_count) <= end - 1)
10014 linecopy(j - line_count, j, wp);
10015 j -= line_count;
10016 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010017 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10018 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 else
10020 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10021 LineWraps[j] = FALSE;
10022 }
10023 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 {
10025 /* whole width, moving the line pointers is faster */
10026 j = row + i;
10027 temp = LineOffset[j];
10028 while ((j += line_count) <= end - 1)
10029 {
10030 LineOffset[j - line_count] = LineOffset[j];
10031 LineWraps[j - line_count] = LineWraps[j];
10032 }
10033 LineOffset[j - line_count] = temp;
10034 LineWraps[j - line_count] = FALSE;
10035 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010036 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 else
10038 lineinvalid(temp, (int)Columns);
10039 }
10040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010042 if (screen_attr != clear_attr)
10043 screen_stop_highlight();
10044 if (clear_attr != 0)
10045 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 /* redraw the characters */
10048 if (type == USE_REDRAW)
10049 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010050 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 {
10052 windgoto(cursor_row, 0);
10053 out_str(T_CD);
10054 screen_start(); /* don't know where cursor is now */
10055 }
10056 else if (type == USE_T_CDL)
10057 {
10058 windgoto(cursor_row, 0);
10059 term_delete_lines(line_count);
10060 screen_start(); /* don't know where cursor is now */
10061 }
10062 /*
10063 * Deleting lines at top of the screen or scroll region: Just scroll
10064 * the whole screen (scroll region) up by outputting newlines on the
10065 * last line.
10066 */
10067 else if (type == USE_NL)
10068 {
10069 windgoto(cursor_end - 1, 0);
10070 for (i = line_count; --i >= 0; )
10071 out_char('\n'); /* cursor will remain on same line */
10072 }
10073 else
10074 {
10075 for (i = line_count; --i >= 0; )
10076 {
10077 if (type == USE_T_DL)
10078 {
10079 windgoto(cursor_row, 0);
10080 out_str(T_DL); /* delete a line */
10081 }
10082 else /* type == USE_T_CE */
10083 {
10084 windgoto(cursor_row + i, 0);
10085 out_str(T_CE); /* erase a line */
10086 }
10087 screen_start(); /* don't know where cursor is now */
10088 }
10089 }
10090
10091 /*
10092 * If the 'db' flag is set, we need to clear the lines that have been
10093 * scrolled up at the bottom of the region.
10094 */
10095 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10096 {
10097 for (i = line_count; i > 0; --i)
10098 {
10099 windgoto(cursor_end - i, 0);
10100 out_str(T_CE); /* erase a line */
10101 screen_start(); /* don't know where cursor is now */
10102 }
10103 }
10104
10105#ifdef FEAT_GUI
10106 gui_can_update_cursor();
10107 if (gui.in_use)
10108 out_flush(); /* always flush after a scroll */
10109#endif
10110
10111 return OK;
10112}
10113
10114/*
10115 * show the current mode and ruler
10116 *
10117 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10118 * If clear_cmdline is FALSE there may be a message there that needs to be
10119 * cleared only if a mode is shown.
10120 * Return the length of the message (0 if no message).
10121 */
10122 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010123showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124{
10125 int need_clear;
10126 int length = 0;
10127 int do_mode;
10128 int attr;
10129 int nwr_save;
10130#ifdef FEAT_INS_EXPAND
10131 int sub_attr;
10132#endif
10133
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010134 do_mode = ((p_smd && msg_silent == 0)
10135 && ((State & INSERT)
10136 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010137 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 if (do_mode || Recording)
10139 {
10140 /*
10141 * Don't show mode right now, when not redrawing or inside a mapping.
10142 * Call char_avail() only when we are going to show something, because
10143 * it takes a bit of time.
10144 */
10145 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10146 {
10147 redraw_cmdline = TRUE; /* show mode later */
10148 return 0;
10149 }
10150
10151 nwr_save = need_wait_return;
10152
10153 /* wait a bit before overwriting an important message */
10154 check_for_delay(FALSE);
10155
10156 /* if the cmdline is more than one line high, erase top lines */
10157 need_clear = clear_cmdline;
10158 if (clear_cmdline && cmdline_row < Rows - 1)
10159 msg_clr_cmdline(); /* will reset clear_cmdline */
10160
10161 /* Position on the last line in the window, column 0 */
10162 msg_pos_mode();
10163 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010164 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 if (do_mode)
10166 {
10167 MSG_PUTS_ATTR("--", attr);
10168#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010169 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010170# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010171 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010172# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010173 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010174# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010175 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010176# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 MSG_PUTS_ATTR(" IM", attr);
10178# else
10179 MSG_PUTS_ATTR(" XIM", attr);
10180# endif
10181#endif
10182#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10183 if (gui.in_use)
10184 {
10185 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010186 {
10187 /* HANGUL */
10188 if (enc_utf8)
10189 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10190 else
10191 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 }
10194#endif
10195#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010196 /* CTRL-X in Insert mode */
10197 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 {
10199 /* These messages can get long, avoid a wrap in a narrow
10200 * window. Prefer showing edit_submode_extra. */
10201 length = (Rows - msg_row) * Columns - 3;
10202 if (edit_submode_extra != NULL)
10203 length -= vim_strsize(edit_submode_extra);
10204 if (length > 0)
10205 {
10206 if (edit_submode_pre != NULL)
10207 length -= vim_strsize(edit_submode_pre);
10208 if (length - vim_strsize(edit_submode) > 0)
10209 {
10210 if (edit_submode_pre != NULL)
10211 msg_puts_attr(edit_submode_pre, attr);
10212 msg_puts_attr(edit_submode, attr);
10213 }
10214 if (edit_submode_extra != NULL)
10215 {
10216 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10217 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010218 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 else
10220 sub_attr = attr;
10221 msg_puts_attr(edit_submode_extra, sub_attr);
10222 }
10223 }
10224 length = 0;
10225 }
10226 else
10227#endif
10228 {
10229#ifdef FEAT_VREPLACE
10230 if (State & VREPLACE_FLAG)
10231 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10232 else
10233#endif
10234 if (State & REPLACE_FLAG)
10235 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10236 else if (State & INSERT)
10237 {
10238#ifdef FEAT_RIGHTLEFT
10239 if (p_ri)
10240 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10241#endif
10242 MSG_PUTS_ATTR(_(" INSERT"), attr);
10243 }
10244 else if (restart_edit == 'I')
10245 MSG_PUTS_ATTR(_(" (insert)"), attr);
10246 else if (restart_edit == 'R')
10247 MSG_PUTS_ATTR(_(" (replace)"), attr);
10248 else if (restart_edit == 'V')
10249 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10250#ifdef FEAT_RIGHTLEFT
10251 if (p_hkmap)
10252 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10253# ifdef FEAT_FKMAP
10254 if (p_fkmap)
10255 MSG_PUTS_ATTR(farsi_text_5, attr);
10256# endif
10257#endif
10258#ifdef FEAT_KEYMAP
10259 if (State & LANGMAP)
10260 {
10261# ifdef FEAT_ARABIC
10262 if (curwin->w_p_arab)
10263 MSG_PUTS_ATTR(_(" Arabic"), attr);
10264 else
10265# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010266 if (get_keymap_str(curwin, (char_u *)" (%s)",
10267 NameBuff, MAXPATHL))
10268 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 }
10270#endif
10271 if ((State & INSERT) && p_paste)
10272 MSG_PUTS_ATTR(_(" (paste)"), attr);
10273
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 if (VIsual_active)
10275 {
10276 char *p;
10277
10278 /* Don't concatenate separate words to avoid translation
10279 * problems. */
10280 switch ((VIsual_select ? 4 : 0)
10281 + (VIsual_mode == Ctrl_V) * 2
10282 + (VIsual_mode == 'V'))
10283 {
10284 case 0: p = N_(" VISUAL"); break;
10285 case 1: p = N_(" VISUAL LINE"); break;
10286 case 2: p = N_(" VISUAL BLOCK"); break;
10287 case 4: p = N_(" SELECT"); break;
10288 case 5: p = N_(" SELECT LINE"); break;
10289 default: p = N_(" SELECT BLOCK"); break;
10290 }
10291 MSG_PUTS_ATTR(_(p), attr);
10292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 MSG_PUTS_ATTR(" --", attr);
10294 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010295
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 need_clear = TRUE;
10297 }
10298 if (Recording
10299#ifdef FEAT_INS_EXPAND
10300 && edit_submode == NULL /* otherwise it gets too long */
10301#endif
10302 )
10303 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010304 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 need_clear = TRUE;
10306 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010307
10308 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 if (need_clear || clear_cmdline)
10310 msg_clr_eos();
10311 msg_didout = FALSE; /* overwrite this message */
10312 length = msg_col;
10313 msg_col = 0;
10314 need_wait_return = nwr_save; /* never ask for hit-return for this */
10315 }
10316 else if (clear_cmdline && msg_silent == 0)
10317 /* Clear the whole command line. Will reset "clear_cmdline". */
10318 msg_clr_cmdline();
10319
10320#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 /* In Visual mode the size of the selected area must be redrawn. */
10322 if (VIsual_active)
10323 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324
10325 /* If the last window has no status line, the ruler is after the mode
10326 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010327 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 win_redr_ruler(lastwin, TRUE);
10329#endif
10330 redraw_cmdline = FALSE;
10331 clear_cmdline = FALSE;
10332
10333 return length;
10334}
10335
10336/*
10337 * Position for a mode message.
10338 */
10339 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010340msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341{
10342 msg_col = 0;
10343 msg_row = Rows - 1;
10344}
10345
10346/*
10347 * Delete mode message. Used when ESC is typed which is expected to end
10348 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010349 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 */
10351 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010352unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353{
10354 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010355 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 */
10357 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10358 redraw_cmdline = TRUE; /* delete mode later */
10359 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010360 clearmode();
10361}
10362
10363/*
10364 * Clear the mode message.
10365 */
10366 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010367clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010368{
10369 msg_pos_mode();
10370 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010371 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010372 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373}
10374
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010375 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010376recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010377{
10378 MSG_PUTS_ATTR(_("recording"), attr);
10379 if (!shortmess(SHM_RECORDING))
10380 {
10381 char_u s[4];
10382 sprintf((char *)s, " @%c", Recording);
10383 MSG_PUTS_ATTR(s, attr);
10384 }
10385}
10386
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010387/*
10388 * Draw the tab pages line at the top of the Vim window.
10389 */
10390 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010391draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010392{
10393 int tabcount = 0;
10394 tabpage_T *tp;
10395 int tabwidth;
10396 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010397 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010398 int attr;
10399 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010400 win_T *cwp;
10401 int wincount;
10402 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010403 int c;
10404 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010405 int attr_sel = HL_ATTR(HLF_TPS);
10406 int attr_nosel = HL_ATTR(HLF_TP);
10407 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010408 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010409 int room;
10410 int use_sep_chars = (t_colors < 8
10411#ifdef FEAT_GUI
10412 && !gui.in_use
10413#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010414#ifdef FEAT_TERMGUICOLORS
10415 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010416#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010417 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010418
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010419 if (ScreenLines == NULL)
10420 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010421 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010422
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010423#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010424 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010425 if (gui_use_tabline())
10426 {
10427 gui_update_tabline();
10428 return;
10429 }
10430#endif
10431
10432 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010433 return;
10434
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010435#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010436
10437 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10438 for (scol = 0; scol < Columns; ++scol)
10439 TabPageIdxs[scol] = 0;
10440
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010441 /* Use the 'tabline' option if it's set. */
10442 if (*p_tal != NUL)
10443 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010444 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010445
10446 /* Check for an error. If there is one we would loop in redrawing the
10447 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010448 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010449 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010450 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010451 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010452 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010453 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010454 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010455 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010456#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010457 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010458 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010459 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010460
Bram Moolenaar238a5642006-02-21 22:12:05 +000010461 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10462 if (tabwidth < 6)
10463 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010464
Bram Moolenaar238a5642006-02-21 22:12:05 +000010465 attr = attr_nosel;
10466 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010467 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010468 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10469 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010470 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010471 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010472
Bram Moolenaar238a5642006-02-21 22:12:05 +000010473 if (tp->tp_topframe == topframe)
10474 attr = attr_sel;
10475 if (use_sep_chars && col > 0)
10476 screen_putchar('|', 0, col++, attr);
10477
10478 if (tp->tp_topframe != topframe)
10479 attr = attr_nosel;
10480
10481 screen_putchar(' ', 0, col++, attr);
10482
10483 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010484 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010485 cwp = curwin;
10486 wp = firstwin;
10487 }
10488 else
10489 {
10490 cwp = tp->tp_curwin;
10491 wp = tp->tp_firstwin;
10492 }
10493
10494 modified = FALSE;
10495 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10496 if (bufIsChanged(wp->w_buffer))
10497 modified = TRUE;
10498 if (modified || wincount > 1)
10499 {
10500 if (wincount > 1)
10501 {
10502 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010503 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010504 if (col + len >= Columns - 3)
10505 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010506 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010507#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010508 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010509#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010510 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010511#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010512 );
10513 col += len;
10514 }
10515 if (modified)
10516 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10517 screen_putchar(' ', 0, col++, attr);
10518 }
10519
10520 room = scol - col + tabwidth - 1;
10521 if (room > 0)
10522 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010523 /* Get buffer name in NameBuff[] */
10524 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010525 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010526 len = vim_strsize(NameBuff);
10527 p = NameBuff;
10528#ifdef FEAT_MBYTE
10529 if (has_mbyte)
10530 while (len > room)
10531 {
10532 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010533 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010534 }
10535 else
10536#endif
10537 if (len > room)
10538 {
10539 p += len - room;
10540 len = room;
10541 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010542 if (len > Columns - col - 1)
10543 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010544
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010545 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010546 col += len;
10547 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010548 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010549
10550 /* Store the tab page number in TabPageIdxs[], so that
10551 * jump_to_mouse() knows where each one is. */
10552 ++tabcount;
10553 while (scol < col)
10554 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010555 }
10556
Bram Moolenaar238a5642006-02-21 22:12:05 +000010557 if (use_sep_chars)
10558 c = '_';
10559 else
10560 c = ' ';
10561 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010562
10563 /* Put an "X" for closing the current tab if there are several. */
10564 if (first_tabpage->tp_next != NULL)
10565 {
10566 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10567 TabPageIdxs[Columns - 1] = -999;
10568 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010569 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010570
10571 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10572 * set. */
10573 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010574}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010575
10576/*
10577 * Get buffer name for "buf" into NameBuff[].
10578 * Takes care of special buffer names and translates special characters.
10579 */
10580 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010581get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010582{
10583 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010584 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010585 else
10586 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10587 trans_characters(NameBuff, MAXPATHL);
10588}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010589
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590/*
10591 * Get the character to use in a status line. Get its attributes in "*attr".
10592 */
10593 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010594fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595{
10596 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010597
10598#ifdef FEAT_TERMINAL
10599 if (bt_terminal(wp->w_buffer))
10600 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010601 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010602 {
10603 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010604 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010605 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010606 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010607 {
10608 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010609 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010610 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010611 }
10612 else
10613#endif
10614 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010616 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 fill = fill_stl;
10618 }
10619 else
10620 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010621 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 fill = fill_stlnc;
10623 }
10624 /* Use fill when there is highlighting, and highlighting of current
10625 * window differs, or the fillchars differ, or this is not the
10626 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010627 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010628 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 || (fill_stl != fill_stlnc)))
10630 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010631 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 return '^';
10633 return '=';
10634}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636/*
10637 * Get the character to use in a separator between vertically split windows.
10638 * Get its attributes in "*attr".
10639 */
10640 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010641fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010643 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 if (*attr == 0 && fill_vert == ' ')
10645 return '|';
10646 else
10647 return fill_vert;
10648}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649
10650/*
10651 * Return TRUE if redrawing should currently be done.
10652 */
10653 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010654redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010656#ifdef FEAT_EVAL
10657 if (disable_redraw_for_testing)
10658 return 0;
10659 else
10660#endif
10661 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10663}
10664
10665/*
10666 * Return TRUE if printing messages should currently be done.
10667 */
10668 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010669messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670{
10671 return (!(p_lz && char_avail() && !KeyTyped));
10672}
10673
10674/*
10675 * Show current status info in ruler and various other places
10676 * If always is FALSE, only show ruler if position has changed.
10677 */
10678 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010679showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680{
10681 if (!always && !redrawing())
10682 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010683#ifdef FEAT_INS_EXPAND
10684 if (pum_visible())
10685 {
10686 /* Don't redraw right now, do it later. */
10687 curwin->w_redr_status = TRUE;
10688 return;
10689 }
10690#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010691#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010692 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010693 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010694 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 else
10697#endif
10698#ifdef FEAT_CMDL_INFO
10699 win_redr_ruler(curwin, always);
10700#endif
10701
10702#ifdef FEAT_TITLE
10703 if (need_maketitle
10704# ifdef FEAT_STL_OPT
10705 || (p_icon && (stl_syntax & STL_IN_ICON))
10706 || (p_title && (stl_syntax & STL_IN_TITLE))
10707# endif
10708 )
10709 maketitle();
10710#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010711 /* Redraw the tab pages line if needed. */
10712 if (redraw_tabline)
10713 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714}
10715
10716#ifdef FEAT_CMDL_INFO
10717 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010718win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010720#define RULER_BUF_LEN 70
10721 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 int row;
10723 int fillchar;
10724 int attr;
10725 int empty_line = FALSE;
10726 colnr_T virtcol;
10727 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010728 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 int this_ru_col;
10731 int off = 0;
10732 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733
10734 /* If 'ruler' off or redrawing disabled, don't do anything */
10735 if (!p_ru)
10736 return;
10737
10738 /*
10739 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10740 * after deleting lines, before cursor.lnum is corrected.
10741 */
10742 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10743 return;
10744
10745#ifdef FEAT_INS_EXPAND
10746 /* Don't draw the ruler while doing insert-completion, it might overwrite
10747 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 if (edit_submode != NULL)
10750 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010751 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10752 if (pum_visible())
10753 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754#endif
10755
10756#ifdef FEAT_STL_OPT
10757 if (*p_ruf)
10758 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010759 int save_called_emsg = called_emsg;
10760
10761 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010763 if (called_emsg)
10764 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010765 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010766 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 return;
10768 }
10769#endif
10770
10771 /*
10772 * Check if not in Insert mode and the line is empty (will show "0-1").
10773 */
10774 if (!(State & INSERT)
10775 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10776 empty_line = TRUE;
10777
10778 /*
10779 * Only draw the ruler when something changed.
10780 */
10781 validate_virtcol_win(wp);
10782 if ( redraw_cmdline
10783 || always
10784 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10785 || wp->w_cursor.col != wp->w_ru_cursor.col
10786 || wp->w_virtcol != wp->w_ru_virtcol
10787#ifdef FEAT_VIRTUALEDIT
10788 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10789#endif
10790 || wp->w_topline != wp->w_ru_topline
10791 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10792#ifdef FEAT_DIFF
10793 || wp->w_topfill != wp->w_ru_topfill
10794#endif
10795 || empty_line != wp->w_ru_empty)
10796 {
10797 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 if (wp->w_status_height)
10799 {
10800 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010801 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 off = W_WINCOL(wp);
10803 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 }
10805 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 {
10807 row = Rows - 1;
10808 fillchar = ' ';
10809 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 width = Columns;
10811 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 }
10813
10814 /* In list mode virtcol needs to be recomputed */
10815 virtcol = wp->w_virtcol;
10816 if (wp->w_p_list && lcs_tab1 == NUL)
10817 {
10818 wp->w_p_list = FALSE;
10819 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10820 wp->w_p_list = TRUE;
10821 }
10822
10823 /*
10824 * Some sprintfs return the length, some return a pointer.
10825 * To avoid portability problems we use strlen() here.
10826 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010827 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10829 ? 0L
10830 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010831 len = STRLEN(buffer);
10832 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10834 (int)virtcol + 1);
10835
10836 /*
10837 * Add a "50%" if there is room for it.
10838 * On the last line, don't print in the last column (scrolls the
10839 * screen up on some terminals).
10840 */
10841 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010842 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 this_ru_col = ru_col - (Columns - width);
10847 if (this_ru_col < 0)
10848 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 /* Never use more than half the window/screen width, leave the other
10850 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010851 if (this_ru_col < (width + 1) / 2)
10852 this_ru_col = (width + 1) / 2;
10853 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010855 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010856 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 {
10858#ifdef FEAT_MBYTE
10859 if (has_mbyte)
10860 i += (*mb_char2bytes)(fillchar, buffer + i);
10861 else
10862#endif
10863 buffer[i++] = fillchar;
10864 ++o;
10865 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010866 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 }
10868 /* Truncate at window boundary. */
10869#ifdef FEAT_MBYTE
10870 if (has_mbyte)
10871 {
10872 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010873 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 {
10875 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010876 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 {
10878 buffer[i] = NUL;
10879 break;
10880 }
10881 }
10882 }
10883 else
10884#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010885 if (this_ru_col + (int)STRLEN(buffer) > width)
10886 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887
Bram Moolenaar4033c552017-09-16 20:54:51 +020010888 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 i = redraw_cmdline;
10890 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010891 this_ru_col + off + (int)STRLEN(buffer),
10892 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 fillchar, fillchar, attr);
10894 /* don't redraw the cmdline because of showing the ruler */
10895 redraw_cmdline = i;
10896 wp->w_ru_cursor = wp->w_cursor;
10897 wp->w_ru_virtcol = wp->w_virtcol;
10898 wp->w_ru_empty = empty_line;
10899 wp->w_ru_topline = wp->w_topline;
10900 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10901#ifdef FEAT_DIFF
10902 wp->w_ru_topfill = wp->w_topfill;
10903#endif
10904 }
10905}
10906#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010907
10908#if defined(FEAT_LINEBREAK) || defined(PROTO)
10909/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010910 * Return the width of the 'number' and 'relativenumber' column.
10911 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010912 * Otherwise it depends on 'numberwidth' and the line count.
10913 */
10914 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010915number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010916{
10917 int n;
10918 linenr_T lnum;
10919
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010920 if (wp->w_p_rnu && !wp->w_p_nu)
10921 /* cursor line shows "0" */
10922 lnum = wp->w_height;
10923 else
10924 /* cursor line shows absolute line number */
10925 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010926
Bram Moolenaar6b314672015-03-20 15:42:10 +010010927 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010928 return wp->w_nrwidth_width;
10929 wp->w_nrwidth_line_count = lnum;
10930
10931 n = 0;
10932 do
10933 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010934 lnum /= 10;
10935 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010936 } while (lnum > 0);
10937
10938 /* 'numberwidth' gives the minimal width plus one */
10939 if (n < wp->w_p_nuw - 1)
10940 n = wp->w_p_nuw - 1;
10941
10942 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010943 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010944 return n;
10945}
10946#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010947
10948/*
10949 * Return the current cursor column. This is the actual position on the
10950 * screen. First column is 0.
10951 */
10952 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010953screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010954{
10955 return screen_cur_col;
10956}
10957
10958/*
10959 * Return the current cursor row. This is the actual position on the screen.
10960 * First row is 0.
10961 */
10962 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010963screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010964{
10965 return screen_cur_row;
10966}