blob: 0ec9ea28166a6ede0696bb463f2450d1b0bc232d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
115/*
116 * Buffer for one screen line (characters and attributes).
117 */
118static schar_T *current_ScreenLine;
119
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100120static void win_update(win_T *wp);
121static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
124static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
125static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200127static int win_line(win_T *, linenr_T, int, int, int nochange, proftime_T *syntax_tm);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100129#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000132#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200136# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void start_search_hl(void);
138static void end_search_hl(void);
139static void init_search_hl(win_T *wp);
140static void prepare_search_hl(win_T *wp, linenr_T lnum);
141static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
142static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void screen_start_highlight(int attr);
145static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screenclear2(void);
150static void lineclear(unsigned off, int width);
151static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100152#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void linecopy(int to, int from, win_T *wp);
154static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del);
157static void win_rest_invalid(win_T *wp);
158static void msg_pos_mode(void);
159static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000160#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100161static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static int fillchar_status(int *attr, int is_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100166#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100167static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
172#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174#endif
175
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100176#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177/* Ugly global: overrule attribute used by screen_char() */
178static int screen_char_attr = 0;
179#endif
180
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200181#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
182/* Can limit syntax highlight time to 'redrawtime'. */
183# define SYN_TIME_LIMIT 1
184#endif
185
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200186#ifdef FEAT_RIGHTLEFT
187# define HAS_RIGHTLEFT(x) x
188#else
189# define HAS_RIGHTLEFT(x) FALSE
190#endif
191
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192/*
193 * Redraw the current window later, with update_screen(type).
194 * Set must_redraw only if not already set to a higher value.
195 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
196 */
197 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100198redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
200 redraw_win_later(curwin, type);
201}
202
203 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100204redraw_win_later(
205 win_T *wp,
206 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207{
208 if (wp->w_redr_type < type)
209 {
210 wp->w_redr_type = type;
211 if (type >= NOT_VALID)
212 wp->w_lines_valid = 0;
213 if (must_redraw < type) /* must_redraw is the maximum of all windows */
214 must_redraw = type;
215 }
216}
217
218/*
219 * Force a complete redraw later. Also resets the highlighting. To be used
220 * after executing a shell command that messes up the screen.
221 */
222 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100223redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224{
225 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000226#ifdef FEAT_GUI
227 if (gui.in_use)
228 /* Use a code that will reset gui.highlight_mask in
229 * gui_stop_highlight(). */
230 screen_attr = HL_ALL + 1;
231 else
232#endif
233 /* Use attributes that is very unlikely to appear in text. */
234 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235}
236
237/*
238 * Mark all windows to be redrawn later.
239 */
240 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100241redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242{
243 win_T *wp;
244
245 FOR_ALL_WINDOWS(wp)
246 {
247 redraw_win_later(wp, type);
248 }
249}
250
251/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000252 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 */
254 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100255redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256{
257 redraw_buf_later(curbuf, type);
258}
259
260 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100261redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262{
263 win_T *wp;
264
265 FOR_ALL_WINDOWS(wp)
266 {
267 if (wp->w_buffer == buf)
268 redraw_win_later(wp, type);
269 }
270}
271
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200272 void
273redraw_buf_and_status_later(buf_T *buf, int type)
274{
275 win_T *wp;
276
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200277#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200278 if (wild_menu_showing != 0)
279 /* Don't redraw while the command line completion is displayed, it
280 * would disappear. */
281 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200282#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200283 FOR_ALL_WINDOWS(wp)
284 {
285 if (wp->w_buffer == buf)
286 {
287 redraw_win_later(wp, type);
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200288#ifdef FEAT_WINDOWS
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200289 wp->w_redr_status = TRUE;
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200290#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200291 }
292 }
293}
294
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200296 * Redraw as soon as possible. When the command line is not scrolled redraw
297 * right away and restore what was on the command line.
298 * Return a code indicating what happened.
299 */
300 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100301redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200302{
303 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200304 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200305 int r;
306 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200307 schar_T *screenline; /* copy from ScreenLines[] */
308 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200309#ifdef FEAT_MBYTE
310 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200311 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200313 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314#endif
315
316 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318 return ret;
319
320 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200323 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200324 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200325 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200326 if (screenline == NULL || screenattr == NULL)
327 ret = 2;
328#ifdef FEAT_MBYTE
329 if (enc_utf8)
330 {
331 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200332 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200333 if (screenlineUC == NULL)
334 ret = 2;
335 for (i = 0; i < p_mco; ++i)
336 {
337 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200338 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200339 if (screenlineC[i] == NULL)
340 ret = 2;
341 }
342 }
343 if (enc_dbcs == DBCS_JPNU)
344 {
345 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200346 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200347 if (screenline2 == NULL)
348 ret = 2;
349 }
350#endif
351
352 if (ret != 2)
353 {
354 /* Save the text displayed in the command line area. */
355 for (r = 0; r < rows; ++r)
356 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200357 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200358 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 (size_t)cols * sizeof(schar_T));
360 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200361 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363#ifdef FEAT_MBYTE
364 if (enc_utf8)
365 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200369 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200370 mch_memmove(screenlineC[i] + r * cols,
371 ScreenLinesC[i] + LineOffset[cmdline_row + r],
372 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200373 }
374 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200375 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200377 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200378#endif
379 }
380
381 update_screen(0);
382 ret = 3;
383
384 if (must_redraw == 0)
385 {
386 int off = (int)(current_ScreenLine - ScreenLines);
387
388 /* Restore the text displayed in the command line area. */
389 for (r = 0; r < rows; ++r)
390 {
391 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 screenline + r * cols,
393 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenattr + r * cols,
396 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397#ifdef FEAT_MBYTE
398 if (enc_utf8)
399 {
400 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200401 screenlineUC + r * cols,
402 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200403 for (i = 0; i < p_mco; ++i)
404 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200405 screenlineC[i] + r * cols,
406 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 }
408 if (enc_dbcs == DBCS_JPNU)
409 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200410 screenline2 + r * cols,
411 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200412#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200413 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200414 }
415 ret = 4;
416 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200417 }
418
419 vim_free(screenline);
420 vim_free(screenattr);
421#ifdef FEAT_MBYTE
422 if (enc_utf8)
423 {
424 vim_free(screenlineUC);
425 for (i = 0; i < p_mco; ++i)
426 vim_free(screenlineC[i]);
427 }
428 if (enc_dbcs == DBCS_JPNU)
429 vim_free(screenline2);
430#endif
431
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200432 /* Show the intro message when appropriate. */
433 maybe_intro_message();
434
435 setcursor();
436
Bram Moolenaar2951b772013-07-03 12:45:31 +0200437 return ret;
438}
439
440/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100441 * Invoked after an asynchronous callback is called.
442 * If an echo command was used the cursor needs to be put back where
443 * it belongs. If highlighting was changed a redraw is needed.
444 */
445 void
Bram Moolenaarcf089462016-06-12 21:18:43 +0200446redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100447{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100448 if (State == HITRETURN || State == ASKMORE)
449 ; /* do nothing */
450 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200451 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200452 /* Redrawing only works when the screen didn't scroll. Don't clear
453 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200454 if (msg_scrolled == 0
455#ifdef FEAT_WILDMENU
456 && wild_menu_showing == 0
457#endif
458 )
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200459 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200460 /* Redraw in the same position, so that the user can continue
461 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200462 redrawcmdline_ex(FALSE);
463 }
Bram Moolenaar144445d2016-07-08 21:41:54 +0200464 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100465 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200466 /* keep the command line if possible */
467 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100468 setcursor();
469 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470 cursor_on();
471 out_flush();
472#ifdef FEAT_GUI
473 if (gui.in_use)
474 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200475 /* Don't update the cursor when it is blinking and off to avoid
476 * flicker. */
477 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200478 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100479 gui_mch_flush();
480 }
481#endif
482}
483
484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 * Changed something in the current window, at buffer line "lnum", that
486 * requires that line and possibly other lines to be redrawn.
487 * Used when entering/leaving Insert mode with the cursor on a folded line.
488 * Used to remove the "$" from a change command.
489 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
490 * may become invalid and the whole window will have to be redrawn.
491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100493redrawWinline(
494 linenr_T lnum,
495 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496{
497#ifdef FEAT_FOLDING
498 int i;
499#endif
500
501 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
502 curwin->w_redraw_top = lnum;
503 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
504 curwin->w_redraw_bot = lnum;
505 redraw_later(VALID);
506
507#ifdef FEAT_FOLDING
508 if (invalid)
509 {
510 /* A w_lines[] entry for this lnum has become invalid. */
511 i = find_wl_entry(curwin, lnum);
512 if (i >= 0)
513 curwin->w_lines[i].wl_valid = FALSE;
514 }
515#endif
516}
517
518/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200519 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 */
521 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100522update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523{
524 redraw_curbuf_later(type);
525 update_screen(type);
526}
527
528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 * Based on the current value of curwin->w_topline, transfer a screenfull
530 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
531 */
532 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200533update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200535 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 win_T *wp;
537 static int did_intro = FALSE;
538#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
539 int did_one;
540#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200541#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200542 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200543 int gui_cursor_col;
544 int gui_cursor_row;
545#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000548 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 if (!screen_valid(TRUE))
550 return;
551
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200552 if (type == VALID_NO_UPDATE)
553 {
554 no_update = TRUE;
555 type = 0;
556 }
557
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 if (must_redraw)
559 {
560 if (type < must_redraw) /* use maximal type */
561 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000562
563 /* must_redraw is reset here, so that when we run into some weird
564 * reason to redraw while busy redrawing (e.g., asynchronous
565 * scrolling), or update_topline() in win_update() will cause a
566 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 must_redraw = 0;
568 }
569
570 /* Need to update w_lines[]. */
571 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
572 type = NOT_VALID;
573
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000574 /* Postpone the redrawing when it's not needed and when being called
575 * recursively. */
576 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {
578 redraw_later(type); /* remember type for next time */
579 must_redraw = type;
580 if (type > INVERTED_ALL)
581 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
582 return;
583 }
584
585 updating_screen = TRUE;
586#ifdef FEAT_SYN_HL
587 ++display_tick; /* let syntax code know we're in a next round of
588 * display updating */
589#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200590 if (no_update)
591 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 /*
594 * if the screen was scrolled up when displaying a message, scroll it down
595 */
596 if (msg_scrolled)
597 {
598 clear_cmdline = TRUE;
599 if (msg_scrolled > Rows - 5) /* clearing is faster */
600 type = CLEAR;
601 else if (type != CLEAR)
602 {
603 check_for_delay(FALSE);
604 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
605 type = CLEAR;
606 FOR_ALL_WINDOWS(wp)
607 {
608 if (W_WINROW(wp) < msg_scrolled)
609 {
610 if (W_WINROW(wp) + wp->w_height > msg_scrolled
611 && wp->w_redr_type < REDRAW_TOP
612 && wp->w_lines_valid > 0
613 && wp->w_topline == wp->w_lines[0].wl_lnum)
614 {
615 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
616 wp->w_redr_type = REDRAW_TOP;
617 }
618 else
619 {
620 wp->w_redr_type = NOT_VALID;
621#ifdef FEAT_WINDOWS
622 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
623 <= msg_scrolled)
624 wp->w_redr_status = TRUE;
625#endif
626 }
627 }
628 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200629 if (!no_update)
630 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000631#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000632 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 }
635 msg_scrolled = 0;
636 need_wait_return = FALSE;
637 }
638
639 /* reset cmdline_row now (may have been changed temporarily) */
640 compute_cmdrow();
641
642 /* Check for changed highlighting */
643 if (need_highlight_changed)
644 highlight_changed();
645
646 if (type == CLEAR) /* first clear screen */
647 {
648 screenclear(); /* will reset clear_cmdline */
649 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200650 /* must_redraw may be set indirectly, avoid another redraw later */
651 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 }
653
654 if (clear_cmdline) /* going to clear cmdline (done below) */
655 check_for_delay(FALSE);
656
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000657#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200658 /* Force redraw when width of 'number' or 'relativenumber' column
659 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000660 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200661 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
662 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000663 curwin->w_redr_type = NOT_VALID;
664#endif
665
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 /*
667 * Only start redrawing if there is really something to do.
668 */
669 if (type == INVERTED)
670 update_curswant();
671 if (curwin->w_redr_type < type
672 && !((type == VALID
673 && curwin->w_lines[0].wl_valid
674#ifdef FEAT_DIFF
675 && curwin->w_topfill == curwin->w_old_topfill
676 && curwin->w_botfill == curwin->w_old_botfill
677#endif
678 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000680 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
682 && curwin->w_old_visual_mode == VIsual_mode
683 && (curwin->w_valid & VALID_VIRTCOL)
684 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 ))
686 curwin->w_redr_type = type;
687
Bram Moolenaar5a305422006-04-28 22:38:25 +0000688#ifdef FEAT_WINDOWS
689 /* Redraw the tab pages line if needed. */
690 if (redraw_tabline || type >= NOT_VALID)
691 draw_tabline();
692#endif
693
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694#ifdef FEAT_SYN_HL
695 /*
696 * Correct stored syntax highlighting info for changes in each displayed
697 * buffer. Each buffer must only be done once.
698 */
699 FOR_ALL_WINDOWS(wp)
700 {
701 if (wp->w_buffer->b_mod_set)
702 {
703# ifdef FEAT_WINDOWS
704 win_T *wwp;
705
706 /* Check if we already did this buffer. */
707 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
708 if (wwp->w_buffer == wp->w_buffer)
709 break;
710# endif
711 if (
712# ifdef FEAT_WINDOWS
713 wwp == wp &&
714# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200715 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 syn_stack_apply_changes(wp->w_buffer);
717 }
718 }
719#endif
720
721 /*
722 * Go from top to bottom through the windows, redrawing the ones that need
723 * it.
724 */
725#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
726 did_one = FALSE;
727#endif
728#ifdef FEAT_SEARCH_EXTRA
729 search_hl.rm.regprog = NULL;
730#endif
731 FOR_ALL_WINDOWS(wp)
732 {
733 if (wp->w_redr_type != 0)
734 {
735 cursor_off();
736#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
737 if (!did_one)
738 {
739 did_one = TRUE;
740# ifdef FEAT_SEARCH_EXTRA
741 start_search_hl();
742# endif
743# ifdef FEAT_CLIPBOARD
744 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200745 if (clip_star.available && clip_isautosel_star())
746 clip_update_selection(&clip_star);
747 if (clip_plus.available && clip_isautosel_plus())
748 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749# endif
750#ifdef FEAT_GUI
751 /* Remove the cursor before starting to do anything, because
752 * scrolling may make it difficult to redraw the text under
753 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200754 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200755 {
756 gui_cursor_col = gui.cursor_col;
757 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200759 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761#endif
762 }
763#endif
764 win_update(wp);
765 }
766
767#ifdef FEAT_WINDOWS
768 /* redraw status line after the window to minimize cursor movement */
769 if (wp->w_redr_status)
770 {
771 cursor_off();
772 win_redr_status(wp);
773 }
774#endif
775 }
776#if defined(FEAT_SEARCH_EXTRA)
777 end_search_hl();
778#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100779#ifdef FEAT_INS_EXPAND
780 /* May need to redraw the popup menu. */
781 if (pum_visible())
782 pum_redraw();
783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784
785#ifdef FEAT_WINDOWS
786 /* Reset b_mod_set flags. Going through all windows is probably faster
787 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200788 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 wp->w_buffer->b_mod_set = FALSE;
790#else
791 curbuf->b_mod_set = FALSE;
792#endif
793
794 updating_screen = FALSE;
795#ifdef FEAT_GUI
796 gui_may_resize_shell();
797#endif
798
799 /* Clear or redraw the command line. Done last, because scrolling may
800 * mess up the command line. */
801 if (clear_cmdline || redraw_cmdline)
802 showmode();
803
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200804 if (no_update)
805 --no_win_do_lines_ins;
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200808 if (!did_intro)
809 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 did_intro = TRUE;
811
812#ifdef FEAT_GUI
813 /* Redraw the cursor and update the scrollbars when all screen updating is
814 * done. */
815 if (gui.in_use)
816 {
817 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200818 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200819 {
820 /* Put the GUI position where the cursor was, gui_update_cursor()
821 * uses that. */
822 gui.col = gui_cursor_col;
823 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200824# ifdef FEAT_MBYTE
825 gui.col = mb_fix_col(gui.col, gui.row);
826# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200828 screen_cur_col = gui.col;
829 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 gui_update_scrollbars(FALSE);
832 }
833#endif
834}
835
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100836#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
837/*
838 * Prepare for updating one or more windows.
839 * Caller must check for "updating_screen" already set to avoid recursiveness.
840 */
841 static void
842update_prepare(void)
843{
844 cursor_off();
845 updating_screen = TRUE;
846#ifdef FEAT_GUI
847 /* Remove the cursor before starting to do anything, because scrolling may
848 * make it difficult to redraw the text under it. */
849 if (gui.in_use)
850 gui_undraw_cursor();
851#endif
852#ifdef FEAT_SEARCH_EXTRA
853 start_search_hl();
854#endif
855}
856
857/*
858 * Finish updating one or more windows.
859 */
860 static void
861update_finish(void)
862{
863 if (redraw_cmdline)
864 showmode();
865
866# ifdef FEAT_SEARCH_EXTRA
867 end_search_hl();
868# endif
869
870 updating_screen = FALSE;
871
872# ifdef FEAT_GUI
873 gui_may_resize_shell();
874
875 /* Redraw the cursor and update the scrollbars when all screen updating is
876 * done. */
877 if (gui.in_use)
878 {
879 out_flush(); /* required before updating the cursor */
880 gui_update_cursor(FALSE, FALSE);
881 gui_update_scrollbars(FALSE);
882 }
883# endif
884}
885#endif
886
Bram Moolenaar860cae12010-06-05 23:22:07 +0200887#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200888/*
889 * Return TRUE if the cursor line in window "wp" may be concealed, according
890 * to the 'concealcursor' option.
891 */
892 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100893conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200894{
895 int c;
896
897 if (*wp->w_p_cocu == NUL)
898 return FALSE;
899 if (get_real_state() & VISUAL)
900 c = 'v';
901 else if (State & INSERT)
902 c = 'i';
903 else if (State & NORMAL)
904 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200905 else if (State & CMDLINE)
906 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200907 else
908 return FALSE;
909 return vim_strchr(wp->w_p_cocu, c) != NULL;
910}
911
912/*
913 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
914 */
915 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100916conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200917{
918 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
919 {
920 need_cursor_line_redraw = TRUE;
921 /* Need to recompute cursor column, e.g., when starting Visual mode
922 * without concealing. */
923 curs_columns(TRUE);
924 }
925}
926
Bram Moolenaar860cae12010-06-05 23:22:07 +0200927 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100928update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200929{
930 int row;
931 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200932#ifdef SYN_TIME_LIMIT
933 proftime_T syntax_tm;
934#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200935
Bram Moolenaar908be432016-05-24 10:51:30 +0200936 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100937 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200938 return;
939
Bram Moolenaar860cae12010-06-05 23:22:07 +0200940 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200941 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200942 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200943#ifdef SYN_TIME_LIMIT
944 /* Set the time limit to 'redrawtime'. */
945 profile_setlimit(p_rdt, &syntax_tm);
946#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100947 update_prepare();
948
Bram Moolenaar860cae12010-06-05 23:22:07 +0200949 row = 0;
950 for (j = 0; j < wp->w_lines_valid; ++j)
951 {
952 if (lnum == wp->w_lines[j].wl_lnum)
953 {
954 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200955# ifdef FEAT_SEARCH_EXTRA
956 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200957 start_search_hl();
958 prepare_search_hl(wp, lnum);
959# endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200960 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE,
961#ifdef SYN_TIME_LIMIT
962 &syntax_tm
963#else
964 NULL
965#endif
966 );
Bram Moolenaar860cae12010-06-05 23:22:07 +0200967# if defined(FEAT_SEARCH_EXTRA)
968 end_search_hl();
969# endif
970 break;
971 }
972 row += wp->w_lines[j].wl_size;
973 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100974
975 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200976 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200977 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978}
979#endif
980
981#if defined(FEAT_SIGNS) || defined(PROTO)
982 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100983update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984{
985 win_T *wp;
986 int doit = FALSE;
987
988# ifdef FEAT_FOLDING
989 win_foldinfo.fi_level = 0;
990# endif
991
992 /* update/delete a specific mark */
993 FOR_ALL_WINDOWS(wp)
994 {
995 if (buf != NULL && lnum > 0)
996 {
997 if (wp->w_buffer == buf && lnum >= wp->w_topline
998 && lnum < wp->w_botline)
999 {
1000 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1001 wp->w_redraw_top = lnum;
1002 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1003 wp->w_redraw_bot = lnum;
1004 redraw_win_later(wp, VALID);
1005 }
1006 }
1007 else
1008 redraw_win_later(wp, VALID);
1009 if (wp->w_redr_type != 0)
1010 doit = TRUE;
1011 }
1012
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001013 /* Return when there is nothing to do, screen updating is already
1014 * happening (recursive call) or still starting up. */
1015 if (!doit || updating_screen
1016#ifdef FEAT_GUI
1017 || gui.starting
1018#endif
1019 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 return;
1021
1022 /* update all windows that need updating */
1023 update_prepare();
1024
1025# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001026 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 {
1028 if (wp->w_redr_type != 0)
1029 win_update(wp);
1030 if (wp->w_redr_status)
1031 win_redr_status(wp);
1032 }
1033# else
1034 if (curwin->w_redr_type != 0)
1035 win_update(curwin);
1036# endif
1037
1038 update_finish();
1039}
1040#endif
1041
1042
1043#if defined(FEAT_GUI) || defined(PROTO)
1044/*
1045 * Update a single window, its status line and maybe the command line msg.
1046 * Used for the GUI scrollbar.
1047 */
1048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001049updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001051 /* return if already busy updating */
1052 if (updating_screen)
1053 return;
1054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 update_prepare();
1056
1057#ifdef FEAT_CLIPBOARD
1058 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001059 if (clip_star.available && clip_isautosel_star())
1060 clip_update_selection(&clip_star);
1061 if (clip_plus.available && clip_isautosel_plus())
1062 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001066
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001068 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001069 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001070 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 if (wp->w_redr_status
1073# ifdef FEAT_CMDL_INFO
1074 || p_ru
1075# endif
1076# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001077 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078# endif
1079 )
1080 win_redr_status(wp);
1081#endif
1082
1083 update_finish();
1084}
1085#endif
1086
1087/*
1088 * Update a single window.
1089 *
1090 * This may cause the windows below it also to be redrawn (when clearing the
1091 * screen or scrolling lines).
1092 *
1093 * How the window is redrawn depends on wp->w_redr_type. Each type also
1094 * implies the one below it.
1095 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001096 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1098 * INVERTED redraw the changed part of the Visual area
1099 * INVERTED_ALL redraw the whole Visual area
1100 * VALID 1. scroll up/down to adjust for a changed w_topline
1101 * 2. update lines at the top when scrolled down
1102 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001103 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 * b_mod_top and b_mod_bot.
1105 * - if wp->w_redraw_top non-zero, redraw lines between
1106 * wp->w_redraw_top and wp->w_redr_bot.
1107 * - continue redrawing when syntax status is invalid.
1108 * 4. if scrolled up, update lines at the bottom.
1109 * This results in three areas that may need updating:
1110 * top: from first row to top_end (when scrolled down)
1111 * mid: from mid_start to mid_end (update inversion or changed text)
1112 * bot: from bot_start to last row (when scrolled up)
1113 */
1114 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001115win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116{
1117 buf_T *buf = wp->w_buffer;
1118 int type;
1119 int top_end = 0; /* Below last row of the top area that needs
1120 updating. 0 when no top area updating. */
1121 int mid_start = 999;/* first row of the mid area that needs
1122 updating. 999 when no mid area updating. */
1123 int mid_end = 0; /* Below last row of the mid area that needs
1124 updating. 0 when no mid area updating. */
1125 int bot_start = 999;/* first row of the bot area that needs
1126 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 int scrolled_down = FALSE; /* TRUE when scrolled down when
1128 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001130 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 int top_to_mod = FALSE; /* redraw above mod_top */
1132#endif
1133
1134 int row; /* current window row to display */
1135 linenr_T lnum; /* current buffer lnum to display */
1136 int idx; /* current index in w_lines[] */
1137 int srow; /* starting row of the current line */
1138
1139 int eof = FALSE; /* if TRUE, we hit the end of the file */
1140 int didline = FALSE; /* if TRUE, we finished the last line */
1141 int i;
1142 long j;
1143 static int recursive = FALSE; /* being called recursively */
1144 int old_botline = wp->w_botline;
1145#ifdef FEAT_FOLDING
1146 long fold_count;
1147#endif
1148#ifdef FEAT_SYN_HL
1149 /* remember what happened to the previous line, to know if
1150 * check_visual_highlight() can be used */
1151#define DID_NONE 1 /* didn't update a line */
1152#define DID_LINE 2 /* updated a normal line */
1153#define DID_FOLD 3 /* updated a folded line */
1154 int did_update = DID_NONE;
1155 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1156#endif
1157 linenr_T mod_top = 0;
1158 linenr_T mod_bot = 0;
1159#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1160 int save_got_int;
1161#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001162#ifdef SYN_TIME_LIMIT
1163 proftime_T syntax_tm;
1164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
1166 type = wp->w_redr_type;
1167
1168 if (type == NOT_VALID)
1169 {
1170#ifdef FEAT_WINDOWS
1171 wp->w_redr_status = TRUE;
1172#endif
1173 wp->w_lines_valid = 0;
1174 }
1175
1176 /* Window is zero-height: nothing to draw. */
1177 if (wp->w_height == 0)
1178 {
1179 wp->w_redr_type = 0;
1180 return;
1181 }
1182
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001183#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 /* Window is zero-width: Only need to draw the separator. */
1185 if (wp->w_width == 0)
1186 {
1187 /* draw the vertical separator right of this window */
1188 draw_vsep_win(wp, 0);
1189 wp->w_redr_type = 0;
1190 return;
1191 }
1192#endif
1193
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001194#ifdef FEAT_TERMINAL
1195 if (wp->w_buffer->b_term != NULL)
1196 {
1197 /* This window contains a terminal, redraw works completely
1198 * differently. */
1199 term_update_window(wp);
1200 wp->w_redr_type = 0;
1201 return;
1202 }
1203#endif
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001206 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#endif
1208
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001209#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001210 /* Force redraw when width of 'number' or 'relativenumber' column
1211 * changes. */
1212 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001213 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001214 {
1215 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001216 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001217 }
1218 else
1219#endif
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1222 {
1223 /*
1224 * When there are both inserted/deleted lines and specific lines to be
1225 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1226 * everything (only happens when redrawing is off for while).
1227 */
1228 type = NOT_VALID;
1229 }
1230 else
1231 {
1232 /*
1233 * Set mod_top to the first line that needs displaying because of
1234 * changes. Set mod_bot to the first line after the changes.
1235 */
1236 mod_top = wp->w_redraw_top;
1237 if (wp->w_redraw_bot != 0)
1238 mod_bot = wp->w_redraw_bot + 1;
1239 else
1240 mod_bot = 0;
1241 wp->w_redraw_top = 0; /* reset for next time */
1242 wp->w_redraw_bot = 0;
1243 if (buf->b_mod_set)
1244 {
1245 if (mod_top == 0 || mod_top > buf->b_mod_top)
1246 {
1247 mod_top = buf->b_mod_top;
1248#ifdef FEAT_SYN_HL
1249 /* Need to redraw lines above the change that may be included
1250 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001251 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001253 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 if (mod_top < 1)
1255 mod_top = 1;
1256 }
1257#endif
1258 }
1259 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1260 mod_bot = buf->b_mod_bot;
1261
1262#ifdef FEAT_SEARCH_EXTRA
1263 /* When 'hlsearch' is on and using a multi-line search pattern, a
1264 * change in one line may make the Search highlighting in a
1265 * previous line invalid. Simple solution: redraw all visible
1266 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001267 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001269 if (search_hl.rm.regprog != NULL
1270 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001272 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001273 {
1274 cur = wp->w_match_head;
1275 while (cur != NULL)
1276 {
1277 if (cur->match.regprog != NULL
1278 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001279 {
1280 top_to_mod = TRUE;
1281 break;
1282 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001283 cur = cur->next;
1284 }
1285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286#endif
1287 }
1288#ifdef FEAT_FOLDING
1289 if (mod_top != 0 && hasAnyFolding(wp))
1290 {
1291 linenr_T lnumt, lnumb;
1292
1293 /*
1294 * A change in a line can cause lines above it to become folded or
1295 * unfolded. Find the top most buffer line that may be affected.
1296 * If the line was previously folded and displayed, get the first
1297 * line of that fold. If the line is folded now, get the first
1298 * folded line. Use the minimum of these two.
1299 */
1300
1301 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1302 * the line below it. If there is no valid entry, use w_topline.
1303 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1304 * to this line. If there is no valid entry, use MAXLNUM. */
1305 lnumt = wp->w_topline;
1306 lnumb = MAXLNUM;
1307 for (i = 0; i < wp->w_lines_valid; ++i)
1308 if (wp->w_lines[i].wl_valid)
1309 {
1310 if (wp->w_lines[i].wl_lastlnum < mod_top)
1311 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1312 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1313 {
1314 lnumb = wp->w_lines[i].wl_lnum;
1315 /* When there is a fold column it might need updating
1316 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001317 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 ++lnumb;
1319 }
1320 }
1321
1322 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1323 if (mod_top > lnumt)
1324 mod_top = lnumt;
1325
1326 /* Now do the same for the bottom line (one above mod_bot). */
1327 --mod_bot;
1328 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1329 ++mod_bot;
1330 if (mod_bot < lnumb)
1331 mod_bot = lnumb;
1332 }
1333#endif
1334
1335 /* When a change starts above w_topline and the end is below
1336 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001337 * If the end of the change is above w_topline: do like no change was
1338 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 if (mod_top != 0 && mod_top < wp->w_topline)
1340 {
1341 if (mod_bot > wp->w_topline)
1342 mod_top = wp->w_topline;
1343#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001344 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 top_end = 1;
1346#endif
1347 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001348
1349 /* When line numbers are displayed need to redraw all lines below
1350 * inserted/deleted lines. */
1351 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1352 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354
1355 /*
1356 * When only displaying the lines at the top, set top_end. Used when
1357 * window has scrolled down for msg_scrolled.
1358 */
1359 if (type == REDRAW_TOP)
1360 {
1361 j = 0;
1362 for (i = 0; i < wp->w_lines_valid; ++i)
1363 {
1364 j += wp->w_lines[i].wl_size;
1365 if (j >= wp->w_upd_rows)
1366 {
1367 top_end = j;
1368 break;
1369 }
1370 }
1371 if (top_end == 0)
1372 /* not found (cannot happen?): redraw everything */
1373 type = NOT_VALID;
1374 else
1375 /* top area defined, the rest is VALID */
1376 type = VALID;
1377 }
1378
Bram Moolenaar367329b2007-08-30 11:53:22 +00001379 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001380 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1381 * non-zero and thus not FALSE) will indicate that screenclear() was not
1382 * called. */
1383 if (screen_cleared)
1384 screen_cleared = MAYBE;
1385
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 /*
1387 * If there are no changes on the screen that require a complete redraw,
1388 * handle three cases:
1389 * 1: we are off the top of the screen by a few lines: scroll down
1390 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1391 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1392 * w_lines[] that needs updating.
1393 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001394 if ((type == VALID || type == SOME_VALID
1395 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396#ifdef FEAT_DIFF
1397 && !wp->w_botfill && !wp->w_old_botfill
1398#endif
1399 )
1400 {
1401 if (mod_top != 0 && wp->w_topline == mod_top)
1402 {
1403 /*
1404 * w_topline is the first changed line, the scrolling will be done
1405 * further down.
1406 */
1407 }
1408 else if (wp->w_lines[0].wl_valid
1409 && (wp->w_topline < wp->w_lines[0].wl_lnum
1410#ifdef FEAT_DIFF
1411 || (wp->w_topline == wp->w_lines[0].wl_lnum
1412 && wp->w_topfill > wp->w_old_topfill)
1413#endif
1414 ))
1415 {
1416 /*
1417 * New topline is above old topline: May scroll down.
1418 */
1419#ifdef FEAT_FOLDING
1420 if (hasAnyFolding(wp))
1421 {
1422 linenr_T ln;
1423
1424 /* count the number of lines we are off, counting a sequence
1425 * of folded lines as one */
1426 j = 0;
1427 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1428 {
1429 ++j;
1430 if (j >= wp->w_height - 2)
1431 break;
1432 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1433 }
1434 }
1435 else
1436#endif
1437 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1438 if (j < wp->w_height - 2) /* not too far off */
1439 {
1440 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1441#ifdef FEAT_DIFF
1442 /* insert extra lines for previously invisible filler lines */
1443 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1444 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1445 - wp->w_old_topfill;
1446#endif
1447 if (i < wp->w_height - 2) /* less than a screen off */
1448 {
1449 /*
1450 * Try to insert the correct number of lines.
1451 * If not the last window, delete the lines at the bottom.
1452 * win_ins_lines may fail when the terminal can't do it.
1453 */
1454 if (i > 0)
1455 check_for_delay(FALSE);
1456 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1457 {
1458 if (wp->w_lines_valid != 0)
1459 {
1460 /* Need to update rows that are new, stop at the
1461 * first one that scrolled down. */
1462 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
1465 /* Move the entries that were scrolled, disable
1466 * the entries for the lines to be redrawn. */
1467 if ((wp->w_lines_valid += j) > wp->w_height)
1468 wp->w_lines_valid = wp->w_height;
1469 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1470 wp->w_lines[idx] = wp->w_lines[idx - j];
1471 while (idx >= 0)
1472 wp->w_lines[idx--].wl_valid = FALSE;
1473 }
1474 }
1475 else
1476 mid_start = 0; /* redraw all lines */
1477 }
1478 else
1479 mid_start = 0; /* redraw all lines */
1480 }
1481 else
1482 mid_start = 0; /* redraw all lines */
1483 }
1484 else
1485 {
1486 /*
1487 * New topline is at or below old topline: May scroll up.
1488 * When topline didn't change, find first entry in w_lines[] that
1489 * needs updating.
1490 */
1491
1492 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1493 j = -1;
1494 row = 0;
1495 for (i = 0; i < wp->w_lines_valid; i++)
1496 {
1497 if (wp->w_lines[i].wl_valid
1498 && wp->w_lines[i].wl_lnum == wp->w_topline)
1499 {
1500 j = i;
1501 break;
1502 }
1503 row += wp->w_lines[i].wl_size;
1504 }
1505 if (j == -1)
1506 {
1507 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1508 * lines */
1509 mid_start = 0;
1510 }
1511 else
1512 {
1513 /*
1514 * Try to delete the correct number of lines.
1515 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1516 */
1517#ifdef FEAT_DIFF
1518 /* If the topline didn't change, delete old filler lines,
1519 * otherwise delete filler lines of the new topline... */
1520 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1521 row += wp->w_old_topfill;
1522 else
1523 row += diff_check_fill(wp, wp->w_topline);
1524 /* ... but don't delete new filler lines. */
1525 row -= wp->w_topfill;
1526#endif
1527 if (row > 0)
1528 {
1529 check_for_delay(FALSE);
1530 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1531 bot_start = wp->w_height - row;
1532 else
1533 mid_start = 0; /* redraw all lines */
1534 }
1535 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1536 {
1537 /*
1538 * Skip the lines (below the deleted lines) that are still
1539 * valid and don't need redrawing. Copy their info
1540 * upwards, to compensate for the deleted lines. Set
1541 * bot_start to the first row that needs redrawing.
1542 */
1543 bot_start = 0;
1544 idx = 0;
1545 for (;;)
1546 {
1547 wp->w_lines[idx] = wp->w_lines[j];
1548 /* stop at line that didn't fit, unless it is still
1549 * valid (no lines deleted) */
1550 if (row > 0 && bot_start + row
1551 + (int)wp->w_lines[j].wl_size > wp->w_height)
1552 {
1553 wp->w_lines_valid = idx + 1;
1554 break;
1555 }
1556 bot_start += wp->w_lines[idx++].wl_size;
1557
1558 /* stop at the last valid entry in w_lines[].wl_size */
1559 if (++j >= wp->w_lines_valid)
1560 {
1561 wp->w_lines_valid = idx;
1562 break;
1563 }
1564 }
1565#ifdef FEAT_DIFF
1566 /* Correct the first entry for filler lines at the top
1567 * when it won't get updated below. */
1568 if (wp->w_p_diff && bot_start > 0)
1569 wp->w_lines[0].wl_size =
1570 plines_win_nofill(wp, wp->w_topline, TRUE)
1571 + wp->w_topfill;
1572#endif
1573 }
1574 }
1575 }
1576
1577 /* When starting redraw in the first line, redraw all lines. When
1578 * there is only one window it's probably faster to clear the screen
1579 * first. */
1580 if (mid_start == 0)
1581 {
1582 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001583 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001584 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001585 /* Clear the screen when it was not done by win_del_lines() or
1586 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1587 * then. */
1588 if (screen_cleared != TRUE)
1589 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001590#ifdef FEAT_WINDOWS
1591 /* The screen was cleared, redraw the tab pages line. */
1592 if (redraw_tabline)
1593 draw_tabline();
1594#endif
1595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001597
1598 /* When win_del_lines() or win_ins_lines() caused the screen to be
1599 * cleared (only happens for the first window) or when screenclear()
1600 * was called directly above, "must_redraw" will have been set to
1601 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1602 if (screen_cleared == TRUE)
1603 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
1605 else
1606 {
1607 /* Not VALID or INVERTED: redraw all lines. */
1608 mid_start = 0;
1609 mid_end = wp->w_height;
1610 }
1611
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001612 if (type == SOME_VALID)
1613 {
1614 /* SOME_VALID: redraw all lines. */
1615 mid_start = 0;
1616 mid_end = wp->w_height;
1617 type = NOT_VALID;
1618 }
1619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 /* check if we are updating or removing the inverted part */
1621 if ((VIsual_active && buf == curwin->w_buffer)
1622 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1623 {
1624 linenr_T from, to;
1625
1626 if (VIsual_active)
1627 {
1628 if (VIsual_active
1629 && (VIsual_mode != wp->w_old_visual_mode
1630 || type == INVERTED_ALL))
1631 {
1632 /*
1633 * If the type of Visual selection changed, redraw the whole
1634 * selection. Also when the ownership of the X selection is
1635 * gained or lost.
1636 */
1637 if (curwin->w_cursor.lnum < VIsual.lnum)
1638 {
1639 from = curwin->w_cursor.lnum;
1640 to = VIsual.lnum;
1641 }
1642 else
1643 {
1644 from = VIsual.lnum;
1645 to = curwin->w_cursor.lnum;
1646 }
1647 /* redraw more when the cursor moved as well */
1648 if (wp->w_old_cursor_lnum < from)
1649 from = wp->w_old_cursor_lnum;
1650 if (wp->w_old_cursor_lnum > to)
1651 to = wp->w_old_cursor_lnum;
1652 if (wp->w_old_visual_lnum < from)
1653 from = wp->w_old_visual_lnum;
1654 if (wp->w_old_visual_lnum > to)
1655 to = wp->w_old_visual_lnum;
1656 }
1657 else
1658 {
1659 /*
1660 * Find the line numbers that need to be updated: The lines
1661 * between the old cursor position and the current cursor
1662 * position. Also check if the Visual position changed.
1663 */
1664 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1665 {
1666 from = curwin->w_cursor.lnum;
1667 to = wp->w_old_cursor_lnum;
1668 }
1669 else
1670 {
1671 from = wp->w_old_cursor_lnum;
1672 to = curwin->w_cursor.lnum;
1673 if (from == 0) /* Visual mode just started */
1674 from = to;
1675 }
1676
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001677 if (VIsual.lnum != wp->w_old_visual_lnum
1678 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 {
1680 if (wp->w_old_visual_lnum < from
1681 && wp->w_old_visual_lnum != 0)
1682 from = wp->w_old_visual_lnum;
1683 if (wp->w_old_visual_lnum > to)
1684 to = wp->w_old_visual_lnum;
1685 if (VIsual.lnum < from)
1686 from = VIsual.lnum;
1687 if (VIsual.lnum > to)
1688 to = VIsual.lnum;
1689 }
1690 }
1691
1692 /*
1693 * If in block mode and changed column or curwin->w_curswant:
1694 * update all lines.
1695 * First compute the actual start and end column.
1696 */
1697 if (VIsual_mode == Ctrl_V)
1698 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001699 colnr_T fromc, toc;
1700#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1701 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
Bram Moolenaar404406a2014-10-09 13:24:43 +02001703 if (curwin->w_p_lbr)
1704 ve_flags = VE_ALL;
1705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001707#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1708 ve_flags = save_ve_flags;
1709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 ++toc;
1711 if (curwin->w_curswant == MAXCOL)
1712 toc = MAXCOL;
1713
1714 if (fromc != wp->w_old_cursor_fcol
1715 || toc != wp->w_old_cursor_lcol)
1716 {
1717 if (from > VIsual.lnum)
1718 from = VIsual.lnum;
1719 if (to < VIsual.lnum)
1720 to = VIsual.lnum;
1721 }
1722 wp->w_old_cursor_fcol = fromc;
1723 wp->w_old_cursor_lcol = toc;
1724 }
1725 }
1726 else
1727 {
1728 /* Use the line numbers of the old Visual area. */
1729 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1730 {
1731 from = wp->w_old_cursor_lnum;
1732 to = wp->w_old_visual_lnum;
1733 }
1734 else
1735 {
1736 from = wp->w_old_visual_lnum;
1737 to = wp->w_old_cursor_lnum;
1738 }
1739 }
1740
1741 /*
1742 * There is no need to update lines above the top of the window.
1743 */
1744 if (from < wp->w_topline)
1745 from = wp->w_topline;
1746
1747 /*
1748 * If we know the value of w_botline, use it to restrict the update to
1749 * the lines that are visible in the window.
1750 */
1751 if (wp->w_valid & VALID_BOTLINE)
1752 {
1753 if (from >= wp->w_botline)
1754 from = wp->w_botline - 1;
1755 if (to >= wp->w_botline)
1756 to = wp->w_botline - 1;
1757 }
1758
1759 /*
1760 * Find the minimal part to be updated.
1761 * Watch out for scrolling that made entries in w_lines[] invalid.
1762 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1763 * top_end; need to redraw from top_end to the "to" line.
1764 * A middle mouse click with a Visual selection may change the text
1765 * above the Visual area and reset wl_valid, do count these for
1766 * mid_end (in srow).
1767 */
1768 if (mid_start > 0)
1769 {
1770 lnum = wp->w_topline;
1771 idx = 0;
1772 srow = 0;
1773 if (scrolled_down)
1774 mid_start = top_end;
1775 else
1776 mid_start = 0;
1777 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1778 {
1779 if (wp->w_lines[idx].wl_valid)
1780 mid_start += wp->w_lines[idx].wl_size;
1781 else if (!scrolled_down)
1782 srow += wp->w_lines[idx].wl_size;
1783 ++idx;
1784# ifdef FEAT_FOLDING
1785 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1786 lnum = wp->w_lines[idx].wl_lnum;
1787 else
1788# endif
1789 ++lnum;
1790 }
1791 srow += mid_start;
1792 mid_end = wp->w_height;
1793 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1794 {
1795 if (wp->w_lines[idx].wl_valid
1796 && wp->w_lines[idx].wl_lnum >= to + 1)
1797 {
1798 /* Only update until first row of this line */
1799 mid_end = srow;
1800 break;
1801 }
1802 srow += wp->w_lines[idx].wl_size;
1803 }
1804 }
1805 }
1806
1807 if (VIsual_active && buf == curwin->w_buffer)
1808 {
1809 wp->w_old_visual_mode = VIsual_mode;
1810 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1811 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001812 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 wp->w_old_curswant = curwin->w_curswant;
1814 }
1815 else
1816 {
1817 wp->w_old_visual_mode = 0;
1818 wp->w_old_cursor_lnum = 0;
1819 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001820 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
1823#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1824 /* reset got_int, otherwise regexp won't work */
1825 save_got_int = got_int;
1826 got_int = 0;
1827#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001828#ifdef SYN_TIME_LIMIT
1829 /* Set the time limit to 'redrawtime'. */
1830 profile_setlimit(p_rdt, &syntax_tm);
1831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832#ifdef FEAT_FOLDING
1833 win_foldinfo.fi_level = 0;
1834#endif
1835
1836 /*
1837 * Update all the window rows.
1838 */
1839 idx = 0; /* first entry in w_lines[].wl_size */
1840 row = 0;
1841 srow = 0;
1842 lnum = wp->w_topline; /* first line shown in window */
1843 for (;;)
1844 {
1845 /* stop updating when reached the end of the window (check for _past_
1846 * the end of the window is at the end of the loop) */
1847 if (row == wp->w_height)
1848 {
1849 didline = TRUE;
1850 break;
1851 }
1852
1853 /* stop updating when hit the end of the file */
1854 if (lnum > buf->b_ml.ml_line_count)
1855 {
1856 eof = TRUE;
1857 break;
1858 }
1859
1860 /* Remember the starting row of the line that is going to be dealt
1861 * with. It is used further down when the line doesn't fit. */
1862 srow = row;
1863
1864 /*
1865 * Update a line when it is in an area that needs updating, when it
1866 * has changes or w_lines[idx] is invalid.
1867 * bot_start may be halfway a wrapped line after using
1868 * win_del_lines(), check if the current line includes it.
1869 * When syntax folding is being used, the saved syntax states will
1870 * already have been updated, we can't see where the syntax state is
1871 * the same again, just update until the end of the window.
1872 */
1873 if (row < top_end
1874 || (row >= mid_start && row < mid_end)
1875#ifdef FEAT_SEARCH_EXTRA
1876 || top_to_mod
1877#endif
1878 || idx >= wp->w_lines_valid
1879 || (row + wp->w_lines[idx].wl_size > bot_start)
1880 || (mod_top != 0
1881 && (lnum == mod_top
1882 || (lnum >= mod_top
1883 && (lnum < mod_bot
1884#ifdef FEAT_SYN_HL
1885 || did_update == DID_FOLD
1886 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001887 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 && (
1889# ifdef FEAT_FOLDING
1890 (foldmethodIsSyntax(wp)
1891 && hasAnyFolding(wp)) ||
1892# endif
1893 syntax_check_changed(lnum)))
1894#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001895#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001896 /* match in fixed position might need redraw
1897 * if lines were inserted or deleted */
1898 || (wp->w_match_head != NULL
1899 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 )))))
1902 {
1903#ifdef FEAT_SEARCH_EXTRA
1904 if (lnum == mod_top)
1905 top_to_mod = FALSE;
1906#endif
1907
1908 /*
1909 * When at start of changed lines: May scroll following lines
1910 * up or down to minimize redrawing.
1911 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001912 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 */
1914 if (lnum == mod_top
1915 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001916 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
1918 int old_rows = 0;
1919 int new_rows = 0;
1920 int xtra_rows;
1921 linenr_T l;
1922
1923 /* Count the old number of window rows, using w_lines[], which
1924 * should still contain the sizes for the lines as they are
1925 * currently displayed. */
1926 for (i = idx; i < wp->w_lines_valid; ++i)
1927 {
1928 /* Only valid lines have a meaningful wl_lnum. Invalid
1929 * lines are part of the changed area. */
1930 if (wp->w_lines[i].wl_valid
1931 && wp->w_lines[i].wl_lnum == mod_bot)
1932 break;
1933 old_rows += wp->w_lines[i].wl_size;
1934#ifdef FEAT_FOLDING
1935 if (wp->w_lines[i].wl_valid
1936 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1937 {
1938 /* Must have found the last valid entry above mod_bot.
1939 * Add following invalid entries. */
1940 ++i;
1941 while (i < wp->w_lines_valid
1942 && !wp->w_lines[i].wl_valid)
1943 old_rows += wp->w_lines[i++].wl_size;
1944 break;
1945 }
1946#endif
1947 }
1948
1949 if (i >= wp->w_lines_valid)
1950 {
1951 /* We can't find a valid line below the changed lines,
1952 * need to redraw until the end of the window.
1953 * Inserting/deleting lines has no use. */
1954 bot_start = 0;
1955 }
1956 else
1957 {
1958 /* Able to count old number of rows: Count new window
1959 * rows, and may insert/delete lines */
1960 j = idx;
1961 for (l = lnum; l < mod_bot; ++l)
1962 {
1963#ifdef FEAT_FOLDING
1964 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1965 ++new_rows;
1966 else
1967#endif
1968#ifdef FEAT_DIFF
1969 if (l == wp->w_topline)
1970 new_rows += plines_win_nofill(wp, l, TRUE)
1971 + wp->w_topfill;
1972 else
1973#endif
1974 new_rows += plines_win(wp, l, TRUE);
1975 ++j;
1976 if (new_rows > wp->w_height - row - 2)
1977 {
1978 /* it's getting too much, must redraw the rest */
1979 new_rows = 9999;
1980 break;
1981 }
1982 }
1983 xtra_rows = new_rows - old_rows;
1984 if (xtra_rows < 0)
1985 {
1986 /* May scroll text up. If there is not enough
1987 * remaining text or scrolling fails, must redraw the
1988 * rest. If scrolling works, must redraw the text
1989 * below the scrolled text. */
1990 if (row - xtra_rows >= wp->w_height - 2)
1991 mod_bot = MAXLNUM;
1992 else
1993 {
1994 check_for_delay(FALSE);
1995 if (win_del_lines(wp, row,
1996 -xtra_rows, FALSE, FALSE) == FAIL)
1997 mod_bot = MAXLNUM;
1998 else
1999 bot_start = wp->w_height + xtra_rows;
2000 }
2001 }
2002 else if (xtra_rows > 0)
2003 {
2004 /* May scroll text down. If there is not enough
2005 * remaining text of scrolling fails, must redraw the
2006 * rest. */
2007 if (row + xtra_rows >= wp->w_height - 2)
2008 mod_bot = MAXLNUM;
2009 else
2010 {
2011 check_for_delay(FALSE);
2012 if (win_ins_lines(wp, row + old_rows,
2013 xtra_rows, FALSE, FALSE) == FAIL)
2014 mod_bot = MAXLNUM;
2015 else if (top_end > row + old_rows)
2016 /* Scrolled the part at the top that requires
2017 * updating down. */
2018 top_end += xtra_rows;
2019 }
2020 }
2021
2022 /* When not updating the rest, may need to move w_lines[]
2023 * entries. */
2024 if (mod_bot != MAXLNUM && i != j)
2025 {
2026 if (j < i)
2027 {
2028 int x = row + new_rows;
2029
2030 /* move entries in w_lines[] upwards */
2031 for (;;)
2032 {
2033 /* stop at last valid entry in w_lines[] */
2034 if (i >= wp->w_lines_valid)
2035 {
2036 wp->w_lines_valid = j;
2037 break;
2038 }
2039 wp->w_lines[j] = wp->w_lines[i];
2040 /* stop at a line that won't fit */
2041 if (x + (int)wp->w_lines[j].wl_size
2042 > wp->w_height)
2043 {
2044 wp->w_lines_valid = j + 1;
2045 break;
2046 }
2047 x += wp->w_lines[j++].wl_size;
2048 ++i;
2049 }
2050 if (bot_start > x)
2051 bot_start = x;
2052 }
2053 else /* j > i */
2054 {
2055 /* move entries in w_lines[] downwards */
2056 j -= i;
2057 wp->w_lines_valid += j;
2058 if (wp->w_lines_valid > wp->w_height)
2059 wp->w_lines_valid = wp->w_height;
2060 for (i = wp->w_lines_valid; i - j >= idx; --i)
2061 wp->w_lines[i] = wp->w_lines[i - j];
2062
2063 /* The w_lines[] entries for inserted lines are
2064 * now invalid, but wl_size may be used above.
2065 * Reset to zero. */
2066 while (i >= idx)
2067 {
2068 wp->w_lines[i].wl_size = 0;
2069 wp->w_lines[i--].wl_valid = FALSE;
2070 }
2071 }
2072 }
2073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075
2076#ifdef FEAT_FOLDING
2077 /*
2078 * When lines are folded, display one line for all of them.
2079 * Otherwise, display normally (can be several display lines when
2080 * 'wrap' is on).
2081 */
2082 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2083 if (fold_count != 0)
2084 {
2085 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2086 ++row;
2087 --fold_count;
2088 wp->w_lines[idx].wl_folded = TRUE;
2089 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2090# ifdef FEAT_SYN_HL
2091 did_update = DID_FOLD;
2092# endif
2093 }
2094 else
2095#endif
2096 if (idx < wp->w_lines_valid
2097 && wp->w_lines[idx].wl_valid
2098 && wp->w_lines[idx].wl_lnum == lnum
2099 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002100 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 && srow + wp->w_lines[idx].wl_size > wp->w_height
2102#ifdef FEAT_DIFF
2103 && diff_check_fill(wp, lnum) == 0
2104#endif
2105 )
2106 {
2107 /* This line is not going to fit. Don't draw anything here,
2108 * will draw "@ " lines below. */
2109 row = wp->w_height + 1;
2110 }
2111 else
2112 {
2113#ifdef FEAT_SEARCH_EXTRA
2114 prepare_search_hl(wp, lnum);
2115#endif
2116#ifdef FEAT_SYN_HL
2117 /* Let the syntax stuff know we skipped a few lines. */
2118 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002119 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 syntax_end_parsing(syntax_last_parsed + 1);
2121#endif
2122
2123 /*
2124 * Display one line.
2125 */
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002126 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0,
2127#ifdef SYN_TIME_LIMIT
2128 &syntax_tm
2129#else
2130 NULL
2131#endif
2132 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
2134#ifdef FEAT_FOLDING
2135 wp->w_lines[idx].wl_folded = FALSE;
2136 wp->w_lines[idx].wl_lastlnum = lnum;
2137#endif
2138#ifdef FEAT_SYN_HL
2139 did_update = DID_LINE;
2140 syntax_last_parsed = lnum;
2141#endif
2142 }
2143
2144 wp->w_lines[idx].wl_lnum = lnum;
2145 wp->w_lines[idx].wl_valid = TRUE;
2146 if (row > wp->w_height) /* past end of screen */
2147 {
2148 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002149 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2151 ++idx;
2152 break;
2153 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002154 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 wp->w_lines[idx].wl_size = row - srow;
2156 ++idx;
2157#ifdef FEAT_FOLDING
2158 lnum += fold_count + 1;
2159#else
2160 ++lnum;
2161#endif
2162 }
2163 else
2164 {
2165 /* This line does not need updating, advance to the next one */
2166 row += wp->w_lines[idx++].wl_size;
2167 if (row > wp->w_height) /* past end of screen */
2168 break;
2169#ifdef FEAT_FOLDING
2170 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2171#else
2172 ++lnum;
2173#endif
2174#ifdef FEAT_SYN_HL
2175 did_update = DID_NONE;
2176#endif
2177 }
2178
2179 if (lnum > buf->b_ml.ml_line_count)
2180 {
2181 eof = TRUE;
2182 break;
2183 }
2184 }
2185 /*
2186 * End of loop over all window lines.
2187 */
2188
2189
2190 if (idx > wp->w_lines_valid)
2191 wp->w_lines_valid = idx;
2192
2193#ifdef FEAT_SYN_HL
2194 /*
2195 * Let the syntax stuff know we stop parsing here.
2196 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002197 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 syntax_end_parsing(syntax_last_parsed + 1);
2199#endif
2200
2201 /*
2202 * If we didn't hit the end of the file, and we didn't finish the last
2203 * line we were working on, then the line didn't fit.
2204 */
2205 wp->w_empty_rows = 0;
2206#ifdef FEAT_DIFF
2207 wp->w_filler_rows = 0;
2208#endif
2209 if (!eof && !didline)
2210 {
2211 if (lnum == wp->w_topline)
2212 {
2213 /*
2214 * Single line that does not fit!
2215 * Don't overwrite it, it can be edited.
2216 */
2217 wp->w_botline = lnum + 1;
2218 }
2219#ifdef FEAT_DIFF
2220 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2221 {
2222 /* Window ends in filler lines. */
2223 wp->w_botline = lnum;
2224 wp->w_filler_rows = wp->w_height - srow;
2225 }
2226#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002227 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2228 {
2229 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2230
2231 /*
2232 * Last line isn't finished: Display "@@@" in the last screen line.
2233 */
2234 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002235 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002236 screen_fill(scr_row, scr_row + 1,
2237 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002238 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002239 set_empty_rows(wp, srow);
2240 wp->w_botline = lnum;
2241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2243 {
2244 /*
2245 * Last line isn't finished: Display "@@@" at the end.
2246 */
2247 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2248 W_WINROW(wp) + wp->w_height,
2249 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002250 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 set_empty_rows(wp, srow);
2252 wp->w_botline = lnum;
2253 }
2254 else
2255 {
2256 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2257 wp->w_botline = lnum;
2258 }
2259 }
2260 else
2261 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002262#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 draw_vsep_win(wp, row);
2264#endif
2265 if (eof) /* we hit the end of the file */
2266 {
2267 wp->w_botline = buf->b_ml.ml_line_count + 1;
2268#ifdef FEAT_DIFF
2269 j = diff_check_fill(wp, wp->w_botline);
2270 if (j > 0 && !wp->w_botfill)
2271 {
2272 /*
2273 * Display filler lines at the end of the file
2274 */
2275 if (char2cells(fill_diff) > 1)
2276 i = '-';
2277 else
2278 i = fill_diff;
2279 if (row + j > wp->w_height)
2280 j = wp->w_height - row;
2281 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2282 row += j;
2283 }
2284#endif
2285 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002286 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 wp->w_botline = lnum;
2288
2289 /* make sure the rest of the screen is blank */
2290 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002291 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
2293
2294 /* Reset the type of redrawing required, the window has been updated. */
2295 wp->w_redr_type = 0;
2296#ifdef FEAT_DIFF
2297 wp->w_old_topfill = wp->w_topfill;
2298 wp->w_old_botfill = wp->w_botfill;
2299#endif
2300
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002301 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 /*
2304 * There is a trick with w_botline. If we invalidate it on each
2305 * change that might modify it, this will cause a lot of expensive
2306 * calls to plines() in update_topline() each time. Therefore the
2307 * value of w_botline is often approximated, and this value is used to
2308 * compute the value of w_topline. If the value of w_botline was
2309 * wrong, check that the value of w_topline is correct (cursor is on
2310 * the visible part of the text). If it's not, we need to redraw
2311 * again. Mostly this just means scrolling up a few lines, so it
2312 * doesn't look too bad. Only do this for the current window (where
2313 * changes are relevant).
2314 */
2315 wp->w_valid |= VALID_BOTLINE;
2316 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2317 {
2318 recursive = TRUE;
2319 curwin->w_valid &= ~VALID_TOPLINE;
2320 update_topline(); /* may invalidate w_botline again */
2321 if (must_redraw != 0)
2322 {
2323 /* Don't update for changes in buffer again. */
2324 i = curbuf->b_mod_set;
2325 curbuf->b_mod_set = FALSE;
2326 win_update(curwin);
2327 must_redraw = 0;
2328 curbuf->b_mod_set = i;
2329 }
2330 recursive = FALSE;
2331 }
2332 }
2333
2334#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2335 /* restore got_int, unless CTRL-C was hit while redrawing */
2336 if (!got_int)
2337 got_int = save_got_int;
2338#endif
2339}
2340
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341/*
2342 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2343 * as the filler character.
2344 */
2345 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002346win_draw_end(
2347 win_T *wp,
2348 int c1,
2349 int c2,
2350 int row,
2351 int endrow,
2352 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353{
2354#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2355 int n = 0;
2356# define FDC_OFF n
2357#else
2358# define FDC_OFF 0
2359#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002360#ifdef FEAT_FOLDING
2361 int fdc = compute_foldcolumn(wp, 0);
2362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
2364#ifdef FEAT_RIGHTLEFT
2365 if (wp->w_p_rl)
2366 {
2367 /* No check for cmdline window: should never be right-left. */
2368# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002369 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
2371 if (n > 0)
2372 {
2373 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002374 if (n > W_WIDTH(wp))
2375 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2377 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002378 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 }
2380# endif
2381# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002382 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
2384 int nn = n + 2;
2385
2386 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002387 if (nn > W_WIDTH(wp))
2388 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2390 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002391 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 n = nn;
2393 }
2394# endif
2395 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2396 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002397 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2399 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002400 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
2402 else
2403#endif
2404 {
2405#ifdef FEAT_CMDWIN
2406 if (cmdwin_type != 0 && wp == curwin)
2407 {
2408 /* draw the cmdline character in the leftmost column */
2409 n = 1;
2410 if (n > wp->w_width)
2411 n = wp->w_width;
2412 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2413 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002414 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416#endif
2417#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002418 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002420 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422 /* draw the fold column at the left */
2423 if (nn > W_WIDTH(wp))
2424 nn = W_WIDTH(wp);
2425 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2426 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002427 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 n = nn;
2429 }
2430#endif
2431#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002432 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
2434 int nn = n + 2;
2435
2436 /* draw the sign column after the fold column */
2437 if (nn > W_WIDTH(wp))
2438 nn = W_WIDTH(wp);
2439 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2440 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002441 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 n = nn;
2443 }
2444#endif
2445 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2446 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002447 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
2449 set_empty_rows(wp, row);
2450}
2451
Bram Moolenaar1a384422010-07-14 19:53:30 +02002452#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002453static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002454
2455/*
2456 * Advance **color_cols and return TRUE when there are columns to draw.
2457 */
2458 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002459advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002460{
2461 while (**color_cols >= 0 && vcol > **color_cols)
2462 ++*color_cols;
2463 return (**color_cols >= 0);
2464}
2465#endif
2466
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467#ifdef FEAT_FOLDING
2468/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002469 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2470 * space is available for window "wp", minus "col".
2471 */
2472 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002473compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002474{
2475 int fdc = wp->w_p_fdc;
2476 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2477 int wwidth = W_WIDTH(wp);
2478
2479 if (fdc > wwidth - (col + wmw))
2480 fdc = wwidth - (col + wmw);
2481 return fdc;
2482}
2483
2484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 * Display one folded line.
2486 */
2487 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002488fold_line(
2489 win_T *wp,
2490 long fold_count,
2491 foldinfo_T *foldinfo,
2492 linenr_T lnum,
2493 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002495 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 pos_T *top, *bot;
2497 linenr_T lnume = lnum + fold_count - 1;
2498 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002499 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 int col;
2502 int txtcol;
2503 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002504 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
2506 /* Build the fold line:
2507 * 1. Add the cmdwin_type for the command-line window
2508 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002509 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 * 4. Compose the text
2511 * 5. Add the text
2512 * 6. set highlighting for the Visual area an other text
2513 */
2514 col = 0;
2515
2516 /*
2517 * 1. Add the cmdwin_type for the command-line window
2518 * Ignores 'rightleft', this window is never right-left.
2519 */
2520#ifdef FEAT_CMDWIN
2521 if (cmdwin_type != 0 && wp == curwin)
2522 {
2523 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002524 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525#ifdef FEAT_MBYTE
2526 if (enc_utf8)
2527 ScreenLinesUC[off] = 0;
2528#endif
2529 ++col;
2530 }
2531#endif
2532
2533 /*
2534 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002535 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002537 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 if (fdc > 0)
2539 {
2540 fill_foldcolumn(buf, wp, TRUE, lnum);
2541#ifdef FEAT_RIGHTLEFT
2542 if (wp->w_p_rl)
2543 {
2544 int i;
2545
2546 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002547 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 /* reverse the fold column */
2549 for (i = 0; i < fdc; ++i)
2550 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2551 }
2552 else
2553#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002554 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 col += fdc;
2556 }
2557
2558#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002559# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2560 for (ri = 0; ri < l; ++ri) \
2561 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2562 else \
2563 for (ri = 0; ri < l; ++ri) \
2564 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002566# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2567 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568#endif
2569
Bram Moolenaar64486672010-05-16 15:46:46 +02002570 /* Set all attributes of the 'number' or 'relativenumber' column and the
2571 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002572 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
2574#ifdef FEAT_SIGNS
2575 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002576 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 {
2578 len = W_WIDTH(wp) - col;
2579 if (len > 0)
2580 {
2581 if (len > 2)
2582 len = 2;
2583# ifdef FEAT_RIGHTLEFT
2584 if (wp->w_p_rl)
2585 /* the line number isn't reversed */
2586 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002587 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 else
2589# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002590 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 col += len;
2592 }
2593 }
2594#endif
2595
2596 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002597 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002599 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 {
2601 len = W_WIDTH(wp) - col;
2602 if (len > 0)
2603 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002604 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002605 long num;
2606 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002607
2608 if (len > w + 1)
2609 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002610
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002611 if (wp->w_p_nu && !wp->w_p_rnu)
2612 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002613 num = (long)lnum;
2614 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002615 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002616 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002617 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002618 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002619 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002620 /* 'number' + 'relativenumber': cursor line shows absolute
2621 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002622 num = lnum;
2623 fmt = "%-*ld ";
2624 }
2625 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002626
Bram Moolenaar700e7342013-01-30 12:31:36 +01002627 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628#ifdef FEAT_RIGHTLEFT
2629 if (wp->w_p_rl)
2630 /* the line number isn't reversed */
2631 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002632 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 else
2634#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002635 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 col += len;
2637 }
2638 }
2639
2640 /*
2641 * 4. Compose the folded-line string with 'foldtext', if set.
2642 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002643 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
2645 txtcol = col; /* remember where text starts */
2646
2647 /*
2648 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2649 * Right-left text is put in columns 0 - number-col, normal text is put
2650 * in columns number-col - window-width.
2651 */
2652#ifdef FEAT_MBYTE
2653 if (has_mbyte)
2654 {
2655 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002656 int u8c, u8cc[MAX_MCO];
2657 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 int idx;
2659 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002660 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661# ifdef FEAT_ARABIC
2662 int prev_c = 0; /* previous Arabic character */
2663 int prev_c1 = 0; /* first composing char for prev_c */
2664# endif
2665
2666# ifdef FEAT_RIGHTLEFT
2667 if (wp->w_p_rl)
2668 idx = off;
2669 else
2670# endif
2671 idx = off + col;
2672
2673 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2674 for (p = text; *p != NUL; )
2675 {
2676 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002677 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (col + cells > W_WIDTH(wp)
2679# ifdef FEAT_RIGHTLEFT
2680 - (wp->w_p_rl ? col : 0)
2681# endif
2682 )
2683 break;
2684 ScreenLines[idx] = *p;
2685 if (enc_utf8)
2686 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002687 u8c = utfc_ptr2char(p, u8cc);
2688 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 {
2690 ScreenLinesUC[idx] = 0;
2691#ifdef FEAT_ARABIC
2692 prev_c = u8c;
2693#endif
2694 }
2695 else
2696 {
2697#ifdef FEAT_ARABIC
2698 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2699 {
2700 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002701 int pc, pc1, nc;
2702 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 int firstbyte = *p;
2704
2705 /* The idea of what is the previous and next
2706 * character depends on 'rightleft'. */
2707 if (wp->w_p_rl)
2708 {
2709 pc = prev_c;
2710 pc1 = prev_c1;
2711 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002712 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 else
2715 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002716 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002718 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 }
2720 prev_c = u8c;
2721
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002722 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 pc, pc1, nc);
2724 ScreenLines[idx] = firstbyte;
2725 }
2726 else
2727 prev_c = u8c;
2728#endif
2729 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002730#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 if (u8c >= 0x10000)
2732 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2733 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002736 for (i = 0; i < Screen_mco; ++i)
2737 {
2738 ScreenLinesC[i][idx] = u8cc[i];
2739 if (u8cc[i] == 0)
2740 break;
2741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
2743 if (cells > 1)
2744 ScreenLines[idx + 1] = 0;
2745 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002746 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2747 /* double-byte single width character */
2748 ScreenLines2[idx] = p[1];
2749 else if (cells > 1)
2750 /* double-width character */
2751 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 col += cells;
2753 idx += cells;
2754 p += c_len;
2755 }
2756 }
2757 else
2758#endif
2759 {
2760 len = (int)STRLEN(text);
2761 if (len > W_WIDTH(wp) - col)
2762 len = W_WIDTH(wp) - col;
2763 if (len > 0)
2764 {
2765#ifdef FEAT_RIGHTLEFT
2766 if (wp->w_p_rl)
2767 STRNCPY(current_ScreenLine, text, len);
2768 else
2769#endif
2770 STRNCPY(current_ScreenLine + col, text, len);
2771 col += len;
2772 }
2773 }
2774
2775 /* Fill the rest of the line with the fold filler */
2776#ifdef FEAT_RIGHTLEFT
2777 if (wp->w_p_rl)
2778 col -= txtcol;
2779#endif
2780 while (col < W_WIDTH(wp)
2781#ifdef FEAT_RIGHTLEFT
2782 - (wp->w_p_rl ? txtcol : 0)
2783#endif
2784 )
2785 {
2786#ifdef FEAT_MBYTE
2787 if (enc_utf8)
2788 {
2789 if (fill_fold >= 0x80)
2790 {
2791 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002792 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002793 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 }
2795 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002798 ScreenLines[off + col] = fill_fold;
2799 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002800 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002802 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002804 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
2806
2807 if (text != buf)
2808 vim_free(text);
2809
2810 /*
2811 * 6. set highlighting for the Visual area an other text.
2812 * If all folded lines are in the Visual area, highlight the line.
2813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2815 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002816 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 {
2818 /* Visual is after curwin->w_cursor */
2819 top = &curwin->w_cursor;
2820 bot = &VIsual;
2821 }
2822 else
2823 {
2824 /* Visual is before curwin->w_cursor */
2825 top = &VIsual;
2826 bot = &curwin->w_cursor;
2827 }
2828 if (lnum >= top->lnum
2829 && lnume <= bot->lnum
2830 && (VIsual_mode != 'v'
2831 || ((lnum > top->lnum
2832 || (lnum == top->lnum
2833 && top->col == 0))
2834 && (lnume < bot->lnum
2835 || (lnume == bot->lnum
2836 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002837 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 {
2839 if (VIsual_mode == Ctrl_V)
2840 {
2841 /* Visual block mode: highlight the chars part of the block */
2842 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2843 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002844 if (wp->w_old_cursor_lcol != MAXCOL
2845 && wp->w_old_cursor_lcol + txtcol
2846 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 len = wp->w_old_cursor_lcol;
2848 else
2849 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002850 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002851 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 }
2853 }
2854 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002857 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 }
2860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002862#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002863 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2864 if (wp->w_p_cc_cols)
2865 {
2866 int i = 0;
2867 int j = wp->w_p_cc_cols[i];
2868 int old_txtcol = txtcol;
2869
2870 while (j > -1)
2871 {
2872 txtcol += j;
2873 if (wp->w_p_wrap)
2874 txtcol -= wp->w_skipcol;
2875 else
2876 txtcol -= wp->w_leftcol;
2877 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2878 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002879 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002880 txtcol = old_txtcol;
2881 j = wp->w_p_cc_cols[++i];
2882 }
2883 }
2884
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002885 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002886 if (wp->w_p_cuc)
2887 {
2888 txtcol += wp->w_virtcol;
2889 if (wp->w_p_wrap)
2890 txtcol -= wp->w_skipcol;
2891 else
2892 txtcol -= wp->w_leftcol;
2893 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2894 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002895 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002896 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002899 screen_line(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 (int)W_WIDTH(wp), FALSE);
2901
2902 /*
2903 * Update w_cline_height and w_cline_folded if the cursor line was
2904 * updated (saves a call to plines() later).
2905 */
2906 if (wp == curwin
2907 && lnum <= curwin->w_cursor.lnum
2908 && lnume >= curwin->w_cursor.lnum)
2909 {
2910 curwin->w_cline_row = row;
2911 curwin->w_cline_height = 1;
2912 curwin->w_cline_folded = TRUE;
2913 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2914 }
2915}
2916
2917/*
2918 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2919 */
2920 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002921copy_text_attr(
2922 int off,
2923 char_u *buf,
2924 int len,
2925 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002927 int i;
2928
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 mch_memmove(ScreenLines + off, buf, (size_t)len);
2930# ifdef FEAT_MBYTE
2931 if (enc_utf8)
2932 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2933# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002934 for (i = 0; i < len; ++i)
2935 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936}
2937
2938/*
2939 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002940 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 */
2942 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002943fill_foldcolumn(
2944 char_u *p,
2945 win_T *wp,
2946 int closed, /* TRUE of FALSE */
2947 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948{
2949 int i = 0;
2950 int level;
2951 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002952 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002953 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
2955 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002956 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
2958 level = win_foldinfo.fi_level;
2959 if (level > 0)
2960 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002961 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002962 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002963
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 /* If the column is too narrow, we start at the lowest level that
2965 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002966 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 if (first_level < 1)
2968 first_level = 1;
2969
Bram Moolenaar1c934292015-01-27 16:39:29 +01002970 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 {
2972 if (win_foldinfo.fi_lnum == lnum
2973 && first_level + i >= win_foldinfo.fi_low_level)
2974 p[i] = '-';
2975 else if (first_level == 1)
2976 p[i] = '|';
2977 else if (first_level + i <= 9)
2978 p[i] = '0' + first_level + i;
2979 else
2980 p[i] = '>';
2981 if (first_level + i == level)
2982 break;
2983 }
2984 }
2985 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002986 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987}
2988#endif /* FEAT_FOLDING */
2989
2990/*
2991 * Display line "lnum" of window 'wp' on the screen.
2992 * Start at row "startrow", stop when "endrow" is reached.
2993 * wp->w_virtcol needs to be valid.
2994 *
2995 * Return the number of last row the line occupies.
2996 */
2997 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002998win_line(
2999 win_T *wp,
3000 linenr_T lnum,
3001 int startrow,
3002 int endrow,
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003003 int nochange UNUSED, /* not updating for changed text */
3004 proftime_T *syntax_tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003006 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3008 int c = 0; /* init for GCC */
3009 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003010#ifdef FEAT_LINEBREAK
3011 long vcol_sbr = -1; /* virtual column after showbreak */
3012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 long vcol_prev = -1; /* "vcol" of previous character */
3014 char_u *line; /* current line */
3015 char_u *ptr; /* current position in "line" */
3016 int row; /* row in the window, excl w_winrow */
3017 int screen_row; /* row on the screen, incl w_winrow */
3018
3019 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3020 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003021 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003022 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 int c_extra = NUL; /* extra chars, all the same */
3024 int extra_attr = 0; /* attributes when n_extra != 0 */
3025 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3026 displaying lcs_eol at end-of-line */
3027 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3028 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3029
3030 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3031 int saved_n_extra = 0;
3032 char_u *saved_p_extra = NULL;
3033 int saved_c_extra = 0;
3034 int saved_char_attr = 0;
3035
3036 int n_attr = 0; /* chars with special attr */
3037 int saved_attr2 = 0; /* char_attr saved for n_attr */
3038 int n_attr3 = 0; /* chars with overruling special attr */
3039 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3040
3041 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3042
3043 int fromcol, tocol; /* start/end of inverting */
3044 int fromcol_prev = -2; /* start of inverting after cursor */
3045 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003047 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 pos_T pos;
3049 long v;
3050
3051 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003052 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3054 in this line */
3055 int attr = 0; /* attributes for area highlighting */
3056 int area_attr = 0; /* attributes desired by highlighting */
3057 int search_attr = 0; /* attributes desired by 'hlsearch' */
3058#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003059 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 int syntax_attr = 0; /* attributes desired by syntax */
3061 int has_syntax = FALSE; /* this buffer has syntax highl. */
3062 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003063 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003064 int draw_color_col = FALSE; /* highlight colorcolumn */
3065 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003066#endif
3067#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003068 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003069# define SPWORDLEN 150
3070 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003071 int nextlinecol = 0; /* column where nextline[] starts */
3072 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003073 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003074 int spell_attr = 0; /* attributes desired by spelling */
3075 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003076 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3077 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003078 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003079 static int cap_col = -1; /* column to check for Cap word */
3080 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003081 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082#endif
3083 int extra_check; /* has syntax or linebreak */
3084#ifdef FEAT_MBYTE
3085 int multi_attr = 0; /* attributes desired by multibyte */
3086 int mb_l = 1; /* multi-byte byte length */
3087 int mb_c = 0; /* decoded multi-byte character */
3088 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003089 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090#endif
3091#ifdef FEAT_DIFF
3092 int filler_lines; /* nr of filler lines to be drawn */
3093 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003094 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 int change_start = MAXCOL; /* first col of changed area */
3096 int change_end = -1; /* last col of changed area */
3097#endif
3098 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3099#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003100 int need_showbreak = FALSE; /* overlong line, skipping first x
3101 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003103#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3104 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003106 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107#endif
3108#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003109 matchitem_T *cur; /* points to the match list */
3110 match_T *shl; /* points to search_hl or a match */
3111 int shl_flag; /* flag to indicate whether search_hl
3112 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003113 int pos_inprogress; /* marks that position match search is
3114 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003115 int prevcol_hl_flag; /* flag to indicate whether prevcol
3116 equals startcol of search_hl or one
3117 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118#endif
3119#ifdef FEAT_ARABIC
3120 int prev_c = 0; /* previous Arabic character */
3121 int prev_c1 = 0; /* first composing char for prev_c */
3122#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003123#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003124 int did_line_attr = 0;
3125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
3127 /* draw_state: items that are drawn in sequence: */
3128#define WL_START 0 /* nothing done yet */
3129#ifdef FEAT_CMDWIN
3130# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3131#else
3132# define WL_CMDLINE WL_START
3133#endif
3134#ifdef FEAT_FOLDING
3135# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3136#else
3137# define WL_FOLD WL_CMDLINE
3138#endif
3139#ifdef FEAT_SIGNS
3140# define WL_SIGN WL_FOLD + 1 /* column for signs */
3141#else
3142# define WL_SIGN WL_FOLD /* column for signs */
3143#endif
3144#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003145#ifdef FEAT_LINEBREAK
3146# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003148# define WL_BRI WL_NR
3149#endif
3150#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3151# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3152#else
3153# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154#endif
3155#define WL_LINE WL_SBR + 1 /* text in the line */
3156 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003157#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 int feedback_col = 0;
3159 int feedback_old_attr = -1;
3160#endif
3161
Bram Moolenaar860cae12010-06-05 23:22:07 +02003162#ifdef FEAT_CONCEAL
3163 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003164 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003165 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003166 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003167 int is_concealing = FALSE;
3168 int boguscols = 0; /* nonexistent columns added to force
3169 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003170 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003171 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003172 int match_conc = 0; /* cchar for match functions */
3173 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003174 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003175# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003176# define FIX_FOR_BOGUSCOLS \
3177 { \
3178 n_extra += vcol_off; \
3179 vcol -= vcol_off; \
3180 vcol_off = 0; \
3181 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003182 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003183 boguscols = 0; \
3184 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003185#else
3186# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 if (startrow > endrow) /* past the end already! */
3190 return startrow;
3191
3192 row = startrow;
3193 screen_row = row + W_WINROW(wp);
3194
3195 /*
3196 * To speed up the loop below, set extra_check when there is linebreak,
3197 * trailing white space and/or syntax processing to be done.
3198 */
3199#ifdef FEAT_LINEBREAK
3200 extra_check = wp->w_p_lbr;
3201#else
3202 extra_check = 0;
3203#endif
3204#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003205 if (syntax_present(wp) && !wp->w_s->b_syn_error
3206# ifdef SYN_TIME_LIMIT
3207 && !wp->w_s->b_syn_slow
3208# endif
3209 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
3211 /* Prepare for syntax highlighting in this line. When there is an
3212 * error, stop syntax highlighting. */
3213 save_did_emsg = did_emsg;
3214 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003215 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003217 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 else
3219 {
3220 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003221#ifdef SYN_TIME_LIMIT
3222 if (!wp->w_s->b_syn_slow)
3223#endif
3224 {
3225 has_syntax = TRUE;
3226 extra_check = TRUE;
3227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003230
3231 /* Check for columns to display for 'colorcolumn'. */
3232 color_cols = wp->w_p_cc_cols;
3233 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003234 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003235#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003236
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003237#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003238 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003239 && *wp->w_s->b_p_spl != NUL
3240 && wp->w_s->b_langp.ga_len > 0
3241 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003242 {
3243 /* Prepare for spell checking. */
3244 has_spell = TRUE;
3245 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003246
3247 /* Get the start of the next line, so that words that wrap to the next
3248 * line are found too: "et<line-break>al.".
3249 * Trick: skip a few chars for C/shell/Vim comments */
3250 nextline[SPWORDLEN] = NUL;
3251 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3252 {
3253 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3254 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3255 }
3256
3257 /* When a word wrapped from the previous line the start of the current
3258 * line is valid. */
3259 if (lnum == checked_lnum)
3260 cur_checked_col = checked_col;
3261 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003262
3263 /* When there was a sentence end in the previous line may require a
3264 * word starting with capital in this line. In line 1 always check
3265 * the first word. */
3266 if (lnum != capcol_lnum)
3267 cap_col = -1;
3268 if (lnum == 1)
3269 cap_col = 0;
3270 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#endif
3273
3274 /*
3275 * handle visual active in this window
3276 */
3277 fromcol = -10;
3278 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3280 {
3281 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003282 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
3284 top = &curwin->w_cursor;
3285 bot = &VIsual;
3286 }
3287 else /* Visual is before curwin->w_cursor */
3288 {
3289 top = &VIsual;
3290 bot = &curwin->w_cursor;
3291 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003292 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (VIsual_mode == Ctrl_V) /* block mode */
3294 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003295 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
3297 fromcol = wp->w_old_cursor_fcol;
3298 tocol = wp->w_old_cursor_lcol;
3299 }
3300 }
3301 else /* non-block mode */
3302 {
3303 if (lnum > top->lnum && lnum <= bot->lnum)
3304 fromcol = 0;
3305 else if (lnum == top->lnum)
3306 {
3307 if (VIsual_mode == 'V') /* linewise */
3308 fromcol = 0;
3309 else
3310 {
3311 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3312 if (gchar_pos(top) == NUL)
3313 tocol = fromcol + 1;
3314 }
3315 }
3316 if (VIsual_mode != 'V' && lnum == bot->lnum)
3317 {
3318 if (*p_sel == 'e' && bot->col == 0
3319#ifdef FEAT_VIRTUALEDIT
3320 && bot->coladd == 0
3321#endif
3322 )
3323 {
3324 fromcol = -10;
3325 tocol = MAXCOL;
3326 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003327 else if (bot->col == MAXCOL)
3328 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 else
3330 {
3331 pos = *bot;
3332 if (*p_sel == 'e')
3333 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3334 else
3335 {
3336 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3337 ++tocol;
3338 }
3339 }
3340 }
3341 }
3342
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 /* Check if the character under the cursor should not be inverted */
3344 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003345#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 )
3349 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351 /* if inverting in this line set area_highlighting */
3352 if (fromcol >= 0)
3353 {
3354 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003355 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003357 if ((clip_star.available && !clip_star.owned
3358 && clip_isautosel_star())
3359 || (clip_plus.available && !clip_plus.owned
3360 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003361 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362#endif
3363 }
3364 }
3365
3366 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003367 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003369 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 && wp == curwin
3371 && lnum >= curwin->w_cursor.lnum
3372 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3373 {
3374 if (lnum == curwin->w_cursor.lnum)
3375 getvcol(curwin, &(curwin->w_cursor),
3376 (colnr_T *)&fromcol, NULL, NULL);
3377 else
3378 fromcol = 0;
3379 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3380 {
3381 pos.lnum = lnum;
3382 pos.col = search_match_endcol;
3383 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3384 }
3385 else
3386 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003387 /* do at least one character; happens when past end of line */
3388 if (fromcol == tocol)
3389 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003391 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 }
3393
3394#ifdef FEAT_DIFF
3395 filler_lines = diff_check(wp, lnum);
3396 if (filler_lines < 0)
3397 {
3398 if (filler_lines == -1)
3399 {
3400 if (diff_find_change(wp, lnum, &change_start, &change_end))
3401 diff_hlf = HLF_ADD; /* added line */
3402 else if (change_start == 0)
3403 diff_hlf = HLF_TXD; /* changed text */
3404 else
3405 diff_hlf = HLF_CHD; /* changed line */
3406 }
3407 else
3408 diff_hlf = HLF_ADD; /* added line */
3409 filler_lines = 0;
3410 area_highlighting = TRUE;
3411 }
3412 if (lnum == wp->w_topline)
3413 filler_lines = wp->w_topfill;
3414 filler_todo = filler_lines;
3415#endif
3416
3417#ifdef LINE_ATTR
3418# ifdef FEAT_SIGNS
3419 /* If this line has a sign with line highlighting set line_attr. */
3420 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3421 if (v != 0)
3422 line_attr = sign_get_attr((int)v, TRUE);
3423# endif
3424# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3425 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003426 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003427 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428# endif
3429 if (line_attr != 0)
3430 area_highlighting = TRUE;
3431#endif
3432
3433 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3434 ptr = line;
3435
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003436#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003437 if (has_spell)
3438 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003439 /* For checking first word with a capital skip white space. */
3440 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003441 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003442
Bram Moolenaar30abd282005-06-22 22:35:10 +00003443 /* To be able to spell-check over line boundaries copy the end of the
3444 * current line into nextline[]. Above the start of the next line was
3445 * copied to nextline[SPWORDLEN]. */
3446 if (nextline[SPWORDLEN] == NUL)
3447 {
3448 /* No next line or it is empty. */
3449 nextlinecol = MAXCOL;
3450 nextline_idx = 0;
3451 }
3452 else
3453 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003454 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003455 if (v < SPWORDLEN)
3456 {
3457 /* Short line, use it completely and append the start of the
3458 * next line. */
3459 nextlinecol = 0;
3460 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003461 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003462 nextline_idx = v + 1;
3463 }
3464 else
3465 {
3466 /* Long line, use only the last SPWORDLEN bytes. */
3467 nextlinecol = v - SPWORDLEN;
3468 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3469 nextline_idx = SPWORDLEN + 1;
3470 }
3471 }
3472 }
3473#endif
3474
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003475 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003477 if (lcs_space || lcs_trail)
3478 extra_check = TRUE;
3479 /* find start of trailing whitespace */
3480 if (lcs_trail)
3481 {
3482 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003483 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003484 --trailcol;
3485 trailcol += (colnr_T) (ptr - line);
3486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488
3489 /*
3490 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3491 * first character to be displayed.
3492 */
3493 if (wp->w_p_wrap)
3494 v = wp->w_skipcol;
3495 else
3496 v = wp->w_leftcol;
3497 if (v > 0)
3498 {
3499#ifdef FEAT_MBYTE
3500 char_u *prev_ptr = ptr;
3501#endif
3502 while (vcol < v && *ptr != NUL)
3503 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003504 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 vcol += c;
3506#ifdef FEAT_MBYTE
3507 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003509 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003512 /* When:
3513 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003514 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003515 * - 'virtualedit' is set, or
3516 * - the visual mode is active,
3517 * the end of the line may be before the start of the displayed part.
3518 */
3519 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003520#ifdef FEAT_SYN_HL
3521 wp->w_p_cuc || draw_color_col ||
3522#endif
3523#ifdef FEAT_VIRTUALEDIT
3524 virtual_active() ||
3525#endif
3526 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
3531 /* Handle a character that's not completely on the screen: Put ptr at
3532 * that character but skip the first few screen characters. */
3533 if (vcol > v)
3534 {
3535 vcol -= c;
3536#ifdef FEAT_MBYTE
3537 ptr = prev_ptr;
3538#else
3539 --ptr;
3540#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003541 /* If the character fits on the screen, don't need to skip it.
3542 * Except for a TAB. */
3543 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003544#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003545 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003546#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003547 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003548 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550
3551 /*
3552 * Adjust for when the inverted text is before the screen,
3553 * and when the start of the inverted text is before the screen.
3554 */
3555 if (tocol <= vcol)
3556 fromcol = 0;
3557 else if (fromcol >= 0 && fromcol < vcol)
3558 fromcol = vcol;
3559
3560#ifdef FEAT_LINEBREAK
3561 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3562 if (wp->w_p_wrap)
3563 need_showbreak = TRUE;
3564#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003565#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003566 /* When spell checking a word we need to figure out the start of the
3567 * word and if it's badly spelled or not. */
3568 if (has_spell)
3569 {
3570 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003571 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003572 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003573
3574 pos = wp->w_cursor;
3575 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003576 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003577 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003578
3579 /* spell_move_to() may call ml_get() and make "line" invalid */
3580 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3581 ptr = line + linecol;
3582
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003583 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003584 {
3585 /* no bad word found at line start, don't check until end of a
3586 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003587 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003588 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003589 }
3590 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003591 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003592 /* bad word found, use attributes until end of word */
3593 word_end = wp->w_cursor.col + len + 1;
3594
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003595 /* Turn index into actual attributes. */
3596 if (spell_hlf != HLF_COUNT)
3597 spell_attr = highlight_attr[spell_hlf];
3598 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003599 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003600
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003601# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003602 /* Need to restart syntax highlighting for this line. */
3603 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003604 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003605# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003606 }
3607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 }
3609
3610 /*
3611 * Correct highlighting for cursor that can't be disabled.
3612 * Avoids having to check this for each character.
3613 */
3614 if (fromcol >= 0)
3615 {
3616 if (noinvcur)
3617 {
3618 if ((colnr_T)fromcol == wp->w_virtcol)
3619 {
3620 /* highlighting starts at cursor, let it start just after the
3621 * cursor */
3622 fromcol_prev = fromcol;
3623 fromcol = -1;
3624 }
3625 else if ((colnr_T)fromcol < wp->w_virtcol)
3626 /* restart highlighting after the cursor */
3627 fromcol_prev = wp->w_virtcol;
3628 }
3629 if (fromcol >= tocol)
3630 fromcol = -1;
3631 }
3632
3633#ifdef FEAT_SEARCH_EXTRA
3634 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003635 * Handle highlighting the last used search pattern and matches.
3636 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003638 cur = wp->w_match_head;
3639 shl_flag = FALSE;
3640 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003642 if (shl_flag == FALSE)
3643 {
3644 shl = &search_hl;
3645 shl_flag = TRUE;
3646 }
3647 else
3648 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003649 shl->startcol = MAXCOL;
3650 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003652 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003653 v = (long)(ptr - line);
3654 if (cur != NULL)
3655 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003656 next_search_hl(wp, shl, lnum, (colnr_T)v,
3657 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003658
3659 /* Need to get the line again, a multi-line regexp may have made it
3660 * invalid. */
3661 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3662 ptr = line + v;
3663
3664 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003666 if (shl->lnum == lnum)
3667 shl->startcol = shl->rm.startpos[0].col;
3668 else
3669 shl->startcol = 0;
3670 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3671 - shl->rm.startpos[0].lnum)
3672 shl->endcol = shl->rm.endpos[0].col;
3673 else
3674 shl->endcol = MAXCOL;
3675 /* Highlight one character for an empty match. */
3676 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003679 if (has_mbyte && line[shl->endcol] != NUL)
3680 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3681 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003683 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003685 if ((long)shl->startcol < v) /* match at leftcol */
3686 {
3687 shl->attr_cur = shl->attr;
3688 search_attr = shl->attr;
3689 }
3690 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003692 if (shl != &search_hl && cur != NULL)
3693 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
3695#endif
3696
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003697#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003698 /* Cursor line highlighting for 'cursorline' in the current window. Not
3699 * when Visual mode is active, because it's not clear what is selected
3700 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003701 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003702 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003703 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003704 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003705 area_highlighting = TRUE;
3706 }
3707#endif
3708
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003709 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 col = 0;
3711#ifdef FEAT_RIGHTLEFT
3712 if (wp->w_p_rl)
3713 {
3714 /* Rightleft window: process the text in the normal direction, but put
3715 * it in current_ScreenLine[] from right to left. Start at the
3716 * rightmost column of the window. */
3717 col = W_WIDTH(wp) - 1;
3718 off += col;
3719 }
3720#endif
3721
3722 /*
3723 * Repeat for the whole displayed line.
3724 */
3725 for (;;)
3726 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003727#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003728 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 /* Skip this quickly when working on the text. */
3731 if (draw_state != WL_LINE)
3732 {
3733#ifdef FEAT_CMDWIN
3734 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3735 {
3736 draw_state = WL_CMDLINE;
3737 if (cmdwin_type != 0 && wp == curwin)
3738 {
3739 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003741 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003742 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
3744 }
3745#endif
3746
3747#ifdef FEAT_FOLDING
3748 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3749 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003750 int fdc = compute_foldcolumn(wp, 0);
3751
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003753 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003755 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003756 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003757 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003758 p_extra_free = alloc(12 + 1);
3759
3760 if (p_extra_free != NULL)
3761 {
3762 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3763 n_extra = fdc;
3764 p_extra_free[n_extra] = NUL;
3765 p_extra = p_extra_free;
3766 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003767 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
3770 }
3771#endif
3772
3773#ifdef FEAT_SIGNS
3774 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3775 {
3776 draw_state = WL_SIGN;
3777 /* Show the sign column when there are any signs in this
3778 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003779 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003781 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003783 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784# endif
3785
3786 /* Draw two cells with the sign value or blank. */
3787 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003788 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 n_extra = 2;
3790
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003791 if (row == startrow
3792#ifdef FEAT_DIFF
3793 + filler_lines && filler_todo <= 0
3794#endif
3795 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
3797 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3798 SIGN_TEXT);
3799# ifdef FEAT_SIGN_ICONS
3800 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3801 SIGN_ICON);
3802 if (gui.in_use && icon_sign != 0)
3803 {
3804 /* Use the image in this position. */
3805 c_extra = SIGN_BYTE;
3806# ifdef FEAT_NETBEANS_INTG
3807 if (buf_signcount(wp->w_buffer, lnum) > 1)
3808 c_extra = MULTISIGN_BYTE;
3809# endif
3810 char_attr = icon_sign;
3811 }
3812 else
3813# endif
3814 if (text_sign != 0)
3815 {
3816 p_extra = sign_get_text(text_sign);
3817 if (p_extra != NULL)
3818 {
3819 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003820 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822 char_attr = sign_get_attr(text_sign, FALSE);
3823 }
3824 }
3825 }
3826 }
3827#endif
3828
3829 if (draw_state == WL_NR - 1 && n_extra == 0)
3830 {
3831 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003832 /* Display the absolute or relative line number. After the
3833 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3834 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 && (row == startrow
3836#ifdef FEAT_DIFF
3837 + filler_lines
3838#endif
3839 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3840 {
3841 /* Draw the line number (empty space after wrapping). */
3842 if (row == startrow
3843#ifdef FEAT_DIFF
3844 + filler_lines
3845#endif
3846 )
3847 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003848 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003849 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003850
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003851 if (wp->w_p_nu && !wp->w_p_rnu)
3852 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003853 num = (long)lnum;
3854 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003855 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003856 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003857 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003858 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003859 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003860 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003861 num = lnum;
3862 fmt = "%-*ld ";
3863 }
3864 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003865
Bram Moolenaar700e7342013-01-30 12:31:36 +01003866 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003867 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 if (wp->w_skipcol > 0)
3869 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3870 *p_extra = '-';
3871#ifdef FEAT_RIGHTLEFT
3872 if (wp->w_p_rl) /* reverse line numbers */
3873 rl_mirror(extra);
3874#endif
3875 p_extra = extra;
3876 c_extra = NUL;
3877 }
3878 else
3879 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003880 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003881 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003882#ifdef FEAT_SYN_HL
3883 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003884 * the current line differently.
3885 * TODO: Can we use CursorLine instead of CursorLineNr
3886 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003887 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003888 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003889 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 }
3892 }
3893
Bram Moolenaar597a4222014-06-25 14:39:50 +02003894#ifdef FEAT_LINEBREAK
3895 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3896 && n_extra == 0 && *p_sbr != NUL)
3897 /* draw indent after showbreak value */
3898 draw_state = WL_BRI;
3899 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3900 /* After the showbreak, draw the breakindent */
3901 draw_state = WL_BRI - 1;
3902
3903 /* draw 'breakindent': indent wrapped text accordingly */
3904 if (draw_state == WL_BRI - 1 && n_extra == 0)
3905 {
3906 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003907 /* if need_showbreak is set, breakindent also applies */
3908 if (wp->w_p_bri && n_extra == 0
3909 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003910# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003911 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003912# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003913 )
3914 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003915 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003916# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003917 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003918 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003919 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003920# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003921 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3922 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003923 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003924# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003925 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003926# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003927 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003928 c_extra = ' ';
3929 n_extra = get_breakindent_win(wp,
3930 ml_get_buf(wp->w_buffer, lnum, FALSE));
3931 /* Correct end of highlighted area for 'breakindent',
3932 * required when 'linebreak' is also set. */
3933 if (tocol == vcol)
3934 tocol += n_extra;
3935 }
3936 }
3937#endif
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3940 if (draw_state == WL_SBR - 1 && n_extra == 0)
3941 {
3942 draw_state = WL_SBR;
3943# ifdef FEAT_DIFF
3944 if (filler_todo > 0)
3945 {
3946 /* Draw "deleted" diff line(s). */
3947 if (char2cells(fill_diff) > 1)
3948 c_extra = '-';
3949 else
3950 c_extra = fill_diff;
3951# ifdef FEAT_RIGHTLEFT
3952 if (wp->w_p_rl)
3953 n_extra = col + 1;
3954 else
3955# endif
3956 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003957 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
3959# endif
3960# ifdef FEAT_LINEBREAK
3961 if (*p_sbr != NUL && need_showbreak)
3962 {
3963 /* Draw 'showbreak' at the start of each broken line. */
3964 p_extra = p_sbr;
3965 c_extra = NUL;
3966 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003967 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003969 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 /* Correct end of highlighted area for 'showbreak',
3971 * required when 'linebreak' is also set. */
3972 if (tocol == vcol)
3973 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003974#ifdef FEAT_SYN_HL
3975 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003976 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003977 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003978 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981# endif
3982 }
3983#endif
3984
3985 if (draw_state == WL_LINE - 1 && n_extra == 0)
3986 {
3987 draw_state = WL_LINE;
3988 if (saved_n_extra)
3989 {
3990 /* Continue item from end of wrapped line. */
3991 n_extra = saved_n_extra;
3992 c_extra = saved_c_extra;
3993 p_extra = saved_p_extra;
3994 char_attr = saved_char_attr;
3995 }
3996 else
3997 char_attr = 0;
3998 }
3999 }
4000
4001 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004002 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004003 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004#ifdef FEAT_DIFF
4005 && filler_todo <= 0
4006#endif
4007 )
4008 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004009 screen_line(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004010 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004011 /* Pretend we have finished updating the window. Except when
4012 * 'cursorcolumn' is set. */
4013#ifdef FEAT_SYN_HL
4014 if (wp->w_p_cuc)
4015 row = wp->w_cline_row + wp->w_cline_height;
4016 else
4017#endif
4018 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 break;
4020 }
4021
4022 if (draw_state == WL_LINE && area_highlighting)
4023 {
4024 /* handle Visual or match highlighting in this line */
4025 if (vcol == fromcol
4026#ifdef FEAT_MBYTE
4027 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4028 && (*mb_ptr2cells)(ptr) > 1)
4029#endif
4030 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004031 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 && vcol < tocol))
4033 area_attr = attr; /* start highlighting */
4034 else if (area_attr != 0
4035 && (vcol == tocol
4036 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
4039#ifdef FEAT_SEARCH_EXTRA
4040 if (!n_extra)
4041 {
4042 /*
4043 * Check for start/end of search pattern match.
4044 * After end, check for start/end of next match.
4045 * When another match, have to check for start again.
4046 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004047 * Do this for 'search_hl' and the match list (ordered by
4048 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004050 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004051 cur = wp->w_match_head;
4052 shl_flag = FALSE;
4053 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004055 if (shl_flag == FALSE
4056 && ((cur != NULL
4057 && cur->priority > SEARCH_HL_PRIORITY)
4058 || cur == NULL))
4059 {
4060 shl = &search_hl;
4061 shl_flag = TRUE;
4062 }
4063 else
4064 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004065 if (cur != NULL)
4066 cur->pos.cur = 0;
4067 pos_inprogress = TRUE;
4068 while (shl->rm.regprog != NULL
4069 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004071 if (shl->startcol != MAXCOL
4072 && v >= (long)shl->startcol
4073 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004075#ifdef FEAT_MBYTE
4076 int tmp_col = v + MB_PTR2LEN(ptr);
4077
4078 if (shl->endcol < tmp_col)
4079 shl->endcol = tmp_col;
4080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004082#ifdef FEAT_CONCEAL
4083 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4084 == cur->hlg_id)
4085 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004086 has_match_conc =
4087 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004088 match_conc = cur->conceal_char;
4089 }
4090 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004091 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004094 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
4096 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004097 next_search_hl(wp, shl, lnum, (colnr_T)v,
4098 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004099 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004100 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101
4102 /* Need to get the line again, a multi-line regexp
4103 * may have made it invalid. */
4104 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4105 ptr = line + v;
4106
4107 if (shl->lnum == lnum)
4108 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004109 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004111 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004113 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004115 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 {
4117 /* highlight empty match, try again after
4118 * it */
4119#ifdef FEAT_MBYTE
4120 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004121 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004122 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 else
4124#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004125 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
4127
4128 /* Loop to check if the match starts at the
4129 * current position */
4130 continue;
4131 }
4132 }
4133 break;
4134 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004135 if (shl != &search_hl && cur != NULL)
4136 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004138
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004139 /* Use attributes from match with highest priority among
4140 * 'search_hl' and the match list. */
4141 search_attr = search_hl.attr_cur;
4142 cur = wp->w_match_head;
4143 shl_flag = FALSE;
4144 while (cur != NULL || shl_flag == FALSE)
4145 {
4146 if (shl_flag == FALSE
4147 && ((cur != NULL
4148 && cur->priority > SEARCH_HL_PRIORITY)
4149 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004150 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004151 shl = &search_hl;
4152 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004153 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004154 else
4155 shl = &cur->hl;
4156 if (shl->attr_cur != 0)
4157 search_attr = shl->attr_cur;
4158 if (shl != &search_hl && cur != NULL)
4159 cur = cur->next;
4160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 }
4162#endif
4163
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004165 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004167 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4168 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004170 if (diff_hlf == HLF_TXD && ptr - line > change_end
4171 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004173 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004174 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004175 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004178
4179 /* Decide which of the highlight attributes to use. */
4180 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004181#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004182 if (area_attr != 0)
4183 char_attr = hl_combine_attr(line_attr, area_attr);
4184 else if (search_attr != 0)
4185 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004186 /* Use line_attr when not in the Visual or 'incsearch' area
4187 * (area_attr may be 0 when "noinvcur" is set). */
4188 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004189 || vcol < fromcol || vcol_prev < fromcol_prev
4190 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004191 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004192#else
4193 if (area_attr != 0)
4194 char_attr = area_attr;
4195 else if (search_attr != 0)
4196 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004197#endif
4198 else
4199 {
4200 attr_pri = FALSE;
4201#ifdef FEAT_SYN_HL
4202 if (has_syntax)
4203 char_attr = syntax_attr;
4204 else
4205#endif
4206 char_attr = 0;
4207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
4209
4210 /*
4211 * Get the next character to put on the screen.
4212 */
4213 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004214 * The "p_extra" points to the extra stuff that is inserted to
4215 * represent special characters (non-printable stuff) and other
4216 * things. When all characters are the same, c_extra is used.
4217 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4218 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4220 */
4221 if (n_extra > 0)
4222 {
4223 if (c_extra != NUL)
4224 {
4225 c = c_extra;
4226#ifdef FEAT_MBYTE
4227 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004228 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 {
4230 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004231 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004232 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
4234 else
4235 mb_utf8 = FALSE;
4236#endif
4237 }
4238 else
4239 {
4240 c = *p_extra;
4241#ifdef FEAT_MBYTE
4242 if (has_mbyte)
4243 {
4244 mb_c = c;
4245 if (enc_utf8)
4246 {
4247 /* If the UTF-8 character is more than one byte:
4248 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004249 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 mb_utf8 = FALSE;
4251 if (mb_l > n_extra)
4252 mb_l = 1;
4253 else if (mb_l > 1)
4254 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004255 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004257 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259 }
4260 else
4261 {
4262 /* if this is a DBCS character, put it in "mb_c" */
4263 mb_l = MB_BYTE2LEN(c);
4264 if (mb_l >= n_extra)
4265 mb_l = 1;
4266 else if (mb_l > 1)
4267 mb_c = (c << 8) + p_extra[1];
4268 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004269 if (mb_l == 0) /* at the NUL at end-of-line */
4270 mb_l = 1;
4271
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 /* If a double-width char doesn't fit display a '>' in the
4273 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004274 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275# ifdef FEAT_RIGHTLEFT
4276 wp->w_p_rl ? (col <= 0) :
4277# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004278 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 && (*mb_char2cells)(mb_c) == 2)
4280 {
4281 c = '>';
4282 mb_c = c;
4283 mb_l = 1;
4284 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004285 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 /* put the pointer back to output the double-width
4287 * character at the start of the next line. */
4288 ++n_extra;
4289 --p_extra;
4290 }
4291 else
4292 {
4293 n_extra -= mb_l - 1;
4294 p_extra += mb_l - 1;
4295 }
4296 }
4297#endif
4298 ++p_extra;
4299 }
4300 --n_extra;
4301 }
4302 else
4303 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004304#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004305 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004306#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004307
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004308 if (p_extra_free != NULL)
4309 {
4310 vim_free(p_extra_free);
4311 p_extra_free = NULL;
4312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 /*
4314 * Get a character from the line itself.
4315 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004316 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004317#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004318 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320#ifdef FEAT_MBYTE
4321 if (has_mbyte)
4322 {
4323 mb_c = c;
4324 if (enc_utf8)
4325 {
4326 /* If the UTF-8 character is more than one byte: Decode it
4327 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004328 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 mb_utf8 = FALSE;
4330 if (mb_l > 1)
4331 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004332 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 /* Overlong encoded ASCII or ASCII with composing char
4334 * is displayed normally, except a NUL. */
4335 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004336 {
4337 c = mb_c;
4338# ifdef FEAT_LINEBREAK
4339 c0 = mb_c;
4340# endif
4341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004343
4344 /* At start of the line we can have a composing char.
4345 * Draw it as a space with a composing char. */
4346 if (utf_iscomposing(mb_c))
4347 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004348 int i;
4349
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004350 for (i = Screen_mco - 1; i > 0; --i)
4351 u8cc[i] = u8cc[i - 1];
4352 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004353 mb_c = ' ';
4354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356
4357 if ((mb_l == 1 && c >= 0x80)
4358 || (mb_l >= 1 && mb_c == 0)
4359 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004360# ifdef UNICODE16
4361 || mb_c >= 0x10000
4362# endif
4363 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 {
4365 /*
4366 * Illegal UTF-8 byte: display as <xx>.
4367 * Non-BMP character : display as ? or fullwidth ?.
4368 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004369# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004371# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
4373 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004374# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 if (wp->w_p_rl) /* reverse */
4376 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004379# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 else if (utf_char2cells(mb_c) != 2)
4381 STRCPY(extra, "?");
4382 else
4383 /* 0xff1f in UTF-8: full-width '?' */
4384 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
4387 p_extra = extra;
4388 c = *p_extra;
4389 mb_c = mb_ptr2char_adv(&p_extra);
4390 mb_utf8 = (c >= 0x80);
4391 n_extra = (int)STRLEN(p_extra);
4392 c_extra = NUL;
4393 if (area_attr == 0 && search_attr == 0)
4394 {
4395 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004396 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 saved_attr2 = char_attr; /* save current attr */
4398 }
4399 }
4400 else if (mb_l == 0) /* at the NUL at end-of-line */
4401 mb_l = 1;
4402#ifdef FEAT_ARABIC
4403 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4404 {
4405 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004406 int pc, pc1, nc;
4407 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
4409 /* The idea of what is the previous and next
4410 * character depends on 'rightleft'. */
4411 if (wp->w_p_rl)
4412 {
4413 pc = prev_c;
4414 pc1 = prev_c1;
4415 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004416 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418 else
4419 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004420 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004422 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424 prev_c = mb_c;
4425
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004426 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 }
4428 else
4429 prev_c = mb_c;
4430#endif
4431 }
4432 else /* enc_dbcs */
4433 {
4434 mb_l = MB_BYTE2LEN(c);
4435 if (mb_l == 0) /* at the NUL at end-of-line */
4436 mb_l = 1;
4437 else if (mb_l > 1)
4438 {
4439 /* We assume a second byte below 32 is illegal.
4440 * Hopefully this is OK for all double-byte encodings!
4441 */
4442 if (ptr[1] >= 32)
4443 mb_c = (c << 8) + ptr[1];
4444 else
4445 {
4446 if (ptr[1] == NUL)
4447 {
4448 /* head byte at end of line */
4449 mb_l = 1;
4450 transchar_nonprint(extra, c);
4451 }
4452 else
4453 {
4454 /* illegal tail byte */
4455 mb_l = 2;
4456 STRCPY(extra, "XX");
4457 }
4458 p_extra = extra;
4459 n_extra = (int)STRLEN(extra) - 1;
4460 c_extra = NUL;
4461 c = *p_extra++;
4462 if (area_attr == 0 && search_attr == 0)
4463 {
4464 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004465 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 saved_attr2 = char_attr; /* save current attr */
4467 }
4468 mb_c = c;
4469 }
4470 }
4471 }
4472 /* If a double-width char doesn't fit display a '>' in the
4473 * last column; the character is displayed at the start of the
4474 * next line. */
4475 if ((
4476# ifdef FEAT_RIGHTLEFT
4477 wp->w_p_rl ? (col <= 0) :
4478# endif
4479 (col >= W_WIDTH(wp) - 1))
4480 && (*mb_char2cells)(mb_c) == 2)
4481 {
4482 c = '>';
4483 mb_c = c;
4484 mb_utf8 = FALSE;
4485 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004486 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 /* Put pointer back so that the character will be
4488 * displayed at the start of the next line. */
4489 --ptr;
4490 }
4491 else if (*ptr != NUL)
4492 ptr += mb_l - 1;
4493
4494 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004495 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004496 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004497 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004500 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 c = ' ';
4502 if (area_attr == 0 && search_attr == 0)
4503 {
4504 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004505 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 saved_attr2 = char_attr; /* save current attr */
4507 }
4508 mb_c = c;
4509 mb_utf8 = FALSE;
4510 mb_l = 1;
4511 }
4512
4513 }
4514#endif
4515 ++ptr;
4516
4517 if (extra_check)
4518 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004519#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004520 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004521#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004522
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004523#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 /* Get syntax attribute, unless still at the start of the line
4525 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004526 v = (long)(ptr - line);
4527 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
4529 /* Get the syntax attribute for the character. If there
4530 * is an error, disable syntax highlighting. */
4531 save_did_emsg = did_emsg;
4532 did_emsg = FALSE;
4533
Bram Moolenaar217ad922005-03-20 22:37:15 +00004534 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004535# ifdef FEAT_SPELL
4536 has_spell ? &can_spell :
4537# endif
4538 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
4540 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004541 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004542 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004543 has_syntax = FALSE;
4544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 else
4546 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004547#ifdef SYN_TIME_LIMIT
4548 if (wp->w_s->b_syn_slow)
4549 has_syntax = FALSE;
4550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552 /* Need to get the line again, a multi-line regexp may
4553 * have made it invalid. */
4554 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4555 ptr = line + v;
4556
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004557 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004559 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004560 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004561# ifdef FEAT_CONCEAL
4562 /* no concealing past the end of the line, it interferes
4563 * with line highlighting */
4564 if (c == NUL)
4565 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004566 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004567 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004568# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004570#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004571
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004572#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004573 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004574 * Only do this when there is no syntax highlighting, the
4575 * @Spell cluster is not used or the current syntax item
4576 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004577 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004578 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004579 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004580# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004581 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004582 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004583# endif
4584 if (c != 0 && (
4585# ifdef FEAT_SYN_HL
4586 !has_syntax ||
4587# endif
4588 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004589 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004590 char_u *prev_ptr, *p;
4591 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004592 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004593# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004594 if (has_mbyte)
4595 {
4596 prev_ptr = ptr - mb_l;
4597 v -= mb_l - 1;
4598 }
4599 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004600# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004601 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004602
4603 /* Use nextline[] if possible, it has the start of the
4604 * next line concatenated. */
4605 if ((prev_ptr - line) - nextlinecol >= 0)
4606 p = nextline + (prev_ptr - line) - nextlinecol;
4607 else
4608 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004609 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004610 len = spell_check(wp, p, &spell_hlf, &cap_col,
4611 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004612 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004613
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004614 /* In Insert mode only highlight a word that
4615 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004616 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004617 && (State & INSERT) != 0
4618 && wp->w_cursor.lnum == lnum
4619 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004620 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004621 && wp->w_cursor.col < (colnr_T)word_end)
4622 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004623 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004624 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004625 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004626
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004627 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004628 && (p - nextline) + len > nextline_idx)
4629 {
4630 /* Remember that the good word continues at the
4631 * start of the next line. */
4632 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004633 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004634 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004635
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004636 /* Turn index into actual attributes. */
4637 if (spell_hlf != HLF_COUNT)
4638 spell_attr = highlight_attr[spell_hlf];
4639
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004640 if (cap_col > 0)
4641 {
4642 if (p != prev_ptr
4643 && (p - nextline) + cap_col >= nextline_idx)
4644 {
4645 /* Remember that the word in the next line
4646 * must start with a capital. */
4647 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004648 cap_col = (int)((p - nextline) + cap_col
4649 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004650 }
4651 else
4652 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004653 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004654 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004655 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004656 }
4657 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004658 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004659 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004660 char_attr = hl_combine_attr(char_attr, spell_attr);
4661 else
4662 char_attr = hl_combine_attr(spell_attr, char_attr);
4663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664#endif
4665#ifdef FEAT_LINEBREAK
4666 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004667 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004669 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004670 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004672# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004673 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004674# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004675 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004677 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004679 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004680
Bram Moolenaar597a4222014-06-25 14:39:50 +02004681 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004682 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004683 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004684 if (c == TAB && n_extra + col > W_WIDTH(wp))
4685 n_extra = (int)wp->w_buffer->b_p_ts
4686 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4687
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004688# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004689 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004690# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004692# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004693 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004694 {
4695#ifdef FEAT_CONCEAL
4696 if (c == TAB)
4697 /* See "Tab alignment" below. */
4698 FIX_FOR_BOGUSCOLS;
4699#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004700 if (!wp->w_p_list)
4701 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704#endif
4705
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004706 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4707 */
4708 if (wp->w_p_list
4709 && (((c == 160
4710#ifdef FEAT_MBYTE
4711 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4712#endif
4713 ) && lcs_nbsp)
4714 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4715 {
4716 c = (c == ' ') ? lcs_space : lcs_nbsp;
4717 if (area_attr == 0 && search_attr == 0)
4718 {
4719 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004720 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004721 saved_attr2 = char_attr; /* save current attr */
4722 }
4723#ifdef FEAT_MBYTE
4724 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004725 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004726 {
4727 mb_utf8 = TRUE;
4728 u8cc[0] = 0;
4729 c = 0xc0;
4730 }
4731 else
4732 mb_utf8 = FALSE;
4733#endif
4734 }
4735
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4737 {
4738 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004739 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
4741 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004742 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 saved_attr2 = char_attr; /* save current attr */
4744 }
4745#ifdef FEAT_MBYTE
4746 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004747 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
4749 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004750 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004751 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
4753 else
4754 mb_utf8 = FALSE;
4755#endif
4756 }
4757 }
4758
4759 /*
4760 * Handling of non-printable characters.
4761 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004762 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
4764 /*
4765 * when getting a character from the file, we may have to
4766 * turn it into something else on the way to putting it
4767 * into "ScreenLines".
4768 */
4769 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4770 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004771 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004772 long vcol_adjusted = vcol; /* removed showbreak length */
4773#ifdef FEAT_LINEBREAK
4774 /* only adjust the tab_len, when at the first column
4775 * after the showbreak value was drawn */
4776 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4777 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004780 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004781 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4782
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004783#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004784 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004785#endif
4786 /* tab amount depends on current column */
4787 n_extra = tab_len;
4788#ifdef FEAT_LINEBREAK
4789 else
4790 {
4791 char_u *p;
4792 int len = n_extra;
4793 int i;
4794 int saved_nextra = n_extra;
4795
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004796#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004797 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004798 /* there are characters to conceal */
4799 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004800 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4801 */
4802 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4803 && n_extra > tab_len)
4804 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004805#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004806
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004807 /* if n_extra > 0, it gives the number of chars, to
4808 * use for a tab, else we need to calculate the width
4809 * for a tab */
4810#ifdef FEAT_MBYTE
4811 len = (tab_len * mb_char2len(lcs_tab2));
4812 if (n_extra > 0)
4813 len += n_extra - tab_len;
4814#endif
4815 c = lcs_tab1;
4816 p = alloc((unsigned)(len + 1));
4817 vim_memset(p, ' ', len);
4818 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004819 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004820 p_extra_free = p;
4821 for (i = 0; i < tab_len; i++)
4822 {
4823#ifdef FEAT_MBYTE
4824 mb_char2bytes(lcs_tab2, p);
4825 p += mb_char2len(lcs_tab2);
4826 n_extra += mb_char2len(lcs_tab2)
4827 - (saved_nextra > 0 ? 1 : 0);
4828#else
4829 p[i] = lcs_tab2;
4830#endif
4831 }
4832 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004833#ifdef FEAT_CONCEAL
4834 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4835 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004836 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004837 n_extra -= vcol_off;
4838#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004839 }
4840#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004841#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004842 {
4843 int vc_saved = vcol_off;
4844
4845 /* Tab alignment should be identical regardless of
4846 * 'conceallevel' value. So tab compensates of all
4847 * previous concealed characters, and thus resets
4848 * vcol_off and boguscols accumulated so far in the
4849 * line. Note that the tab can be longer than
4850 * 'tabstop' when there are concealed characters. */
4851 FIX_FOR_BOGUSCOLS;
4852
4853 /* Make sure, the highlighting for the tab char will be
4854 * correctly set further below (effectively reverts the
4855 * FIX_FOR_BOGSUCOLS macro */
4856 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004857 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004858 tab_len += vc_saved;
4859 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861#ifdef FEAT_MBYTE
4862 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4863#endif
4864 if (wp->w_p_list)
4865 {
4866 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004867#ifdef FEAT_LINEBREAK
4868 if (wp->w_p_lbr)
4869 c_extra = NUL; /* using p_extra from above */
4870 else
4871#endif
4872 c_extra = lcs_tab2;
4873 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004874 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 saved_attr2 = char_attr; /* save current attr */
4876#ifdef FEAT_MBYTE
4877 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004878 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
4880 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004881 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004882 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884#endif
4885 }
4886 else
4887 {
4888 c_extra = ' ';
4889 c = ' ';
4890 }
4891 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004892 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004893 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004894 || ((fromcol >= 0 || fromcol_prev >= 0)
4895 && tocol > vcol
4896 && VIsual_mode != Ctrl_V
4897 && (
4898# ifdef FEAT_RIGHTLEFT
4899 wp->w_p_rl ? (col >= 0) :
4900# endif
4901 (col < W_WIDTH(wp)))
4902 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004903 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004904 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004905 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004907 /* Display a '$' after the line or highlight an extra
4908 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4910 /* For a diff line the highlighting continues after the
4911 * "$". */
4912 if (
4913# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004914 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915# ifdef LINE_ATTR
4916 &&
4917# endif
4918# endif
4919# ifdef LINE_ATTR
4920 line_attr == 0
4921# endif
4922 )
4923#endif
4924 {
4925#ifdef FEAT_VIRTUALEDIT
4926 /* In virtualedit, visual selections may extend
4927 * beyond end of line. */
4928 if (area_highlighting && virtual_active()
4929 && tocol != MAXCOL && vcol < tocol)
4930 n_extra = 0;
4931 else
4932#endif
4933 {
4934 p_extra = at_end_str;
4935 n_extra = 1;
4936 c_extra = NUL;
4937 }
4938 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004939 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004940 c = lcs_eol;
4941 else
4942 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 lcs_eol_one = -1;
4944 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004945 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004947 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 n_attr = 1;
4949 }
4950#ifdef FEAT_MBYTE
4951 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004952 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
4954 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004955 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004956 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
4958 else
4959 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4960#endif
4961 }
4962 else if (c != NUL)
4963 {
4964 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004965 if (n_extra == 0)
4966 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967#ifdef FEAT_RIGHTLEFT
4968 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4969 rl_mirror(p_extra); /* reverse "<12>" */
4970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004972#ifdef FEAT_LINEBREAK
4973 if (wp->w_p_lbr)
4974 {
4975 char_u *p;
4976
4977 c = *p_extra;
4978 p = alloc((unsigned)n_extra + 1);
4979 vim_memset(p, ' ', n_extra);
4980 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4981 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004982 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004983 p_extra_free = p_extra = p;
4984 }
4985 else
4986#endif
4987 {
4988 n_extra = byte2cells(c) - 1;
4989 c = *p_extra++;
4990 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004991 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
4993 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004994 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 saved_attr2 = char_attr; /* save current attr */
4996 }
4997#ifdef FEAT_MBYTE
4998 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4999#endif
5000 }
5001#ifdef FEAT_VIRTUALEDIT
5002 else if (VIsual_active
5003 && (VIsual_mode == Ctrl_V
5004 || VIsual_mode == 'v')
5005 && virtual_active()
5006 && tocol != MAXCOL
5007 && vcol < tocol
5008 && (
5009# ifdef FEAT_RIGHTLEFT
5010 wp->w_p_rl ? (col >= 0) :
5011# endif
5012 (col < W_WIDTH(wp))))
5013 {
5014 c = ' ';
5015 --ptr; /* put it back at the NUL */
5016 }
5017#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005018#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 else if ((
5020# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005021 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 ) && (
5025# ifdef FEAT_RIGHTLEFT
5026 wp->w_p_rl ? (col >= 0) :
5027# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005028 (col
5029# ifdef FEAT_CONCEAL
5030 - boguscols
5031# endif
5032 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
5034 /* Highlight until the right side of the window */
5035 c = ' ';
5036 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005037
5038 /* Remember we do the char for line highlighting. */
5039 ++did_line_attr;
5040
5041 /* don't do search HL for the rest of the line */
5042 if (line_attr != 0 && char_attr == search_attr && col > 0)
5043 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044# ifdef FEAT_DIFF
5045 if (diff_hlf == HLF_TXD)
5046 {
5047 diff_hlf = HLF_CHD;
5048 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005049 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005050 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005051 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5052 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005053 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
5056# endif
5057 }
5058#endif
5059 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005060
5061#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005062 if ( wp->w_p_cole > 0
5063 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005064 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005065 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005066 && !(lnum_in_visual_area
5067 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005068 {
5069 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005070 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005071 && (syn_get_sub_char() != NUL || match_conc
5072 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005073 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005074 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005075 /* First time at this concealed item: display one
5076 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005077 if (match_conc)
5078 c = match_conc;
5079 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005080 c = syn_get_sub_char();
5081 else if (lcs_conceal != NUL)
5082 c = lcs_conceal;
5083 else
5084 c = ' ';
5085
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005086 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005087
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005088 if (n_extra > 0)
5089 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005090 vcol += n_extra;
5091 if (wp->w_p_wrap && n_extra > 0)
5092 {
5093# ifdef FEAT_RIGHTLEFT
5094 if (wp->w_p_rl)
5095 {
5096 col -= n_extra;
5097 boguscols -= n_extra;
5098 }
5099 else
5100# endif
5101 {
5102 boguscols += n_extra;
5103 col += n_extra;
5104 }
5105 }
5106 n_extra = 0;
5107 n_attr = 0;
5108 }
5109 else if (n_skip == 0)
5110 {
5111 is_concealing = TRUE;
5112 n_skip = 1;
5113 }
5114# ifdef FEAT_MBYTE
5115 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005116 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005117 {
5118 mb_utf8 = TRUE;
5119 u8cc[0] = 0;
5120 c = 0xc0;
5121 }
5122 else
5123 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5124# endif
5125 }
5126 else
5127 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005128 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005129 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005130 }
5131#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
5133
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005134#ifdef FEAT_CONCEAL
5135 /* In the cursor line and we may be concealing characters: correct
5136 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005137 if (!did_wcol && draw_state == WL_LINE
5138 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005139 && conceal_cursor_line(wp)
5140 && (int)wp->w_virtcol <= vcol + n_skip)
5141 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005142# ifdef FEAT_RIGHTLEFT
5143 if (wp->w_p_rl)
5144 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5145 else
5146# endif
5147 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005148 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005149 did_wcol = TRUE;
5150 }
5151#endif
5152
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 /* Don't override visual selection highlighting. */
5154 if (n_attr > 0
5155 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005156 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 char_attr = extra_attr;
5158
Bram Moolenaar81695252004-12-29 20:58:21 +00005159#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 /* XIM don't send preedit_start and preedit_end, but they send
5161 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5162 * im_is_preediting() here. */
5163 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005164 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 && (State & INSERT)
5166 && !p_imdisable
5167 && im_is_preediting()
5168 && draw_state == WL_LINE)
5169 {
5170 colnr_T tcol;
5171
5172 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005173 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 else
5175 tcol = preedit_end_col;
5176 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5177 {
5178 if (feedback_old_attr < 0)
5179 {
5180 feedback_col = 0;
5181 feedback_old_attr = char_attr;
5182 }
5183 char_attr = im_get_feedback_attr(feedback_col);
5184 if (char_attr < 0)
5185 char_attr = feedback_old_attr;
5186 feedback_col++;
5187 }
5188 else if (feedback_old_attr >= 0)
5189 {
5190 char_attr = feedback_old_attr;
5191 feedback_old_attr = -1;
5192 feedback_col = 0;
5193 }
5194 }
5195#endif
5196 /*
5197 * Handle the case where we are in column 0 but not on the first
5198 * character of the line and the user wants us to show us a
5199 * special character (via 'listchars' option "precedes:<char>".
5200 */
5201 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005202 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5204#ifdef FEAT_DIFF
5205 && filler_todo <= 0
5206#endif
5207 && draw_state > WL_NR
5208 && c != NUL)
5209 {
5210 c = lcs_prec;
5211 lcs_prec_todo = NUL;
5212#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005213 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5214 {
5215 /* Double-width character being overwritten by the "precedes"
5216 * character, need to fill up half the character. */
5217 c_extra = MB_FILLER_CHAR;
5218 n_extra = 1;
5219 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005220 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005223 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 {
5225 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005226 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005227 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229 else
5230 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5231#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005232 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 {
5234 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005235 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 n_attr3 = 1;
5237 }
5238 }
5239
5240 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005241 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005243 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005244#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005245 || did_line_attr == 1
5246#endif
5247 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005249#ifdef FEAT_SEARCH_EXTRA
5250 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005251
5252 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005253 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005254 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005255#endif
5256
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005257 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 * highlight match at end of line. If it's beyond the last
5259 * char on the screen, just overwrite that one (tricky!) Not
5260 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005261#ifdef FEAT_SEARCH_EXTRA
5262 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005263 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005264 prevcol_hl_flag = TRUE;
5265 else
5266 {
5267 cur = wp->w_match_head;
5268 while (cur != NULL)
5269 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005270 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005271 {
5272 prevcol_hl_flag = TRUE;
5273 break;
5274 }
5275 cur = cur->next;
5276 }
5277 }
5278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005280 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005281 && (VIsual_mode != Ctrl_V
5282 || lnum == VIsual.lnum
5283 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005284 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285#ifdef FEAT_SEARCH_EXTRA
5286 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005287 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005288# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005289 && did_line_attr <= 1
5290# endif
5291 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292#endif
5293 ))
5294 {
5295 int n = 0;
5296
5297#ifdef FEAT_RIGHTLEFT
5298 if (wp->w_p_rl)
5299 {
5300 if (col < 0)
5301 n = 1;
5302 }
5303 else
5304#endif
5305 {
5306 if (col >= W_WIDTH(wp))
5307 n = -1;
5308 }
5309 if (n != 0)
5310 {
5311 /* At the window boundary, highlight the last character
5312 * instead (better than nothing). */
5313 off += n;
5314 col += n;
5315 }
5316 else
5317 {
5318 /* Add a blank character to highlight. */
5319 ScreenLines[off] = ' ';
5320#ifdef FEAT_MBYTE
5321 if (enc_utf8)
5322 ScreenLinesUC[off] = 0;
5323#endif
5324 }
5325#ifdef FEAT_SEARCH_EXTRA
5326 if (area_attr == 0)
5327 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005328 /* Use attributes from match with highest priority among
5329 * 'search_hl' and the match list. */
5330 char_attr = search_hl.attr;
5331 cur = wp->w_match_head;
5332 shl_flag = FALSE;
5333 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005334 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005335 if (shl_flag == FALSE
5336 && ((cur != NULL
5337 && cur->priority > SEARCH_HL_PRIORITY)
5338 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005339 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005340 shl = &search_hl;
5341 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005342 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005343 else
5344 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005345 if ((ptr - line) - 1 == (long)shl->startcol
5346 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005347 char_attr = shl->attr;
5348 if (shl != &search_hl && cur != NULL)
5349 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
5352#endif
5353 ScreenAttrs[off] = char_attr;
5354#ifdef FEAT_RIGHTLEFT
5355 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005358 --off;
5359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 else
5361#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005364 ++off;
5365 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005366 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005367#ifdef FEAT_SYN_HL
5368 eol_hl_off = 1;
5369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
Bram Moolenaar91170f82006-05-05 21:15:17 +00005373 /*
5374 * At end of the text line.
5375 */
5376 if (c == NUL)
5377 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005378#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005379 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5380 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005381 {
5382 /* highlight last char after line */
5383 --col;
5384 --off;
5385 --vcol;
5386 }
5387
Bram Moolenaar1a384422010-07-14 19:53:30 +02005388 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005389 if (wp->w_p_wrap)
5390 v = wp->w_skipcol;
5391 else
5392 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005393
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005394 /* check if line ends before left margin */
5395 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005396 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005397#ifdef FEAT_CONCEAL
5398 /* Get rid of the boguscols now, we want to draw until the right
5399 * edge for 'cursorcolumn'. */
5400 col -= boguscols;
5401 boguscols = 0;
5402#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005403
5404 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005405 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005406
5407 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005408 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5409 && (int)wp->w_virtcol <
5410 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005411 && lnum != wp->w_cursor.lnum)
5412 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005413# ifdef FEAT_RIGHTLEFT
5414 && !wp->w_p_rl
5415# endif
5416 )
5417 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005418 int rightmost_vcol = 0;
5419 int i;
5420
5421 if (wp->w_p_cuc)
5422 rightmost_vcol = wp->w_virtcol;
5423 if (draw_color_col)
5424 /* determine rightmost colorcolumn to possibly draw */
5425 for (i = 0; color_cols[i] >= 0; ++i)
5426 if (rightmost_vcol < color_cols[i])
5427 rightmost_vcol = color_cols[i];
5428
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005429 while (col < W_WIDTH(wp))
5430 {
5431 ScreenLines[off] = ' ';
5432#ifdef FEAT_MBYTE
5433 if (enc_utf8)
5434 ScreenLinesUC[off] = 0;
5435#endif
5436 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005437 if (draw_color_col)
5438 draw_color_col = advance_color_col(VCOL_HLC,
5439 &color_cols);
5440
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005441 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005442 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005443 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005444 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005445 else
5446 ScreenAttrs[off++] = 0;
5447
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005448 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005449 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005450
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005451 ++vcol;
5452 }
5453 }
5454#endif
5455
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005456 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005457 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 row++;
5459
5460 /*
5461 * Update w_cline_height and w_cline_folded if the cursor line was
5462 * updated (saves a call to plines() later).
5463 */
5464 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5465 {
5466 curwin->w_cline_row = startrow;
5467 curwin->w_cline_height = row - startrow;
5468#ifdef FEAT_FOLDING
5469 curwin->w_cline_folded = FALSE;
5470#endif
5471 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5472 }
5473
5474 break;
5475 }
5476
5477 /* line continues beyond line end */
5478 if (lcs_ext
5479 && !wp->w_p_wrap
5480#ifdef FEAT_DIFF
5481 && filler_todo <= 0
5482#endif
5483 && (
5484#ifdef FEAT_RIGHTLEFT
5485 wp->w_p_rl ? col == 0 :
5486#endif
5487 col == W_WIDTH(wp) - 1)
5488 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005489 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5491 {
5492 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005493 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494#ifdef FEAT_MBYTE
5495 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005496 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 {
5498 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005499 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005500 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
5502 else
5503 mb_utf8 = FALSE;
5504#endif
5505 }
5506
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005507#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005508 /* advance to the next 'colorcolumn' */
5509 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005510 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005511
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005512 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005513 * highlight the cursor position itself.
5514 * Also highlight the 'colorcolumn' if it is different than
5515 * 'cursorcolumn' */
5516 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005517 if (draw_state == WL_LINE && !lnum_in_visual_area
5518 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005519 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005520 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005521 && lnum != wp->w_cursor.lnum)
5522 {
5523 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005524 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005525 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005526 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005527 {
5528 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005529 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005530 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005531 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005532#endif
5533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 /*
5535 * Store character to be displayed.
5536 * Skip characters that are left of the screen for 'nowrap'.
5537 */
5538 vcol_prev = vcol;
5539 if (draw_state < WL_LINE || n_skip <= 0)
5540 {
5541 /*
5542 * Store the character.
5543 */
5544#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5545 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5546 {
5547 /* A double-wide character is: put first halve in left cell. */
5548 --off;
5549 --col;
5550 }
5551#endif
5552 ScreenLines[off] = c;
5553#ifdef FEAT_MBYTE
5554 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005555 {
5556 if ((mb_c & 0xff00) == 0x8e00)
5557 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 else if (enc_utf8)
5561 {
5562 if (mb_utf8)
5563 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005564 int i;
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005567 if ((c & 0xff) == 0)
5568 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005569 for (i = 0; i < Screen_mco; ++i)
5570 {
5571 ScreenLinesC[i][off] = u8cc[i];
5572 if (u8cc[i] == 0)
5573 break;
5574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 }
5576 else
5577 ScreenLinesUC[off] = 0;
5578 }
5579 if (multi_attr)
5580 {
5581 ScreenAttrs[off] = multi_attr;
5582 multi_attr = 0;
5583 }
5584 else
5585#endif
5586 ScreenAttrs[off] = char_attr;
5587
5588#ifdef FEAT_MBYTE
5589 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5590 {
5591 /* Need to fill two screen columns. */
5592 ++off;
5593 ++col;
5594 if (enc_utf8)
5595 /* UTF-8: Put a 0 in the second screen char. */
5596 ScreenLines[off] = 0;
5597 else
5598 /* DBCS: Put second byte in the second screen char. */
5599 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005600 if (draw_state > WL_NR
5601#ifdef FEAT_DIFF
5602 && filler_todo <= 0
5603#endif
5604 )
5605 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 /* When "tocol" is halfway a character, set it to the end of
5607 * the character, otherwise highlighting won't stop. */
5608 if (tocol == vcol)
5609 ++tocol;
5610#ifdef FEAT_RIGHTLEFT
5611 if (wp->w_p_rl)
5612 {
5613 /* now it's time to backup one cell */
5614 --off;
5615 --col;
5616 }
5617#endif
5618 }
5619#endif
5620#ifdef FEAT_RIGHTLEFT
5621 if (wp->w_p_rl)
5622 {
5623 --off;
5624 --col;
5625 }
5626 else
5627#endif
5628 {
5629 ++off;
5630 ++col;
5631 }
5632 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005633#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005634 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005635 {
5636 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005637 ++vcol_off;
5638 if (n_extra > 0)
5639 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005640 if (wp->w_p_wrap)
5641 {
5642 /*
5643 * Special voodoo required if 'wrap' is on.
5644 *
5645 * Advance the column indicator to force the line
5646 * drawing to wrap early. This will make the line
5647 * take up the same screen space when parts are concealed,
5648 * so that cursor line computations aren't messed up.
5649 *
5650 * To avoid the fictitious advance of 'col' causing
5651 * trailing junk to be written out of the screen line
5652 * we are building, 'boguscols' keeps track of the number
5653 * of bad columns we have advanced.
5654 */
5655 if (n_extra > 0)
5656 {
5657 vcol += n_extra;
5658# ifdef FEAT_RIGHTLEFT
5659 if (wp->w_p_rl)
5660 {
5661 col -= n_extra;
5662 boguscols -= n_extra;
5663 }
5664 else
5665# endif
5666 {
5667 col += n_extra;
5668 boguscols += n_extra;
5669 }
5670 n_extra = 0;
5671 n_attr = 0;
5672 }
5673
5674
5675# ifdef FEAT_MBYTE
5676 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5677 {
5678 /* Need to fill two screen columns. */
5679# ifdef FEAT_RIGHTLEFT
5680 if (wp->w_p_rl)
5681 {
5682 --boguscols;
5683 --col;
5684 }
5685 else
5686# endif
5687 {
5688 ++boguscols;
5689 ++col;
5690 }
5691 }
5692# endif
5693
5694# ifdef FEAT_RIGHTLEFT
5695 if (wp->w_p_rl)
5696 {
5697 --boguscols;
5698 --col;
5699 }
5700 else
5701# endif
5702 {
5703 ++boguscols;
5704 ++col;
5705 }
5706 }
5707 else
5708 {
5709 if (n_extra > 0)
5710 {
5711 vcol += n_extra;
5712 n_extra = 0;
5713 n_attr = 0;
5714 }
5715 }
5716
5717 }
5718#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 else
5720 --n_skip;
5721
Bram Moolenaar64486672010-05-16 15:46:46 +02005722 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5723 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005724 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725#ifdef FEAT_DIFF
5726 && filler_todo <= 0
5727#endif
5728 )
5729 ++vcol;
5730
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005731#ifdef FEAT_SYN_HL
5732 if (vcol_save_attr >= 0)
5733 char_attr = vcol_save_attr;
5734#endif
5735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 /* restore attributes after "predeces" in 'listchars' */
5737 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5738 char_attr = saved_attr3;
5739
5740 /* restore attributes after last 'listchars' or 'number' char */
5741 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5742 char_attr = saved_attr2;
5743
5744 /*
5745 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005746 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 */
5748 if ((
5749#ifdef FEAT_RIGHTLEFT
5750 wp->w_p_rl ? (col < 0) :
5751#endif
5752 (col >= W_WIDTH(wp)))
5753 && (*ptr != NUL
5754#ifdef FEAT_DIFF
5755 || filler_todo > 0
5756#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005757 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5759 )
5760 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005761#ifdef FEAT_CONCEAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005762 screen_line(screen_row, W_WINCOL(wp), col - boguscols,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005763 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005764 boguscols = 0;
5765#else
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005766 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005767 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 ++row;
5770 ++screen_row;
5771
5772 /* When not wrapping and finished diff lines, or when displayed
5773 * '$' and highlighting until last column, break here. */
5774 if ((!wp->w_p_wrap
5775#ifdef FEAT_DIFF
5776 && filler_todo <= 0
5777#endif
5778 ) || lcs_eol_one == -1)
5779 break;
5780
5781 /* When the window is too narrow draw all "@" lines. */
5782 if (draw_state != WL_LINE
5783#ifdef FEAT_DIFF
5784 && filler_todo <= 0
5785#endif
5786 )
5787 {
5788 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005789#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 draw_vsep_win(wp, row);
5791#endif
5792 row = endrow;
5793 }
5794
5795 /* When line got too long for screen break here. */
5796 if (row == endrow)
5797 {
5798 ++row;
5799 break;
5800 }
5801
5802 if (screen_cur_row == screen_row - 1
5803#ifdef FEAT_DIFF
5804 && filler_todo <= 0
5805#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005806#ifdef FEAT_WINDOWS
5807 && W_WIDTH(wp) == Columns
5808#endif
5809 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
5811 /* Remember that the line wraps, used for modeless copy. */
5812 LineWraps[screen_row - 1] = TRUE;
5813
5814 /*
5815 * Special trick to make copy/paste of wrapped lines work with
5816 * xterm/screen: write an extra character beyond the end of
5817 * the line. This will work with all terminal types
5818 * (regardless of the xn,am settings).
5819 * Only do this on a fast tty.
5820 * Only do this if the cursor is on the current line
5821 * (something has been written in it).
5822 * Don't do this for the GUI.
5823 * Don't do this for double-width characters.
5824 * Don't do this for a window not at the right screen border.
5825 */
5826 if (p_tf
5827#ifdef FEAT_GUI
5828 && !gui.in_use
5829#endif
5830#ifdef FEAT_MBYTE
5831 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005832 && ((*mb_off2cells)(LineOffset[screen_row],
5833 LineOffset[screen_row] + screen_Columns)
5834 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005836 + (int)Columns - 2,
5837 LineOffset[screen_row] + screen_Columns)
5838 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839#endif
5840 )
5841 {
5842 /* First make sure we are at the end of the screen line,
5843 * then output the same character again to let the
5844 * terminal know about the wrap. If the terminal doesn't
5845 * auto-wrap, we overwrite the character. */
5846 if (screen_cur_col != W_WIDTH(wp))
5847 screen_char(LineOffset[screen_row - 1]
5848 + (unsigned)Columns - 1,
5849 screen_row - 1, (int)(Columns - 1));
5850
5851#ifdef FEAT_MBYTE
5852 /* When there is a multi-byte character, just output a
5853 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005854 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5855 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 out_char(' ');
5857 else
5858#endif
5859 out_char(ScreenLines[LineOffset[screen_row - 1]
5860 + (Columns - 1)]);
5861 /* force a redraw of the first char on the next line */
5862 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5863 screen_start(); /* don't know where cursor is now */
5864 }
5865 }
5866
5867 col = 0;
5868 off = (unsigned)(current_ScreenLine - ScreenLines);
5869#ifdef FEAT_RIGHTLEFT
5870 if (wp->w_p_rl)
5871 {
5872 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5873 off += col;
5874 }
5875#endif
5876
5877 /* reset the drawing state for the start of a wrapped line */
5878 draw_state = WL_START;
5879 saved_n_extra = n_extra;
5880 saved_p_extra = p_extra;
5881 saved_c_extra = c_extra;
5882 saved_char_attr = char_attr;
5883 n_extra = 0;
5884 lcs_prec_todo = lcs_prec;
5885#ifdef FEAT_LINEBREAK
5886# ifdef FEAT_DIFF
5887 if (filler_todo <= 0)
5888# endif
5889 need_showbreak = TRUE;
5890#endif
5891#ifdef FEAT_DIFF
5892 --filler_todo;
5893 /* When the filler lines are actually below the last line of the
5894 * file, don't draw the line itself, break here. */
5895 if (filler_todo == 0 && wp->w_botfill)
5896 break;
5897#endif
5898 }
5899
5900 } /* for every character in the line */
5901
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005902#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005903 /* After an empty line check first word for capital. */
5904 if (*skipwhite(line) == NUL)
5905 {
5906 capcol_lnum = lnum + 1;
5907 cap_col = 0;
5908 }
5909#endif
5910
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005911 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 return row;
5913}
5914
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005915#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005916static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005917
5918/*
5919 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005920 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005921 */
5922 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005923comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005924{
5925 int i;
5926
5927 for (i = 0; i < Screen_mco; ++i)
5928 {
5929 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5930 return TRUE;
5931 if (ScreenLinesC[i][off_from] == 0)
5932 break;
5933 }
5934 return FALSE;
5935}
5936#endif
5937
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938/*
5939 * Check whether the given character needs redrawing:
5940 * - the (first byte of the) character is different
5941 * - the attributes are different
5942 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005943 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 */
5945 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005946char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947{
5948 if (cols > 0
5949 && ((ScreenLines[off_from] != ScreenLines[off_to]
5950 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5951
5952#ifdef FEAT_MBYTE
5953 || (enc_dbcs != 0
5954 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5955 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5956 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5957 : (cols > 1 && ScreenLines[off_from + 1]
5958 != ScreenLines[off_to + 1])))
5959 || (enc_utf8
5960 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5961 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005962 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005963 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5964 && ScreenLines[off_from + 1]
5965 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966#endif
5967 ))
5968 return TRUE;
5969 return FALSE;
5970}
5971
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005972#if defined(FEAT_TERMINAL) || defined(PROTO)
5973/*
5974 * Return the index in ScreenLines[] for the current screen line.
5975 */
5976 int
5977screen_get_current_line_off()
5978{
5979 return (int)(current_ScreenLine - ScreenLines);
5980}
5981#endif
5982
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983/*
5984 * Move one "cooked" screen line to the screen, but only the characters that
5985 * have actually changed. Handle insert/delete character.
5986 * "coloff" gives the first column on the screen for this line.
5987 * "endcol" gives the columns where valid characters are.
5988 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5989 * needs to be cleared, negative otherwise.
5990 * "rlflag" is TRUE in a rightleft window:
5991 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5992 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5993 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005994 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005995screen_line(
5996 int row,
5997 int coloff,
5998 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005999 int clear_width,
6000 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001{
6002 unsigned off_from;
6003 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006004#ifdef FEAT_MBYTE
6005 unsigned max_off_from;
6006 unsigned max_off_to;
6007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006009#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 int hl;
6011#endif
6012 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 Moolenaar44a2f922016-03-19 22:11:51 +01006348#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 off_to += clear_width - col;
6350 col = clear_width;
6351#endif
6352 }
6353 }
6354
6355 if (clear_width > 0)
6356 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006357#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 /* For a window that's left of another, draw the separator char. */
6359 if (col + coloff < Columns)
6360 {
6361 int c;
6362
6363 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006364 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006366 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6367 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368# endif
6369 || ScreenAttrs[off_to] != hl)
6370 {
6371 ScreenLines[off_to] = c;
6372 ScreenAttrs[off_to] = hl;
6373# ifdef FEAT_MBYTE
6374 if (enc_utf8)
6375 {
6376 if (c >= 0x80)
6377 {
6378 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006379 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 }
6381 else
6382 ScreenLinesUC[off_to] = 0;
6383 }
6384# endif
6385 screen_char(off_to, row, col + coloff);
6386 }
6387 }
6388 else
6389#endif
6390 LineWraps[row] = FALSE;
6391 }
6392}
6393
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006394#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006396 * Mirror text "str" for right-left displaying.
6397 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006399 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006400rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401{
6402 char_u *p1, *p2;
6403 int t;
6404
6405 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6406 {
6407 t = *p1;
6408 *p1 = *p2;
6409 *p2 = t;
6410 }
6411}
6412#endif
6413
6414#if defined(FEAT_WINDOWS) || defined(PROTO)
6415/*
6416 * mark all status lines for redraw; used after first :cd
6417 */
6418 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006419status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420{
6421 win_T *wp;
6422
Bram Moolenaar29323592016-07-24 22:04:11 +02006423 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 if (wp->w_status_height)
6425 {
6426 wp->w_redr_status = TRUE;
6427 redraw_later(VALID);
6428 }
6429}
6430
6431/*
6432 * mark all status lines of the current buffer for redraw
6433 */
6434 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006435status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
6437 win_T *wp;
6438
Bram Moolenaar29323592016-07-24 22:04:11 +02006439 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6441 {
6442 wp->w_redr_status = TRUE;
6443 redraw_later(VALID);
6444 }
6445}
6446
6447/*
6448 * Redraw all status lines that need to be redrawn.
6449 */
6450 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006451redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
6453 win_T *wp;
6454
Bram Moolenaar29323592016-07-24 22:04:11 +02006455 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 if (wp->w_redr_status)
6457 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006458 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006459 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460}
6461#endif
6462
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006463#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464/*
6465 * Redraw all status lines at the bottom of frame "frp".
6466 */
6467 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006468win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470 if (frp->fr_layout == FR_LEAF)
6471 frp->fr_win->w_redr_status = TRUE;
6472 else if (frp->fr_layout == FR_ROW)
6473 {
6474 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6475 win_redraw_last_status(frp);
6476 }
6477 else /* frp->fr_layout == FR_COL */
6478 {
6479 frp = frp->fr_child;
6480 while (frp->fr_next != NULL)
6481 frp = frp->fr_next;
6482 win_redraw_last_status(frp);
6483 }
6484}
6485#endif
6486
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006487#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488/*
6489 * Draw the verticap separator right of window "wp" starting with line "row".
6490 */
6491 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006492draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
6494 int hl;
6495 int c;
6496
6497 if (wp->w_vsep_width)
6498 {
6499 /* draw the vertical separator right of this window */
6500 c = fillchar_vsep(&hl);
6501 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6502 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6503 c, ' ', hl);
6504 }
6505}
6506#endif
6507
6508#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006509static int status_match_len(expand_T *xp, char_u *s);
6510static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
6512/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006513 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 */
6515 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006516status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
6518 int len = 0;
6519
6520#ifdef FEAT_MENU
6521 int emenu = (xp->xp_context == EXPAND_MENUS
6522 || xp->xp_context == EXPAND_MENUNAMES);
6523
6524 /* Check for menu separators - replace with '|'. */
6525 if (emenu && menu_is_separator(s))
6526 return 1;
6527#endif
6528
6529 while (*s != NUL)
6530 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006531 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006532 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006533 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 }
6535
6536 return len;
6537}
6538
6539/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006540 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006541 * These are backslashes used for escaping. Do show backslashes in help tags.
6542 */
6543 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006544skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006545{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006546 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006547#ifdef FEAT_MENU
6548 || ((xp->xp_context == EXPAND_MENUS
6549 || xp->xp_context == EXPAND_MENUNAMES)
6550 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6551#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006552 )
6553 {
6554#ifndef BACKSLASH_IN_FILENAME
6555 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6556 return 2;
6557#endif
6558 return 1;
6559 }
6560 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006561}
6562
6563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 * Show wildchar matches in the status line.
6565 * Show at least the "match" item.
6566 * We start at item 'first_match' in the list and show all matches that fit.
6567 *
6568 * If inversion is possible we use it. Else '=' characters are used.
6569 */
6570 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006571win_redr_status_matches(
6572 expand_T *xp,
6573 int num_matches,
6574 char_u **matches, /* list of matches */
6575 int match,
6576 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
6578#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6579 int row;
6580 char_u *buf;
6581 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006582 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 int fillchar;
6584 int attr;
6585 int i;
6586 int highlight = TRUE;
6587 char_u *selstart = NULL;
6588 int selstart_col = 0;
6589 char_u *selend = NULL;
6590 static int first_match = 0;
6591 int add_left = FALSE;
6592 char_u *s;
6593#ifdef FEAT_MENU
6594 int emenu;
6595#endif
6596#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6597 int l;
6598#endif
6599
6600 if (matches == NULL) /* interrupted completion? */
6601 return;
6602
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006603#ifdef FEAT_MBYTE
6604 if (has_mbyte)
6605 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6606 else
6607#endif
6608 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 if (buf == NULL)
6610 return;
6611
6612 if (match == -1) /* don't show match but original text */
6613 {
6614 match = 0;
6615 highlight = FALSE;
6616 }
6617 /* count 1 for the ending ">" */
6618 clen = status_match_len(xp, L_MATCH(match)) + 3;
6619 if (match == 0)
6620 first_match = 0;
6621 else if (match < first_match)
6622 {
6623 /* jumping left, as far as we can go */
6624 first_match = match;
6625 add_left = TRUE;
6626 }
6627 else
6628 {
6629 /* check if match fits on the screen */
6630 for (i = first_match; i < match; ++i)
6631 clen += status_match_len(xp, L_MATCH(i)) + 2;
6632 if (first_match > 0)
6633 clen += 2;
6634 /* jumping right, put match at the left */
6635 if ((long)clen > Columns)
6636 {
6637 first_match = match;
6638 /* if showing the last match, we can add some on the left */
6639 clen = 2;
6640 for (i = match; i < num_matches; ++i)
6641 {
6642 clen += status_match_len(xp, L_MATCH(i)) + 2;
6643 if ((long)clen >= Columns)
6644 break;
6645 }
6646 if (i == num_matches)
6647 add_left = TRUE;
6648 }
6649 }
6650 if (add_left)
6651 while (first_match > 0)
6652 {
6653 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6654 if ((long)clen >= Columns)
6655 break;
6656 --first_match;
6657 }
6658
6659 fillchar = fillchar_status(&attr, TRUE);
6660
6661 if (first_match == 0)
6662 {
6663 *buf = NUL;
6664 len = 0;
6665 }
6666 else
6667 {
6668 STRCPY(buf, "< ");
6669 len = 2;
6670 }
6671 clen = len;
6672
6673 i = first_match;
6674 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6675 {
6676 if (i == match)
6677 {
6678 selstart = buf + len;
6679 selstart_col = clen;
6680 }
6681
6682 s = L_MATCH(i);
6683 /* Check for menu separators - replace with '|' */
6684#ifdef FEAT_MENU
6685 emenu = (xp->xp_context == EXPAND_MENUS
6686 || xp->xp_context == EXPAND_MENUNAMES);
6687 if (emenu && menu_is_separator(s))
6688 {
6689 STRCPY(buf + len, transchar('|'));
6690 l = (int)STRLEN(buf + len);
6691 len += l;
6692 clen += l;
6693 }
6694 else
6695#endif
6696 for ( ; *s != NUL; ++s)
6697 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006698 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 clen += ptr2cells(s);
6700#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006701 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 {
6703 STRNCPY(buf + len, s, l);
6704 s += l - 1;
6705 len += l;
6706 }
6707 else
6708#endif
6709 {
6710 STRCPY(buf + len, transchar_byte(*s));
6711 len += (int)STRLEN(buf + len);
6712 }
6713 }
6714 if (i == match)
6715 selend = buf + len;
6716
6717 *(buf + len++) = ' ';
6718 *(buf + len++) = ' ';
6719 clen += 2;
6720 if (++i == num_matches)
6721 break;
6722 }
6723
6724 if (i != num_matches)
6725 {
6726 *(buf + len++) = '>';
6727 ++clen;
6728 }
6729
6730 buf[len] = NUL;
6731
6732 row = cmdline_row - 1;
6733 if (row >= 0)
6734 {
6735 if (wild_menu_showing == 0)
6736 {
6737 if (msg_scrolled > 0)
6738 {
6739 /* Put the wildmenu just above the command line. If there is
6740 * no room, scroll the screen one line up. */
6741 if (cmdline_row == Rows - 1)
6742 {
6743 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6744 ++msg_scrolled;
6745 }
6746 else
6747 {
6748 ++cmdline_row;
6749 ++row;
6750 }
6751 wild_menu_showing = WM_SCROLLED;
6752 }
6753 else
6754 {
6755 /* Create status line if needed by setting 'laststatus' to 2.
6756 * Set 'winminheight' to zero to avoid that the window is
6757 * resized. */
6758 if (lastwin->w_status_height == 0)
6759 {
6760 save_p_ls = p_ls;
6761 save_p_wmh = p_wmh;
6762 p_ls = 2;
6763 p_wmh = 0;
6764 last_status(FALSE);
6765 }
6766 wild_menu_showing = WM_SHOWN;
6767 }
6768 }
6769
6770 screen_puts(buf, row, 0, attr);
6771 if (selstart != NULL && highlight)
6772 {
6773 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006774 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 }
6776
6777 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6778 }
6779
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006780#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 win_redraw_last_status(topframe);
6782#else
6783 lastwin->w_redr_status = TRUE;
6784#endif
6785 vim_free(buf);
6786}
6787#endif
6788
6789#if defined(FEAT_WINDOWS) || defined(PROTO)
6790/*
6791 * Redraw the status line of window wp.
6792 *
6793 * If inversion is possible we use it. Else '=' characters are used.
6794 */
6795 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006796win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797{
6798 int row;
6799 char_u *p;
6800 int len;
6801 int fillchar;
6802 int attr;
6803 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006804 static int busy = FALSE;
6805
6806 /* It's possible to get here recursively when 'statusline' (indirectly)
6807 * invokes ":redrawstatus". Simply ignore the call then. */
6808 if (busy)
6809 return;
6810 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811
6812 wp->w_redr_status = FALSE;
6813 if (wp->w_status_height == 0)
6814 {
6815 /* no status line, can only be last window */
6816 redraw_cmdline = TRUE;
6817 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006818 else if (!redrawing()
6819#ifdef FEAT_INS_EXPAND
6820 /* don't update status line when popup menu is visible and may be
6821 * drawn over it */
6822 || pum_visible()
6823#endif
6824 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 {
6826 /* Don't redraw right now, do it later. */
6827 wp->w_redr_status = TRUE;
6828 }
6829#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006830 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 {
6832 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006833 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 }
6835#endif
6836 else
6837 {
6838 fillchar = fillchar_status(&attr, wp == curwin);
6839
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006840 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 p = NameBuff;
6842 len = (int)STRLEN(p);
6843
6844 if (wp->w_buffer->b_help
6845#ifdef FEAT_QUICKFIX
6846 || wp->w_p_pvw
6847#endif
6848 || bufIsChanged(wp->w_buffer)
6849 || wp->w_buffer->b_p_ro)
6850 *(p + len++) = ' ';
6851 if (wp->w_buffer->b_help)
6852 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006853 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 len += (int)STRLEN(p + len);
6855 }
6856#ifdef FEAT_QUICKFIX
6857 if (wp->w_p_pvw)
6858 {
6859 STRCPY(p + len, _("[Preview]"));
6860 len += (int)STRLEN(p + len);
6861 }
6862#endif
6863 if (bufIsChanged(wp->w_buffer))
6864 {
6865 STRCPY(p + len, "[+]");
6866 len += 3;
6867 }
6868 if (wp->w_buffer->b_p_ro)
6869 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006870 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006871 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 }
6873
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6875 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6876 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6877 if (this_ru_col <= 1)
6878 {
6879 p = (char_u *)"<"; /* No room for file name! */
6880 len = 1;
6881 }
6882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883#ifdef FEAT_MBYTE
6884 if (has_mbyte)
6885 {
6886 int clen = 0, i;
6887
6888 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006889 clen = mb_string2cells(p, -1);
6890
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 /* Find first character that will fit.
6892 * Going from start to end is much faster for DBCS. */
6893 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006894 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 clen -= (*mb_ptr2cells)(p + i);
6896 len = clen;
6897 if (i > 0)
6898 {
6899 p = p + i - 1;
6900 *p = '<';
6901 ++len;
6902 }
6903
6904 }
6905 else
6906#endif
6907 if (len > this_ru_col - 1)
6908 {
6909 p += len - (this_ru_col - 1);
6910 *p = '<';
6911 len = this_ru_col - 1;
6912 }
6913
6914 row = W_WINROW(wp) + wp->w_height;
6915 screen_puts(p, row, W_WINCOL(wp), attr);
6916 screen_fill(row, row + 1, len + W_WINCOL(wp),
6917 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6918
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006919 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6921 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6922 - 1 + W_WINCOL(wp)), attr);
6923
6924#ifdef FEAT_CMDL_INFO
6925 win_redr_ruler(wp, TRUE);
6926#endif
6927 }
6928
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 /*
6930 * May need to draw the character below the vertical separator.
6931 */
6932 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6933 {
6934 if (stl_connected(wp))
6935 fillchar = fillchar_status(&attr, wp == curwin);
6936 else
6937 fillchar = fillchar_vsep(&attr);
6938 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6939 attr);
6940 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006941 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942}
6943
Bram Moolenaar238a5642006-02-21 22:12:05 +00006944#ifdef FEAT_STL_OPT
6945/*
6946 * Redraw the status line according to 'statusline' and take care of any
6947 * errors encountered.
6948 */
6949 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006950redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006951{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006952 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006953 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006954
6955 /* When called recursively return. This can happen when the statusline
6956 * contains an expression that triggers a redraw. */
6957 if (entered)
6958 return;
6959 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006960
Bram Moolenaara742e082016-04-05 21:10:38 +02006961 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006962 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006963 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006964 {
6965 /* When there is an error disable the statusline, otherwise the
6966 * display is messed up with errors and a redraw triggers the problem
6967 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006968 set_string_option_direct((char_u *)"statusline", -1,
6969 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006970 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006971 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006972 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006973 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006974}
6975#endif
6976
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977/*
6978 * Return TRUE if the status line of window "wp" is connected to the status
6979 * line of the window right of it. If not, then it's a vertical separator.
6980 * Only call if (wp->w_vsep_width != 0).
6981 */
6982 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006983stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984{
6985 frame_T *fr;
6986
6987 fr = wp->w_frame;
6988 while (fr->fr_parent != NULL)
6989 {
6990 if (fr->fr_parent->fr_layout == FR_COL)
6991 {
6992 if (fr->fr_next != NULL)
6993 break;
6994 }
6995 else
6996 {
6997 if (fr->fr_next != NULL)
6998 return TRUE;
6999 }
7000 fr = fr->fr_parent;
7001 }
7002 return FALSE;
7003}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
7005#endif /* FEAT_WINDOWS */
7006
7007#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
7008/*
7009 * Get the value to show for the language mappings, active 'keymap'.
7010 */
7011 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007012get_keymap_str(
7013 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007014 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007015 char_u *buf, /* buffer for the result */
7016 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017{
7018 char_u *p;
7019
7020 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7021 return FALSE;
7022
7023 {
7024#ifdef FEAT_EVAL
7025 buf_T *old_curbuf = curbuf;
7026 win_T *old_curwin = curwin;
7027 char_u *s;
7028
7029 curbuf = wp->w_buffer;
7030 curwin = wp;
7031 STRCPY(buf, "b:keymap_name"); /* must be writable */
7032 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007033 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 --emsg_skip;
7035 curbuf = old_curbuf;
7036 curwin = old_curwin;
7037 if (p == NULL || *p == NUL)
7038#endif
7039 {
7040#ifdef FEAT_KEYMAP
7041 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7042 p = wp->w_buffer->b_p_keymap;
7043 else
7044#endif
7045 p = (char_u *)"lang";
7046 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007047 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 buf[0] = NUL;
7049#ifdef FEAT_EVAL
7050 vim_free(s);
7051#endif
7052 }
7053 return buf[0] != NUL;
7054}
7055#endif
7056
7057#if defined(FEAT_STL_OPT) || defined(PROTO)
7058/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007059 * Redraw the status line or ruler of window "wp".
7060 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 */
7062 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007063win_redr_custom(
7064 win_T *wp,
7065 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007067 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 int attr;
7069 int curattr;
7070 int row;
7071 int col = 0;
7072 int maxwidth;
7073 int width;
7074 int n;
7075 int len;
7076 int fillchar;
7077 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007078 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007080 struct stl_hlrec hltab[STL_MAX_ITEM];
7081 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007082 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007083 win_T *ewp;
7084 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085
Bram Moolenaar1d633412013-12-11 15:52:01 +01007086 /* There is a tiny chance that this gets called recursively: When
7087 * redrawing a status line triggers redrawing the ruler or tabline.
7088 * Avoid trouble by not allowing recursion. */
7089 if (entered)
7090 return;
7091 entered = TRUE;
7092
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007094 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007096 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007097 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007098 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007099 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007100 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007101 maxwidth = Columns;
7102# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007103 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007104# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007106 else
7107 {
7108 row = W_WINROW(wp) + wp->w_height;
7109 fillchar = fillchar_status(&attr, wp == curwin);
7110 maxwidth = W_WIDTH(wp);
7111
7112 if (draw_ruler)
7113 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007114 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007115 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007116 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007117 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007118 if (*++stl == '-')
7119 stl++;
7120 if (atoi((char *)stl))
7121 while (VIM_ISDIGIT(*stl))
7122 stl++;
7123 if (*stl++ != '(')
7124 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007125 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007126#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007127 col = ru_col - (Columns - W_WIDTH(wp));
7128 if (col < (W_WIDTH(wp) + 1) / 2)
7129 col = (W_WIDTH(wp) + 1) / 2;
7130#else
7131 col = ru_col;
7132 if (col > (Columns + 1) / 2)
7133 col = (Columns + 1) / 2;
7134#endif
7135 maxwidth = W_WIDTH(wp) - col;
7136#ifdef FEAT_WINDOWS
7137 if (!wp->w_status_height)
7138#endif
7139 {
7140 row = Rows - 1;
7141 --maxwidth; /* writing in last column may cause scrolling */
7142 fillchar = ' ';
7143 attr = 0;
7144 }
7145
7146# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007147 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007148# endif
7149 }
7150 else
7151 {
7152 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007153 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007154 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007155 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007156# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007157 use_sandbox = was_set_insecurely((char_u *)"statusline",
7158 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007159# endif
7160 }
7161
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007162#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007163 col += W_WINCOL(wp);
7164#endif
7165 }
7166
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007168 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169
Bram Moolenaar61452852011-02-01 18:01:11 +01007170 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7171 * the cursor away and back. */
7172 ewp = wp == NULL ? curwin : wp;
7173 p_crb_save = ewp->w_p_crb;
7174 ewp->w_p_crb = FALSE;
7175
Bram Moolenaar362f3562009-11-03 16:20:34 +00007176 /* Make a copy, because the statusline may include a function call that
7177 * might change the option value and free the memory. */
7178 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007179 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007180 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007181 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007182 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007183 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007185 /* Make all characters printable. */
7186 p = transstr(buf);
7187 if (p != NULL)
7188 {
7189 vim_strncpy(buf, p, sizeof(buf) - 1);
7190 vim_free(p);
7191 }
7192
7193 /* fill up with "fillchar" */
7194 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007195 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 {
7197#ifdef FEAT_MBYTE
7198 len += (*mb_char2bytes)(fillchar, buf + len);
7199#else
7200 buf[len++] = fillchar;
7201#endif
7202 ++width;
7203 }
7204 buf[len] = NUL;
7205
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007206 /*
7207 * Draw each snippet with the specified highlighting.
7208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 curattr = attr;
7210 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007211 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007213 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 screen_puts_len(p, len, row, col, curattr);
7215 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007216 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007218 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007220 else if (hltab[n].userhl < 0)
7221 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007223 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007224 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225#endif
7226 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007227 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 }
7229 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007230
7231 if (wp == NULL)
7232 {
7233 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7234 col = 0;
7235 len = 0;
7236 p = buf;
7237 fillchar = 0;
7238 for (n = 0; tabtab[n].start != NULL; n++)
7239 {
7240 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7241 while (col < len)
7242 TabPageIdxs[col++] = fillchar;
7243 p = tabtab[n].start;
7244 fillchar = tabtab[n].userhl;
7245 }
7246 while (col < Columns)
7247 TabPageIdxs[col++] = fillchar;
7248 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007249
7250theend:
7251 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252}
7253
7254#endif /* FEAT_STL_OPT */
7255
7256/*
7257 * Output a single character directly to the screen and update ScreenLines.
7258 */
7259 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007260screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 char_u buf[MB_MAXBYTES + 1];
7263
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007264#ifdef FEAT_MBYTE
7265 if (has_mbyte)
7266 buf[(*mb_char2bytes)(c, buf)] = NUL;
7267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007269 {
7270 buf[0] = c;
7271 buf[1] = NUL;
7272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 screen_puts(buf, row, col, attr);
7274}
7275
7276/*
7277 * Get a single character directly from ScreenLines into "bytes[]".
7278 * Also return its attribute in *attrp;
7279 */
7280 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007281screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282{
7283 unsigned off;
7284
7285 /* safety check */
7286 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7287 {
7288 off = LineOffset[row] + col;
7289 *attrp = ScreenAttrs[off];
7290 bytes[0] = ScreenLines[off];
7291 bytes[1] = NUL;
7292
7293#ifdef FEAT_MBYTE
7294 if (enc_utf8 && ScreenLinesUC[off] != 0)
7295 bytes[utfc_char2bytes(off, bytes)] = NUL;
7296 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7297 {
7298 bytes[0] = ScreenLines[off];
7299 bytes[1] = ScreenLines2[off];
7300 bytes[2] = NUL;
7301 }
7302 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7303 {
7304 bytes[1] = ScreenLines[off + 1];
7305 bytes[2] = NUL;
7306 }
7307#endif
7308 }
7309}
7310
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007311#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007312static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007313
7314/*
7315 * Return TRUE if composing characters for screen posn "off" differs from
7316 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007317 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007318 */
7319 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007320screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007321{
7322 int i;
7323
7324 for (i = 0; i < Screen_mco; ++i)
7325 {
7326 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7327 return TRUE;
7328 if (u8cc[i] == 0)
7329 break;
7330 }
7331 return FALSE;
7332}
7333#endif
7334
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335/*
7336 * Put string '*text' on the screen at position 'row' and 'col', with
7337 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7338 * Note: only outputs within one row, message is truncated at screen boundary!
7339 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7340 */
7341 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007342screen_puts(
7343 char_u *text,
7344 int row,
7345 int col,
7346 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347{
7348 screen_puts_len(text, -1, row, col, attr);
7349}
7350
7351/*
7352 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7353 * a NUL.
7354 */
7355 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007356screen_puts_len(
7357 char_u *text,
7358 int textlen,
7359 int row,
7360 int col,
7361 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362{
7363 unsigned off;
7364 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007365 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 int c;
7367#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007368 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 int mbyte_blen = 1;
7370 int mbyte_cells = 1;
7371 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007372 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 int clear_next_cell = FALSE;
7374# ifdef FEAT_ARABIC
7375 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007376 int pc, nc, nc1;
7377 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378# endif
7379#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007380#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7381 int force_redraw_this;
7382 int force_redraw_next = FALSE;
7383#endif
7384 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
7386 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7387 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007388 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389
Bram Moolenaarc236c162008-07-13 17:41:49 +00007390#ifdef FEAT_MBYTE
7391 /* When drawing over the right halve of a double-wide char clear out the
7392 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007393 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007394# ifdef FEAT_GUI
7395 && !gui.in_use
7396# endif
7397 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007398 {
7399 ScreenLines[off - 1] = ' ';
7400 ScreenAttrs[off - 1] = 0;
7401 if (enc_utf8)
7402 {
7403 ScreenLinesUC[off - 1] = 0;
7404 ScreenLinesC[0][off - 1] = 0;
7405 }
7406 /* redraw the previous cell, make it empty */
7407 screen_char(off - 1, row, col - 1);
7408 /* force the cell at "col" to be redrawn */
7409 force_redraw_next = TRUE;
7410 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007411#endif
7412
Bram Moolenaar367329b2007-08-30 11:53:22 +00007413#ifdef FEAT_MBYTE
7414 max_off = LineOffset[row] + screen_Columns;
7415#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007416 while (col < screen_Columns
7417 && (len < 0 || (int)(ptr - text) < len)
7418 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 {
7420 c = *ptr;
7421#ifdef FEAT_MBYTE
7422 /* check if this is the first byte of a multibyte */
7423 if (has_mbyte)
7424 {
7425 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007426 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007428 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7430 mbyte_cells = 1;
7431 else if (enc_dbcs != 0)
7432 mbyte_cells = mbyte_blen;
7433 else /* enc_utf8 */
7434 {
7435 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007436 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 (int)((text + len) - ptr));
7438 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007439 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007441# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 /* Non-BMP character: display as ? or fullwidth ?. */
7443 if (u8c >= 0x10000)
7444 {
7445 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7446 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007447 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450# ifdef FEAT_ARABIC
7451 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7452 {
7453 /* Do Arabic shaping. */
7454 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7455 {
7456 /* Past end of string to be displayed. */
7457 nc = NUL;
7458 nc1 = NUL;
7459 }
7460 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007461 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007462 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7463 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007464 nc1 = pcc[0];
7465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 pc = prev_c;
7467 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007468 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 }
7470 else
7471 prev_c = u8c;
7472# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007473 if (col + mbyte_cells > screen_Columns)
7474 {
7475 /* Only 1 cell left, but character requires 2 cells:
7476 * display a '>' in the last column to avoid wrapping. */
7477 c = '>';
7478 mbyte_cells = 1;
7479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 }
7481 }
7482#endif
7483
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007484#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7485 force_redraw_this = force_redraw_next;
7486 force_redraw_next = FALSE;
7487#endif
7488
7489 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490#ifdef FEAT_MBYTE
7491 || (mbyte_cells == 2
7492 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7493 || (enc_dbcs == DBCS_JPNU
7494 && c == 0x8e
7495 && ScreenLines2[off] != ptr[1])
7496 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007497 && (ScreenLinesUC[off] !=
7498 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7499 || (ScreenLinesUC[off] != 0
7500 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501#endif
7502 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007503 || exmode_active;
7504
7505 if (need_redraw
7506#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7507 || force_redraw_this
7508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 )
7510 {
7511#if defined(FEAT_GUI) || defined(UNIX)
7512 /* The bold trick makes a single row of pixels appear in the next
7513 * character. When a bold character is removed, the next
7514 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007515 * and for some xterms. */
7516 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517# ifdef FEAT_GUI
7518 gui.in_use
7519# endif
7520# if defined(FEAT_GUI) && defined(UNIX)
7521 ||
7522# endif
7523# ifdef UNIX
7524 term_is_xterm
7525# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007526 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007528 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007530 if (n > HL_ALL)
7531 n = syn_attr2attr(n);
7532 if (n & HL_BOLD)
7533 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 }
7535#endif
7536#ifdef FEAT_MBYTE
7537 /* When at the end of the text and overwriting a two-cell
7538 * character with a one-cell character, need to clear the next
7539 * cell. Also when overwriting the left halve of a two-cell char
7540 * with the right halve of a two-cell char. Do this only once
7541 * (mb_off2cells() may return 2 on the right halve). */
7542 if (clear_next_cell)
7543 clear_next_cell = FALSE;
7544 else if (has_mbyte
7545 && (len < 0 ? ptr[mbyte_blen] == NUL
7546 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007547 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007549 && (*mb_off2cells)(off, max_off) == 1
7550 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 clear_next_cell = TRUE;
7552
7553 /* Make sure we never leave a second byte of a double-byte behind,
7554 * it confuses mb_off2cells(). */
7555 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007556 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007558 && (*mb_off2cells)(off, max_off) == 1
7559 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 ScreenLines[off + mbyte_blen] = 0;
7561#endif
7562 ScreenLines[off] = c;
7563 ScreenAttrs[off] = attr;
7564#ifdef FEAT_MBYTE
7565 if (enc_utf8)
7566 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007567 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 ScreenLinesUC[off] = 0;
7569 else
7570 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007571 int i;
7572
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007574 for (i = 0; i < Screen_mco; ++i)
7575 {
7576 ScreenLinesC[i][off] = u8cc[i];
7577 if (u8cc[i] == 0)
7578 break;
7579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 }
7581 if (mbyte_cells == 2)
7582 {
7583 ScreenLines[off + 1] = 0;
7584 ScreenAttrs[off + 1] = attr;
7585 }
7586 screen_char(off, row, col);
7587 }
7588 else if (mbyte_cells == 2)
7589 {
7590 ScreenLines[off + 1] = ptr[1];
7591 ScreenAttrs[off + 1] = attr;
7592 screen_char_2(off, row, col);
7593 }
7594 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7595 {
7596 ScreenLines2[off] = ptr[1];
7597 screen_char(off, row, col);
7598 }
7599 else
7600#endif
7601 screen_char(off, row, col);
7602 }
7603#ifdef FEAT_MBYTE
7604 if (has_mbyte)
7605 {
7606 off += mbyte_cells;
7607 col += mbyte_cells;
7608 ptr += mbyte_blen;
7609 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007610 {
7611 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007613 len = -1;
7614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 }
7616 else
7617#endif
7618 {
7619 ++off;
7620 ++col;
7621 ++ptr;
7622 }
7623 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007624
7625#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7626 /* If we detected the next character needs to be redrawn, but the text
7627 * doesn't extend up to there, update the character here. */
7628 if (force_redraw_next && col < screen_Columns)
7629 {
7630# ifdef FEAT_MBYTE
7631 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7632 screen_char_2(off, row, col);
7633 else
7634# endif
7635 screen_char(off, row, col);
7636 }
7637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638}
7639
7640#ifdef FEAT_SEARCH_EXTRA
7641/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007642 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 */
7644 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007645start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646{
7647 if (p_hls && !no_hlsearch)
7648 {
7649 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007650 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007651# ifdef FEAT_RELTIME
7652 /* Set the time limit to 'redrawtime'. */
7653 profile_setlimit(p_rdt, &search_hl.tm);
7654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 }
7656}
7657
7658/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007659 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 */
7661 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007662end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
7664 if (search_hl.rm.regprog != NULL)
7665 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007666 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 search_hl.rm.regprog = NULL;
7668 }
7669}
7670
7671/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007672 * Init for calling prepare_search_hl().
7673 */
7674 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007675init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007676{
7677 matchitem_T *cur;
7678
7679 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7680 * match */
7681 cur = wp->w_match_head;
7682 while (cur != NULL)
7683 {
7684 cur->hl.rm = cur->match;
7685 if (cur->hlg_id == 0)
7686 cur->hl.attr = 0;
7687 else
7688 cur->hl.attr = syn_id2attr(cur->hlg_id);
7689 cur->hl.buf = wp->w_buffer;
7690 cur->hl.lnum = 0;
7691 cur->hl.first_lnum = 0;
7692# ifdef FEAT_RELTIME
7693 /* Set the time limit to 'redrawtime'. */
7694 profile_setlimit(p_rdt, &(cur->hl.tm));
7695# endif
7696 cur = cur->next;
7697 }
7698 search_hl.buf = wp->w_buffer;
7699 search_hl.lnum = 0;
7700 search_hl.first_lnum = 0;
7701 /* time limit is set at the toplevel, for all windows */
7702}
7703
7704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 * Advance to the match in window "wp" line "lnum" or past it.
7706 */
7707 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007708prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007710 matchitem_T *cur; /* points to the match list */
7711 match_T *shl; /* points to search_hl or a match */
7712 int shl_flag; /* flag to indicate whether search_hl
7713 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007714 int pos_inprogress; /* marks that position match search is
7715 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 int n;
7717
7718 /*
7719 * When using a multi-line pattern, start searching at the top
7720 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007721 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007723 cur = wp->w_match_head;
7724 shl_flag = FALSE;
7725 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007727 if (shl_flag == FALSE)
7728 {
7729 shl = &search_hl;
7730 shl_flag = TRUE;
7731 }
7732 else
7733 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 if (shl->rm.regprog != NULL
7735 && shl->lnum == 0
7736 && re_multiline(shl->rm.regprog))
7737 {
7738 if (shl->first_lnum == 0)
7739 {
7740# ifdef FEAT_FOLDING
7741 for (shl->first_lnum = lnum;
7742 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7743 if (hasFoldingWin(wp, shl->first_lnum - 1,
7744 NULL, NULL, TRUE, NULL))
7745 break;
7746# else
7747 shl->first_lnum = wp->w_topline;
7748# endif
7749 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007750 if (cur != NULL)
7751 cur->pos.cur = 0;
7752 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007754 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7755 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007757 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7758 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007759 pos_inprogress = cur == NULL || cur->pos.cur == 0
7760 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 if (shl->lnum != 0)
7762 {
7763 shl->first_lnum = shl->lnum
7764 + shl->rm.endpos[0].lnum
7765 - shl->rm.startpos[0].lnum;
7766 n = shl->rm.endpos[0].col;
7767 }
7768 else
7769 {
7770 ++shl->first_lnum;
7771 n = 0;
7772 }
7773 }
7774 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007775 if (shl != &search_hl && cur != NULL)
7776 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 }
7778}
7779
7780/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007781 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 * Uses shl->buf.
7783 * Sets shl->lnum and shl->rm contents.
7784 * Note: Assumes a previous match is always before "lnum", unless
7785 * shl->lnum is zero.
7786 * Careful: Any pointers for buffer lines will become invalid.
7787 */
7788 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007789next_search_hl(
7790 win_T *win,
7791 match_T *shl, /* points to search_hl or a match */
7792 linenr_T lnum,
7793 colnr_T mincol, /* minimal column for a match */
7794 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 linenr_T l;
7797 colnr_T matchcol;
7798 long nmatched;
7799
7800 if (shl->lnum != 0)
7801 {
7802 /* Check for three situations:
7803 * 1. If the "lnum" is below a previous match, start a new search.
7804 * 2. If the previous match includes "mincol", use it.
7805 * 3. Continue after the previous match.
7806 */
7807 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7808 if (lnum > l)
7809 shl->lnum = 0;
7810 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7811 return;
7812 }
7813
7814 /*
7815 * Repeat searching for a match until one is found that includes "mincol"
7816 * or none is found in this line.
7817 */
7818 called_emsg = FALSE;
7819 for (;;)
7820 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007821#ifdef FEAT_RELTIME
7822 /* Stop searching after passing the time limit. */
7823 if (profile_passed_limit(&(shl->tm)))
7824 {
7825 shl->lnum = 0; /* no match found in time */
7826 break;
7827 }
7828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 /* Three situations:
7830 * 1. No useful previous match: search from start of line.
7831 * 2. Not Vi compatible or empty match: continue at next character.
7832 * Break the loop if this is beyond the end of the line.
7833 * 3. Vi compatible searching: continue at end of previous match.
7834 */
7835 if (shl->lnum == 0)
7836 matchcol = 0;
7837 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7838 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007839 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007841 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007842
7843 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007844 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007845 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007847 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 shl->lnum = 0;
7849 break;
7850 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007851#ifdef FEAT_MBYTE
7852 if (has_mbyte)
7853 matchcol += mb_ptr2len(ml);
7854 else
7855#endif
7856 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 }
7858 else
7859 matchcol = shl->rm.endpos[0].col;
7860
7861 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007862 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007864 /* Remember whether shl->rm is using a copy of the regprog in
7865 * cur->match. */
7866 int regprog_is_copy = (shl != &search_hl && cur != NULL
7867 && shl == &cur->hl
7868 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007869 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007870
Bram Moolenaarb3414592014-06-17 17:48:32 +02007871 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7872 matchcol,
7873#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007874 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007875#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007876 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007877#endif
7878 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007879 /* Copy the regprog, in case it got freed and recompiled. */
7880 if (regprog_is_copy)
7881 cur->match.regprog = cur->hl.rm.regprog;
7882
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007883 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007884 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007885 /* Error while handling regexp: stop using this regexp. */
7886 if (shl == &search_hl)
7887 {
7888 /* don't free regprog in the match list, it's a copy */
7889 vim_regfree(shl->rm.regprog);
7890 SET_NO_HLSEARCH(TRUE);
7891 }
7892 shl->rm.regprog = NULL;
7893 shl->lnum = 0;
7894 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7895 message */
7896 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007897 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007898 }
7899 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007900 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007901 else
7902 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 if (nmatched == 0)
7904 {
7905 shl->lnum = 0; /* no match found */
7906 break;
7907 }
7908 if (shl->rm.startpos[0].lnum > 0
7909 || shl->rm.startpos[0].col >= mincol
7910 || nmatched > 1
7911 || shl->rm.endpos[0].col > mincol)
7912 {
7913 shl->lnum += shl->rm.startpos[0].lnum;
7914 break; /* useful match found */
7915 }
7916 }
7917}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918
Bram Moolenaar85077472016-10-16 14:35:48 +02007919/*
7920 * If there is a match fill "shl" and return one.
7921 * Return zero otherwise.
7922 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007923 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007924next_search_hl_pos(
7925 match_T *shl, /* points to a match */
7926 linenr_T lnum,
7927 posmatch_T *posmatch, /* match positions */
7928 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007929{
7930 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007931 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007932
Bram Moolenaarb3414592014-06-17 17:48:32 +02007933 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7934 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007935 llpos_T *pos = &posmatch->pos[i];
7936
7937 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007938 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007939 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007940 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007941 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007942 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007943 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007944 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007945 /* if this match comes before the one at "found" then swap
7946 * them */
7947 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007948 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007949 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007950
Bram Moolenaar85077472016-10-16 14:35:48 +02007951 *pos = posmatch->pos[found];
7952 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007953 }
7954 }
7955 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007956 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007957 }
7958 }
7959 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007960 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007962 colnr_T start = posmatch->pos[found].col == 0
7963 ? 0 : posmatch->pos[found].col - 1;
7964 colnr_T end = posmatch->pos[found].col == 0
7965 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007966
Bram Moolenaar85077472016-10-16 14:35:48 +02007967 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007968 shl->rm.startpos[0].lnum = 0;
7969 shl->rm.startpos[0].col = start;
7970 shl->rm.endpos[0].lnum = 0;
7971 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007972 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007973 posmatch->cur = found + 1;
7974 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007975 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007976 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007977}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007978#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007979
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007981screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982{
7983 attrentry_T *aep = NULL;
7984
7985 screen_attr = attr;
7986 if (full_screen
7987#ifdef WIN3264
7988 && termcap_active
7989#endif
7990 )
7991 {
7992#ifdef FEAT_GUI
7993 if (gui.in_use)
7994 {
7995 char buf[20];
7996
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007997 /* The GUI handles this internally. */
7998 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 OUT_STR(buf);
8000 }
8001 else
8002#endif
8003 {
8004 if (attr > HL_ALL) /* special HL attr. */
8005 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008006 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 aep = syn_cterm_attr2entry(attr);
8008 else
8009 aep = syn_term_attr2entry(attr);
8010 if (aep == NULL) /* did ":syntax clear" */
8011 attr = 0;
8012 else
8013 attr = aep->ae_attr;
8014 }
8015 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8016 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008017 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008018#ifdef FEAT_TERMGUICOLORS
8019 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008020 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008021#endif
8022 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008023#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008024 )
8025#endif
8026 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008027 /* If the Normal FG color has BOLD attribute and the new HL
8028 * has a FG color defined, clear BOLD. */
8029 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8031 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008032 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8033 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 out_str(T_US);
8035 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8036 out_str(T_CZH);
8037 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8038 out_str(T_MR);
8039
8040 /*
8041 * Output the color or start string after bold etc., in case the
8042 * bold etc. override the color setting.
8043 */
8044 if (aep != NULL)
8045 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008046#ifdef FEAT_TERMGUICOLORS
8047 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008049 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008050 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008051 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008052 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 }
8054 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008057 if (t_colors > 1)
8058 {
8059 if (aep->ae_u.cterm.fg_color)
8060 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8061 if (aep->ae_u.cterm.bg_color)
8062 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8063 }
8064 else
8065 {
8066 if (aep->ae_u.term.start != NULL)
8067 out_str(aep->ae_u.term.start);
8068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 }
8070 }
8071 }
8072 }
8073}
8074
8075 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008076screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077{
8078 int do_ME = FALSE; /* output T_ME code */
8079
8080 if (screen_attr != 0
8081#ifdef WIN3264
8082 && termcap_active
8083#endif
8084 )
8085 {
8086#ifdef FEAT_GUI
8087 if (gui.in_use)
8088 {
8089 char buf[20];
8090
8091 /* use internal GUI code */
8092 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8093 OUT_STR(buf);
8094 }
8095 else
8096#endif
8097 {
8098 if (screen_attr > HL_ALL) /* special HL attr. */
8099 {
8100 attrentry_T *aep;
8101
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008102 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {
8104 /*
8105 * Assume that t_me restores the original colors!
8106 */
8107 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008108 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008109#ifdef FEAT_TERMGUICOLORS
8110 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008111 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8112 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008113#endif
8114 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008115#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008116 )
8117#endif
8118 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 do_ME = TRUE;
8120 }
8121 else
8122 {
8123 aep = syn_term_attr2entry(screen_attr);
8124 if (aep != NULL && aep->ae_u.term.stop != NULL)
8125 {
8126 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8127 do_ME = TRUE;
8128 else
8129 out_str(aep->ae_u.term.stop);
8130 }
8131 }
8132 if (aep == NULL) /* did ":syntax clear" */
8133 screen_attr = 0;
8134 else
8135 screen_attr = aep->ae_attr;
8136 }
8137
8138 /*
8139 * Often all ending-codes are equal to T_ME. Avoid outputting the
8140 * same sequence several times.
8141 */
8142 if (screen_attr & HL_STANDOUT)
8143 {
8144 if (STRCMP(T_SE, T_ME) == 0)
8145 do_ME = TRUE;
8146 else
8147 out_str(T_SE);
8148 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008149 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {
8151 if (STRCMP(T_UE, T_ME) == 0)
8152 do_ME = TRUE;
8153 else
8154 out_str(T_UE);
8155 }
8156 if (screen_attr & HL_ITALIC)
8157 {
8158 if (STRCMP(T_CZR, T_ME) == 0)
8159 do_ME = TRUE;
8160 else
8161 out_str(T_CZR);
8162 }
8163 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8164 out_str(T_ME);
8165
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008166#ifdef FEAT_TERMGUICOLORS
8167 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008169 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008170 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008171 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008172 term_bg_rgb_color(cterm_normal_bg_gui_color);
8173 }
8174 else
8175#endif
8176 {
8177 if (t_colors > 1)
8178 {
8179 /* set Normal cterm colors */
8180 if (cterm_normal_fg_color != 0)
8181 term_fg_color(cterm_normal_fg_color - 1);
8182 if (cterm_normal_bg_color != 0)
8183 term_bg_color(cterm_normal_bg_color - 1);
8184 if (cterm_normal_fg_bold)
8185 out_str(T_MD);
8186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 }
8188 }
8189 }
8190 screen_attr = 0;
8191}
8192
8193/*
8194 * Reset the colors for a cterm. Used when leaving Vim.
8195 * The machine specific code may override this again.
8196 */
8197 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008198reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008200 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 {
8202 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008203#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008204 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8205 || cterm_normal_bg_gui_color != INVALCOLOR)
8206 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008207#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {
8211 out_str(T_OP);
8212 screen_attr = -1;
8213 }
8214 if (cterm_normal_fg_bold)
8215 {
8216 out_str(T_ME);
8217 screen_attr = -1;
8218 }
8219 }
8220}
8221
8222/*
8223 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8224 * using the attributes from ScreenAttrs["off"].
8225 */
8226 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008227screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
8229 int attr;
8230
8231 /* Check for illegal values, just in case (could happen just after
8232 * resizing). */
8233 if (row >= screen_Rows || col >= screen_Columns)
8234 return;
8235
Bram Moolenaar494838a2015-02-10 19:20:37 +01008236 /* Outputting a character in the last cell on the screen may scroll the
8237 * screen up. Only do it when the "xn" termcap property is set, otherwise
8238 * mark the character invalid (update it when scrolled up). */
8239 if (*T_XN == NUL
8240 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241#ifdef FEAT_RIGHTLEFT
8242 /* account for first command-line character in rightleft mode */
8243 && !cmdmsg_rl
8244#endif
8245 )
8246 {
8247 ScreenAttrs[off] = (sattr_T)-1;
8248 return;
8249 }
8250
8251 /*
8252 * Stop highlighting first, so it's easier to move the cursor.
8253 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008254#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 if (screen_char_attr != 0)
8256 attr = screen_char_attr;
8257 else
8258#endif
8259 attr = ScreenAttrs[off];
8260 if (screen_attr != attr)
8261 screen_stop_highlight();
8262
8263 windgoto(row, col);
8264
8265 if (screen_attr != attr)
8266 screen_start_highlight(attr);
8267
8268#ifdef FEAT_MBYTE
8269 if (enc_utf8 && ScreenLinesUC[off] != 0)
8270 {
8271 char_u buf[MB_MAXBYTES + 1];
8272
8273 /* Convert UTF-8 character to bytes and write it. */
8274
8275 buf[utfc_char2bytes(off, buf)] = NUL;
8276
8277 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008278 if (utf_ambiguous_width(ScreenLinesUC[off]))
8279 screen_cur_col = 9999;
8280 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 ++screen_cur_col;
8282 }
8283 else
8284#endif
8285 {
8286#ifdef FEAT_MBYTE
8287 out_flush_check();
8288#endif
8289 out_char(ScreenLines[off]);
8290#ifdef FEAT_MBYTE
8291 /* double-byte character in single-width cell */
8292 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8293 out_char(ScreenLines2[off]);
8294#endif
8295 }
8296
8297 screen_cur_col++;
8298}
8299
8300#ifdef FEAT_MBYTE
8301
8302/*
8303 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8304 * on the screen at position 'row' and 'col'.
8305 * The attributes of the first byte is used for all. This is required to
8306 * output the two bytes of a double-byte character with nothing in between.
8307 */
8308 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008309screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310{
8311 /* Check for illegal values (could be wrong when screen was resized). */
8312 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8313 return;
8314
8315 /* Outputting the last character on the screen may scrollup the screen.
8316 * Don't to it! Mark the character invalid (update it when scrolled up) */
8317 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8318 {
8319 ScreenAttrs[off] = (sattr_T)-1;
8320 return;
8321 }
8322
8323 /* Output the first byte normally (positions the cursor), then write the
8324 * second byte directly. */
8325 screen_char(off, row, col);
8326 out_char(ScreenLines[off + 1]);
8327 ++screen_cur_col;
8328}
8329#endif
8330
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008331#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332/*
8333 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8334 * This uses the contents of ScreenLines[] and doesn't change it.
8335 */
8336 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008337screen_draw_rectangle(
8338 int row,
8339 int col,
8340 int height,
8341 int width,
8342 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343{
8344 int r, c;
8345 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008346#ifdef FEAT_MBYTE
8347 int max_off;
8348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008350 /* Can't use ScreenLines unless initialized */
8351 if (ScreenLines == NULL)
8352 return;
8353
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 if (invert)
8355 screen_char_attr = HL_INVERSE;
8356 for (r = row; r < row + height; ++r)
8357 {
8358 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008359#ifdef FEAT_MBYTE
8360 max_off = off + screen_Columns;
8361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 for (c = col; c < col + width; ++c)
8363 {
8364#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008365 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {
8367 screen_char_2(off + c, r, c);
8368 ++c;
8369 }
8370 else
8371#endif
8372 {
8373 screen_char(off + c, r, c);
8374#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008375 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 ++c;
8377#endif
8378 }
8379 }
8380 }
8381 screen_char_attr = 0;
8382}
8383#endif
8384
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008385#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386/*
8387 * Redraw the characters for a vertically split window.
8388 */
8389 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008390redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391{
8392 int col;
8393 int width;
8394
8395# ifdef FEAT_CLIPBOARD
8396 clip_may_clear_selection(row, end - 1);
8397# endif
8398
8399 if (wp == NULL)
8400 {
8401 col = 0;
8402 width = Columns;
8403 }
8404 else
8405 {
8406 col = wp->w_wincol;
8407 width = wp->w_width;
8408 }
8409 screen_draw_rectangle(row, col, end - row, width, FALSE);
8410}
8411#endif
8412
8413/*
8414 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8415 * with character 'c1' in first column followed by 'c2' in the other columns.
8416 * Use attributes 'attr'.
8417 */
8418 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008419screen_fill(
8420 int start_row,
8421 int end_row,
8422 int start_col,
8423 int end_col,
8424 int c1,
8425 int c2,
8426 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427{
8428 int row;
8429 int col;
8430 int off;
8431 int end_off;
8432 int did_delete;
8433 int c;
8434 int norm_term;
8435#if defined(FEAT_GUI) || defined(UNIX)
8436 int force_next = FALSE;
8437#endif
8438
8439 if (end_row > screen_Rows) /* safety check */
8440 end_row = screen_Rows;
8441 if (end_col > screen_Columns) /* safety check */
8442 end_col = screen_Columns;
8443 if (ScreenLines == NULL
8444 || start_row >= end_row
8445 || start_col >= end_col) /* nothing to do */
8446 return;
8447
8448 /* it's a "normal" terminal when not in a GUI or cterm */
8449 norm_term = (
8450#ifdef FEAT_GUI
8451 !gui.in_use &&
8452#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008453 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 for (row = start_row; row < end_row; ++row)
8455 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008456#ifdef FEAT_MBYTE
8457 if (has_mbyte
8458# ifdef FEAT_GUI
8459 && !gui.in_use
8460# endif
8461 )
8462 {
8463 /* When drawing over the right halve of a double-wide char clear
8464 * out the left halve. When drawing over the left halve of a
8465 * double wide-char clear out the right halve. Only needed in a
8466 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008467 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008468 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008469 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008470 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008471 }
8472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 /*
8474 * Try to use delete-line termcap code, when no attributes or in a
8475 * "normal" terminal, where a bold/italic space is just a
8476 * space.
8477 */
8478 did_delete = FALSE;
8479 if (c2 == ' '
8480 && end_col == Columns
8481 && can_clear(T_CE)
8482 && (attr == 0
8483 || (norm_term
8484 && attr <= HL_ALL
8485 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8486 {
8487 /*
8488 * check if we really need to clear something
8489 */
8490 col = start_col;
8491 if (c1 != ' ') /* don't clear first char */
8492 ++col;
8493
8494 off = LineOffset[row] + col;
8495 end_off = LineOffset[row] + end_col;
8496
8497 /* skip blanks (used often, keep it fast!) */
8498#ifdef FEAT_MBYTE
8499 if (enc_utf8)
8500 while (off < end_off && ScreenLines[off] == ' '
8501 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8502 ++off;
8503 else
8504#endif
8505 while (off < end_off && ScreenLines[off] == ' '
8506 && ScreenAttrs[off] == 0)
8507 ++off;
8508 if (off < end_off) /* something to be cleared */
8509 {
8510 col = off - LineOffset[row];
8511 screen_stop_highlight();
8512 term_windgoto(row, col);/* clear rest of this screen line */
8513 out_str(T_CE);
8514 screen_start(); /* don't know where cursor is now */
8515 col = end_col - col;
8516 while (col--) /* clear chars in ScreenLines */
8517 {
8518 ScreenLines[off] = ' ';
8519#ifdef FEAT_MBYTE
8520 if (enc_utf8)
8521 ScreenLinesUC[off] = 0;
8522#endif
8523 ScreenAttrs[off] = 0;
8524 ++off;
8525 }
8526 }
8527 did_delete = TRUE; /* the chars are cleared now */
8528 }
8529
8530 off = LineOffset[row] + start_col;
8531 c = c1;
8532 for (col = start_col; col < end_col; ++col)
8533 {
8534 if (ScreenLines[off] != c
8535#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008536 || (enc_utf8 && (int)ScreenLinesUC[off]
8537 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538#endif
8539 || ScreenAttrs[off] != attr
8540#if defined(FEAT_GUI) || defined(UNIX)
8541 || force_next
8542#endif
8543 )
8544 {
8545#if defined(FEAT_GUI) || defined(UNIX)
8546 /* The bold trick may make a single row of pixels appear in
8547 * the next character. When a bold character is removed, the
8548 * next character should be redrawn too. This happens for our
8549 * own GUI and for some xterms. */
8550 if (
8551# ifdef FEAT_GUI
8552 gui.in_use
8553# endif
8554# if defined(FEAT_GUI) && defined(UNIX)
8555 ||
8556# endif
8557# ifdef UNIX
8558 term_is_xterm
8559# endif
8560 )
8561 {
8562 if (ScreenLines[off] != ' '
8563 && (ScreenAttrs[off] > HL_ALL
8564 || ScreenAttrs[off] & HL_BOLD))
8565 force_next = TRUE;
8566 else
8567 force_next = FALSE;
8568 }
8569#endif
8570 ScreenLines[off] = c;
8571#ifdef FEAT_MBYTE
8572 if (enc_utf8)
8573 {
8574 if (c >= 0x80)
8575 {
8576 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008577 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 }
8579 else
8580 ScreenLinesUC[off] = 0;
8581 }
8582#endif
8583 ScreenAttrs[off] = attr;
8584 if (!did_delete || c != ' ')
8585 screen_char(off, row, col);
8586 }
8587 ++off;
8588 if (col == start_col)
8589 {
8590 if (did_delete)
8591 break;
8592 c = c2;
8593 }
8594 }
8595 if (end_col == Columns)
8596 LineWraps[row] = FALSE;
8597 if (row == Rows - 1) /* overwritten the command line */
8598 {
8599 redraw_cmdline = TRUE;
8600 if (c1 == ' ' && c2 == ' ')
8601 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008602 if (start_col == 0)
8603 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 }
8605 }
8606}
8607
8608/*
8609 * Check if there should be a delay. Used before clearing or redrawing the
8610 * screen or the command line.
8611 */
8612 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008613check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
8615 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8616 && !did_wait_return
8617 && emsg_silent == 0)
8618 {
8619 out_flush();
8620 ui_delay(1000L, TRUE);
8621 emsg_on_display = FALSE;
8622 if (check_msg_scroll)
8623 msg_scroll = FALSE;
8624 }
8625}
8626
8627/*
8628 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008629 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 * Returns TRUE if there is a valid screen to write to.
8631 * Returns FALSE when starting up and screen not initialized yet.
8632 */
8633 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008634screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008636 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 return (ScreenLines != NULL);
8638}
8639
8640/*
8641 * Resize the shell to Rows and Columns.
8642 * Allocate ScreenLines[] and associated items.
8643 *
8644 * There may be some time between setting Rows and Columns and (re)allocating
8645 * ScreenLines[]. This happens when starting up and when (manually) changing
8646 * the shell size. Always use screen_Rows and screen_Columns to access items
8647 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8648 * final size of the shell is needed.
8649 */
8650 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008651screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652{
8653 int new_row, old_row;
8654#ifdef FEAT_GUI
8655 int old_Rows;
8656#endif
8657 win_T *wp;
8658 int outofmem = FALSE;
8659 int len;
8660 schar_T *new_ScreenLines;
8661#ifdef FEAT_MBYTE
8662 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008663 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008665 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666#endif
8667 sattr_T *new_ScreenAttrs;
8668 unsigned *new_LineOffset;
8669 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008670#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008671 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008672 tabpage_T *tp;
8673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008675 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008676#ifdef FEAT_AUTOCMD
8677 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008679retry:
8680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 /*
8682 * Allocation of the screen buffers is done only when the size changes and
8683 * when Rows and Columns have been set and we have started doing full
8684 * screen stuff.
8685 */
8686 if ((ScreenLines != NULL
8687 && Rows == screen_Rows
8688 && Columns == screen_Columns
8689#ifdef FEAT_MBYTE
8690 && enc_utf8 == (ScreenLinesUC != NULL)
8691 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008692 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693#endif
8694 )
8695 || Rows == 0
8696 || Columns == 0
8697 || (!full_screen && ScreenLines == NULL))
8698 return;
8699
8700 /*
8701 * It's possible that we produce an out-of-memory message below, which
8702 * will cause this function to be called again. To break the loop, just
8703 * return here.
8704 */
8705 if (entered)
8706 return;
8707 entered = TRUE;
8708
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008709 /*
8710 * Note that the window sizes are updated before reallocating the arrays,
8711 * thus we must not redraw here!
8712 */
8713 ++RedrawingDisabled;
8714
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 win_new_shellsize(); /* fit the windows in the new sized shell */
8716
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 comp_col(); /* recompute columns for shown command and ruler */
8718
8719 /*
8720 * We're changing the size of the screen.
8721 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8722 * - Move lines from the old arrays into the new arrays, clear extra
8723 * lines (unless the screen is going to be cleared).
8724 * - Free the old arrays.
8725 *
8726 * If anything fails, make ScreenLines NULL, so we don't do anything!
8727 * Continuing with the old ScreenLines may result in a crash, because the
8728 * size is wrong.
8729 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008730 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008732#ifdef FEAT_AUTOCMD
8733 if (aucmd_win != NULL)
8734 win_free_lsize(aucmd_win);
8735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736
8737 new_ScreenLines = (schar_T *)lalloc((long_u)(
8738 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8739#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008740 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 if (enc_utf8)
8742 {
8743 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8744 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008745 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008746 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8748 }
8749 if (enc_dbcs == DBCS_JPNU)
8750 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8751 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8752#endif
8753 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8754 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8755 new_LineOffset = (unsigned *)lalloc((long_u)(
8756 Rows * sizeof(unsigned)), FALSE);
8757 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008758#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008759 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008762 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 {
8764 if (win_alloc_lines(wp) == FAIL)
8765 {
8766 outofmem = TRUE;
8767#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008768 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769#endif
8770 }
8771 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008772#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008773 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8774 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008775 outofmem = TRUE;
8776#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008777#ifdef FEAT_WINDOWS
8778give_up:
8779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008781#ifdef FEAT_MBYTE
8782 for (i = 0; i < p_mco; ++i)
8783 if (new_ScreenLinesC[i] == NULL)
8784 break;
8785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 if (new_ScreenLines == NULL
8787#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008788 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8790#endif
8791 || new_ScreenAttrs == NULL
8792 || new_LineOffset == NULL
8793 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008794#ifdef FEAT_WINDOWS
8795 || new_TabPageIdxs == NULL
8796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 || outofmem)
8798 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008799 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008800 {
8801 /* guess the size */
8802 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8803
8804 /* Remember we did this to avoid getting outofmem messages over
8805 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008806 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 vim_free(new_ScreenLines);
8809 new_ScreenLines = NULL;
8810#ifdef FEAT_MBYTE
8811 vim_free(new_ScreenLinesUC);
8812 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008813 for (i = 0; i < p_mco; ++i)
8814 {
8815 vim_free(new_ScreenLinesC[i]);
8816 new_ScreenLinesC[i] = NULL;
8817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 vim_free(new_ScreenLines2);
8819 new_ScreenLines2 = NULL;
8820#endif
8821 vim_free(new_ScreenAttrs);
8822 new_ScreenAttrs = NULL;
8823 vim_free(new_LineOffset);
8824 new_LineOffset = NULL;
8825 vim_free(new_LineWraps);
8826 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008827#ifdef FEAT_WINDOWS
8828 vim_free(new_TabPageIdxs);
8829 new_TabPageIdxs = NULL;
8830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 }
8832 else
8833 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008834 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008835
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 for (new_row = 0; new_row < Rows; ++new_row)
8837 {
8838 new_LineOffset[new_row] = new_row * Columns;
8839 new_LineWraps[new_row] = FALSE;
8840
8841 /*
8842 * If the screen is not going to be cleared, copy as much as
8843 * possible from the old screen to the new one and clear the rest
8844 * (used when resizing the window at the "--more--" prompt or when
8845 * executing an external command, for the GUI).
8846 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008847 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 {
8849 (void)vim_memset(new_ScreenLines + new_row * Columns,
8850 ' ', (size_t)Columns * sizeof(schar_T));
8851#ifdef FEAT_MBYTE
8852 if (enc_utf8)
8853 {
8854 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8855 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008856 for (i = 0; i < p_mco; ++i)
8857 (void)vim_memset(new_ScreenLinesC[i]
8858 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 0, (size_t)Columns * sizeof(u8char_T));
8860 }
8861 if (enc_dbcs == DBCS_JPNU)
8862 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8863 0, (size_t)Columns * sizeof(schar_T));
8864#endif
8865 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8866 0, (size_t)Columns * sizeof(sattr_T));
8867 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008868 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 {
8870 if (screen_Columns < Columns)
8871 len = screen_Columns;
8872 else
8873 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008874#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008875 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008876 * may be invalid now. Also when p_mco changes. */
8877 if (!(enc_utf8 && ScreenLinesUC == NULL)
8878 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008879#endif
8880 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8881 ScreenLines + LineOffset[old_row],
8882 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008884 if (enc_utf8 && ScreenLinesUC != NULL
8885 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 {
8887 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8888 ScreenLinesUC + LineOffset[old_row],
8889 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008890 for (i = 0; i < p_mco; ++i)
8891 mch_memmove(new_ScreenLinesC[i]
8892 + new_LineOffset[new_row],
8893 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 (size_t)len * sizeof(u8char_T));
8895 }
8896 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8897 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8898 ScreenLines2 + LineOffset[old_row],
8899 (size_t)len * sizeof(schar_T));
8900#endif
8901 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8902 ScreenAttrs + LineOffset[old_row],
8903 (size_t)len * sizeof(sattr_T));
8904 }
8905 }
8906 }
8907 /* Use the last line of the screen for the current line. */
8908 current_ScreenLine = new_ScreenLines + Rows * Columns;
8909 }
8910
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008911 free_screenlines();
8912
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 ScreenLines = new_ScreenLines;
8914#ifdef FEAT_MBYTE
8915 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008916 for (i = 0; i < p_mco; ++i)
8917 ScreenLinesC[i] = new_ScreenLinesC[i];
8918 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 ScreenLines2 = new_ScreenLines2;
8920#endif
8921 ScreenAttrs = new_ScreenAttrs;
8922 LineOffset = new_LineOffset;
8923 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008924#ifdef FEAT_WINDOWS
8925 TabPageIdxs = new_TabPageIdxs;
8926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927
8928 /* It's important that screen_Rows and screen_Columns reflect the actual
8929 * size of ScreenLines[]. Set them before calling anything. */
8930#ifdef FEAT_GUI
8931 old_Rows = screen_Rows;
8932#endif
8933 screen_Rows = Rows;
8934 screen_Columns = Columns;
8935
8936 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008937 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 screenclear2();
8939
8940#ifdef FEAT_GUI
8941 else if (gui.in_use
8942 && !gui.starting
8943 && ScreenLines != NULL
8944 && old_Rows != Rows)
8945 {
8946 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8947 /*
8948 * Adjust the position of the cursor, for when executing an external
8949 * command.
8950 */
8951 if (msg_row >= Rows) /* Rows got smaller */
8952 msg_row = Rows - 1; /* put cursor at last row */
8953 else if (Rows > old_Rows) /* Rows got bigger */
8954 msg_row += Rows - old_Rows; /* put cursor in same place */
8955 if (msg_col >= Columns) /* Columns got smaller */
8956 msg_col = Columns - 1; /* put cursor at last column */
8957 }
8958#endif
8959
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008961 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008962
8963#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008964 /*
8965 * Do not apply autocommands more than 3 times to avoid an endless loop
8966 * in case applying autocommands always changes Rows or Columns.
8967 */
8968 if (starting == 0 && ++retry_count <= 3)
8969 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008970 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008971 /* In rare cases, autocommands may have altered Rows or Columns,
8972 * jump back to check if we need to allocate the screen again. */
8973 goto retry;
8974 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976}
8977
8978 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008979free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008980{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008981#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008982 int i;
8983
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008984 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008985 for (i = 0; i < Screen_mco; ++i)
8986 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008987 vim_free(ScreenLines2);
8988#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008989 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008990 vim_free(ScreenAttrs);
8991 vim_free(LineOffset);
8992 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008993#ifdef FEAT_WINDOWS
8994 vim_free(TabPageIdxs);
8995#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008996}
8997
8998 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008999screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000{
9001 check_for_delay(FALSE);
9002 screenalloc(FALSE); /* allocate screen buffers if size changed */
9003 screenclear2(); /* clear the screen */
9004}
9005
9006 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009007screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
9009 int i;
9010
9011 if (starting == NO_SCREEN || ScreenLines == NULL
9012#ifdef FEAT_GUI
9013 || (gui.in_use && gui.starting)
9014#endif
9015 )
9016 return;
9017
9018#ifdef FEAT_GUI
9019 if (!gui.in_use)
9020#endif
9021 screen_attr = -1; /* force setting the Normal colors */
9022 screen_stop_highlight(); /* don't want highlighting here */
9023
9024#ifdef FEAT_CLIPBOARD
9025 /* disable selection without redrawing it */
9026 clip_scroll_selection(9999);
9027#endif
9028
9029 /* blank out ScreenLines */
9030 for (i = 0; i < Rows; ++i)
9031 {
9032 lineclear(LineOffset[i], (int)Columns);
9033 LineWraps[i] = FALSE;
9034 }
9035
9036 if (can_clear(T_CL))
9037 {
9038 out_str(T_CL); /* clear the display */
9039 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009040 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 }
9042 else
9043 {
9044 /* can't clear the screen, mark all chars with invalid attributes */
9045 for (i = 0; i < Rows; ++i)
9046 lineinvalid(LineOffset[i], (int)Columns);
9047 clear_cmdline = TRUE;
9048 }
9049
9050 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9051
9052 win_rest_invalid(firstwin);
9053 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009054#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009055 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 if (must_redraw == CLEAR) /* no need to clear again */
9058 must_redraw = NOT_VALID;
9059 compute_cmdrow();
9060 msg_row = cmdline_row; /* put cursor on last line for messages */
9061 msg_col = 0;
9062 screen_start(); /* don't know where cursor is now */
9063 msg_scrolled = 0; /* can't scroll back */
9064 msg_didany = FALSE;
9065 msg_didout = FALSE;
9066}
9067
9068/*
9069 * Clear one line in ScreenLines.
9070 */
9071 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009072lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073{
9074 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9075#ifdef FEAT_MBYTE
9076 if (enc_utf8)
9077 (void)vim_memset(ScreenLinesUC + off, 0,
9078 (size_t)width * sizeof(u8char_T));
9079#endif
9080 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
9081}
9082
9083/*
9084 * Mark one line in ScreenLines invalid by setting the attributes to an
9085 * invalid value.
9086 */
9087 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009088lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089{
9090 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9091}
9092
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009093#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094/*
9095 * Copy part of a Screenline for vertically split window "wp".
9096 */
9097 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009098linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099{
9100 unsigned off_to = LineOffset[to] + wp->w_wincol;
9101 unsigned off_from = LineOffset[from] + wp->w_wincol;
9102
9103 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9104 wp->w_width * sizeof(schar_T));
9105# ifdef FEAT_MBYTE
9106 if (enc_utf8)
9107 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009108 int i;
9109
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9111 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009112 for (i = 0; i < p_mco; ++i)
9113 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9114 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 }
9116 if (enc_dbcs == DBCS_JPNU)
9117 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9118 wp->w_width * sizeof(schar_T));
9119# endif
9120 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9121 wp->w_width * sizeof(sattr_T));
9122}
9123#endif
9124
9125/*
9126 * Return TRUE if clearing with term string "p" would work.
9127 * It can't work when the string is empty or it won't set the right background.
9128 */
9129 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009130can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131{
9132 return (*p != NUL && (t_colors <= 1
9133#ifdef FEAT_GUI
9134 || gui.in_use
9135#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009136#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009137 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009138 || (!p_tgc && cterm_normal_bg_color == 0)
9139#else
9140 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009141#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009142 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143}
9144
9145/*
9146 * Reset cursor position. Use whenever cursor was moved because of outputting
9147 * something directly to the screen (shell commands) or a terminal control
9148 * code.
9149 */
9150 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009151screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152{
9153 screen_cur_row = screen_cur_col = 9999;
9154}
9155
9156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 * Move the cursor to position "row","col" in the screen.
9158 * This tries to find the most efficient way to move, minimizing the number of
9159 * characters sent to the terminal.
9160 */
9161 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009162windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009164 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 int i;
9166 int plan;
9167 int cost;
9168 int wouldbe_col;
9169 int noinvcurs;
9170 char_u *bs;
9171 int goto_cost;
9172 int attr;
9173
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009174#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9176
9177#define PLAN_LE 1
9178#define PLAN_CR 2
9179#define PLAN_NL 3
9180#define PLAN_WRITE 4
9181 /* Can't use ScreenLines unless initialized */
9182 if (ScreenLines == NULL)
9183 return;
9184
9185 if (col != screen_cur_col || row != screen_cur_row)
9186 {
9187 /* Check for valid position. */
9188 if (row < 0) /* window without text lines? */
9189 row = 0;
9190 if (row >= screen_Rows)
9191 row = screen_Rows - 1;
9192 if (col >= screen_Columns)
9193 col = screen_Columns - 1;
9194
9195 /* check if no cursor movement is allowed in highlight mode */
9196 if (screen_attr && *T_MS == NUL)
9197 noinvcurs = HIGHL_COST;
9198 else
9199 noinvcurs = 0;
9200 goto_cost = GOTO_COST + noinvcurs;
9201
9202 /*
9203 * Plan how to do the positioning:
9204 * 1. Use CR to move it to column 0, same row.
9205 * 2. Use T_LE to move it a few columns to the left.
9206 * 3. Use NL to move a few lines down, column 0.
9207 * 4. Move a few columns to the right with T_ND or by writing chars.
9208 *
9209 * Don't do this if the cursor went beyond the last column, the cursor
9210 * position is unknown then (some terminals wrap, some don't )
9211 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009212 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 * characters to move the cursor to the right.
9214 */
9215 if (row >= screen_cur_row && screen_cur_col < Columns)
9216 {
9217 /*
9218 * If the cursor is in the same row, bigger col, we can use CR
9219 * or T_LE.
9220 */
9221 bs = NULL; /* init for GCC */
9222 attr = screen_attr;
9223 if (row == screen_cur_row && col < screen_cur_col)
9224 {
9225 /* "le" is preferred over "bc", because "bc" is obsolete */
9226 if (*T_LE)
9227 bs = T_LE; /* "cursor left" */
9228 else
9229 bs = T_BC; /* "backspace character (old) */
9230 if (*bs)
9231 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9232 else
9233 cost = 999;
9234 if (col + 1 < cost) /* using CR is less characters */
9235 {
9236 plan = PLAN_CR;
9237 wouldbe_col = 0;
9238 cost = 1; /* CR is just one character */
9239 }
9240 else
9241 {
9242 plan = PLAN_LE;
9243 wouldbe_col = col;
9244 }
9245 if (noinvcurs) /* will stop highlighting */
9246 {
9247 cost += noinvcurs;
9248 attr = 0;
9249 }
9250 }
9251
9252 /*
9253 * If the cursor is above where we want to be, we can use CR LF.
9254 */
9255 else if (row > screen_cur_row)
9256 {
9257 plan = PLAN_NL;
9258 wouldbe_col = 0;
9259 cost = (row - screen_cur_row) * 2; /* CR LF */
9260 if (noinvcurs) /* will stop highlighting */
9261 {
9262 cost += noinvcurs;
9263 attr = 0;
9264 }
9265 }
9266
9267 /*
9268 * If the cursor is in the same row, smaller col, just use write.
9269 */
9270 else
9271 {
9272 plan = PLAN_WRITE;
9273 wouldbe_col = screen_cur_col;
9274 cost = 0;
9275 }
9276
9277 /*
9278 * Check if any characters that need to be written have the
9279 * correct attributes. Also avoid UTF-8 characters.
9280 */
9281 i = col - wouldbe_col;
9282 if (i > 0)
9283 cost += i;
9284 if (cost < goto_cost && i > 0)
9285 {
9286 /*
9287 * Check if the attributes are correct without additionally
9288 * stopping highlighting.
9289 */
9290 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9291 while (i && *p++ == attr)
9292 --i;
9293 if (i != 0)
9294 {
9295 /*
9296 * Try if it works when highlighting is stopped here.
9297 */
9298 if (*--p == 0)
9299 {
9300 cost += noinvcurs;
9301 while (i && *p++ == 0)
9302 --i;
9303 }
9304 if (i != 0)
9305 cost = 999; /* different attributes, don't do it */
9306 }
9307#ifdef FEAT_MBYTE
9308 if (enc_utf8)
9309 {
9310 /* Don't use an UTF-8 char for positioning, it's slow. */
9311 for (i = wouldbe_col; i < col; ++i)
9312 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9313 {
9314 cost = 999;
9315 break;
9316 }
9317 }
9318#endif
9319 }
9320
9321 /*
9322 * We can do it without term_windgoto()!
9323 */
9324 if (cost < goto_cost)
9325 {
9326 if (plan == PLAN_LE)
9327 {
9328 if (noinvcurs)
9329 screen_stop_highlight();
9330 while (screen_cur_col > col)
9331 {
9332 out_str(bs);
9333 --screen_cur_col;
9334 }
9335 }
9336 else if (plan == PLAN_CR)
9337 {
9338 if (noinvcurs)
9339 screen_stop_highlight();
9340 out_char('\r');
9341 screen_cur_col = 0;
9342 }
9343 else if (plan == PLAN_NL)
9344 {
9345 if (noinvcurs)
9346 screen_stop_highlight();
9347 while (screen_cur_row < row)
9348 {
9349 out_char('\n');
9350 ++screen_cur_row;
9351 }
9352 screen_cur_col = 0;
9353 }
9354
9355 i = col - screen_cur_col;
9356 if (i > 0)
9357 {
9358 /*
9359 * Use cursor-right if it's one character only. Avoids
9360 * removing a line of pixels from the last bold char, when
9361 * using the bold trick in the GUI.
9362 */
9363 if (T_ND[0] != NUL && T_ND[1] == NUL)
9364 {
9365 while (i-- > 0)
9366 out_char(*T_ND);
9367 }
9368 else
9369 {
9370 int off;
9371
9372 off = LineOffset[row] + screen_cur_col;
9373 while (i-- > 0)
9374 {
9375 if (ScreenAttrs[off] != screen_attr)
9376 screen_stop_highlight();
9377#ifdef FEAT_MBYTE
9378 out_flush_check();
9379#endif
9380 out_char(ScreenLines[off]);
9381#ifdef FEAT_MBYTE
9382 if (enc_dbcs == DBCS_JPNU
9383 && ScreenLines[off] == 0x8e)
9384 out_char(ScreenLines2[off]);
9385#endif
9386 ++off;
9387 }
9388 }
9389 }
9390 }
9391 }
9392 else
9393 cost = 999;
9394
9395 if (cost >= goto_cost)
9396 {
9397 if (noinvcurs)
9398 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009399 if (row == screen_cur_row && (col > screen_cur_col)
9400 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 term_cursor_right(col - screen_cur_col);
9402 else
9403 term_windgoto(row, col);
9404 }
9405 screen_cur_row = row;
9406 screen_cur_col = col;
9407 }
9408}
9409
9410/*
9411 * Set cursor to its position in the current window.
9412 */
9413 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009414setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415{
9416 if (redrawing())
9417 {
9418 validate_cursor();
9419 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9420 W_WINCOL(curwin) + (
9421#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009422 /* With 'rightleft' set and the cursor on a double-wide
9423 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9425# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009426 (has_mbyte
9427 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9428 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429# endif
9430 1)) :
9431#endif
9432 curwin->w_wcol));
9433 }
9434}
9435
9436
9437/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009438 * Insert 'line_count' lines at 'row' in window 'wp'.
9439 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9440 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 * scrolling.
9442 * Returns FAIL if the lines are not inserted, OK for success.
9443 */
9444 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009445win_ins_lines(
9446 win_T *wp,
9447 int row,
9448 int line_count,
9449 int invalid,
9450 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
9452 int did_delete;
9453 int nextrow;
9454 int lastrow;
9455 int retval;
9456
9457 if (invalid)
9458 wp->w_lines_valid = 0;
9459
9460 if (wp->w_height < 5)
9461 return FAIL;
9462
9463 if (line_count > wp->w_height - row)
9464 line_count = wp->w_height - row;
9465
9466 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9467 if (retval != MAYBE)
9468 return retval;
9469
9470 /*
9471 * If there is a next window or a status line, we first try to delete the
9472 * lines at the bottom to avoid messing what is after the window.
9473 * If this fails and there are following windows, don't do anything to avoid
9474 * messing up those windows, better just redraw.
9475 */
9476 did_delete = FALSE;
9477#ifdef FEAT_WINDOWS
9478 if (wp->w_next != NULL || wp->w_status_height)
9479 {
9480 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9481 line_count, (int)Rows, FALSE, NULL) == OK)
9482 did_delete = TRUE;
9483 else if (wp->w_next)
9484 return FAIL;
9485 }
9486#endif
9487 /*
9488 * if no lines deleted, blank the lines that will end up below the window
9489 */
9490 if (!did_delete)
9491 {
9492#ifdef FEAT_WINDOWS
9493 wp->w_redr_status = TRUE;
9494#endif
9495 redraw_cmdline = TRUE;
9496 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9497 lastrow = nextrow + line_count;
9498 if (lastrow > Rows)
9499 lastrow = Rows;
9500 screen_fill(nextrow - line_count, lastrow - line_count,
9501 W_WINCOL(wp), (int)W_ENDCOL(wp),
9502 ' ', ' ', 0);
9503 }
9504
9505 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9506 == FAIL)
9507 {
9508 /* deletion will have messed up other windows */
9509 if (did_delete)
9510 {
9511#ifdef FEAT_WINDOWS
9512 wp->w_redr_status = TRUE;
9513#endif
9514 win_rest_invalid(W_NEXT(wp));
9515 }
9516 return FAIL;
9517 }
9518
9519 return OK;
9520}
9521
9522/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009523 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9525 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9526 * scrolling
9527 * Return OK for success, FAIL if the lines are not deleted.
9528 */
9529 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009530win_del_lines(
9531 win_T *wp,
9532 int row,
9533 int line_count,
9534 int invalid,
9535 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536{
9537 int retval;
9538
9539 if (invalid)
9540 wp->w_lines_valid = 0;
9541
9542 if (line_count > wp->w_height - row)
9543 line_count = wp->w_height - row;
9544
9545 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9546 if (retval != MAYBE)
9547 return retval;
9548
9549 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9550 (int)Rows, FALSE, NULL) == FAIL)
9551 return FAIL;
9552
9553#ifdef FEAT_WINDOWS
9554 /*
9555 * If there are windows or status lines below, try to put them at the
9556 * correct place. If we can't do that, they have to be redrawn.
9557 */
9558 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9559 {
9560 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9561 line_count, (int)Rows, NULL) == FAIL)
9562 {
9563 wp->w_redr_status = TRUE;
9564 win_rest_invalid(wp->w_next);
9565 }
9566 }
9567 /*
9568 * If this is the last window and there is no status line, redraw the
9569 * command line later.
9570 */
9571 else
9572#endif
9573 redraw_cmdline = TRUE;
9574 return OK;
9575}
9576
9577/*
9578 * Common code for win_ins_lines() and win_del_lines().
9579 * Returns OK or FAIL when the work has been done.
9580 * Returns MAYBE when not finished yet.
9581 */
9582 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009583win_do_lines(
9584 win_T *wp,
9585 int row,
9586 int line_count,
9587 int mayclear,
9588 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589{
9590 int retval;
9591
9592 if (!redrawing() || line_count <= 0)
9593 return FAIL;
9594
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009595 /* When inserting lines would result in loss of command output, just redraw
9596 * the lines. */
9597 if (no_win_do_lines_ins && !del)
9598 return FAIL;
9599
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 /* only a few lines left: redraw is faster */
9601 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009602#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 && wp->w_width == Columns
9604#endif
9605 )
9606 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009607 if (!no_win_do_lines_ins)
9608 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 return FAIL;
9610 }
9611
9612 /*
9613 * Delete all remaining lines
9614 */
9615 if (row + line_count >= wp->w_height)
9616 {
9617 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9618 W_WINCOL(wp), (int)W_ENDCOL(wp),
9619 ' ', ' ', 0);
9620 return OK;
9621 }
9622
9623 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009624 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009626 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009628 if (!no_win_do_lines_ins)
9629 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630
9631 /*
9632 * If the terminal can set a scroll region, use that.
9633 * Always do this in a vertically split window. This will redraw from
9634 * ScreenLines[] when t_CV isn't defined. That's faster than using
9635 * win_line().
9636 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009637 * a character in the lower right corner of the scroll region may cause a
9638 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 */
9640 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009641#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 || W_WIDTH(wp) != Columns
9643#endif
9644 )
9645 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009646#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9648#endif
9649 scroll_region_set(wp, row);
9650 if (del)
9651 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9652 wp->w_height - row, FALSE, wp);
9653 else
9654 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9655 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009656#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9658#endif
9659 scroll_region_reset();
9660 return retval;
9661 }
9662
9663#ifdef FEAT_WINDOWS
9664 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9665 return FAIL;
9666#endif
9667
9668 return MAYBE;
9669}
9670
9671/*
9672 * window 'wp' and everything after it is messed up, mark it for redraw
9673 */
9674 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009675win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676{
9677#ifdef FEAT_WINDOWS
9678 while (wp != NULL)
9679#else
9680 if (wp != NULL)
9681#endif
9682 {
9683 redraw_win_later(wp, NOT_VALID);
9684#ifdef FEAT_WINDOWS
9685 wp->w_redr_status = TRUE;
9686 wp = wp->w_next;
9687#endif
9688 }
9689 redraw_cmdline = TRUE;
9690}
9691
9692/*
9693 * The rest of the routines in this file perform screen manipulations. The
9694 * given operation is performed physically on the screen. The corresponding
9695 * change is also made to the internal screen image. In this way, the editor
9696 * anticipates the effect of editing changes on the appearance of the screen.
9697 * That way, when we call screenupdate a complete redraw isn't usually
9698 * necessary. Another advantage is that we can keep adding code to anticipate
9699 * screen changes, and in the meantime, everything still works.
9700 */
9701
9702/*
9703 * types for inserting or deleting lines
9704 */
9705#define USE_T_CAL 1
9706#define USE_T_CDL 2
9707#define USE_T_AL 3
9708#define USE_T_CE 4
9709#define USE_T_DL 5
9710#define USE_T_SR 6
9711#define USE_NL 7
9712#define USE_T_CD 8
9713#define USE_REDRAW 9
9714
9715/*
9716 * insert lines on the screen and update ScreenLines[]
9717 * 'end' is the line after the scrolled part. Normally it is Rows.
9718 * When scrolling region used 'off' is the offset from the top for the region.
9719 * 'row' and 'end' are relative to the start of the region.
9720 *
9721 * return FAIL for failure, OK for success.
9722 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009723 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009724screen_ins_lines(
9725 int off,
9726 int row,
9727 int line_count,
9728 int end,
9729 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731 int i;
9732 int j;
9733 unsigned temp;
9734 int cursor_row;
9735 int type;
9736 int result_empty;
9737 int can_ce = can_clear(T_CE);
9738
9739 /*
9740 * FAIL if
9741 * - there is no valid screen
9742 * - the screen has to be redrawn completely
9743 * - the line count is less than one
9744 * - the line count is more than 'ttyscroll'
9745 */
9746 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9747 return FAIL;
9748
9749 /*
9750 * There are seven ways to insert lines:
9751 * 0. When in a vertically split window and t_CV isn't set, redraw the
9752 * characters from ScreenLines[].
9753 * 1. Use T_CD (clear to end of display) if it exists and the result of
9754 * the insert is just empty lines
9755 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9756 * present or line_count > 1. It looks better if we do all the inserts
9757 * at once.
9758 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9759 * insert is just empty lines and T_CE is not present or line_count >
9760 * 1.
9761 * 4. Use T_AL (insert line) if it exists.
9762 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9763 * just empty lines.
9764 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9765 * just empty lines.
9766 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9767 * the 'da' flag is not set or we have clear line capability.
9768 * 8. redraw the characters from ScreenLines[].
9769 *
9770 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9771 * the scrollbar for the window. It does have insert line, use that if it
9772 * exists.
9773 */
9774 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009775#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9777 type = USE_REDRAW;
9778 else
9779#endif
9780 if (can_clear(T_CD) && result_empty)
9781 type = USE_T_CD;
9782 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9783 type = USE_T_CAL;
9784 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9785 type = USE_T_CDL;
9786 else if (*T_AL != NUL)
9787 type = USE_T_AL;
9788 else if (can_ce && result_empty)
9789 type = USE_T_CE;
9790 else if (*T_DL != NUL && result_empty)
9791 type = USE_T_DL;
9792 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9793 type = USE_T_SR;
9794 else
9795 return FAIL;
9796
9797 /*
9798 * For clearing the lines screen_del_lines() is used. This will also take
9799 * care of t_db if necessary.
9800 */
9801 if (type == USE_T_CD || type == USE_T_CDL ||
9802 type == USE_T_CE || type == USE_T_DL)
9803 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9804
9805 /*
9806 * If text is retained below the screen, first clear or delete as many
9807 * lines at the bottom of the window as are about to be inserted so that
9808 * the deleted lines won't later surface during a screen_del_lines.
9809 */
9810 if (*T_DB)
9811 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9812
9813#ifdef FEAT_CLIPBOARD
9814 /* Remove a modeless selection when inserting lines halfway the screen
9815 * or not the full width of the screen. */
9816 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009817# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 || (wp != NULL && wp->w_width != Columns)
9819# endif
9820 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009821 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 else
9823 clip_scroll_selection(-line_count);
9824#endif
9825
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826#ifdef FEAT_GUI
9827 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9828 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009829 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830#endif
9831
9832 if (*T_CCS != NUL) /* cursor relative to region */
9833 cursor_row = row;
9834 else
9835 cursor_row = row + off;
9836
9837 /*
9838 * Shift LineOffset[] line_count down to reflect the inserted lines.
9839 * Clear the inserted lines in ScreenLines[].
9840 */
9841 row += off;
9842 end += off;
9843 for (i = 0; i < line_count; ++i)
9844 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009845#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 if (wp != NULL && wp->w_width != Columns)
9847 {
9848 /* need to copy part of a line */
9849 j = end - 1 - i;
9850 while ((j -= line_count) >= row)
9851 linecopy(j + line_count, j, wp);
9852 j += line_count;
9853 if (can_clear((char_u *)" "))
9854 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9855 else
9856 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9857 LineWraps[j] = FALSE;
9858 }
9859 else
9860#endif
9861 {
9862 j = end - 1 - i;
9863 temp = LineOffset[j];
9864 while ((j -= line_count) >= row)
9865 {
9866 LineOffset[j + line_count] = LineOffset[j];
9867 LineWraps[j + line_count] = LineWraps[j];
9868 }
9869 LineOffset[j + line_count] = temp;
9870 LineWraps[j + line_count] = FALSE;
9871 if (can_clear((char_u *)" "))
9872 lineclear(temp, (int)Columns);
9873 else
9874 lineinvalid(temp, (int)Columns);
9875 }
9876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877
9878 screen_stop_highlight();
9879 windgoto(cursor_row, 0);
9880
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009881#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 /* redraw the characters */
9883 if (type == USE_REDRAW)
9884 redraw_block(row, end, wp);
9885 else
9886#endif
9887 if (type == USE_T_CAL)
9888 {
9889 term_append_lines(line_count);
9890 screen_start(); /* don't know where cursor is now */
9891 }
9892 else
9893 {
9894 for (i = 0; i < line_count; i++)
9895 {
9896 if (type == USE_T_AL)
9897 {
9898 if (i && cursor_row != 0)
9899 windgoto(cursor_row, 0);
9900 out_str(T_AL);
9901 }
9902 else /* type == USE_T_SR */
9903 out_str(T_SR);
9904 screen_start(); /* don't know where cursor is now */
9905 }
9906 }
9907
9908 /*
9909 * With scroll-reverse and 'da' flag set we need to clear the lines that
9910 * have been scrolled down into the region.
9911 */
9912 if (type == USE_T_SR && *T_DA)
9913 {
9914 for (i = 0; i < line_count; ++i)
9915 {
9916 windgoto(off + i, 0);
9917 out_str(T_CE);
9918 screen_start(); /* don't know where cursor is now */
9919 }
9920 }
9921
9922#ifdef FEAT_GUI
9923 gui_can_update_cursor();
9924 if (gui.in_use)
9925 out_flush(); /* always flush after a scroll */
9926#endif
9927 return OK;
9928}
9929
9930/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009931 * Delete lines on the screen and update ScreenLines[].
9932 * "end" is the line after the scrolled part. Normally it is Rows.
9933 * When scrolling region used "off" is the offset from the top for the region.
9934 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 *
9936 * Return OK for success, FAIL if the lines are not deleted.
9937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009939screen_del_lines(
9940 int off,
9941 int row,
9942 int line_count,
9943 int end,
9944 int force, /* even when line_count > p_ttyscroll */
9945 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946{
9947 int j;
9948 int i;
9949 unsigned temp;
9950 int cursor_row;
9951 int cursor_end;
9952 int result_empty; /* result is empty until end of region */
9953 int can_delete; /* deleting line codes can be used */
9954 int type;
9955
9956 /*
9957 * FAIL if
9958 * - there is no valid screen
9959 * - the screen has to be redrawn completely
9960 * - the line count is less than one
9961 * - the line count is more than 'ttyscroll'
9962 */
9963 if (!screen_valid(TRUE) || line_count <= 0 ||
9964 (!force && line_count > p_ttyscroll))
9965 return FAIL;
9966
9967 /*
9968 * Check if the rest of the current region will become empty.
9969 */
9970 result_empty = row + line_count >= end;
9971
9972 /*
9973 * We can delete lines only when 'db' flag not set or when 'ce' option
9974 * available.
9975 */
9976 can_delete = (*T_DB == NUL || can_clear(T_CE));
9977
9978 /*
9979 * There are six ways to delete lines:
9980 * 0. When in a vertically split window and t_CV isn't set, redraw the
9981 * characters from ScreenLines[].
9982 * 1. Use T_CD if it exists and the result is empty.
9983 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9984 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9985 * none of the other ways work.
9986 * 4. Use T_CE (erase line) if the result is empty.
9987 * 5. Use T_DL (delete line) if it exists.
9988 * 6. redraw the characters from ScreenLines[].
9989 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009990#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9992 type = USE_REDRAW;
9993 else
9994#endif
9995 if (can_clear(T_CD) && result_empty)
9996 type = USE_T_CD;
9997#if defined(__BEOS__) && defined(BEOS_DR8)
9998 /*
9999 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10000 * its internal termcap... this works okay for tests which test *T_DB !=
10001 * NUL. It has the disadvantage that the user cannot use any :set t_*
10002 * command to get T_DB (back) to empty_option, only :set term=... will do
10003 * the trick...
10004 * Anyway, this hack will hopefully go away with the next OS release.
10005 * (Olaf Seibert)
10006 */
10007 else if (row == 0 && T_DB == empty_option
10008 && (line_count == 1 || *T_CDL == NUL))
10009#else
10010 else if (row == 0 && (
10011#ifndef AMIGA
10012 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10013 * up, so use delete-line command */
10014 line_count == 1 ||
10015#endif
10016 *T_CDL == NUL))
10017#endif
10018 type = USE_NL;
10019 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10020 type = USE_T_CDL;
10021 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010022#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 && (wp == NULL || wp->w_width == Columns)
10024#endif
10025 )
10026 type = USE_T_CE;
10027 else if (*T_DL != NUL && can_delete)
10028 type = USE_T_DL;
10029 else if (*T_CDL != NUL && can_delete)
10030 type = USE_T_CDL;
10031 else
10032 return FAIL;
10033
10034#ifdef FEAT_CLIPBOARD
10035 /* Remove a modeless selection when deleting lines halfway the screen or
10036 * not the full width of the screen. */
10037 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010038# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 || (wp != NULL && wp->w_width != Columns)
10040# endif
10041 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010042 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 else
10044 clip_scroll_selection(line_count);
10045#endif
10046
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047#ifdef FEAT_GUI
10048 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10049 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010050 gui_dont_update_cursor(gui.cursor_row >= row + off
10051 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052#endif
10053
10054 if (*T_CCS != NUL) /* cursor relative to region */
10055 {
10056 cursor_row = row;
10057 cursor_end = end;
10058 }
10059 else
10060 {
10061 cursor_row = row + off;
10062 cursor_end = end + off;
10063 }
10064
10065 /*
10066 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10067 * Clear the inserted lines in ScreenLines[].
10068 */
10069 row += off;
10070 end += off;
10071 for (i = 0; i < line_count; ++i)
10072 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010073#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 if (wp != NULL && wp->w_width != Columns)
10075 {
10076 /* need to copy part of a line */
10077 j = row + i;
10078 while ((j += line_count) <= end - 1)
10079 linecopy(j - line_count, j, wp);
10080 j -= line_count;
10081 if (can_clear((char_u *)" "))
10082 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
10083 else
10084 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10085 LineWraps[j] = FALSE;
10086 }
10087 else
10088#endif
10089 {
10090 /* whole width, moving the line pointers is faster */
10091 j = row + i;
10092 temp = LineOffset[j];
10093 while ((j += line_count) <= end - 1)
10094 {
10095 LineOffset[j - line_count] = LineOffset[j];
10096 LineWraps[j - line_count] = LineWraps[j];
10097 }
10098 LineOffset[j - line_count] = temp;
10099 LineWraps[j - line_count] = FALSE;
10100 if (can_clear((char_u *)" "))
10101 lineclear(temp, (int)Columns);
10102 else
10103 lineinvalid(temp, (int)Columns);
10104 }
10105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106
10107 screen_stop_highlight();
10108
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010109#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 /* redraw the characters */
10111 if (type == USE_REDRAW)
10112 redraw_block(row, end, wp);
10113 else
10114#endif
10115 if (type == USE_T_CD) /* delete the lines */
10116 {
10117 windgoto(cursor_row, 0);
10118 out_str(T_CD);
10119 screen_start(); /* don't know where cursor is now */
10120 }
10121 else if (type == USE_T_CDL)
10122 {
10123 windgoto(cursor_row, 0);
10124 term_delete_lines(line_count);
10125 screen_start(); /* don't know where cursor is now */
10126 }
10127 /*
10128 * Deleting lines at top of the screen or scroll region: Just scroll
10129 * the whole screen (scroll region) up by outputting newlines on the
10130 * last line.
10131 */
10132 else if (type == USE_NL)
10133 {
10134 windgoto(cursor_end - 1, 0);
10135 for (i = line_count; --i >= 0; )
10136 out_char('\n'); /* cursor will remain on same line */
10137 }
10138 else
10139 {
10140 for (i = line_count; --i >= 0; )
10141 {
10142 if (type == USE_T_DL)
10143 {
10144 windgoto(cursor_row, 0);
10145 out_str(T_DL); /* delete a line */
10146 }
10147 else /* type == USE_T_CE */
10148 {
10149 windgoto(cursor_row + i, 0);
10150 out_str(T_CE); /* erase a line */
10151 }
10152 screen_start(); /* don't know where cursor is now */
10153 }
10154 }
10155
10156 /*
10157 * If the 'db' flag is set, we need to clear the lines that have been
10158 * scrolled up at the bottom of the region.
10159 */
10160 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10161 {
10162 for (i = line_count; i > 0; --i)
10163 {
10164 windgoto(cursor_end - i, 0);
10165 out_str(T_CE); /* erase a line */
10166 screen_start(); /* don't know where cursor is now */
10167 }
10168 }
10169
10170#ifdef FEAT_GUI
10171 gui_can_update_cursor();
10172 if (gui.in_use)
10173 out_flush(); /* always flush after a scroll */
10174#endif
10175
10176 return OK;
10177}
10178
10179/*
10180 * show the current mode and ruler
10181 *
10182 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10183 * If clear_cmdline is FALSE there may be a message there that needs to be
10184 * cleared only if a mode is shown.
10185 * Return the length of the message (0 if no message).
10186 */
10187 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010188showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189{
10190 int need_clear;
10191 int length = 0;
10192 int do_mode;
10193 int attr;
10194 int nwr_save;
10195#ifdef FEAT_INS_EXPAND
10196 int sub_attr;
10197#endif
10198
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010199 do_mode = ((p_smd && msg_silent == 0)
10200 && ((State & INSERT)
10201 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010202 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 if (do_mode || Recording)
10204 {
10205 /*
10206 * Don't show mode right now, when not redrawing or inside a mapping.
10207 * Call char_avail() only when we are going to show something, because
10208 * it takes a bit of time.
10209 */
10210 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10211 {
10212 redraw_cmdline = TRUE; /* show mode later */
10213 return 0;
10214 }
10215
10216 nwr_save = need_wait_return;
10217
10218 /* wait a bit before overwriting an important message */
10219 check_for_delay(FALSE);
10220
10221 /* if the cmdline is more than one line high, erase top lines */
10222 need_clear = clear_cmdline;
10223 if (clear_cmdline && cmdline_row < Rows - 1)
10224 msg_clr_cmdline(); /* will reset clear_cmdline */
10225
10226 /* Position on the last line in the window, column 0 */
10227 msg_pos_mode();
10228 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010229 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 if (do_mode)
10231 {
10232 MSG_PUTS_ATTR("--", attr);
10233#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010234 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010235# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010236 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010237# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010238 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010239# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010240 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010241# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 MSG_PUTS_ATTR(" IM", attr);
10243# else
10244 MSG_PUTS_ATTR(" XIM", attr);
10245# endif
10246#endif
10247#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10248 if (gui.in_use)
10249 {
10250 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010251 {
10252 /* HANGUL */
10253 if (enc_utf8)
10254 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10255 else
10256 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 }
10259#endif
10260#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010261 /* CTRL-X in Insert mode */
10262 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 {
10264 /* These messages can get long, avoid a wrap in a narrow
10265 * window. Prefer showing edit_submode_extra. */
10266 length = (Rows - msg_row) * Columns - 3;
10267 if (edit_submode_extra != NULL)
10268 length -= vim_strsize(edit_submode_extra);
10269 if (length > 0)
10270 {
10271 if (edit_submode_pre != NULL)
10272 length -= vim_strsize(edit_submode_pre);
10273 if (length - vim_strsize(edit_submode) > 0)
10274 {
10275 if (edit_submode_pre != NULL)
10276 msg_puts_attr(edit_submode_pre, attr);
10277 msg_puts_attr(edit_submode, attr);
10278 }
10279 if (edit_submode_extra != NULL)
10280 {
10281 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10282 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010283 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 else
10285 sub_attr = attr;
10286 msg_puts_attr(edit_submode_extra, sub_attr);
10287 }
10288 }
10289 length = 0;
10290 }
10291 else
10292#endif
10293 {
10294#ifdef FEAT_VREPLACE
10295 if (State & VREPLACE_FLAG)
10296 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10297 else
10298#endif
10299 if (State & REPLACE_FLAG)
10300 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10301 else if (State & INSERT)
10302 {
10303#ifdef FEAT_RIGHTLEFT
10304 if (p_ri)
10305 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10306#endif
10307 MSG_PUTS_ATTR(_(" INSERT"), attr);
10308 }
10309 else if (restart_edit == 'I')
10310 MSG_PUTS_ATTR(_(" (insert)"), attr);
10311 else if (restart_edit == 'R')
10312 MSG_PUTS_ATTR(_(" (replace)"), attr);
10313 else if (restart_edit == 'V')
10314 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10315#ifdef FEAT_RIGHTLEFT
10316 if (p_hkmap)
10317 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10318# ifdef FEAT_FKMAP
10319 if (p_fkmap)
10320 MSG_PUTS_ATTR(farsi_text_5, attr);
10321# endif
10322#endif
10323#ifdef FEAT_KEYMAP
10324 if (State & LANGMAP)
10325 {
10326# ifdef FEAT_ARABIC
10327 if (curwin->w_p_arab)
10328 MSG_PUTS_ATTR(_(" Arabic"), attr);
10329 else
10330# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010331 if (get_keymap_str(curwin, (char_u *)" (%s)",
10332 NameBuff, MAXPATHL))
10333 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 }
10335#endif
10336 if ((State & INSERT) && p_paste)
10337 MSG_PUTS_ATTR(_(" (paste)"), attr);
10338
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 if (VIsual_active)
10340 {
10341 char *p;
10342
10343 /* Don't concatenate separate words to avoid translation
10344 * problems. */
10345 switch ((VIsual_select ? 4 : 0)
10346 + (VIsual_mode == Ctrl_V) * 2
10347 + (VIsual_mode == 'V'))
10348 {
10349 case 0: p = N_(" VISUAL"); break;
10350 case 1: p = N_(" VISUAL LINE"); break;
10351 case 2: p = N_(" VISUAL BLOCK"); break;
10352 case 4: p = N_(" SELECT"); break;
10353 case 5: p = N_(" SELECT LINE"); break;
10354 default: p = N_(" SELECT BLOCK"); break;
10355 }
10356 MSG_PUTS_ATTR(_(p), attr);
10357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 MSG_PUTS_ATTR(" --", attr);
10359 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010360
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 need_clear = TRUE;
10362 }
10363 if (Recording
10364#ifdef FEAT_INS_EXPAND
10365 && edit_submode == NULL /* otherwise it gets too long */
10366#endif
10367 )
10368 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010369 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 need_clear = TRUE;
10371 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010372
10373 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 if (need_clear || clear_cmdline)
10375 msg_clr_eos();
10376 msg_didout = FALSE; /* overwrite this message */
10377 length = msg_col;
10378 msg_col = 0;
10379 need_wait_return = nwr_save; /* never ask for hit-return for this */
10380 }
10381 else if (clear_cmdline && msg_silent == 0)
10382 /* Clear the whole command line. Will reset "clear_cmdline". */
10383 msg_clr_cmdline();
10384
10385#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 /* In Visual mode the size of the selected area must be redrawn. */
10387 if (VIsual_active)
10388 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389
10390 /* If the last window has no status line, the ruler is after the mode
10391 * message and must be redrawn */
10392 if (redrawing()
10393# ifdef FEAT_WINDOWS
10394 && lastwin->w_status_height == 0
10395# endif
10396 )
10397 win_redr_ruler(lastwin, TRUE);
10398#endif
10399 redraw_cmdline = FALSE;
10400 clear_cmdline = FALSE;
10401
10402 return length;
10403}
10404
10405/*
10406 * Position for a mode message.
10407 */
10408 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010409msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410{
10411 msg_col = 0;
10412 msg_row = Rows - 1;
10413}
10414
10415/*
10416 * Delete mode message. Used when ESC is typed which is expected to end
10417 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010418 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 */
10420 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010421unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422{
10423 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010424 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 */
10426 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10427 redraw_cmdline = TRUE; /* delete mode later */
10428 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010429 clearmode();
10430}
10431
10432/*
10433 * Clear the mode message.
10434 */
10435 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010436clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010437{
10438 msg_pos_mode();
10439 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010440 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010441 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442}
10443
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010444 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010445recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010446{
10447 MSG_PUTS_ATTR(_("recording"), attr);
10448 if (!shortmess(SHM_RECORDING))
10449 {
10450 char_u s[4];
10451 sprintf((char *)s, " @%c", Recording);
10452 MSG_PUTS_ATTR(s, attr);
10453 }
10454}
10455
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010456#if defined(FEAT_WINDOWS)
10457/*
10458 * Draw the tab pages line at the top of the Vim window.
10459 */
10460 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010461draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010462{
10463 int tabcount = 0;
10464 tabpage_T *tp;
10465 int tabwidth;
10466 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010467 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010468 int attr;
10469 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010470 win_T *cwp;
10471 int wincount;
10472 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010473 int c;
10474 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010475 int attr_sel = HL_ATTR(HLF_TPS);
10476 int attr_nosel = HL_ATTR(HLF_TP);
10477 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010478 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010479 int room;
10480 int use_sep_chars = (t_colors < 8
10481#ifdef FEAT_GUI
10482 && !gui.in_use
10483#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010484#ifdef FEAT_TERMGUICOLORS
10485 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010486#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010487 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010488
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010489 if (ScreenLines == NULL)
10490 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010491 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010492
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010493#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010494 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010495 if (gui_use_tabline())
10496 {
10497 gui_update_tabline();
10498 return;
10499 }
10500#endif
10501
10502 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010503 return;
10504
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010505#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010506
10507 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10508 for (scol = 0; scol < Columns; ++scol)
10509 TabPageIdxs[scol] = 0;
10510
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010511 /* Use the 'tabline' option if it's set. */
10512 if (*p_tal != NUL)
10513 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010514 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010515
10516 /* Check for an error. If there is one we would loop in redrawing the
10517 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010518 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010519 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010520 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010521 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010522 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010523 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010524 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010525 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010526#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010527 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010528 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010529 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010530
Bram Moolenaar238a5642006-02-21 22:12:05 +000010531 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10532 if (tabwidth < 6)
10533 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010534
Bram Moolenaar238a5642006-02-21 22:12:05 +000010535 attr = attr_nosel;
10536 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010537 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010538 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10539 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010540 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010541 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010542
Bram Moolenaar238a5642006-02-21 22:12:05 +000010543 if (tp->tp_topframe == topframe)
10544 attr = attr_sel;
10545 if (use_sep_chars && col > 0)
10546 screen_putchar('|', 0, col++, attr);
10547
10548 if (tp->tp_topframe != topframe)
10549 attr = attr_nosel;
10550
10551 screen_putchar(' ', 0, col++, attr);
10552
10553 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010554 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010555 cwp = curwin;
10556 wp = firstwin;
10557 }
10558 else
10559 {
10560 cwp = tp->tp_curwin;
10561 wp = tp->tp_firstwin;
10562 }
10563
10564 modified = FALSE;
10565 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10566 if (bufIsChanged(wp->w_buffer))
10567 modified = TRUE;
10568 if (modified || wincount > 1)
10569 {
10570 if (wincount > 1)
10571 {
10572 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010573 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010574 if (col + len >= Columns - 3)
10575 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010576 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010577#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010578 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010579#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010580 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010581#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010582 );
10583 col += len;
10584 }
10585 if (modified)
10586 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10587 screen_putchar(' ', 0, col++, attr);
10588 }
10589
10590 room = scol - col + tabwidth - 1;
10591 if (room > 0)
10592 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010593 /* Get buffer name in NameBuff[] */
10594 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010595 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010596 len = vim_strsize(NameBuff);
10597 p = NameBuff;
10598#ifdef FEAT_MBYTE
10599 if (has_mbyte)
10600 while (len > room)
10601 {
10602 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010603 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010604 }
10605 else
10606#endif
10607 if (len > room)
10608 {
10609 p += len - room;
10610 len = room;
10611 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010612 if (len > Columns - col - 1)
10613 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010614
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010615 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010616 col += len;
10617 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010618 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010619
10620 /* Store the tab page number in TabPageIdxs[], so that
10621 * jump_to_mouse() knows where each one is. */
10622 ++tabcount;
10623 while (scol < col)
10624 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010625 }
10626
Bram Moolenaar238a5642006-02-21 22:12:05 +000010627 if (use_sep_chars)
10628 c = '_';
10629 else
10630 c = ' ';
10631 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010632
10633 /* Put an "X" for closing the current tab if there are several. */
10634 if (first_tabpage->tp_next != NULL)
10635 {
10636 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10637 TabPageIdxs[Columns - 1] = -999;
10638 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010639 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010640
10641 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10642 * set. */
10643 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010644}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010645
10646/*
10647 * Get buffer name for "buf" into NameBuff[].
10648 * Takes care of special buffer names and translates special characters.
10649 */
10650 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010651get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010652{
10653 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010654 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010655 else
10656 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10657 trans_characters(NameBuff, MAXPATHL);
10658}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010659#endif
10660
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10662/*
10663 * Get the character to use in a status line. Get its attributes in "*attr".
10664 */
10665 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010666fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667{
10668 int fill;
10669 if (is_curwin)
10670 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010671 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 fill = fill_stl;
10673 }
10674 else
10675 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010676 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 fill = fill_stlnc;
10678 }
10679 /* Use fill when there is highlighting, and highlighting of current
10680 * window differs, or the fillchars differ, or this is not the
10681 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010682 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010683 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 || (fill_stl != fill_stlnc)))
10685 return fill;
10686 if (is_curwin)
10687 return '^';
10688 return '=';
10689}
10690#endif
10691
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010692#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693/*
10694 * Get the character to use in a separator between vertically split windows.
10695 * Get its attributes in "*attr".
10696 */
10697 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010698fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010700 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 if (*attr == 0 && fill_vert == ' ')
10702 return '|';
10703 else
10704 return fill_vert;
10705}
10706#endif
10707
10708/*
10709 * Return TRUE if redrawing should currently be done.
10710 */
10711 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010712redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010714#ifdef FEAT_EVAL
10715 if (disable_redraw_for_testing)
10716 return 0;
10717 else
10718#endif
10719 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10721}
10722
10723/*
10724 * Return TRUE if printing messages should currently be done.
10725 */
10726 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010727messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728{
10729 return (!(p_lz && char_avail() && !KeyTyped));
10730}
10731
10732/*
10733 * Show current status info in ruler and various other places
10734 * If always is FALSE, only show ruler if position has changed.
10735 */
10736 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010737showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738{
10739 if (!always && !redrawing())
10740 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010741#ifdef FEAT_INS_EXPAND
10742 if (pum_visible())
10743 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010744# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010745 /* Don't redraw right now, do it later. */
10746 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010747# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010748 return;
10749 }
10750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010752 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010753 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010754 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 else
10757#endif
10758#ifdef FEAT_CMDL_INFO
10759 win_redr_ruler(curwin, always);
10760#endif
10761
10762#ifdef FEAT_TITLE
10763 if (need_maketitle
10764# ifdef FEAT_STL_OPT
10765 || (p_icon && (stl_syntax & STL_IN_ICON))
10766 || (p_title && (stl_syntax & STL_IN_TITLE))
10767# endif
10768 )
10769 maketitle();
10770#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010771#ifdef FEAT_WINDOWS
10772 /* Redraw the tab pages line if needed. */
10773 if (redraw_tabline)
10774 draw_tabline();
10775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776}
10777
10778#ifdef FEAT_CMDL_INFO
10779 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010780win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010782#define RULER_BUF_LEN 70
10783 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 int row;
10785 int fillchar;
10786 int attr;
10787 int empty_line = FALSE;
10788 colnr_T virtcol;
10789 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010790 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010792#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 int this_ru_col;
10794 int off = 0;
10795 int width = Columns;
10796# define WITH_OFF(x) x
10797# define WITH_WIDTH(x) x
10798#else
10799# define WITH_OFF(x) 0
10800# define WITH_WIDTH(x) Columns
10801# define this_ru_col ru_col
10802#endif
10803
10804 /* If 'ruler' off or redrawing disabled, don't do anything */
10805 if (!p_ru)
10806 return;
10807
10808 /*
10809 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10810 * after deleting lines, before cursor.lnum is corrected.
10811 */
10812 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10813 return;
10814
10815#ifdef FEAT_INS_EXPAND
10816 /* Don't draw the ruler while doing insert-completion, it might overwrite
10817 * the (long) mode message. */
10818# ifdef FEAT_WINDOWS
10819 if (wp == lastwin && lastwin->w_status_height == 0)
10820# endif
10821 if (edit_submode != NULL)
10822 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010823 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10824 if (pum_visible())
10825 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826#endif
10827
10828#ifdef FEAT_STL_OPT
10829 if (*p_ruf)
10830 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010831 int save_called_emsg = called_emsg;
10832
10833 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010835 if (called_emsg)
10836 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010837 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010838 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 return;
10840 }
10841#endif
10842
10843 /*
10844 * Check if not in Insert mode and the line is empty (will show "0-1").
10845 */
10846 if (!(State & INSERT)
10847 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10848 empty_line = TRUE;
10849
10850 /*
10851 * Only draw the ruler when something changed.
10852 */
10853 validate_virtcol_win(wp);
10854 if ( redraw_cmdline
10855 || always
10856 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10857 || wp->w_cursor.col != wp->w_ru_cursor.col
10858 || wp->w_virtcol != wp->w_ru_virtcol
10859#ifdef FEAT_VIRTUALEDIT
10860 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10861#endif
10862 || wp->w_topline != wp->w_ru_topline
10863 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10864#ifdef FEAT_DIFF
10865 || wp->w_topfill != wp->w_ru_topfill
10866#endif
10867 || empty_line != wp->w_ru_empty)
10868 {
10869 cursor_off();
10870#ifdef FEAT_WINDOWS
10871 if (wp->w_status_height)
10872 {
10873 row = W_WINROW(wp) + wp->w_height;
10874 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 off = W_WINCOL(wp);
10876 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 }
10878 else
10879#endif
10880 {
10881 row = Rows - 1;
10882 fillchar = ' ';
10883 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010884#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 width = Columns;
10886 off = 0;
10887#endif
10888 }
10889
10890 /* In list mode virtcol needs to be recomputed */
10891 virtcol = wp->w_virtcol;
10892 if (wp->w_p_list && lcs_tab1 == NUL)
10893 {
10894 wp->w_p_list = FALSE;
10895 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10896 wp->w_p_list = TRUE;
10897 }
10898
10899 /*
10900 * Some sprintfs return the length, some return a pointer.
10901 * To avoid portability problems we use strlen() here.
10902 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010903 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10905 ? 0L
10906 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010907 len = STRLEN(buffer);
10908 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10910 (int)virtcol + 1);
10911
10912 /*
10913 * Add a "50%" if there is room for it.
10914 * On the last line, don't print in the last column (scrolls the
10915 * screen up on some terminals).
10916 */
10917 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010918 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 o = i + vim_strsize(buffer + i + 1);
10920#ifdef FEAT_WINDOWS
10921 if (wp->w_status_height == 0) /* can't use last char of screen */
10922#endif
10923 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010924#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 this_ru_col = ru_col - (Columns - width);
10926 if (this_ru_col < 0)
10927 this_ru_col = 0;
10928#endif
10929 /* Never use more than half the window/screen width, leave the other
10930 * half for the filename. */
10931 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10932 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10933 if (this_ru_col + o < WITH_WIDTH(width))
10934 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010935 /* need at least 3 chars left for get_rel_pos() + NUL */
10936 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 {
10938#ifdef FEAT_MBYTE
10939 if (has_mbyte)
10940 i += (*mb_char2bytes)(fillchar, buffer + i);
10941 else
10942#endif
10943 buffer[i++] = fillchar;
10944 ++o;
10945 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010946 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 }
10948 /* Truncate at window boundary. */
10949#ifdef FEAT_MBYTE
10950 if (has_mbyte)
10951 {
10952 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010953 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 {
10955 o += (*mb_ptr2cells)(buffer + i);
10956 if (this_ru_col + o > WITH_WIDTH(width))
10957 {
10958 buffer[i] = NUL;
10959 break;
10960 }
10961 }
10962 }
10963 else
10964#endif
10965 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10966 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10967
10968 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10969 i = redraw_cmdline;
10970 screen_fill(row, row + 1,
10971 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10972 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10973 fillchar, fillchar, attr);
10974 /* don't redraw the cmdline because of showing the ruler */
10975 redraw_cmdline = i;
10976 wp->w_ru_cursor = wp->w_cursor;
10977 wp->w_ru_virtcol = wp->w_virtcol;
10978 wp->w_ru_empty = empty_line;
10979 wp->w_ru_topline = wp->w_topline;
10980 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10981#ifdef FEAT_DIFF
10982 wp->w_ru_topfill = wp->w_topfill;
10983#endif
10984 }
10985}
10986#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010987
10988#if defined(FEAT_LINEBREAK) || defined(PROTO)
10989/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010990 * Return the width of the 'number' and 'relativenumber' column.
10991 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010992 * Otherwise it depends on 'numberwidth' and the line count.
10993 */
10994 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010995number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010996{
10997 int n;
10998 linenr_T lnum;
10999
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011000 if (wp->w_p_rnu && !wp->w_p_nu)
11001 /* cursor line shows "0" */
11002 lnum = wp->w_height;
11003 else
11004 /* cursor line shows absolute line number */
11005 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011006
Bram Moolenaar6b314672015-03-20 15:42:10 +010011007 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011008 return wp->w_nrwidth_width;
11009 wp->w_nrwidth_line_count = lnum;
11010
11011 n = 0;
11012 do
11013 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011014 lnum /= 10;
11015 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011016 } while (lnum > 0);
11017
11018 /* 'numberwidth' gives the minimal width plus one */
11019 if (n < wp->w_p_nuw - 1)
11020 n = wp->w_p_nuw - 1;
11021
11022 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011023 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011024 return n;
11025}
11026#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011027
11028/*
11029 * Return the current cursor column. This is the actual position on the
11030 * screen. First column is 0.
11031 */
11032 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011033screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011034{
11035 return screen_cur_col;
11036}
11037
11038/*
11039 * Return the current cursor row. This is the actual position on the screen.
11040 * First row is 0.
11041 */
11042 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011043screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011044{
11045 return screen_cur_row;
11046}