blob: 05853d442eeb9241e6b4ab7f85b4c01995a0e739 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200110#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
111static int text_to_screenline(win_T *wp, char_u *text, int col);
112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_FOLDING
114static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
117
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200118/* Flag that is set when drawing for a callback, not from the main command
119 * loop. */
120static int redrawing_for_callback = 0;
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122/*
123 * Buffer for one screen line (characters and attributes).
124 */
125static schar_T *current_ScreenLine;
126
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void win_update(win_T *wp);
128static 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 +0000129#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
131static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
132static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200134static int win_line(win_T *, linenr_T, int, int, int nochange);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200141# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void start_search_hl(void);
143static void end_search_hl(void);
144static void init_search_hl(win_T *wp);
145static void prepare_search_hl(win_T *wp, linenr_T lnum);
146static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
147static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screen_start_highlight(int attr);
150static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200155static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void lineinvalid(unsigned off, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void linecopy(int to, int from, win_T *wp);
158static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200159static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void win_rest_invalid(win_T *wp);
161static void msg_pos_mode(void);
162static void recording_mode(int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static void draw_tabline(void);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200164static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200166#ifdef FEAT_MENU
167static void redraw_win_toolbar(win_T *wp);
168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#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 Moolenaar071d4272004-06-13 20:20:40 +0000176/* Ugly global: overrule attribute used by screen_char() */
177static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200179#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
180/* Can limit syntax highlight time to 'redrawtime'. */
181# define SYN_TIME_LIMIT 1
182#endif
183
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200184#ifdef FEAT_RIGHTLEFT
185# define HAS_RIGHTLEFT(x) x
186#else
187# define HAS_RIGHTLEFT(x) FALSE
188#endif
189
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190/*
191 * Redraw the current window later, with update_screen(type).
192 * Set must_redraw only if not already set to a higher value.
193 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
194 */
195 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100196redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197{
198 redraw_win_later(curwin, type);
199}
200
201 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100202redraw_win_later(
203 win_T *wp,
204 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200206 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {
208 wp->w_redr_type = type;
209 if (type >= NOT_VALID)
210 wp->w_lines_valid = 0;
211 if (must_redraw < type) /* must_redraw is the maximum of all windows */
212 must_redraw = type;
213 }
214}
215
216/*
217 * Force a complete redraw later. Also resets the highlighting. To be used
218 * after executing a shell command that messes up the screen.
219 */
220 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100221redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000224#ifdef FEAT_GUI
225 if (gui.in_use)
226 /* Use a code that will reset gui.highlight_mask in
227 * gui_stop_highlight(). */
228 screen_attr = HL_ALL + 1;
229 else
230#endif
231 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200232 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233}
234
235/*
236 * Mark all windows to be redrawn later.
237 */
238 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100239redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240{
241 win_T *wp;
242
243 FOR_ALL_WINDOWS(wp)
244 {
245 redraw_win_later(wp, type);
246 }
247}
248
249/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000250 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 */
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 redraw_buf_later(curbuf, type);
256}
257
258 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100259redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 win_T *wp;
262
263 FOR_ALL_WINDOWS(wp)
264 {
265 if (wp->w_buffer == buf)
266 redraw_win_later(wp, type);
267 }
268}
269
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200270 void
271redraw_buf_and_status_later(buf_T *buf, int type)
272{
273 win_T *wp;
274
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200275#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200276 if (wild_menu_showing != 0)
277 /* Don't redraw while the command line completion is displayed, it
278 * would disappear. */
279 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200280#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200281 FOR_ALL_WINDOWS(wp)
282 {
283 if (wp->w_buffer == buf)
284 {
285 redraw_win_later(wp, type);
286 wp->w_redr_status = TRUE;
287 }
288 }
289}
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200292 * Redraw as soon as possible. When the command line is not scrolled redraw
293 * right away and restore what was on the command line.
294 * Return a code indicating what happened.
295 */
296 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100297redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200298{
299 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200300 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200301 int r;
302 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 schar_T *screenline; /* copy from ScreenLines[] */
304 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200305#ifdef FEAT_MBYTE
306 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200307 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200309 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310#endif
311
312 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 return ret;
315
316 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 if (screenline == NULL || screenattr == NULL)
323 ret = 2;
324#ifdef FEAT_MBYTE
325 if (enc_utf8)
326 {
327 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenlineUC == NULL)
330 ret = 2;
331 for (i = 0; i < p_mco; ++i)
332 {
333 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200334 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (screenlineC[i] == NULL)
336 ret = 2;
337 }
338 }
339 if (enc_dbcs == DBCS_JPNU)
340 {
341 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200342 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenline2 == NULL)
344 ret = 2;
345 }
346#endif
347
348 if (ret != 2)
349 {
350 /* Save the text displayed in the command line area. */
351 for (r = 0; r < rows; ++r)
352 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200353 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200354 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200355 (size_t)cols * sizeof(schar_T));
356 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359#ifdef FEAT_MBYTE
360 if (enc_utf8)
361 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 mch_memmove(screenlineC[i] + r * cols,
367 ScreenLinesC[i] + LineOffset[cmdline_row + r],
368 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200369 }
370 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200372 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200373 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374#endif
375 }
376
377 update_screen(0);
378 ret = 3;
379
380 if (must_redraw == 0)
381 {
382 int off = (int)(current_ScreenLine - ScreenLines);
383
384 /* Restore the text displayed in the command line area. */
385 for (r = 0; r < rows; ++r)
386 {
387 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200388 screenline + r * cols,
389 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenattr + r * cols,
392 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393#ifdef FEAT_MBYTE
394 if (enc_utf8)
395 {
396 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenlineUC + r * cols,
398 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 for (i = 0; i < p_mco; ++i)
400 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200401 screenlineC[i] + r * cols,
402 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200403 }
404 if (enc_dbcs == DBCS_JPNU)
405 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200406 screenline2 + r * cols,
407 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200408#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200409 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 }
411 ret = 4;
412 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200413 }
414
415 vim_free(screenline);
416 vim_free(screenattr);
417#ifdef FEAT_MBYTE
418 if (enc_utf8)
419 {
420 vim_free(screenlineUC);
421 for (i = 0; i < p_mco; ++i)
422 vim_free(screenlineC[i]);
423 }
424 if (enc_dbcs == DBCS_JPNU)
425 vim_free(screenline2);
426#endif
427
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200428 /* Show the intro message when appropriate. */
429 maybe_intro_message();
430
431 setcursor();
432
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433 return ret;
434}
435
436/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100437 * Invoked after an asynchronous callback is called.
438 * If an echo command was used the cursor needs to be put back where
439 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200440 * If "call_update_screen" is FALSE don't call update_screen() when at the
441 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100442 */
443 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200444redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200446 ++redrawing_for_callback;
447
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
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200458 && call_update_screen)
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 Moolenaar1b9645d2017-09-17 23:03:31 +0200464 else if (State & (NORMAL | INSERT | TERMINAL))
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
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200482
483 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100484}
485
486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 * Changed something in the current window, at buffer line "lnum", that
488 * requires that line and possibly other lines to be redrawn.
489 * Used when entering/leaving Insert mode with the cursor on a folded line.
490 * Used to remove the "$" from a change command.
491 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
492 * may become invalid and the whole window will have to be redrawn.
493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100495redrawWinline(
496 linenr_T lnum,
497 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499#ifdef FEAT_FOLDING
500 int i;
501#endif
502
503 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
504 curwin->w_redraw_top = lnum;
505 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
506 curwin->w_redraw_bot = lnum;
507 redraw_later(VALID);
508
509#ifdef FEAT_FOLDING
510 if (invalid)
511 {
512 /* A w_lines[] entry for this lnum has become invalid. */
513 i = find_wl_entry(curwin, lnum);
514 if (i >= 0)
515 curwin->w_lines[i].wl_valid = FALSE;
516 }
517#endif
518}
519
520/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200521 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 */
523 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100524update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
526 redraw_curbuf_later(type);
527 update_screen(type);
528}
529
530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 * Based on the current value of curwin->w_topline, transfer a screenfull
532 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200533 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200535 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200536update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200538 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 win_T *wp;
540 static int did_intro = FALSE;
541#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
542 int did_one;
543#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200544#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200545 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200546 int gui_cursor_col;
547 int gui_cursor_row;
548#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200549 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000551 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200553 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200555 if (type == VALID_NO_UPDATE)
556 {
557 no_update = TRUE;
558 type = 0;
559 }
560
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 if (must_redraw)
562 {
563 if (type < must_redraw) /* use maximal type */
564 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000565
566 /* must_redraw is reset here, so that when we run into some weird
567 * reason to redraw while busy redrawing (e.g., asynchronous
568 * scrolling), or update_topline() in win_update() will cause a
569 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 must_redraw = 0;
571 }
572
573 /* Need to update w_lines[]. */
574 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
575 type = NOT_VALID;
576
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000577 /* Postpone the redrawing when it's not needed and when being called
578 * recursively. */
579 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {
581 redraw_later(type); /* remember type for next time */
582 must_redraw = type;
583 if (type > INVERTED_ALL)
584 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200585 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587
588 updating_screen = TRUE;
589#ifdef FEAT_SYN_HL
590 ++display_tick; /* let syntax code know we're in a next round of
591 * display updating */
592#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200593 if (no_update)
594 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
596 /*
597 * if the screen was scrolled up when displaying a message, scroll it down
598 */
599 if (msg_scrolled)
600 {
601 clear_cmdline = TRUE;
602 if (msg_scrolled > Rows - 5) /* clearing is faster */
603 type = CLEAR;
604 else if (type != CLEAR)
605 {
606 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200607 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
608 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 type = CLEAR;
610 FOR_ALL_WINDOWS(wp)
611 {
612 if (W_WINROW(wp) < msg_scrolled)
613 {
614 if (W_WINROW(wp) + wp->w_height > msg_scrolled
615 && wp->w_redr_type < REDRAW_TOP
616 && wp->w_lines_valid > 0
617 && wp->w_topline == wp->w_lines[0].wl_lnum)
618 {
619 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
620 wp->w_redr_type = REDRAW_TOP;
621 }
622 else
623 {
624 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200625 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
626 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
629 }
630 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200631 if (!no_update)
632 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000633 redraw_tabline = TRUE;
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 /* Redraw the tab pages line if needed. */
689 if (redraw_tabline || type >= NOT_VALID)
690 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000691
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692#ifdef FEAT_SYN_HL
693 /*
694 * Correct stored syntax highlighting info for changes in each displayed
695 * buffer. Each buffer must only be done once.
696 */
697 FOR_ALL_WINDOWS(wp)
698 {
699 if (wp->w_buffer->b_mod_set)
700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 win_T *wwp;
702
703 /* Check if we already did this buffer. */
704 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
705 if (wwp->w_buffer == wp->w_buffer)
706 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200707 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 syn_stack_apply_changes(wp->w_buffer);
709 }
710 }
711#endif
712
713 /*
714 * Go from top to bottom through the windows, redrawing the ones that need
715 * it.
716 */
717#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
718 did_one = FALSE;
719#endif
720#ifdef FEAT_SEARCH_EXTRA
721 search_hl.rm.regprog = NULL;
722#endif
723 FOR_ALL_WINDOWS(wp)
724 {
725 if (wp->w_redr_type != 0)
726 {
727 cursor_off();
728#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
729 if (!did_one)
730 {
731 did_one = TRUE;
732# ifdef FEAT_SEARCH_EXTRA
733 start_search_hl();
734# endif
735# ifdef FEAT_CLIPBOARD
736 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200737 if (clip_star.available && clip_isautosel_star())
738 clip_update_selection(&clip_star);
739 if (clip_plus.available && clip_isautosel_plus())
740 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741# endif
742#ifdef FEAT_GUI
743 /* Remove the cursor before starting to do anything, because
744 * scrolling may make it difficult to redraw the text under
745 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200746 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200747 {
748 gui_cursor_col = gui.cursor_col;
749 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200751 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753#endif
754 }
755#endif
756 win_update(wp);
757 }
758
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 /* redraw status line after the window to minimize cursor movement */
760 if (wp->w_redr_status)
761 {
762 cursor_off();
763 win_redr_status(wp);
764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 }
766#if defined(FEAT_SEARCH_EXTRA)
767 end_search_hl();
768#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100769#ifdef FEAT_INS_EXPAND
770 /* May need to redraw the popup menu. */
771 if (pum_visible())
772 pum_redraw();
773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 /* Reset b_mod_set flags. Going through all windows is probably faster
776 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200777 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780 updating_screen = FALSE;
781#ifdef FEAT_GUI
782 gui_may_resize_shell();
783#endif
784
785 /* Clear or redraw the command line. Done last, because scrolling may
786 * mess up the command line. */
787 if (clear_cmdline || redraw_cmdline)
788 showmode();
789
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200790 if (no_update)
791 --no_win_do_lines_ins;
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200794 if (!did_intro)
795 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 did_intro = TRUE;
797
798#ifdef FEAT_GUI
799 /* Redraw the cursor and update the scrollbars when all screen updating is
800 * done. */
801 if (gui.in_use)
802 {
803 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200804 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200805 {
806 /* Put the GUI position where the cursor was, gui_update_cursor()
807 * uses that. */
808 gui.col = gui_cursor_col;
809 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200810# ifdef FEAT_MBYTE
811 gui.col = mb_fix_col(gui.col, gui.row);
812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200814 screen_cur_col = gui.col;
815 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 gui_update_scrollbars(FALSE);
818 }
819#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200820 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821}
822
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100823#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
824/*
825 * Prepare for updating one or more windows.
826 * Caller must check for "updating_screen" already set to avoid recursiveness.
827 */
828 static void
829update_prepare(void)
830{
831 cursor_off();
832 updating_screen = TRUE;
833#ifdef FEAT_GUI
834 /* Remove the cursor before starting to do anything, because scrolling may
835 * make it difficult to redraw the text under it. */
836 if (gui.in_use)
837 gui_undraw_cursor();
838#endif
839#ifdef FEAT_SEARCH_EXTRA
840 start_search_hl();
841#endif
842}
843
844/*
845 * Finish updating one or more windows.
846 */
847 static void
848update_finish(void)
849{
850 if (redraw_cmdline)
851 showmode();
852
853# ifdef FEAT_SEARCH_EXTRA
854 end_search_hl();
855# endif
856
857 updating_screen = FALSE;
858
859# ifdef FEAT_GUI
860 gui_may_resize_shell();
861
862 /* Redraw the cursor and update the scrollbars when all screen updating is
863 * done. */
864 if (gui.in_use)
865 {
866 out_flush(); /* required before updating the cursor */
867 gui_update_cursor(FALSE, FALSE);
868 gui_update_scrollbars(FALSE);
869 }
870# endif
871}
872#endif
873
Bram Moolenaar860cae12010-06-05 23:22:07 +0200874#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200875/*
876 * Return TRUE if the cursor line in window "wp" may be concealed, according
877 * to the 'concealcursor' option.
878 */
879 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100880conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200881{
882 int c;
883
884 if (*wp->w_p_cocu == NUL)
885 return FALSE;
886 if (get_real_state() & VISUAL)
887 c = 'v';
888 else if (State & INSERT)
889 c = 'i';
890 else if (State & NORMAL)
891 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200892 else if (State & CMDLINE)
893 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200894 else
895 return FALSE;
896 return vim_strchr(wp->w_p_cocu, c) != NULL;
897}
898
899/*
900 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
901 */
902 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100903conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200904{
905 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
906 {
907 need_cursor_line_redraw = TRUE;
908 /* Need to recompute cursor column, e.g., when starting Visual mode
909 * without concealing. */
910 curs_columns(TRUE);
911 }
912}
913
Bram Moolenaar860cae12010-06-05 23:22:07 +0200914 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100915update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200916{
917 int row;
918 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200919#ifdef SYN_TIME_LIMIT
920 proftime_T syntax_tm;
921#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200922
Bram Moolenaar908be432016-05-24 10:51:30 +0200923 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100924 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200925 return;
926
Bram Moolenaar860cae12010-06-05 23:22:07 +0200927 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200928 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200929 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200930#ifdef SYN_TIME_LIMIT
931 /* Set the time limit to 'redrawtime'. */
932 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200933 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200934#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100935 update_prepare();
936
Bram Moolenaar860cae12010-06-05 23:22:07 +0200937 row = 0;
938 for (j = 0; j < wp->w_lines_valid; ++j)
939 {
940 if (lnum == wp->w_lines[j].wl_lnum)
941 {
942 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200943# ifdef FEAT_SEARCH_EXTRA
944 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200945 start_search_hl();
946 prepare_search_hl(wp, lnum);
947# endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200948 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200949# if defined(FEAT_SEARCH_EXTRA)
950 end_search_hl();
951# endif
952 break;
953 }
954 row += wp->w_lines[j].wl_size;
955 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100956
957 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200958
959#ifdef SYN_TIME_LIMIT
960 syn_set_timeout(NULL);
961#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200962 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200963 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964}
965#endif
966
967#if defined(FEAT_SIGNS) || defined(PROTO)
968 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100969update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
971 win_T *wp;
972 int doit = FALSE;
973
974# ifdef FEAT_FOLDING
975 win_foldinfo.fi_level = 0;
976# endif
977
978 /* update/delete a specific mark */
979 FOR_ALL_WINDOWS(wp)
980 {
981 if (buf != NULL && lnum > 0)
982 {
983 if (wp->w_buffer == buf && lnum >= wp->w_topline
984 && lnum < wp->w_botline)
985 {
986 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
987 wp->w_redraw_top = lnum;
988 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
989 wp->w_redraw_bot = lnum;
990 redraw_win_later(wp, VALID);
991 }
992 }
993 else
994 redraw_win_later(wp, VALID);
995 if (wp->w_redr_type != 0)
996 doit = TRUE;
997 }
998
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100999 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001000 * happening (recursive call), messages on the screen or still starting up.
1001 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001002 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001003 || State == ASKMORE || State == HITRETURN
1004 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001005#ifdef FEAT_GUI
1006 || gui.starting
1007#endif
1008 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 return;
1010
1011 /* update all windows that need updating */
1012 update_prepare();
1013
Bram Moolenaar29323592016-07-24 22:04:11 +02001014 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 {
1016 if (wp->w_redr_type != 0)
1017 win_update(wp);
1018 if (wp->w_redr_status)
1019 win_redr_status(wp);
1020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021
1022 update_finish();
1023}
1024#endif
1025
1026
1027#if defined(FEAT_GUI) || defined(PROTO)
1028/*
1029 * Update a single window, its status line and maybe the command line msg.
1030 * Used for the GUI scrollbar.
1031 */
1032 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001033updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001035 /* return if already busy updating */
1036 if (updating_screen)
1037 return;
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 update_prepare();
1040
1041#ifdef FEAT_CLIPBOARD
1042 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001043 if (clip_star.available && clip_isautosel_star())
1044 clip_update_selection(&clip_star);
1045 if (clip_plus.available && clip_isautosel_plus())
1046 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001050
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001051 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001052 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001053 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 if (wp->w_redr_status
1056# ifdef FEAT_CMDL_INFO
1057 || p_ru
1058# endif
1059# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001060 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061# endif
1062 )
1063 win_redr_status(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
1065 update_finish();
1066}
1067#endif
1068
1069/*
1070 * Update a single window.
1071 *
1072 * This may cause the windows below it also to be redrawn (when clearing the
1073 * screen or scrolling lines).
1074 *
1075 * How the window is redrawn depends on wp->w_redr_type. Each type also
1076 * implies the one below it.
1077 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001078 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1080 * INVERTED redraw the changed part of the Visual area
1081 * INVERTED_ALL redraw the whole Visual area
1082 * VALID 1. scroll up/down to adjust for a changed w_topline
1083 * 2. update lines at the top when scrolled down
1084 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001085 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 * b_mod_top and b_mod_bot.
1087 * - if wp->w_redraw_top non-zero, redraw lines between
1088 * wp->w_redraw_top and wp->w_redr_bot.
1089 * - continue redrawing when syntax status is invalid.
1090 * 4. if scrolled up, update lines at the bottom.
1091 * This results in three areas that may need updating:
1092 * top: from first row to top_end (when scrolled down)
1093 * mid: from mid_start to mid_end (update inversion or changed text)
1094 * bot: from bot_start to last row (when scrolled up)
1095 */
1096 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001097win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098{
1099 buf_T *buf = wp->w_buffer;
1100 int type;
1101 int top_end = 0; /* Below last row of the top area that needs
1102 updating. 0 when no top area updating. */
1103 int mid_start = 999;/* first row of the mid area that needs
1104 updating. 999 when no mid area updating. */
1105 int mid_end = 0; /* Below last row of the mid area that needs
1106 updating. 0 when no mid area updating. */
1107 int bot_start = 999;/* first row of the bot area that needs
1108 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 int scrolled_down = FALSE; /* TRUE when scrolled down when
1110 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001112 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 int top_to_mod = FALSE; /* redraw above mod_top */
1114#endif
1115
1116 int row; /* current window row to display */
1117 linenr_T lnum; /* current buffer lnum to display */
1118 int idx; /* current index in w_lines[] */
1119 int srow; /* starting row of the current line */
1120
1121 int eof = FALSE; /* if TRUE, we hit the end of the file */
1122 int didline = FALSE; /* if TRUE, we finished the last line */
1123 int i;
1124 long j;
1125 static int recursive = FALSE; /* being called recursively */
1126 int old_botline = wp->w_botline;
1127#ifdef FEAT_FOLDING
1128 long fold_count;
1129#endif
1130#ifdef FEAT_SYN_HL
1131 /* remember what happened to the previous line, to know if
1132 * check_visual_highlight() can be used */
1133#define DID_NONE 1 /* didn't update a line */
1134#define DID_LINE 2 /* updated a normal line */
1135#define DID_FOLD 3 /* updated a folded line */
1136 int did_update = DID_NONE;
1137 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1138#endif
1139 linenr_T mod_top = 0;
1140 linenr_T mod_bot = 0;
1141#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1142 int save_got_int;
1143#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001144#ifdef SYN_TIME_LIMIT
1145 proftime_T syntax_tm;
1146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
1148 type = wp->w_redr_type;
1149
1150 if (type == NOT_VALID)
1151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 wp->w_lines_valid = 0;
1154 }
1155
1156 /* Window is zero-height: nothing to draw. */
1157 if (wp->w_height == 0)
1158 {
1159 wp->w_redr_type = 0;
1160 return;
1161 }
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 /* Window is zero-width: Only need to draw the separator. */
1164 if (wp->w_width == 0)
1165 {
1166 /* draw the vertical separator right of this window */
1167 draw_vsep_win(wp, 0);
1168 wp->w_redr_type = 0;
1169 return;
1170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001172#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001173 /* If this window contains a terminal, redraw works completely differently.
1174 */
1175 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001176 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001177 wp->w_redr_type = 0;
1178 return;
1179 }
1180#endif
1181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001183 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#endif
1185
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001186#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001187 /* Force redraw when width of 'number' or 'relativenumber' column
1188 * changes. */
1189 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001190 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001191 {
1192 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001193 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001194 }
1195 else
1196#endif
1197
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1199 {
1200 /*
1201 * When there are both inserted/deleted lines and specific lines to be
1202 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1203 * everything (only happens when redrawing is off for while).
1204 */
1205 type = NOT_VALID;
1206 }
1207 else
1208 {
1209 /*
1210 * Set mod_top to the first line that needs displaying because of
1211 * changes. Set mod_bot to the first line after the changes.
1212 */
1213 mod_top = wp->w_redraw_top;
1214 if (wp->w_redraw_bot != 0)
1215 mod_bot = wp->w_redraw_bot + 1;
1216 else
1217 mod_bot = 0;
1218 wp->w_redraw_top = 0; /* reset for next time */
1219 wp->w_redraw_bot = 0;
1220 if (buf->b_mod_set)
1221 {
1222 if (mod_top == 0 || mod_top > buf->b_mod_top)
1223 {
1224 mod_top = buf->b_mod_top;
1225#ifdef FEAT_SYN_HL
1226 /* Need to redraw lines above the change that may be included
1227 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001228 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001230 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 if (mod_top < 1)
1232 mod_top = 1;
1233 }
1234#endif
1235 }
1236 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1237 mod_bot = buf->b_mod_bot;
1238
1239#ifdef FEAT_SEARCH_EXTRA
1240 /* When 'hlsearch' is on and using a multi-line search pattern, a
1241 * change in one line may make the Search highlighting in a
1242 * previous line invalid. Simple solution: redraw all visible
1243 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001244 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001246 if (search_hl.rm.regprog != NULL
1247 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001249 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001250 {
1251 cur = wp->w_match_head;
1252 while (cur != NULL)
1253 {
1254 if (cur->match.regprog != NULL
1255 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001256 {
1257 top_to_mod = TRUE;
1258 break;
1259 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001260 cur = cur->next;
1261 }
1262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263#endif
1264 }
1265#ifdef FEAT_FOLDING
1266 if (mod_top != 0 && hasAnyFolding(wp))
1267 {
1268 linenr_T lnumt, lnumb;
1269
1270 /*
1271 * A change in a line can cause lines above it to become folded or
1272 * unfolded. Find the top most buffer line that may be affected.
1273 * If the line was previously folded and displayed, get the first
1274 * line of that fold. If the line is folded now, get the first
1275 * folded line. Use the minimum of these two.
1276 */
1277
1278 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1279 * the line below it. If there is no valid entry, use w_topline.
1280 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1281 * to this line. If there is no valid entry, use MAXLNUM. */
1282 lnumt = wp->w_topline;
1283 lnumb = MAXLNUM;
1284 for (i = 0; i < wp->w_lines_valid; ++i)
1285 if (wp->w_lines[i].wl_valid)
1286 {
1287 if (wp->w_lines[i].wl_lastlnum < mod_top)
1288 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1289 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1290 {
1291 lnumb = wp->w_lines[i].wl_lnum;
1292 /* When there is a fold column it might need updating
1293 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001294 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 ++lnumb;
1296 }
1297 }
1298
1299 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1300 if (mod_top > lnumt)
1301 mod_top = lnumt;
1302
1303 /* Now do the same for the bottom line (one above mod_bot). */
1304 --mod_bot;
1305 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1306 ++mod_bot;
1307 if (mod_bot < lnumb)
1308 mod_bot = lnumb;
1309 }
1310#endif
1311
1312 /* When a change starts above w_topline and the end is below
1313 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001314 * If the end of the change is above w_topline: do like no change was
1315 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (mod_top != 0 && mod_top < wp->w_topline)
1317 {
1318 if (mod_bot > wp->w_topline)
1319 mod_top = wp->w_topline;
1320#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001321 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 top_end = 1;
1323#endif
1324 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001325
1326 /* When line numbers are displayed need to redraw all lines below
1327 * inserted/deleted lines. */
1328 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1329 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 }
1331
1332 /*
1333 * When only displaying the lines at the top, set top_end. Used when
1334 * window has scrolled down for msg_scrolled.
1335 */
1336 if (type == REDRAW_TOP)
1337 {
1338 j = 0;
1339 for (i = 0; i < wp->w_lines_valid; ++i)
1340 {
1341 j += wp->w_lines[i].wl_size;
1342 if (j >= wp->w_upd_rows)
1343 {
1344 top_end = j;
1345 break;
1346 }
1347 }
1348 if (top_end == 0)
1349 /* not found (cannot happen?): redraw everything */
1350 type = NOT_VALID;
1351 else
1352 /* top area defined, the rest is VALID */
1353 type = VALID;
1354 }
1355
Bram Moolenaar367329b2007-08-30 11:53:22 +00001356 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001357 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1358 * non-zero and thus not FALSE) will indicate that screenclear() was not
1359 * called. */
1360 if (screen_cleared)
1361 screen_cleared = MAYBE;
1362
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 /*
1364 * If there are no changes on the screen that require a complete redraw,
1365 * handle three cases:
1366 * 1: we are off the top of the screen by a few lines: scroll down
1367 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1368 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1369 * w_lines[] that needs updating.
1370 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001371 if ((type == VALID || type == SOME_VALID
1372 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373#ifdef FEAT_DIFF
1374 && !wp->w_botfill && !wp->w_old_botfill
1375#endif
1376 )
1377 {
1378 if (mod_top != 0 && wp->w_topline == mod_top)
1379 {
1380 /*
1381 * w_topline is the first changed line, the scrolling will be done
1382 * further down.
1383 */
1384 }
1385 else if (wp->w_lines[0].wl_valid
1386 && (wp->w_topline < wp->w_lines[0].wl_lnum
1387#ifdef FEAT_DIFF
1388 || (wp->w_topline == wp->w_lines[0].wl_lnum
1389 && wp->w_topfill > wp->w_old_topfill)
1390#endif
1391 ))
1392 {
1393 /*
1394 * New topline is above old topline: May scroll down.
1395 */
1396#ifdef FEAT_FOLDING
1397 if (hasAnyFolding(wp))
1398 {
1399 linenr_T ln;
1400
1401 /* count the number of lines we are off, counting a sequence
1402 * of folded lines as one */
1403 j = 0;
1404 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1405 {
1406 ++j;
1407 if (j >= wp->w_height - 2)
1408 break;
1409 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1410 }
1411 }
1412 else
1413#endif
1414 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1415 if (j < wp->w_height - 2) /* not too far off */
1416 {
1417 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1418#ifdef FEAT_DIFF
1419 /* insert extra lines for previously invisible filler lines */
1420 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1421 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1422 - wp->w_old_topfill;
1423#endif
1424 if (i < wp->w_height - 2) /* less than a screen off */
1425 {
1426 /*
1427 * Try to insert the correct number of lines.
1428 * If not the last window, delete the lines at the bottom.
1429 * win_ins_lines may fail when the terminal can't do it.
1430 */
1431 if (i > 0)
1432 check_for_delay(FALSE);
1433 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1434 {
1435 if (wp->w_lines_valid != 0)
1436 {
1437 /* Need to update rows that are new, stop at the
1438 * first one that scrolled down. */
1439 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
1442 /* Move the entries that were scrolled, disable
1443 * the entries for the lines to be redrawn. */
1444 if ((wp->w_lines_valid += j) > wp->w_height)
1445 wp->w_lines_valid = wp->w_height;
1446 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1447 wp->w_lines[idx] = wp->w_lines[idx - j];
1448 while (idx >= 0)
1449 wp->w_lines[idx--].wl_valid = FALSE;
1450 }
1451 }
1452 else
1453 mid_start = 0; /* redraw all lines */
1454 }
1455 else
1456 mid_start = 0; /* redraw all lines */
1457 }
1458 else
1459 mid_start = 0; /* redraw all lines */
1460 }
1461 else
1462 {
1463 /*
1464 * New topline is at or below old topline: May scroll up.
1465 * When topline didn't change, find first entry in w_lines[] that
1466 * needs updating.
1467 */
1468
1469 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1470 j = -1;
1471 row = 0;
1472 for (i = 0; i < wp->w_lines_valid; i++)
1473 {
1474 if (wp->w_lines[i].wl_valid
1475 && wp->w_lines[i].wl_lnum == wp->w_topline)
1476 {
1477 j = i;
1478 break;
1479 }
1480 row += wp->w_lines[i].wl_size;
1481 }
1482 if (j == -1)
1483 {
1484 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1485 * lines */
1486 mid_start = 0;
1487 }
1488 else
1489 {
1490 /*
1491 * Try to delete the correct number of lines.
1492 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1493 */
1494#ifdef FEAT_DIFF
1495 /* If the topline didn't change, delete old filler lines,
1496 * otherwise delete filler lines of the new topline... */
1497 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1498 row += wp->w_old_topfill;
1499 else
1500 row += diff_check_fill(wp, wp->w_topline);
1501 /* ... but don't delete new filler lines. */
1502 row -= wp->w_topfill;
1503#endif
1504 if (row > 0)
1505 {
1506 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001507 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1508 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 bot_start = wp->w_height - row;
1510 else
1511 mid_start = 0; /* redraw all lines */
1512 }
1513 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1514 {
1515 /*
1516 * Skip the lines (below the deleted lines) that are still
1517 * valid and don't need redrawing. Copy their info
1518 * upwards, to compensate for the deleted lines. Set
1519 * bot_start to the first row that needs redrawing.
1520 */
1521 bot_start = 0;
1522 idx = 0;
1523 for (;;)
1524 {
1525 wp->w_lines[idx] = wp->w_lines[j];
1526 /* stop at line that didn't fit, unless it is still
1527 * valid (no lines deleted) */
1528 if (row > 0 && bot_start + row
1529 + (int)wp->w_lines[j].wl_size > wp->w_height)
1530 {
1531 wp->w_lines_valid = idx + 1;
1532 break;
1533 }
1534 bot_start += wp->w_lines[idx++].wl_size;
1535
1536 /* stop at the last valid entry in w_lines[].wl_size */
1537 if (++j >= wp->w_lines_valid)
1538 {
1539 wp->w_lines_valid = idx;
1540 break;
1541 }
1542 }
1543#ifdef FEAT_DIFF
1544 /* Correct the first entry for filler lines at the top
1545 * when it won't get updated below. */
1546 if (wp->w_p_diff && bot_start > 0)
1547 wp->w_lines[0].wl_size =
1548 plines_win_nofill(wp, wp->w_topline, TRUE)
1549 + wp->w_topfill;
1550#endif
1551 }
1552 }
1553 }
1554
1555 /* When starting redraw in the first line, redraw all lines. When
1556 * there is only one window it's probably faster to clear the screen
1557 * first. */
1558 if (mid_start == 0)
1559 {
1560 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001561 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001562 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001563 /* Clear the screen when it was not done by win_del_lines() or
1564 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1565 * then. */
1566 if (screen_cleared != TRUE)
1567 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001568 /* The screen was cleared, redraw the tab pages line. */
1569 if (redraw_tabline)
1570 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001573
1574 /* When win_del_lines() or win_ins_lines() caused the screen to be
1575 * cleared (only happens for the first window) or when screenclear()
1576 * was called directly above, "must_redraw" will have been set to
1577 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1578 if (screen_cleared == TRUE)
1579 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
1581 else
1582 {
1583 /* Not VALID or INVERTED: redraw all lines. */
1584 mid_start = 0;
1585 mid_end = wp->w_height;
1586 }
1587
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001588 if (type == SOME_VALID)
1589 {
1590 /* SOME_VALID: redraw all lines. */
1591 mid_start = 0;
1592 mid_end = wp->w_height;
1593 type = NOT_VALID;
1594 }
1595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 /* check if we are updating or removing the inverted part */
1597 if ((VIsual_active && buf == curwin->w_buffer)
1598 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1599 {
1600 linenr_T from, to;
1601
1602 if (VIsual_active)
1603 {
1604 if (VIsual_active
1605 && (VIsual_mode != wp->w_old_visual_mode
1606 || type == INVERTED_ALL))
1607 {
1608 /*
1609 * If the type of Visual selection changed, redraw the whole
1610 * selection. Also when the ownership of the X selection is
1611 * gained or lost.
1612 */
1613 if (curwin->w_cursor.lnum < VIsual.lnum)
1614 {
1615 from = curwin->w_cursor.lnum;
1616 to = VIsual.lnum;
1617 }
1618 else
1619 {
1620 from = VIsual.lnum;
1621 to = curwin->w_cursor.lnum;
1622 }
1623 /* redraw more when the cursor moved as well */
1624 if (wp->w_old_cursor_lnum < from)
1625 from = wp->w_old_cursor_lnum;
1626 if (wp->w_old_cursor_lnum > to)
1627 to = wp->w_old_cursor_lnum;
1628 if (wp->w_old_visual_lnum < from)
1629 from = wp->w_old_visual_lnum;
1630 if (wp->w_old_visual_lnum > to)
1631 to = wp->w_old_visual_lnum;
1632 }
1633 else
1634 {
1635 /*
1636 * Find the line numbers that need to be updated: The lines
1637 * between the old cursor position and the current cursor
1638 * position. Also check if the Visual position changed.
1639 */
1640 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1641 {
1642 from = curwin->w_cursor.lnum;
1643 to = wp->w_old_cursor_lnum;
1644 }
1645 else
1646 {
1647 from = wp->w_old_cursor_lnum;
1648 to = curwin->w_cursor.lnum;
1649 if (from == 0) /* Visual mode just started */
1650 from = to;
1651 }
1652
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001653 if (VIsual.lnum != wp->w_old_visual_lnum
1654 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 {
1656 if (wp->w_old_visual_lnum < from
1657 && wp->w_old_visual_lnum != 0)
1658 from = wp->w_old_visual_lnum;
1659 if (wp->w_old_visual_lnum > to)
1660 to = wp->w_old_visual_lnum;
1661 if (VIsual.lnum < from)
1662 from = VIsual.lnum;
1663 if (VIsual.lnum > to)
1664 to = VIsual.lnum;
1665 }
1666 }
1667
1668 /*
1669 * If in block mode and changed column or curwin->w_curswant:
1670 * update all lines.
1671 * First compute the actual start and end column.
1672 */
1673 if (VIsual_mode == Ctrl_V)
1674 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001675 colnr_T fromc, toc;
1676#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1677 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaar404406a2014-10-09 13:24:43 +02001679 if (curwin->w_p_lbr)
1680 ve_flags = VE_ALL;
1681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001683#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1684 ve_flags = save_ve_flags;
1685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 ++toc;
1687 if (curwin->w_curswant == MAXCOL)
1688 toc = MAXCOL;
1689
1690 if (fromc != wp->w_old_cursor_fcol
1691 || toc != wp->w_old_cursor_lcol)
1692 {
1693 if (from > VIsual.lnum)
1694 from = VIsual.lnum;
1695 if (to < VIsual.lnum)
1696 to = VIsual.lnum;
1697 }
1698 wp->w_old_cursor_fcol = fromc;
1699 wp->w_old_cursor_lcol = toc;
1700 }
1701 }
1702 else
1703 {
1704 /* Use the line numbers of the old Visual area. */
1705 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1706 {
1707 from = wp->w_old_cursor_lnum;
1708 to = wp->w_old_visual_lnum;
1709 }
1710 else
1711 {
1712 from = wp->w_old_visual_lnum;
1713 to = wp->w_old_cursor_lnum;
1714 }
1715 }
1716
1717 /*
1718 * There is no need to update lines above the top of the window.
1719 */
1720 if (from < wp->w_topline)
1721 from = wp->w_topline;
1722
1723 /*
1724 * If we know the value of w_botline, use it to restrict the update to
1725 * the lines that are visible in the window.
1726 */
1727 if (wp->w_valid & VALID_BOTLINE)
1728 {
1729 if (from >= wp->w_botline)
1730 from = wp->w_botline - 1;
1731 if (to >= wp->w_botline)
1732 to = wp->w_botline - 1;
1733 }
1734
1735 /*
1736 * Find the minimal part to be updated.
1737 * Watch out for scrolling that made entries in w_lines[] invalid.
1738 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1739 * top_end; need to redraw from top_end to the "to" line.
1740 * A middle mouse click with a Visual selection may change the text
1741 * above the Visual area and reset wl_valid, do count these for
1742 * mid_end (in srow).
1743 */
1744 if (mid_start > 0)
1745 {
1746 lnum = wp->w_topline;
1747 idx = 0;
1748 srow = 0;
1749 if (scrolled_down)
1750 mid_start = top_end;
1751 else
1752 mid_start = 0;
1753 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1754 {
1755 if (wp->w_lines[idx].wl_valid)
1756 mid_start += wp->w_lines[idx].wl_size;
1757 else if (!scrolled_down)
1758 srow += wp->w_lines[idx].wl_size;
1759 ++idx;
1760# ifdef FEAT_FOLDING
1761 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1762 lnum = wp->w_lines[idx].wl_lnum;
1763 else
1764# endif
1765 ++lnum;
1766 }
1767 srow += mid_start;
1768 mid_end = wp->w_height;
1769 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1770 {
1771 if (wp->w_lines[idx].wl_valid
1772 && wp->w_lines[idx].wl_lnum >= to + 1)
1773 {
1774 /* Only update until first row of this line */
1775 mid_end = srow;
1776 break;
1777 }
1778 srow += wp->w_lines[idx].wl_size;
1779 }
1780 }
1781 }
1782
1783 if (VIsual_active && buf == curwin->w_buffer)
1784 {
1785 wp->w_old_visual_mode = VIsual_mode;
1786 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1787 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001788 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 wp->w_old_curswant = curwin->w_curswant;
1790 }
1791 else
1792 {
1793 wp->w_old_visual_mode = 0;
1794 wp->w_old_cursor_lnum = 0;
1795 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001796 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
1799#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1800 /* reset got_int, otherwise regexp won't work */
1801 save_got_int = got_int;
1802 got_int = 0;
1803#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001804#ifdef SYN_TIME_LIMIT
1805 /* Set the time limit to 'redrawtime'. */
1806 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001807 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#ifdef FEAT_FOLDING
1810 win_foldinfo.fi_level = 0;
1811#endif
1812
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001813#ifdef FEAT_MENU
1814 /*
1815 * Draw the window toolbar, if there is one.
1816 * TODO: only when needed.
1817 */
1818 if (winbar_height(wp) > 0)
1819 redraw_win_toolbar(wp);
1820#endif
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 /*
1823 * Update all the window rows.
1824 */
1825 idx = 0; /* first entry in w_lines[].wl_size */
1826 row = 0;
1827 srow = 0;
1828 lnum = wp->w_topline; /* first line shown in window */
1829 for (;;)
1830 {
1831 /* stop updating when reached the end of the window (check for _past_
1832 * the end of the window is at the end of the loop) */
1833 if (row == wp->w_height)
1834 {
1835 didline = TRUE;
1836 break;
1837 }
1838
1839 /* stop updating when hit the end of the file */
1840 if (lnum > buf->b_ml.ml_line_count)
1841 {
1842 eof = TRUE;
1843 break;
1844 }
1845
1846 /* Remember the starting row of the line that is going to be dealt
1847 * with. It is used further down when the line doesn't fit. */
1848 srow = row;
1849
1850 /*
1851 * Update a line when it is in an area that needs updating, when it
1852 * has changes or w_lines[idx] is invalid.
1853 * bot_start may be halfway a wrapped line after using
1854 * win_del_lines(), check if the current line includes it.
1855 * When syntax folding is being used, the saved syntax states will
1856 * already have been updated, we can't see where the syntax state is
1857 * the same again, just update until the end of the window.
1858 */
1859 if (row < top_end
1860 || (row >= mid_start && row < mid_end)
1861#ifdef FEAT_SEARCH_EXTRA
1862 || top_to_mod
1863#endif
1864 || idx >= wp->w_lines_valid
1865 || (row + wp->w_lines[idx].wl_size > bot_start)
1866 || (mod_top != 0
1867 && (lnum == mod_top
1868 || (lnum >= mod_top
1869 && (lnum < mod_bot
1870#ifdef FEAT_SYN_HL
1871 || did_update == DID_FOLD
1872 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001873 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 && (
1875# ifdef FEAT_FOLDING
1876 (foldmethodIsSyntax(wp)
1877 && hasAnyFolding(wp)) ||
1878# endif
1879 syntax_check_changed(lnum)))
1880#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001881#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001882 /* match in fixed position might need redraw
1883 * if lines were inserted or deleted */
1884 || (wp->w_match_head != NULL
1885 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 )))))
1888 {
1889#ifdef FEAT_SEARCH_EXTRA
1890 if (lnum == mod_top)
1891 top_to_mod = FALSE;
1892#endif
1893
1894 /*
1895 * When at start of changed lines: May scroll following lines
1896 * up or down to minimize redrawing.
1897 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001898 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 */
1900 if (lnum == mod_top
1901 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001902 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 {
1904 int old_rows = 0;
1905 int new_rows = 0;
1906 int xtra_rows;
1907 linenr_T l;
1908
1909 /* Count the old number of window rows, using w_lines[], which
1910 * should still contain the sizes for the lines as they are
1911 * currently displayed. */
1912 for (i = idx; i < wp->w_lines_valid; ++i)
1913 {
1914 /* Only valid lines have a meaningful wl_lnum. Invalid
1915 * lines are part of the changed area. */
1916 if (wp->w_lines[i].wl_valid
1917 && wp->w_lines[i].wl_lnum == mod_bot)
1918 break;
1919 old_rows += wp->w_lines[i].wl_size;
1920#ifdef FEAT_FOLDING
1921 if (wp->w_lines[i].wl_valid
1922 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1923 {
1924 /* Must have found the last valid entry above mod_bot.
1925 * Add following invalid entries. */
1926 ++i;
1927 while (i < wp->w_lines_valid
1928 && !wp->w_lines[i].wl_valid)
1929 old_rows += wp->w_lines[i++].wl_size;
1930 break;
1931 }
1932#endif
1933 }
1934
1935 if (i >= wp->w_lines_valid)
1936 {
1937 /* We can't find a valid line below the changed lines,
1938 * need to redraw until the end of the window.
1939 * Inserting/deleting lines has no use. */
1940 bot_start = 0;
1941 }
1942 else
1943 {
1944 /* Able to count old number of rows: Count new window
1945 * rows, and may insert/delete lines */
1946 j = idx;
1947 for (l = lnum; l < mod_bot; ++l)
1948 {
1949#ifdef FEAT_FOLDING
1950 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1951 ++new_rows;
1952 else
1953#endif
1954#ifdef FEAT_DIFF
1955 if (l == wp->w_topline)
1956 new_rows += plines_win_nofill(wp, l, TRUE)
1957 + wp->w_topfill;
1958 else
1959#endif
1960 new_rows += plines_win(wp, l, TRUE);
1961 ++j;
1962 if (new_rows > wp->w_height - row - 2)
1963 {
1964 /* it's getting too much, must redraw the rest */
1965 new_rows = 9999;
1966 break;
1967 }
1968 }
1969 xtra_rows = new_rows - old_rows;
1970 if (xtra_rows < 0)
1971 {
1972 /* May scroll text up. If there is not enough
1973 * remaining text or scrolling fails, must redraw the
1974 * rest. If scrolling works, must redraw the text
1975 * below the scrolled text. */
1976 if (row - xtra_rows >= wp->w_height - 2)
1977 mod_bot = MAXLNUM;
1978 else
1979 {
1980 check_for_delay(FALSE);
1981 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001982 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 mod_bot = MAXLNUM;
1984 else
1985 bot_start = wp->w_height + xtra_rows;
1986 }
1987 }
1988 else if (xtra_rows > 0)
1989 {
1990 /* May scroll text down. If there is not enough
1991 * remaining text of scrolling fails, must redraw the
1992 * rest. */
1993 if (row + xtra_rows >= wp->w_height - 2)
1994 mod_bot = MAXLNUM;
1995 else
1996 {
1997 check_for_delay(FALSE);
1998 if (win_ins_lines(wp, row + old_rows,
1999 xtra_rows, FALSE, FALSE) == FAIL)
2000 mod_bot = MAXLNUM;
2001 else if (top_end > row + old_rows)
2002 /* Scrolled the part at the top that requires
2003 * updating down. */
2004 top_end += xtra_rows;
2005 }
2006 }
2007
2008 /* When not updating the rest, may need to move w_lines[]
2009 * entries. */
2010 if (mod_bot != MAXLNUM && i != j)
2011 {
2012 if (j < i)
2013 {
2014 int x = row + new_rows;
2015
2016 /* move entries in w_lines[] upwards */
2017 for (;;)
2018 {
2019 /* stop at last valid entry in w_lines[] */
2020 if (i >= wp->w_lines_valid)
2021 {
2022 wp->w_lines_valid = j;
2023 break;
2024 }
2025 wp->w_lines[j] = wp->w_lines[i];
2026 /* stop at a line that won't fit */
2027 if (x + (int)wp->w_lines[j].wl_size
2028 > wp->w_height)
2029 {
2030 wp->w_lines_valid = j + 1;
2031 break;
2032 }
2033 x += wp->w_lines[j++].wl_size;
2034 ++i;
2035 }
2036 if (bot_start > x)
2037 bot_start = x;
2038 }
2039 else /* j > i */
2040 {
2041 /* move entries in w_lines[] downwards */
2042 j -= i;
2043 wp->w_lines_valid += j;
2044 if (wp->w_lines_valid > wp->w_height)
2045 wp->w_lines_valid = wp->w_height;
2046 for (i = wp->w_lines_valid; i - j >= idx; --i)
2047 wp->w_lines[i] = wp->w_lines[i - j];
2048
2049 /* The w_lines[] entries for inserted lines are
2050 * now invalid, but wl_size may be used above.
2051 * Reset to zero. */
2052 while (i >= idx)
2053 {
2054 wp->w_lines[i].wl_size = 0;
2055 wp->w_lines[i--].wl_valid = FALSE;
2056 }
2057 }
2058 }
2059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 }
2061
2062#ifdef FEAT_FOLDING
2063 /*
2064 * When lines are folded, display one line for all of them.
2065 * Otherwise, display normally (can be several display lines when
2066 * 'wrap' is on).
2067 */
2068 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2069 if (fold_count != 0)
2070 {
2071 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2072 ++row;
2073 --fold_count;
2074 wp->w_lines[idx].wl_folded = TRUE;
2075 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2076# ifdef FEAT_SYN_HL
2077 did_update = DID_FOLD;
2078# endif
2079 }
2080 else
2081#endif
2082 if (idx < wp->w_lines_valid
2083 && wp->w_lines[idx].wl_valid
2084 && wp->w_lines[idx].wl_lnum == lnum
2085 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002086 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 && srow + wp->w_lines[idx].wl_size > wp->w_height
2088#ifdef FEAT_DIFF
2089 && diff_check_fill(wp, lnum) == 0
2090#endif
2091 )
2092 {
2093 /* This line is not going to fit. Don't draw anything here,
2094 * will draw "@ " lines below. */
2095 row = wp->w_height + 1;
2096 }
2097 else
2098 {
2099#ifdef FEAT_SEARCH_EXTRA
2100 prepare_search_hl(wp, lnum);
2101#endif
2102#ifdef FEAT_SYN_HL
2103 /* Let the syntax stuff know we skipped a few lines. */
2104 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002105 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 syntax_end_parsing(syntax_last_parsed + 1);
2107#endif
2108
2109 /*
2110 * Display one line.
2111 */
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002112 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
2114#ifdef FEAT_FOLDING
2115 wp->w_lines[idx].wl_folded = FALSE;
2116 wp->w_lines[idx].wl_lastlnum = lnum;
2117#endif
2118#ifdef FEAT_SYN_HL
2119 did_update = DID_LINE;
2120 syntax_last_parsed = lnum;
2121#endif
2122 }
2123
2124 wp->w_lines[idx].wl_lnum = lnum;
2125 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002126
2127 /* Past end of the window or end of the screen. Note that after
2128 * resizing wp->w_height may be end up too big. That's a problem
2129 * elsewhere, but prevent a crash here. */
2130 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
2132 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002133 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2135 ++idx;
2136 break;
2137 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002138 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 wp->w_lines[idx].wl_size = row - srow;
2140 ++idx;
2141#ifdef FEAT_FOLDING
2142 lnum += fold_count + 1;
2143#else
2144 ++lnum;
2145#endif
2146 }
2147 else
2148 {
2149 /* This line does not need updating, advance to the next one */
2150 row += wp->w_lines[idx++].wl_size;
2151 if (row > wp->w_height) /* past end of screen */
2152 break;
2153#ifdef FEAT_FOLDING
2154 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2155#else
2156 ++lnum;
2157#endif
2158#ifdef FEAT_SYN_HL
2159 did_update = DID_NONE;
2160#endif
2161 }
2162
2163 if (lnum > buf->b_ml.ml_line_count)
2164 {
2165 eof = TRUE;
2166 break;
2167 }
2168 }
2169 /*
2170 * End of loop over all window lines.
2171 */
2172
2173
2174 if (idx > wp->w_lines_valid)
2175 wp->w_lines_valid = idx;
2176
2177#ifdef FEAT_SYN_HL
2178 /*
2179 * Let the syntax stuff know we stop parsing here.
2180 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002181 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 syntax_end_parsing(syntax_last_parsed + 1);
2183#endif
2184
2185 /*
2186 * If we didn't hit the end of the file, and we didn't finish the last
2187 * line we were working on, then the line didn't fit.
2188 */
2189 wp->w_empty_rows = 0;
2190#ifdef FEAT_DIFF
2191 wp->w_filler_rows = 0;
2192#endif
2193 if (!eof && !didline)
2194 {
2195 if (lnum == wp->w_topline)
2196 {
2197 /*
2198 * Single line that does not fit!
2199 * Don't overwrite it, it can be edited.
2200 */
2201 wp->w_botline = lnum + 1;
2202 }
2203#ifdef FEAT_DIFF
2204 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2205 {
2206 /* Window ends in filler lines. */
2207 wp->w_botline = lnum;
2208 wp->w_filler_rows = wp->w_height - srow;
2209 }
2210#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002211 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2212 {
2213 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2214
2215 /*
2216 * Last line isn't finished: Display "@@@" in the last screen line.
2217 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002218 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002219 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002220 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002221 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002222 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002223 set_empty_rows(wp, srow);
2224 wp->w_botline = lnum;
2225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2227 {
2228 /*
2229 * Last line isn't finished: Display "@@@" at the end.
2230 */
2231 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2232 W_WINROW(wp) + wp->w_height,
2233 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002234 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 set_empty_rows(wp, srow);
2236 wp->w_botline = lnum;
2237 }
2238 else
2239 {
2240 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2241 wp->w_botline = lnum;
2242 }
2243 }
2244 else
2245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 if (eof) /* we hit the end of the file */
2248 {
2249 wp->w_botline = buf->b_ml.ml_line_count + 1;
2250#ifdef FEAT_DIFF
2251 j = diff_check_fill(wp, wp->w_botline);
2252 if (j > 0 && !wp->w_botfill)
2253 {
2254 /*
2255 * Display filler lines at the end of the file
2256 */
2257 if (char2cells(fill_diff) > 1)
2258 i = '-';
2259 else
2260 i = fill_diff;
2261 if (row + j > wp->w_height)
2262 j = wp->w_height - row;
2263 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2264 row += j;
2265 }
2266#endif
2267 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002268 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 wp->w_botline = lnum;
2270
2271 /* make sure the rest of the screen is blank */
2272 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002273 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002276#ifdef SYN_TIME_LIMIT
2277 syn_set_timeout(NULL);
2278#endif
2279
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 /* Reset the type of redrawing required, the window has been updated. */
2281 wp->w_redr_type = 0;
2282#ifdef FEAT_DIFF
2283 wp->w_old_topfill = wp->w_topfill;
2284 wp->w_old_botfill = wp->w_botfill;
2285#endif
2286
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002287 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 /*
2290 * There is a trick with w_botline. If we invalidate it on each
2291 * change that might modify it, this will cause a lot of expensive
2292 * calls to plines() in update_topline() each time. Therefore the
2293 * value of w_botline is often approximated, and this value is used to
2294 * compute the value of w_topline. If the value of w_botline was
2295 * wrong, check that the value of w_topline is correct (cursor is on
2296 * the visible part of the text). If it's not, we need to redraw
2297 * again. Mostly this just means scrolling up a few lines, so it
2298 * doesn't look too bad. Only do this for the current window (where
2299 * changes are relevant).
2300 */
2301 wp->w_valid |= VALID_BOTLINE;
2302 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2303 {
2304 recursive = TRUE;
2305 curwin->w_valid &= ~VALID_TOPLINE;
2306 update_topline(); /* may invalidate w_botline again */
2307 if (must_redraw != 0)
2308 {
2309 /* Don't update for changes in buffer again. */
2310 i = curbuf->b_mod_set;
2311 curbuf->b_mod_set = FALSE;
2312 win_update(curwin);
2313 must_redraw = 0;
2314 curbuf->b_mod_set = i;
2315 }
2316 recursive = FALSE;
2317 }
2318 }
2319
2320#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2321 /* restore got_int, unless CTRL-C was hit while redrawing */
2322 if (!got_int)
2323 got_int = save_got_int;
2324#endif
2325}
2326
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327/*
2328 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2329 * as the filler character.
2330 */
2331 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002332win_draw_end(
2333 win_T *wp,
2334 int c1,
2335 int c2,
2336 int row,
2337 int endrow,
2338 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339{
2340#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2341 int n = 0;
2342# define FDC_OFF n
2343#else
2344# define FDC_OFF 0
2345#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002346#ifdef FEAT_FOLDING
2347 int fdc = compute_foldcolumn(wp, 0);
2348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350#ifdef FEAT_RIGHTLEFT
2351 if (wp->w_p_rl)
2352 {
2353 /* No check for cmdline window: should never be right-left. */
2354# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002355 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
2357 if (n > 0)
2358 {
2359 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002360 if (n > wp->w_width)
2361 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2363 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002364 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366# endif
2367# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002368 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
2370 int nn = n + 2;
2371
2372 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002373 if (nn > wp->w_width)
2374 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2376 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002377 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 n = nn;
2379 }
2380# endif
2381 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002382 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002383 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2385 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002386 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 }
2388 else
2389#endif
2390 {
2391#ifdef FEAT_CMDWIN
2392 if (cmdwin_type != 0 && wp == curwin)
2393 {
2394 /* draw the cmdline character in the leftmost column */
2395 n = 1;
2396 if (n > wp->w_width)
2397 n = wp->w_width;
2398 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002399 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002400 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
2402#endif
2403#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002404 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002406 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002409 if (nn > wp->w_width)
2410 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002412 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002413 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 n = nn;
2415 }
2416#endif
2417#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002418 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
2420 int nn = n + 2;
2421
2422 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002423 if (nn > wp->w_width)
2424 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002426 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002427 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 n = nn;
2429 }
2430#endif
2431 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002432 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002433 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435 set_empty_rows(wp, row);
2436}
2437
Bram Moolenaar1a384422010-07-14 19:53:30 +02002438#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002439static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002440
2441/*
2442 * Advance **color_cols and return TRUE when there are columns to draw.
2443 */
2444 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002445advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002446{
2447 while (**color_cols >= 0 && vcol > **color_cols)
2448 ++*color_cols;
2449 return (**color_cols >= 0);
2450}
2451#endif
2452
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002453#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2454/*
2455 * Copy "text" to ScreenLines using "attr".
2456 * Returns the next screen column.
2457 */
2458 static int
2459text_to_screenline(win_T *wp, char_u *text, int col)
2460{
2461 int off = (int)(current_ScreenLine - ScreenLines);
2462
2463#ifdef FEAT_MBYTE
2464 if (has_mbyte)
2465 {
2466 int cells;
2467 int u8c, u8cc[MAX_MCO];
2468 int i;
2469 int idx;
2470 int c_len;
2471 char_u *p;
2472# ifdef FEAT_ARABIC
2473 int prev_c = 0; /* previous Arabic character */
2474 int prev_c1 = 0; /* first composing char for prev_c */
2475# endif
2476
2477# ifdef FEAT_RIGHTLEFT
2478 if (wp->w_p_rl)
2479 idx = off;
2480 else
2481# endif
2482 idx = off + col;
2483
2484 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2485 for (p = text; *p != NUL; )
2486 {
2487 cells = (*mb_ptr2cells)(p);
2488 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002489 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002490# ifdef FEAT_RIGHTLEFT
2491 - (wp->w_p_rl ? col : 0)
2492# endif
2493 )
2494 break;
2495 ScreenLines[idx] = *p;
2496 if (enc_utf8)
2497 {
2498 u8c = utfc_ptr2char(p, u8cc);
2499 if (*p < 0x80 && u8cc[0] == 0)
2500 {
2501 ScreenLinesUC[idx] = 0;
2502#ifdef FEAT_ARABIC
2503 prev_c = u8c;
2504#endif
2505 }
2506 else
2507 {
2508#ifdef FEAT_ARABIC
2509 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2510 {
2511 /* Do Arabic shaping. */
2512 int pc, pc1, nc;
2513 int pcc[MAX_MCO];
2514 int firstbyte = *p;
2515
2516 /* The idea of what is the previous and next
2517 * character depends on 'rightleft'. */
2518 if (wp->w_p_rl)
2519 {
2520 pc = prev_c;
2521 pc1 = prev_c1;
2522 nc = utf_ptr2char(p + c_len);
2523 prev_c1 = u8cc[0];
2524 }
2525 else
2526 {
2527 pc = utfc_ptr2char(p + c_len, pcc);
2528 nc = prev_c;
2529 pc1 = pcc[0];
2530 }
2531 prev_c = u8c;
2532
2533 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2534 pc, pc1, nc);
2535 ScreenLines[idx] = firstbyte;
2536 }
2537 else
2538 prev_c = u8c;
2539#endif
2540 /* Non-BMP character: display as ? or fullwidth ?. */
2541#ifdef UNICODE16
2542 if (u8c >= 0x10000)
2543 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2544 else
2545#endif
2546 ScreenLinesUC[idx] = u8c;
2547 for (i = 0; i < Screen_mco; ++i)
2548 {
2549 ScreenLinesC[i][idx] = u8cc[i];
2550 if (u8cc[i] == 0)
2551 break;
2552 }
2553 }
2554 if (cells > 1)
2555 ScreenLines[idx + 1] = 0;
2556 }
2557 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2558 /* double-byte single width character */
2559 ScreenLines2[idx] = p[1];
2560 else if (cells > 1)
2561 /* double-width character */
2562 ScreenLines[idx + 1] = p[1];
2563 col += cells;
2564 idx += cells;
2565 p += c_len;
2566 }
2567 }
2568 else
2569#endif
2570 {
2571 int len = (int)STRLEN(text);
2572
Bram Moolenaar02631462017-09-22 15:20:32 +02002573 if (len > wp->w_width - col)
2574 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002575 if (len > 0)
2576 {
2577#ifdef FEAT_RIGHTLEFT
2578 if (wp->w_p_rl)
2579 STRNCPY(current_ScreenLine, text, len);
2580 else
2581#endif
2582 STRNCPY(current_ScreenLine + col, text, len);
2583 col += len;
2584 }
2585 }
2586 return col;
2587}
2588#endif
2589
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590#ifdef FEAT_FOLDING
2591/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002592 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2593 * space is available for window "wp", minus "col".
2594 */
2595 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002596compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002597{
2598 int fdc = wp->w_p_fdc;
2599 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002600 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002601
2602 if (fdc > wwidth - (col + wmw))
2603 fdc = wwidth - (col + wmw);
2604 return fdc;
2605}
2606
2607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 * Display one folded line.
2609 */
2610 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002611fold_line(
2612 win_T *wp,
2613 long fold_count,
2614 foldinfo_T *foldinfo,
2615 linenr_T lnum,
2616 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002618 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 pos_T *top, *bot;
2620 linenr_T lnume = lnum + fold_count - 1;
2621 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002622 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 int col;
2625 int txtcol;
2626 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002627 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
2629 /* Build the fold line:
2630 * 1. Add the cmdwin_type for the command-line window
2631 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002632 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 * 4. Compose the text
2634 * 5. Add the text
2635 * 6. set highlighting for the Visual area an other text
2636 */
2637 col = 0;
2638
2639 /*
2640 * 1. Add the cmdwin_type for the command-line window
2641 * Ignores 'rightleft', this window is never right-left.
2642 */
2643#ifdef FEAT_CMDWIN
2644 if (cmdwin_type != 0 && wp == curwin)
2645 {
2646 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002647 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648#ifdef FEAT_MBYTE
2649 if (enc_utf8)
2650 ScreenLinesUC[off] = 0;
2651#endif
2652 ++col;
2653 }
2654#endif
2655
2656 /*
2657 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002658 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002660 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (fdc > 0)
2662 {
2663 fill_foldcolumn(buf, wp, TRUE, lnum);
2664#ifdef FEAT_RIGHTLEFT
2665 if (wp->w_p_rl)
2666 {
2667 int i;
2668
Bram Moolenaar02631462017-09-22 15:20:32 +02002669 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002670 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 /* reverse the fold column */
2672 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002673 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 }
2675 else
2676#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002677 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 col += fdc;
2679 }
2680
2681#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002682# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2683 for (ri = 0; ri < l; ++ri) \
Bram Moolenaar02631462017-09-22 15:20:32 +02002684 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002685 else \
2686 for (ri = 0; ri < l; ++ri) \
2687 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002689# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2690 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691#endif
2692
Bram Moolenaar64486672010-05-16 15:46:46 +02002693 /* Set all attributes of the 'number' or 'relativenumber' column and the
2694 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002695 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697#ifdef FEAT_SIGNS
2698 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002699 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002701 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (len > 0)
2703 {
2704 if (len > 2)
2705 len = 2;
2706# ifdef FEAT_RIGHTLEFT
2707 if (wp->w_p_rl)
2708 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002709 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002710 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 else
2712# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002713 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 col += len;
2715 }
2716 }
2717#endif
2718
2719 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002720 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002722 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002724 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (len > 0)
2726 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002727 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002728 long num;
2729 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002730
2731 if (len > w + 1)
2732 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002733
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002734 if (wp->w_p_nu && !wp->w_p_rnu)
2735 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002736 num = (long)lnum;
2737 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002738 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002739 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002740 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002741 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002742 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002743 /* 'number' + 'relativenumber': cursor line shows absolute
2744 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002745 num = lnum;
2746 fmt = "%-*ld ";
2747 }
2748 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002749
Bram Moolenaar700e7342013-01-30 12:31:36 +01002750 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#ifdef FEAT_RIGHTLEFT
2752 if (wp->w_p_rl)
2753 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002754 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002755 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 else
2757#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002758 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 col += len;
2760 }
2761 }
2762
2763 /*
2764 * 4. Compose the folded-line string with 'foldtext', if set.
2765 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002766 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
2768 txtcol = col; /* remember where text starts */
2769
2770 /*
2771 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2772 * Right-left text is put in columns 0 - number-col, normal text is put
2773 * in columns number-col - window-width.
2774 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002775 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776
2777 /* Fill the rest of the line with the fold filler */
2778#ifdef FEAT_RIGHTLEFT
2779 if (wp->w_p_rl)
2780 col -= txtcol;
2781#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002782 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783#ifdef FEAT_RIGHTLEFT
2784 - (wp->w_p_rl ? txtcol : 0)
2785#endif
2786 )
2787 {
2788#ifdef FEAT_MBYTE
2789 if (enc_utf8)
2790 {
2791 if (fill_fold >= 0x80)
2792 {
2793 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002794 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002795 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
2797 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002800 ScreenLines[off + col] = fill_fold;
2801 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002802 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002804 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002806 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
2808
2809 if (text != buf)
2810 vim_free(text);
2811
2812 /*
2813 * 6. set highlighting for the Visual area an other text.
2814 * If all folded lines are in the Visual area, highlight the line.
2815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2817 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002818 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 {
2820 /* Visual is after curwin->w_cursor */
2821 top = &curwin->w_cursor;
2822 bot = &VIsual;
2823 }
2824 else
2825 {
2826 /* Visual is before curwin->w_cursor */
2827 top = &VIsual;
2828 bot = &curwin->w_cursor;
2829 }
2830 if (lnum >= top->lnum
2831 && lnume <= bot->lnum
2832 && (VIsual_mode != 'v'
2833 || ((lnum > top->lnum
2834 || (lnum == top->lnum
2835 && top->col == 0))
2836 && (lnume < bot->lnum
2837 || (lnume == bot->lnum
2838 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002839 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {
2841 if (VIsual_mode == Ctrl_V)
2842 {
2843 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002844 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002846 if (wp->w_old_cursor_lcol != MAXCOL
2847 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002848 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 len = wp->w_old_cursor_lcol;
2850 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002851 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002852 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002853 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 }
2855 }
2856 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002859 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
2862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002864#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002865 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2866 if (wp->w_p_cc_cols)
2867 {
2868 int i = 0;
2869 int j = wp->w_p_cc_cols[i];
2870 int old_txtcol = txtcol;
2871
2872 while (j > -1)
2873 {
2874 txtcol += j;
2875 if (wp->w_p_wrap)
2876 txtcol -= wp->w_skipcol;
2877 else
2878 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002879 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002880 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002881 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002882 txtcol = old_txtcol;
2883 j = wp->w_p_cc_cols[++i];
2884 }
2885 }
2886
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002887 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002888 if (wp->w_p_cuc)
2889 {
2890 txtcol += wp->w_virtcol;
2891 if (wp->w_p_wrap)
2892 txtcol -= wp->w_skipcol;
2893 else
2894 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002895 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002896 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002897 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002898 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900
Bram Moolenaar02631462017-09-22 15:20:32 +02002901 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2902 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
2904 /*
2905 * Update w_cline_height and w_cline_folded if the cursor line was
2906 * updated (saves a call to plines() later).
2907 */
2908 if (wp == curwin
2909 && lnum <= curwin->w_cursor.lnum
2910 && lnume >= curwin->w_cursor.lnum)
2911 {
2912 curwin->w_cline_row = row;
2913 curwin->w_cline_height = 1;
2914 curwin->w_cline_folded = TRUE;
2915 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2916 }
2917}
2918
2919/*
2920 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2921 */
2922 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002923copy_text_attr(
2924 int off,
2925 char_u *buf,
2926 int len,
2927 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002929 int i;
2930
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 mch_memmove(ScreenLines + off, buf, (size_t)len);
2932# ifdef FEAT_MBYTE
2933 if (enc_utf8)
2934 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2935# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002936 for (i = 0; i < len; ++i)
2937 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938}
2939
2940/*
2941 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002942 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 */
2944 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002945fill_foldcolumn(
2946 char_u *p,
2947 win_T *wp,
2948 int closed, /* TRUE of FALSE */
2949 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 int i = 0;
2952 int level;
2953 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002954 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002955 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
2957 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002958 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
2960 level = win_foldinfo.fi_level;
2961 if (level > 0)
2962 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002963 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002964 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002965
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 /* If the column is too narrow, we start at the lowest level that
2967 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002968 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 if (first_level < 1)
2970 first_level = 1;
2971
Bram Moolenaar1c934292015-01-27 16:39:29 +01002972 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 {
2974 if (win_foldinfo.fi_lnum == lnum
2975 && first_level + i >= win_foldinfo.fi_low_level)
2976 p[i] = '-';
2977 else if (first_level == 1)
2978 p[i] = '|';
2979 else if (first_level + i <= 9)
2980 p[i] = '0' + first_level + i;
2981 else
2982 p[i] = '>';
2983 if (first_level + i == level)
2984 break;
2985 }
2986 }
2987 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002988 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989}
2990#endif /* FEAT_FOLDING */
2991
2992/*
2993 * Display line "lnum" of window 'wp' on the screen.
2994 * Start at row "startrow", stop when "endrow" is reached.
2995 * wp->w_virtcol needs to be valid.
2996 *
2997 * Return the number of last row the line occupies.
2998 */
2999 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003000win_line(
3001 win_T *wp,
3002 linenr_T lnum,
3003 int startrow,
3004 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003005 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003007 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3009 int c = 0; /* init for GCC */
3010 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003011#ifdef FEAT_LINEBREAK
3012 long vcol_sbr = -1; /* virtual column after showbreak */
3013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 long vcol_prev = -1; /* "vcol" of previous character */
3015 char_u *line; /* current line */
3016 char_u *ptr; /* current position in "line" */
3017 int row; /* row in the window, excl w_winrow */
3018 int screen_row; /* row on the screen, incl w_winrow */
3019
3020 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3021 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003022 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003023 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 int c_extra = NUL; /* extra chars, all the same */
3025 int extra_attr = 0; /* attributes when n_extra != 0 */
3026 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3027 displaying lcs_eol at end-of-line */
3028 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3029 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3030
3031 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3032 int saved_n_extra = 0;
3033 char_u *saved_p_extra = NULL;
3034 int saved_c_extra = 0;
3035 int saved_char_attr = 0;
3036
3037 int n_attr = 0; /* chars with special attr */
3038 int saved_attr2 = 0; /* char_attr saved for n_attr */
3039 int n_attr3 = 0; /* chars with overruling special attr */
3040 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3041
3042 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3043
3044 int fromcol, tocol; /* start/end of inverting */
3045 int fromcol_prev = -2; /* start of inverting after cursor */
3046 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003048 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 pos_T pos;
3050 long v;
3051
3052 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003053 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3055 in this line */
3056 int attr = 0; /* attributes for area highlighting */
3057 int area_attr = 0; /* attributes desired by highlighting */
3058 int search_attr = 0; /* attributes desired by 'hlsearch' */
3059#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003060 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 int syntax_attr = 0; /* attributes desired by syntax */
3062 int has_syntax = FALSE; /* this buffer has syntax highl. */
3063 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003064 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003065 int draw_color_col = FALSE; /* highlight colorcolumn */
3066 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003067#endif
3068#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003069 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003070# define SPWORDLEN 150
3071 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003072 int nextlinecol = 0; /* column where nextline[] starts */
3073 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003074 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003075 int spell_attr = 0; /* attributes desired by spelling */
3076 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003077 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3078 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003079 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003080 static int cap_col = -1; /* column to check for Cap word */
3081 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003082 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083#endif
3084 int extra_check; /* has syntax or linebreak */
3085#ifdef FEAT_MBYTE
3086 int multi_attr = 0; /* attributes desired by multibyte */
3087 int mb_l = 1; /* multi-byte byte length */
3088 int mb_c = 0; /* decoded multi-byte character */
3089 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003090 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091#endif
3092#ifdef FEAT_DIFF
3093 int filler_lines; /* nr of filler lines to be drawn */
3094 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003095 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 int change_start = MAXCOL; /* first col of changed area */
3097 int change_end = -1; /* last col of changed area */
3098#endif
3099 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3100#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003101 int need_showbreak = FALSE; /* overlong line, skipping first x
3102 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003104#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003105 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003107 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108#endif
3109#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003110 matchitem_T *cur; /* points to the match list */
3111 match_T *shl; /* points to search_hl or a match */
3112 int shl_flag; /* flag to indicate whether search_hl
3113 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003114 int pos_inprogress; /* marks that position match search is
3115 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003116 int prevcol_hl_flag; /* flag to indicate whether prevcol
3117 equals startcol of search_hl or one
3118 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119#endif
3120#ifdef FEAT_ARABIC
3121 int prev_c = 0; /* previous Arabic character */
3122 int prev_c1 = 0; /* first composing char for prev_c */
3123#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003124#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003125 int did_line_attr = 0;
3126#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003127#ifdef FEAT_TERMINAL
3128 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003129 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
3132 /* draw_state: items that are drawn in sequence: */
3133#define WL_START 0 /* nothing done yet */
3134#ifdef FEAT_CMDWIN
3135# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3136#else
3137# define WL_CMDLINE WL_START
3138#endif
3139#ifdef FEAT_FOLDING
3140# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3141#else
3142# define WL_FOLD WL_CMDLINE
3143#endif
3144#ifdef FEAT_SIGNS
3145# define WL_SIGN WL_FOLD + 1 /* column for signs */
3146#else
3147# define WL_SIGN WL_FOLD /* column for signs */
3148#endif
3149#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003150#ifdef FEAT_LINEBREAK
3151# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003153# define WL_BRI WL_NR
3154#endif
3155#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3156# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3157#else
3158# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159#endif
3160#define WL_LINE WL_SBR + 1 /* text in the line */
3161 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003162#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 int feedback_col = 0;
3164 int feedback_old_attr = -1;
3165#endif
3166
Bram Moolenaar860cae12010-06-05 23:22:07 +02003167#ifdef FEAT_CONCEAL
3168 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003169 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003170 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003171 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003172 int is_concealing = FALSE;
3173 int boguscols = 0; /* nonexistent columns added to force
3174 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003175 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003176 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003177 int match_conc = 0; /* cchar for match functions */
3178 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003179 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003180# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003181# define FIX_FOR_BOGUSCOLS \
3182 { \
3183 n_extra += vcol_off; \
3184 vcol -= vcol_off; \
3185 vcol_off = 0; \
3186 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003187 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003188 boguscols = 0; \
3189 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003190#else
3191# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
3194 if (startrow > endrow) /* past the end already! */
3195 return startrow;
3196
3197 row = startrow;
3198 screen_row = row + W_WINROW(wp);
3199
3200 /*
3201 * To speed up the loop below, set extra_check when there is linebreak,
3202 * trailing white space and/or syntax processing to be done.
3203 */
3204#ifdef FEAT_LINEBREAK
3205 extra_check = wp->w_p_lbr;
3206#else
3207 extra_check = 0;
3208#endif
3209#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003210 if (syntax_present(wp) && !wp->w_s->b_syn_error
3211# ifdef SYN_TIME_LIMIT
3212 && !wp->w_s->b_syn_slow
3213# endif
3214 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
3216 /* Prepare for syntax highlighting in this line. When there is an
3217 * error, stop syntax highlighting. */
3218 save_did_emsg = did_emsg;
3219 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003220 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003222 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 else
3224 {
3225 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003226#ifdef SYN_TIME_LIMIT
3227 if (!wp->w_s->b_syn_slow)
3228#endif
3229 {
3230 has_syntax = TRUE;
3231 extra_check = TRUE;
3232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
3234 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003235
3236 /* Check for columns to display for 'colorcolumn'. */
3237 color_cols = wp->w_p_cc_cols;
3238 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003239 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003240#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003241
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003242#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003243 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003244 {
3245 extra_check = TRUE;
3246 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003247 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003248 }
3249#endif
3250
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003251#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003252 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003253 && *wp->w_s->b_p_spl != NUL
3254 && wp->w_s->b_langp.ga_len > 0
3255 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003256 {
3257 /* Prepare for spell checking. */
3258 has_spell = TRUE;
3259 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003260
3261 /* Get the start of the next line, so that words that wrap to the next
3262 * line are found too: "et<line-break>al.".
3263 * Trick: skip a few chars for C/shell/Vim comments */
3264 nextline[SPWORDLEN] = NUL;
3265 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3266 {
3267 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3268 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3269 }
3270
3271 /* When a word wrapped from the previous line the start of the current
3272 * line is valid. */
3273 if (lnum == checked_lnum)
3274 cur_checked_col = checked_col;
3275 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003276
3277 /* When there was a sentence end in the previous line may require a
3278 * word starting with capital in this line. In line 1 always check
3279 * the first word. */
3280 if (lnum != capcol_lnum)
3281 cap_col = -1;
3282 if (lnum == 1)
3283 cap_col = 0;
3284 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286#endif
3287
3288 /*
3289 * handle visual active in this window
3290 */
3291 fromcol = -10;
3292 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3294 {
3295 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003296 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
3298 top = &curwin->w_cursor;
3299 bot = &VIsual;
3300 }
3301 else /* Visual is before curwin->w_cursor */
3302 {
3303 top = &VIsual;
3304 bot = &curwin->w_cursor;
3305 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003306 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 if (VIsual_mode == Ctrl_V) /* block mode */
3308 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003309 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
3311 fromcol = wp->w_old_cursor_fcol;
3312 tocol = wp->w_old_cursor_lcol;
3313 }
3314 }
3315 else /* non-block mode */
3316 {
3317 if (lnum > top->lnum && lnum <= bot->lnum)
3318 fromcol = 0;
3319 else if (lnum == top->lnum)
3320 {
3321 if (VIsual_mode == 'V') /* linewise */
3322 fromcol = 0;
3323 else
3324 {
3325 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3326 if (gchar_pos(top) == NUL)
3327 tocol = fromcol + 1;
3328 }
3329 }
3330 if (VIsual_mode != 'V' && lnum == bot->lnum)
3331 {
3332 if (*p_sel == 'e' && bot->col == 0
3333#ifdef FEAT_VIRTUALEDIT
3334 && bot->coladd == 0
3335#endif
3336 )
3337 {
3338 fromcol = -10;
3339 tocol = MAXCOL;
3340 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003341 else if (bot->col == MAXCOL)
3342 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 else
3344 {
3345 pos = *bot;
3346 if (*p_sel == 'e')
3347 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3348 else
3349 {
3350 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3351 ++tocol;
3352 }
3353 }
3354 }
3355 }
3356
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 /* Check if the character under the cursor should not be inverted */
3358 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003359#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 )
3363 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
3365 /* if inverting in this line set area_highlighting */
3366 if (fromcol >= 0)
3367 {
3368 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003369 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003371 if ((clip_star.available && !clip_star.owned
3372 && clip_isautosel_star())
3373 || (clip_plus.available && !clip_plus.owned
3374 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003375 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376#endif
3377 }
3378 }
3379
3380 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003381 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003383 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 && wp == curwin
3385 && lnum >= curwin->w_cursor.lnum
3386 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3387 {
3388 if (lnum == curwin->w_cursor.lnum)
3389 getvcol(curwin, &(curwin->w_cursor),
3390 (colnr_T *)&fromcol, NULL, NULL);
3391 else
3392 fromcol = 0;
3393 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3394 {
3395 pos.lnum = lnum;
3396 pos.col = search_match_endcol;
3397 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3398 }
3399 else
3400 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003401 /* do at least one character; happens when past end of line */
3402 if (fromcol == tocol)
3403 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003405 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
3407
3408#ifdef FEAT_DIFF
3409 filler_lines = diff_check(wp, lnum);
3410 if (filler_lines < 0)
3411 {
3412 if (filler_lines == -1)
3413 {
3414 if (diff_find_change(wp, lnum, &change_start, &change_end))
3415 diff_hlf = HLF_ADD; /* added line */
3416 else if (change_start == 0)
3417 diff_hlf = HLF_TXD; /* changed text */
3418 else
3419 diff_hlf = HLF_CHD; /* changed line */
3420 }
3421 else
3422 diff_hlf = HLF_ADD; /* added line */
3423 filler_lines = 0;
3424 area_highlighting = TRUE;
3425 }
3426 if (lnum == wp->w_topline)
3427 filler_lines = wp->w_topfill;
3428 filler_todo = filler_lines;
3429#endif
3430
3431#ifdef LINE_ATTR
3432# ifdef FEAT_SIGNS
3433 /* If this line has a sign with line highlighting set line_attr. */
3434 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3435 if (v != 0)
3436 line_attr = sign_get_attr((int)v, TRUE);
3437# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003438# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003440 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003441 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442# endif
3443 if (line_attr != 0)
3444 area_highlighting = TRUE;
3445#endif
3446
3447 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3448 ptr = line;
3449
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003450#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003451 if (has_spell)
3452 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003453 /* For checking first word with a capital skip white space. */
3454 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003455 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003456
Bram Moolenaar30abd282005-06-22 22:35:10 +00003457 /* To be able to spell-check over line boundaries copy the end of the
3458 * current line into nextline[]. Above the start of the next line was
3459 * copied to nextline[SPWORDLEN]. */
3460 if (nextline[SPWORDLEN] == NUL)
3461 {
3462 /* No next line or it is empty. */
3463 nextlinecol = MAXCOL;
3464 nextline_idx = 0;
3465 }
3466 else
3467 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003468 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003469 if (v < SPWORDLEN)
3470 {
3471 /* Short line, use it completely and append the start of the
3472 * next line. */
3473 nextlinecol = 0;
3474 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003475 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003476 nextline_idx = v + 1;
3477 }
3478 else
3479 {
3480 /* Long line, use only the last SPWORDLEN bytes. */
3481 nextlinecol = v - SPWORDLEN;
3482 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3483 nextline_idx = SPWORDLEN + 1;
3484 }
3485 }
3486 }
3487#endif
3488
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003489 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003491 if (lcs_space || lcs_trail)
3492 extra_check = TRUE;
3493 /* find start of trailing whitespace */
3494 if (lcs_trail)
3495 {
3496 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003497 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003498 --trailcol;
3499 trailcol += (colnr_T) (ptr - line);
3500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502
3503 /*
3504 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3505 * first character to be displayed.
3506 */
3507 if (wp->w_p_wrap)
3508 v = wp->w_skipcol;
3509 else
3510 v = wp->w_leftcol;
3511 if (v > 0)
3512 {
3513#ifdef FEAT_MBYTE
3514 char_u *prev_ptr = ptr;
3515#endif
3516 while (vcol < v && *ptr != NUL)
3517 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003518 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 vcol += c;
3520#ifdef FEAT_MBYTE
3521 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003523 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
3525
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003526 /* When:
3527 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003528 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003529 * - 'virtualedit' is set, or
3530 * - the visual mode is active,
3531 * the end of the line may be before the start of the displayed part.
3532 */
3533 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003534#ifdef FEAT_SYN_HL
3535 wp->w_p_cuc || draw_color_col ||
3536#endif
3537#ifdef FEAT_VIRTUALEDIT
3538 virtual_active() ||
3539#endif
3540 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003541 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
3545 /* Handle a character that's not completely on the screen: Put ptr at
3546 * that character but skip the first few screen characters. */
3547 if (vcol > v)
3548 {
3549 vcol -= c;
3550#ifdef FEAT_MBYTE
3551 ptr = prev_ptr;
3552#else
3553 --ptr;
3554#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003555 /* If the character fits on the screen, don't need to skip it.
3556 * Except for a TAB. */
3557 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003558#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003559 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003560#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003561 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003562 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
3564
3565 /*
3566 * Adjust for when the inverted text is before the screen,
3567 * and when the start of the inverted text is before the screen.
3568 */
3569 if (tocol <= vcol)
3570 fromcol = 0;
3571 else if (fromcol >= 0 && fromcol < vcol)
3572 fromcol = vcol;
3573
3574#ifdef FEAT_LINEBREAK
3575 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3576 if (wp->w_p_wrap)
3577 need_showbreak = TRUE;
3578#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003579#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003580 /* When spell checking a word we need to figure out the start of the
3581 * word and if it's badly spelled or not. */
3582 if (has_spell)
3583 {
3584 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003585 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003586 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003587
3588 pos = wp->w_cursor;
3589 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003590 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003591 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003592
3593 /* spell_move_to() may call ml_get() and make "line" invalid */
3594 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3595 ptr = line + linecol;
3596
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003597 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003598 {
3599 /* no bad word found at line start, don't check until end of a
3600 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003601 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003602 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003603 }
3604 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003605 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003606 /* bad word found, use attributes until end of word */
3607 word_end = wp->w_cursor.col + len + 1;
3608
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003609 /* Turn index into actual attributes. */
3610 if (spell_hlf != HLF_COUNT)
3611 spell_attr = highlight_attr[spell_hlf];
3612 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003613 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003614
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003615# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003616 /* Need to restart syntax highlighting for this line. */
3617 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003618 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003619# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003620 }
3621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
3623
3624 /*
3625 * Correct highlighting for cursor that can't be disabled.
3626 * Avoids having to check this for each character.
3627 */
3628 if (fromcol >= 0)
3629 {
3630 if (noinvcur)
3631 {
3632 if ((colnr_T)fromcol == wp->w_virtcol)
3633 {
3634 /* highlighting starts at cursor, let it start just after the
3635 * cursor */
3636 fromcol_prev = fromcol;
3637 fromcol = -1;
3638 }
3639 else if ((colnr_T)fromcol < wp->w_virtcol)
3640 /* restart highlighting after the cursor */
3641 fromcol_prev = wp->w_virtcol;
3642 }
3643 if (fromcol >= tocol)
3644 fromcol = -1;
3645 }
3646
3647#ifdef FEAT_SEARCH_EXTRA
3648 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003649 * Handle highlighting the last used search pattern and matches.
3650 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003652 cur = wp->w_match_head;
3653 shl_flag = FALSE;
3654 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003656 if (shl_flag == FALSE)
3657 {
3658 shl = &search_hl;
3659 shl_flag = TRUE;
3660 }
3661 else
3662 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003663 shl->startcol = MAXCOL;
3664 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003666 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003667 v = (long)(ptr - line);
3668 if (cur != NULL)
3669 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003670 next_search_hl(wp, shl, lnum, (colnr_T)v,
3671 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003672
3673 /* Need to get the line again, a multi-line regexp may have made it
3674 * invalid. */
3675 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3676 ptr = line + v;
3677
3678 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003680 if (shl->lnum == lnum)
3681 shl->startcol = shl->rm.startpos[0].col;
3682 else
3683 shl->startcol = 0;
3684 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3685 - shl->rm.startpos[0].lnum)
3686 shl->endcol = shl->rm.endpos[0].col;
3687 else
3688 shl->endcol = MAXCOL;
3689 /* Highlight one character for an empty match. */
3690 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003693 if (has_mbyte && line[shl->endcol] != NUL)
3694 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3695 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003697 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003699 if ((long)shl->startcol < v) /* match at leftcol */
3700 {
3701 shl->attr_cur = shl->attr;
3702 search_attr = shl->attr;
3703 }
3704 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003706 if (shl != &search_hl && cur != NULL)
3707 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709#endif
3710
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003711#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003712 /* Cursor line highlighting for 'cursorline' in the current window. Not
3713 * when Visual mode is active, because it's not clear what is selected
3714 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003715 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003716 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003717 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003718 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003719 area_highlighting = TRUE;
3720 }
3721#endif
3722
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003723 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 col = 0;
3725#ifdef FEAT_RIGHTLEFT
3726 if (wp->w_p_rl)
3727 {
3728 /* Rightleft window: process the text in the normal direction, but put
3729 * it in current_ScreenLine[] from right to left. Start at the
3730 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003731 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 off += col;
3733 }
3734#endif
3735
3736 /*
3737 * Repeat for the whole displayed line.
3738 */
3739 for (;;)
3740 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003741#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003742 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 /* Skip this quickly when working on the text. */
3745 if (draw_state != WL_LINE)
3746 {
3747#ifdef FEAT_CMDWIN
3748 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3749 {
3750 draw_state = WL_CMDLINE;
3751 if (cmdwin_type != 0 && wp == curwin)
3752 {
3753 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003755 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003756 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
3758 }
3759#endif
3760
3761#ifdef FEAT_FOLDING
3762 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3763 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003764 int fdc = compute_foldcolumn(wp, 0);
3765
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003767 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003769 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003770 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003771 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003772 p_extra_free = alloc(12 + 1);
3773
3774 if (p_extra_free != NULL)
3775 {
3776 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3777 n_extra = fdc;
3778 p_extra_free[n_extra] = NUL;
3779 p_extra = p_extra_free;
3780 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003781 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784 }
3785#endif
3786
3787#ifdef FEAT_SIGNS
3788 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3789 {
3790 draw_state = WL_SIGN;
3791 /* Show the sign column when there are any signs in this
3792 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003793 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003795 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003797 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798# endif
3799
3800 /* Draw two cells with the sign value or blank. */
3801 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003802 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 n_extra = 2;
3804
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003805 if (row == startrow
3806#ifdef FEAT_DIFF
3807 + filler_lines && filler_todo <= 0
3808#endif
3809 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 {
3811 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3812 SIGN_TEXT);
3813# ifdef FEAT_SIGN_ICONS
3814 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3815 SIGN_ICON);
3816 if (gui.in_use && icon_sign != 0)
3817 {
3818 /* Use the image in this position. */
3819 c_extra = SIGN_BYTE;
3820# ifdef FEAT_NETBEANS_INTG
3821 if (buf_signcount(wp->w_buffer, lnum) > 1)
3822 c_extra = MULTISIGN_BYTE;
3823# endif
3824 char_attr = icon_sign;
3825 }
3826 else
3827# endif
3828 if (text_sign != 0)
3829 {
3830 p_extra = sign_get_text(text_sign);
3831 if (p_extra != NULL)
3832 {
3833 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003834 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836 char_attr = sign_get_attr(text_sign, FALSE);
3837 }
3838 }
3839 }
3840 }
3841#endif
3842
3843 if (draw_state == WL_NR - 1 && n_extra == 0)
3844 {
3845 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003846 /* Display the absolute or relative line number. After the
3847 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3848 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 && (row == startrow
3850#ifdef FEAT_DIFF
3851 + filler_lines
3852#endif
3853 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3854 {
3855 /* Draw the line number (empty space after wrapping). */
3856 if (row == startrow
3857#ifdef FEAT_DIFF
3858 + filler_lines
3859#endif
3860 )
3861 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003862 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003863 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003864
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003865 if (wp->w_p_nu && !wp->w_p_rnu)
3866 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003867 num = (long)lnum;
3868 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003869 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003870 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003871 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003872 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003873 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003874 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003875 num = lnum;
3876 fmt = "%-*ld ";
3877 }
3878 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003879
Bram Moolenaar700e7342013-01-30 12:31:36 +01003880 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003881 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (wp->w_skipcol > 0)
3883 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3884 *p_extra = '-';
3885#ifdef FEAT_RIGHTLEFT
3886 if (wp->w_p_rl) /* reverse line numbers */
3887 rl_mirror(extra);
3888#endif
3889 p_extra = extra;
3890 c_extra = NUL;
3891 }
3892 else
3893 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003894 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003895 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003896#ifdef FEAT_SYN_HL
3897 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003898 * the current line differently.
3899 * TODO: Can we use CursorLine instead of CursorLineNr
3900 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003901 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003902 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003903 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 }
3906 }
3907
Bram Moolenaar597a4222014-06-25 14:39:50 +02003908#ifdef FEAT_LINEBREAK
3909 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3910 && n_extra == 0 && *p_sbr != NUL)
3911 /* draw indent after showbreak value */
3912 draw_state = WL_BRI;
3913 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3914 /* After the showbreak, draw the breakindent */
3915 draw_state = WL_BRI - 1;
3916
3917 /* draw 'breakindent': indent wrapped text accordingly */
3918 if (draw_state == WL_BRI - 1 && n_extra == 0)
3919 {
3920 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003921 /* if need_showbreak is set, breakindent also applies */
3922 if (wp->w_p_bri && n_extra == 0
3923 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003924# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003925 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003926# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003927 )
3928 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003929 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003930# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003931 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003932 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003933 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003934# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003935 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3936 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003937 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003938# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003939 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003940# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003941 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003942 c_extra = ' ';
3943 n_extra = get_breakindent_win(wp,
3944 ml_get_buf(wp->w_buffer, lnum, FALSE));
3945 /* Correct end of highlighted area for 'breakindent',
3946 * required when 'linebreak' is also set. */
3947 if (tocol == vcol)
3948 tocol += n_extra;
3949 }
3950 }
3951#endif
3952
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3954 if (draw_state == WL_SBR - 1 && n_extra == 0)
3955 {
3956 draw_state = WL_SBR;
3957# ifdef FEAT_DIFF
3958 if (filler_todo > 0)
3959 {
3960 /* Draw "deleted" diff line(s). */
3961 if (char2cells(fill_diff) > 1)
3962 c_extra = '-';
3963 else
3964 c_extra = fill_diff;
3965# ifdef FEAT_RIGHTLEFT
3966 if (wp->w_p_rl)
3967 n_extra = col + 1;
3968 else
3969# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02003970 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003971 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973# endif
3974# ifdef FEAT_LINEBREAK
3975 if (*p_sbr != NUL && need_showbreak)
3976 {
3977 /* Draw 'showbreak' at the start of each broken line. */
3978 p_extra = p_sbr;
3979 c_extra = NUL;
3980 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003981 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003983 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 /* Correct end of highlighted area for 'showbreak',
3985 * required when 'linebreak' is also set. */
3986 if (tocol == vcol)
3987 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003988#ifdef FEAT_SYN_HL
3989 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003990 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003991 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003992 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
3995# endif
3996 }
3997#endif
3998
3999 if (draw_state == WL_LINE - 1 && n_extra == 0)
4000 {
4001 draw_state = WL_LINE;
4002 if (saved_n_extra)
4003 {
4004 /* Continue item from end of wrapped line. */
4005 n_extra = saved_n_extra;
4006 c_extra = saved_c_extra;
4007 p_extra = saved_p_extra;
4008 char_attr = saved_char_attr;
4009 }
4010 else
4011 char_attr = 0;
4012 }
4013 }
4014
4015 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004016 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004017 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018#ifdef FEAT_DIFF
4019 && filler_todo <= 0
4020#endif
4021 )
4022 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004023 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004024 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004025 /* Pretend we have finished updating the window. Except when
4026 * 'cursorcolumn' is set. */
4027#ifdef FEAT_SYN_HL
4028 if (wp->w_p_cuc)
4029 row = wp->w_cline_row + wp->w_cline_height;
4030 else
4031#endif
4032 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 break;
4034 }
4035
4036 if (draw_state == WL_LINE && area_highlighting)
4037 {
4038 /* handle Visual or match highlighting in this line */
4039 if (vcol == fromcol
4040#ifdef FEAT_MBYTE
4041 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4042 && (*mb_ptr2cells)(ptr) > 1)
4043#endif
4044 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004045 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 && vcol < tocol))
4047 area_attr = attr; /* start highlighting */
4048 else if (area_attr != 0
4049 && (vcol == tocol
4050 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
4053#ifdef FEAT_SEARCH_EXTRA
4054 if (!n_extra)
4055 {
4056 /*
4057 * Check for start/end of search pattern match.
4058 * After end, check for start/end of next match.
4059 * When another match, have to check for start again.
4060 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004061 * Do this for 'search_hl' and the match list (ordered by
4062 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004064 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004065 cur = wp->w_match_head;
4066 shl_flag = FALSE;
4067 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004069 if (shl_flag == FALSE
4070 && ((cur != NULL
4071 && cur->priority > SEARCH_HL_PRIORITY)
4072 || cur == NULL))
4073 {
4074 shl = &search_hl;
4075 shl_flag = TRUE;
4076 }
4077 else
4078 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004079 if (cur != NULL)
4080 cur->pos.cur = 0;
4081 pos_inprogress = TRUE;
4082 while (shl->rm.regprog != NULL
4083 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004085 if (shl->startcol != MAXCOL
4086 && v >= (long)shl->startcol
4087 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004089#ifdef FEAT_MBYTE
4090 int tmp_col = v + MB_PTR2LEN(ptr);
4091
4092 if (shl->endcol < tmp_col)
4093 shl->endcol = tmp_col;
4094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004096#ifdef FEAT_CONCEAL
4097 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4098 == cur->hlg_id)
4099 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004100 has_match_conc =
4101 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004102 match_conc = cur->conceal_char;
4103 }
4104 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004105 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004108 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
4110 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004111 next_search_hl(wp, shl, lnum, (colnr_T)v,
4112 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004113 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004114 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
4116 /* Need to get the line again, a multi-line regexp
4117 * may have made it invalid. */
4118 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4119 ptr = line + v;
4120
4121 if (shl->lnum == lnum)
4122 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004123 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004125 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004127 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004129 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 {
4131 /* highlight empty match, try again after
4132 * it */
4133#ifdef FEAT_MBYTE
4134 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004135 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004136 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 else
4138#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004139 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141
4142 /* Loop to check if the match starts at the
4143 * current position */
4144 continue;
4145 }
4146 }
4147 break;
4148 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004149 if (shl != &search_hl && cur != NULL)
4150 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004152
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004153 /* Use attributes from match with highest priority among
4154 * 'search_hl' and the match list. */
4155 search_attr = search_hl.attr_cur;
4156 cur = wp->w_match_head;
4157 shl_flag = FALSE;
4158 while (cur != NULL || shl_flag == FALSE)
4159 {
4160 if (shl_flag == FALSE
4161 && ((cur != NULL
4162 && cur->priority > SEARCH_HL_PRIORITY)
4163 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004164 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004165 shl = &search_hl;
4166 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004167 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004168 else
4169 shl = &cur->hl;
4170 if (shl->attr_cur != 0)
4171 search_attr = shl->attr_cur;
4172 if (shl != &search_hl && cur != NULL)
4173 cur = cur->next;
4174 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004175 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004176 if (*ptr == NUL && (did_line_attr >= 1
4177 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004178 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180#endif
4181
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004183 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004185 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4186 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004188 if (diff_hlf == HLF_TXD && ptr - line > change_end
4189 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004191 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004192 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004193 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004196
4197 /* Decide which of the highlight attributes to use. */
4198 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004199#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004200 if (area_attr != 0)
4201 char_attr = hl_combine_attr(line_attr, area_attr);
4202 else if (search_attr != 0)
4203 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004204 /* Use line_attr when not in the Visual or 'incsearch' area
4205 * (area_attr may be 0 when "noinvcur" is set). */
4206 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004207 || vcol < fromcol || vcol_prev < fromcol_prev
4208 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004209 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004210#else
4211 if (area_attr != 0)
4212 char_attr = area_attr;
4213 else if (search_attr != 0)
4214 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004215#endif
4216 else
4217 {
4218 attr_pri = FALSE;
4219#ifdef FEAT_SYN_HL
4220 if (has_syntax)
4221 char_attr = syntax_attr;
4222 else
4223#endif
4224 char_attr = 0;
4225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
4227
4228 /*
4229 * Get the next character to put on the screen.
4230 */
4231 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004232 * The "p_extra" points to the extra stuff that is inserted to
4233 * represent special characters (non-printable stuff) and other
4234 * things. When all characters are the same, c_extra is used.
4235 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4236 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4238 */
4239 if (n_extra > 0)
4240 {
4241 if (c_extra != NUL)
4242 {
4243 c = c_extra;
4244#ifdef FEAT_MBYTE
4245 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004246 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
4248 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004249 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004250 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
4252 else
4253 mb_utf8 = FALSE;
4254#endif
4255 }
4256 else
4257 {
4258 c = *p_extra;
4259#ifdef FEAT_MBYTE
4260 if (has_mbyte)
4261 {
4262 mb_c = c;
4263 if (enc_utf8)
4264 {
4265 /* If the UTF-8 character is more than one byte:
4266 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004267 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 mb_utf8 = FALSE;
4269 if (mb_l > n_extra)
4270 mb_l = 1;
4271 else if (mb_l > 1)
4272 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004273 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004275 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 }
4278 else
4279 {
4280 /* if this is a DBCS character, put it in "mb_c" */
4281 mb_l = MB_BYTE2LEN(c);
4282 if (mb_l >= n_extra)
4283 mb_l = 1;
4284 else if (mb_l > 1)
4285 mb_c = (c << 8) + p_extra[1];
4286 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004287 if (mb_l == 0) /* at the NUL at end-of-line */
4288 mb_l = 1;
4289
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 /* If a double-width char doesn't fit display a '>' in the
4291 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004292 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293# ifdef FEAT_RIGHTLEFT
4294 wp->w_p_rl ? (col <= 0) :
4295# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004296 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 && (*mb_char2cells)(mb_c) == 2)
4298 {
4299 c = '>';
4300 mb_c = c;
4301 mb_l = 1;
4302 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004303 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 /* put the pointer back to output the double-width
4305 * character at the start of the next line. */
4306 ++n_extra;
4307 --p_extra;
4308 }
4309 else
4310 {
4311 n_extra -= mb_l - 1;
4312 p_extra += mb_l - 1;
4313 }
4314 }
4315#endif
4316 ++p_extra;
4317 }
4318 --n_extra;
4319 }
4320 else
4321 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004322#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004323 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004324#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004325
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004326 if (p_extra_free != NULL)
4327 {
4328 vim_free(p_extra_free);
4329 p_extra_free = NULL;
4330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 /*
4332 * Get a character from the line itself.
4333 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004334 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004335#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004336 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338#ifdef FEAT_MBYTE
4339 if (has_mbyte)
4340 {
4341 mb_c = c;
4342 if (enc_utf8)
4343 {
4344 /* If the UTF-8 character is more than one byte: Decode it
4345 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004346 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 mb_utf8 = FALSE;
4348 if (mb_l > 1)
4349 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004350 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 /* Overlong encoded ASCII or ASCII with composing char
4352 * is displayed normally, except a NUL. */
4353 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004354 {
4355 c = mb_c;
4356# ifdef FEAT_LINEBREAK
4357 c0 = mb_c;
4358# endif
4359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004361
4362 /* At start of the line we can have a composing char.
4363 * Draw it as a space with a composing char. */
4364 if (utf_iscomposing(mb_c))
4365 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004366 int i;
4367
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004368 for (i = Screen_mco - 1; i > 0; --i)
4369 u8cc[i] = u8cc[i - 1];
4370 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004371 mb_c = ' ';
4372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374
4375 if ((mb_l == 1 && c >= 0x80)
4376 || (mb_l >= 1 && mb_c == 0)
4377 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004378# ifdef UNICODE16
4379 || mb_c >= 0x10000
4380# endif
4381 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 {
4383 /*
4384 * Illegal UTF-8 byte: display as <xx>.
4385 * Non-BMP character : display as ? or fullwidth ?.
4386 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004387# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
4391 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004392# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 if (wp->w_p_rl) /* reverse */
4394 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004397# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 else if (utf_char2cells(mb_c) != 2)
4399 STRCPY(extra, "?");
4400 else
4401 /* 0xff1f in UTF-8: full-width '?' */
4402 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 p_extra = extra;
4406 c = *p_extra;
4407 mb_c = mb_ptr2char_adv(&p_extra);
4408 mb_utf8 = (c >= 0x80);
4409 n_extra = (int)STRLEN(p_extra);
4410 c_extra = NUL;
4411 if (area_attr == 0 && search_attr == 0)
4412 {
4413 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004414 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 saved_attr2 = char_attr; /* save current attr */
4416 }
4417 }
4418 else if (mb_l == 0) /* at the NUL at end-of-line */
4419 mb_l = 1;
4420#ifdef FEAT_ARABIC
4421 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4422 {
4423 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004424 int pc, pc1, nc;
4425 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427 /* The idea of what is the previous and next
4428 * character depends on 'rightleft'. */
4429 if (wp->w_p_rl)
4430 {
4431 pc = prev_c;
4432 pc1 = prev_c1;
4433 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004434 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
4436 else
4437 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004438 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004440 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
4442 prev_c = mb_c;
4443
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004444 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446 else
4447 prev_c = mb_c;
4448#endif
4449 }
4450 else /* enc_dbcs */
4451 {
4452 mb_l = MB_BYTE2LEN(c);
4453 if (mb_l == 0) /* at the NUL at end-of-line */
4454 mb_l = 1;
4455 else if (mb_l > 1)
4456 {
4457 /* We assume a second byte below 32 is illegal.
4458 * Hopefully this is OK for all double-byte encodings!
4459 */
4460 if (ptr[1] >= 32)
4461 mb_c = (c << 8) + ptr[1];
4462 else
4463 {
4464 if (ptr[1] == NUL)
4465 {
4466 /* head byte at end of line */
4467 mb_l = 1;
4468 transchar_nonprint(extra, c);
4469 }
4470 else
4471 {
4472 /* illegal tail byte */
4473 mb_l = 2;
4474 STRCPY(extra, "XX");
4475 }
4476 p_extra = extra;
4477 n_extra = (int)STRLEN(extra) - 1;
4478 c_extra = NUL;
4479 c = *p_extra++;
4480 if (area_attr == 0 && search_attr == 0)
4481 {
4482 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004483 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 saved_attr2 = char_attr; /* save current attr */
4485 }
4486 mb_c = c;
4487 }
4488 }
4489 }
4490 /* If a double-width char doesn't fit display a '>' in the
4491 * last column; the character is displayed at the start of the
4492 * next line. */
4493 if ((
4494# ifdef FEAT_RIGHTLEFT
4495 wp->w_p_rl ? (col <= 0) :
4496# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004497 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 && (*mb_char2cells)(mb_c) == 2)
4499 {
4500 c = '>';
4501 mb_c = c;
4502 mb_utf8 = FALSE;
4503 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004504 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 /* Put pointer back so that the character will be
4506 * displayed at the start of the next line. */
4507 --ptr;
4508 }
4509 else if (*ptr != NUL)
4510 ptr += mb_l - 1;
4511
4512 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004513 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004514 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004515 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004518 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 c = ' ';
4520 if (area_attr == 0 && search_attr == 0)
4521 {
4522 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004523 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 saved_attr2 = char_attr; /* save current attr */
4525 }
4526 mb_c = c;
4527 mb_utf8 = FALSE;
4528 mb_l = 1;
4529 }
4530
4531 }
4532#endif
4533 ++ptr;
4534
4535 if (extra_check)
4536 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004537#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004538 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004539#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004540
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004541#ifdef FEAT_TERMINAL
4542 if (get_term_attr)
4543 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004544 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004545
4546 if (!attr_pri)
4547 char_attr = syntax_attr;
4548 else
4549 char_attr = hl_combine_attr(syntax_attr, char_attr);
4550 }
4551#endif
4552
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004553#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 /* Get syntax attribute, unless still at the start of the line
4555 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004556 v = (long)(ptr - line);
4557 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
4559 /* Get the syntax attribute for the character. If there
4560 * is an error, disable syntax highlighting. */
4561 save_did_emsg = did_emsg;
4562 did_emsg = FALSE;
4563
Bram Moolenaar217ad922005-03-20 22:37:15 +00004564 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004565# ifdef FEAT_SPELL
4566 has_spell ? &can_spell :
4567# endif
4568 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569
4570 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004571 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004572 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004573 has_syntax = FALSE;
4574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 else
4576 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004577#ifdef SYN_TIME_LIMIT
4578 if (wp->w_s->b_syn_slow)
4579 has_syntax = FALSE;
4580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
4582 /* Need to get the line again, a multi-line regexp may
4583 * have made it invalid. */
4584 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4585 ptr = line + v;
4586
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004587 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004589 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004590 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004591# ifdef FEAT_CONCEAL
4592 /* no concealing past the end of the line, it interferes
4593 * with line highlighting */
4594 if (c == NUL)
4595 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004596 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004597 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004600#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004601
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004602#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004603 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004604 * Only do this when there is no syntax highlighting, the
4605 * @Spell cluster is not used or the current syntax item
4606 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004607 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004608 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004609 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004610# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004611 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004612 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004613# endif
4614 if (c != 0 && (
4615# ifdef FEAT_SYN_HL
4616 !has_syntax ||
4617# endif
4618 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004619 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004620 char_u *prev_ptr, *p;
4621 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004622 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004623# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004624 if (has_mbyte)
4625 {
4626 prev_ptr = ptr - mb_l;
4627 v -= mb_l - 1;
4628 }
4629 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004630# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004631 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004632
4633 /* Use nextline[] if possible, it has the start of the
4634 * next line concatenated. */
4635 if ((prev_ptr - line) - nextlinecol >= 0)
4636 p = nextline + (prev_ptr - line) - nextlinecol;
4637 else
4638 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004639 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004640 len = spell_check(wp, p, &spell_hlf, &cap_col,
4641 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004642 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004643
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004644 /* In Insert mode only highlight a word that
4645 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004646 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004647 && (State & INSERT) != 0
4648 && wp->w_cursor.lnum == lnum
4649 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004650 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004651 && wp->w_cursor.col < (colnr_T)word_end)
4652 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004653 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004654 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004655 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004656
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004657 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004658 && (p - nextline) + len > nextline_idx)
4659 {
4660 /* Remember that the good word continues at the
4661 * start of the next line. */
4662 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004663 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004664 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004665
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004666 /* Turn index into actual attributes. */
4667 if (spell_hlf != HLF_COUNT)
4668 spell_attr = highlight_attr[spell_hlf];
4669
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004670 if (cap_col > 0)
4671 {
4672 if (p != prev_ptr
4673 && (p - nextline) + cap_col >= nextline_idx)
4674 {
4675 /* Remember that the word in the next line
4676 * must start with a capital. */
4677 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004678 cap_col = (int)((p - nextline) + cap_col
4679 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004680 }
4681 else
4682 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004683 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004684 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004685 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004686 }
4687 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004688 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004689 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004690 char_attr = hl_combine_attr(char_attr, spell_attr);
4691 else
4692 char_attr = hl_combine_attr(spell_attr, char_attr);
4693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694#endif
4695#ifdef FEAT_LINEBREAK
4696 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004697 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004699 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004700 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004702# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004703 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004704# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004705 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004707 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004709 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004710
Bram Moolenaar597a4222014-06-25 14:39:50 +02004711 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004712 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004713 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004714 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaara3650912014-11-19 13:21:57 +01004715 n_extra = (int)wp->w_buffer->b_p_ts
4716 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4717
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004718# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004719 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004720# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004722# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004723 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004724 {
4725#ifdef FEAT_CONCEAL
4726 if (c == TAB)
4727 /* See "Tab alignment" below. */
4728 FIX_FOR_BOGUSCOLS;
4729#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004730 if (!wp->w_p_list)
4731 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 }
4734#endif
4735
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004736 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4737 */
4738 if (wp->w_p_list
4739 && (((c == 160
4740#ifdef FEAT_MBYTE
4741 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4742#endif
4743 ) && lcs_nbsp)
4744 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4745 {
4746 c = (c == ' ') ? lcs_space : lcs_nbsp;
4747 if (area_attr == 0 && search_attr == 0)
4748 {
4749 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004750 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004751 saved_attr2 = char_attr; /* save current attr */
4752 }
4753#ifdef FEAT_MBYTE
4754 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004755 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004756 {
4757 mb_utf8 = TRUE;
4758 u8cc[0] = 0;
4759 c = 0xc0;
4760 }
4761 else
4762 mb_utf8 = FALSE;
4763#endif
4764 }
4765
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4767 {
4768 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004769 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
4771 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004772 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 saved_attr2 = char_attr; /* save current attr */
4774 }
4775#ifdef FEAT_MBYTE
4776 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004777 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
4779 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004780 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004781 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
4783 else
4784 mb_utf8 = FALSE;
4785#endif
4786 }
4787 }
4788
4789 /*
4790 * Handling of non-printable characters.
4791 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004792 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
4794 /*
4795 * when getting a character from the file, we may have to
4796 * turn it into something else on the way to putting it
4797 * into "ScreenLines".
4798 */
4799 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4800 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004801 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004802 long vcol_adjusted = vcol; /* removed showbreak length */
4803#ifdef FEAT_LINEBREAK
4804 /* only adjust the tab_len, when at the first column
4805 * after the showbreak value was drawn */
4806 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4807 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004810 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004811 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4812
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004813#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004814 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004815#endif
4816 /* tab amount depends on current column */
4817 n_extra = tab_len;
4818#ifdef FEAT_LINEBREAK
4819 else
4820 {
4821 char_u *p;
4822 int len = n_extra;
4823 int i;
4824 int saved_nextra = n_extra;
4825
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004826#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004827 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004828 /* there are characters to conceal */
4829 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004830 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4831 */
4832 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4833 && n_extra > tab_len)
4834 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004835#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004836
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004837 /* if n_extra > 0, it gives the number of chars, to
4838 * use for a tab, else we need to calculate the width
4839 * for a tab */
4840#ifdef FEAT_MBYTE
4841 len = (tab_len * mb_char2len(lcs_tab2));
4842 if (n_extra > 0)
4843 len += n_extra - tab_len;
4844#endif
4845 c = lcs_tab1;
4846 p = alloc((unsigned)(len + 1));
4847 vim_memset(p, ' ', len);
4848 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004849 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004850 p_extra_free = p;
4851 for (i = 0; i < tab_len; i++)
4852 {
4853#ifdef FEAT_MBYTE
4854 mb_char2bytes(lcs_tab2, p);
4855 p += mb_char2len(lcs_tab2);
4856 n_extra += mb_char2len(lcs_tab2)
4857 - (saved_nextra > 0 ? 1 : 0);
4858#else
4859 p[i] = lcs_tab2;
4860#endif
4861 }
4862 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004863#ifdef FEAT_CONCEAL
4864 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4865 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004866 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004867 n_extra -= vcol_off;
4868#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004869 }
4870#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004871#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004872 {
4873 int vc_saved = vcol_off;
4874
4875 /* Tab alignment should be identical regardless of
4876 * 'conceallevel' value. So tab compensates of all
4877 * previous concealed characters, and thus resets
4878 * vcol_off and boguscols accumulated so far in the
4879 * line. Note that the tab can be longer than
4880 * 'tabstop' when there are concealed characters. */
4881 FIX_FOR_BOGUSCOLS;
4882
4883 /* Make sure, the highlighting for the tab char will be
4884 * correctly set further below (effectively reverts the
4885 * FIX_FOR_BOGSUCOLS macro */
4886 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004887 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004888 tab_len += vc_saved;
4889 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891#ifdef FEAT_MBYTE
4892 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4893#endif
4894 if (wp->w_p_list)
4895 {
4896 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004897#ifdef FEAT_LINEBREAK
4898 if (wp->w_p_lbr)
4899 c_extra = NUL; /* using p_extra from above */
4900 else
4901#endif
4902 c_extra = lcs_tab2;
4903 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004904 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 saved_attr2 = char_attr; /* save current attr */
4906#ifdef FEAT_MBYTE
4907 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004908 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
4910 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004911 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004912 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 }
4914#endif
4915 }
4916 else
4917 {
4918 c_extra = ' ';
4919 c = ' ';
4920 }
4921 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004922 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004923 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004924 || ((fromcol >= 0 || fromcol_prev >= 0)
4925 && tocol > vcol
4926 && VIsual_mode != Ctrl_V
4927 && (
4928# ifdef FEAT_RIGHTLEFT
4929 wp->w_p_rl ? (col >= 0) :
4930# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004931 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004932 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004933 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004934 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004935 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004937 /* Display a '$' after the line or highlight an extra
4938 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4940 /* For a diff line the highlighting continues after the
4941 * "$". */
4942 if (
4943# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004944 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945# ifdef LINE_ATTR
4946 &&
4947# endif
4948# endif
4949# ifdef LINE_ATTR
4950 line_attr == 0
4951# endif
4952 )
4953#endif
4954 {
4955#ifdef FEAT_VIRTUALEDIT
4956 /* In virtualedit, visual selections may extend
4957 * beyond end of line. */
4958 if (area_highlighting && virtual_active()
4959 && tocol != MAXCOL && vcol < tocol)
4960 n_extra = 0;
4961 else
4962#endif
4963 {
4964 p_extra = at_end_str;
4965 n_extra = 1;
4966 c_extra = NUL;
4967 }
4968 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004969 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004970 c = lcs_eol;
4971 else
4972 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 lcs_eol_one = -1;
4974 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004975 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004977 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 n_attr = 1;
4979 }
4980#ifdef FEAT_MBYTE
4981 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004982 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
4984 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004985 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004986 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988 else
4989 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4990#endif
4991 }
4992 else if (c != NUL)
4993 {
4994 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004995 if (n_extra == 0)
4996 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997#ifdef FEAT_RIGHTLEFT
4998 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4999 rl_mirror(p_extra); /* reverse "<12>" */
5000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005002#ifdef FEAT_LINEBREAK
5003 if (wp->w_p_lbr)
5004 {
5005 char_u *p;
5006
5007 c = *p_extra;
5008 p = alloc((unsigned)n_extra + 1);
5009 vim_memset(p, ' ', n_extra);
5010 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5011 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005012 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005013 p_extra_free = p_extra = p;
5014 }
5015 else
5016#endif
5017 {
5018 n_extra = byte2cells(c) - 1;
5019 c = *p_extra++;
5020 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005021 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 {
5023 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005024 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 saved_attr2 = char_attr; /* save current attr */
5026 }
5027#ifdef FEAT_MBYTE
5028 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5029#endif
5030 }
5031#ifdef FEAT_VIRTUALEDIT
5032 else if (VIsual_active
5033 && (VIsual_mode == Ctrl_V
5034 || VIsual_mode == 'v')
5035 && virtual_active()
5036 && tocol != MAXCOL
5037 && vcol < tocol
5038 && (
5039# ifdef FEAT_RIGHTLEFT
5040 wp->w_p_rl ? (col >= 0) :
5041# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005042 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
5044 c = ' ';
5045 --ptr; /* put it back at the NUL */
5046 }
5047#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005048#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 else if ((
5050# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005051 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005053# ifdef FEAT_TERMINAL
5054 term_attr != 0 ||
5055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 ) && (
5058# ifdef FEAT_RIGHTLEFT
5059 wp->w_p_rl ? (col >= 0) :
5060# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005061 (col
5062# ifdef FEAT_CONCEAL
5063 - boguscols
5064# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005065 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 /* Highlight until the right side of the window */
5068 c = ' ';
5069 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005070
5071 /* Remember we do the char for line highlighting. */
5072 ++did_line_attr;
5073
5074 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005075 if (line_attr != 0 && char_attr == search_attr
5076 && (did_line_attr > 1
5077 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005078 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079# ifdef FEAT_DIFF
5080 if (diff_hlf == HLF_TXD)
5081 {
5082 diff_hlf = HLF_CHD;
5083 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005084 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005085 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005086 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5087 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005088 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
5091# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005092# ifdef FEAT_TERMINAL
5093 if (term_attr != 0)
5094 {
5095 char_attr = term_attr;
5096 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5097 char_attr = hl_combine_attr(char_attr,
5098 HL_ATTR(HLF_CUL));
5099 }
5100# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102#endif
5103 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005104
5105#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005106 if ( wp->w_p_cole > 0
5107 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005108 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005109 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005110 && !(lnum_in_visual_area
5111 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005112 {
5113 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005114 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005115 && (syn_get_sub_char() != NUL || match_conc
5116 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005117 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005118 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005119 /* First time at this concealed item: display one
5120 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005121 if (match_conc)
5122 c = match_conc;
5123 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005124 c = syn_get_sub_char();
5125 else if (lcs_conceal != NUL)
5126 c = lcs_conceal;
5127 else
5128 c = ' ';
5129
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005130 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005131
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005132 if (n_extra > 0)
5133 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005134 vcol += n_extra;
5135 if (wp->w_p_wrap && n_extra > 0)
5136 {
5137# ifdef FEAT_RIGHTLEFT
5138 if (wp->w_p_rl)
5139 {
5140 col -= n_extra;
5141 boguscols -= n_extra;
5142 }
5143 else
5144# endif
5145 {
5146 boguscols += n_extra;
5147 col += n_extra;
5148 }
5149 }
5150 n_extra = 0;
5151 n_attr = 0;
5152 }
5153 else if (n_skip == 0)
5154 {
5155 is_concealing = TRUE;
5156 n_skip = 1;
5157 }
5158# ifdef FEAT_MBYTE
5159 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005160 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005161 {
5162 mb_utf8 = TRUE;
5163 u8cc[0] = 0;
5164 c = 0xc0;
5165 }
5166 else
5167 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5168# endif
5169 }
5170 else
5171 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005172 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005173 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005174 }
5175#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005178#ifdef FEAT_CONCEAL
5179 /* In the cursor line and we may be concealing characters: correct
5180 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005181 if (!did_wcol && draw_state == WL_LINE
5182 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005183 && conceal_cursor_line(wp)
5184 && (int)wp->w_virtcol <= vcol + n_skip)
5185 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005186# ifdef FEAT_RIGHTLEFT
5187 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005188 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005189 else
5190# endif
5191 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005192 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005193 did_wcol = TRUE;
5194 }
5195#endif
5196
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 /* Don't override visual selection highlighting. */
5198 if (n_attr > 0
5199 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005200 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 char_attr = extra_attr;
5202
Bram Moolenaar81695252004-12-29 20:58:21 +00005203#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 /* XIM don't send preedit_start and preedit_end, but they send
5205 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5206 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005207 if (p_imst == IM_ON_THE_SPOT
5208 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005209 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 && (State & INSERT)
5211 && !p_imdisable
5212 && im_is_preediting()
5213 && draw_state == WL_LINE)
5214 {
5215 colnr_T tcol;
5216
5217 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005218 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 else
5220 tcol = preedit_end_col;
5221 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5222 {
5223 if (feedback_old_attr < 0)
5224 {
5225 feedback_col = 0;
5226 feedback_old_attr = char_attr;
5227 }
5228 char_attr = im_get_feedback_attr(feedback_col);
5229 if (char_attr < 0)
5230 char_attr = feedback_old_attr;
5231 feedback_col++;
5232 }
5233 else if (feedback_old_attr >= 0)
5234 {
5235 char_attr = feedback_old_attr;
5236 feedback_old_attr = -1;
5237 feedback_col = 0;
5238 }
5239 }
5240#endif
5241 /*
5242 * Handle the case where we are in column 0 but not on the first
5243 * character of the line and the user wants us to show us a
5244 * special character (via 'listchars' option "precedes:<char>".
5245 */
5246 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005247 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5249#ifdef FEAT_DIFF
5250 && filler_todo <= 0
5251#endif
5252 && draw_state > WL_NR
5253 && c != NUL)
5254 {
5255 c = lcs_prec;
5256 lcs_prec_todo = NUL;
5257#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005258 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5259 {
5260 /* Double-width character being overwritten by the "precedes"
5261 * character, need to fill up half the character. */
5262 c_extra = MB_FILLER_CHAR;
5263 n_extra = 1;
5264 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005265 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005268 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 {
5270 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005271 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005272 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 }
5274 else
5275 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5276#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005277 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
5279 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005280 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 n_attr3 = 1;
5282 }
5283 }
5284
5285 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005286 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005288 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005289#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005290 || did_line_attr == 1
5291#endif
5292 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005294#ifdef FEAT_SEARCH_EXTRA
5295 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005296
5297 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005298 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005299 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005300#endif
5301
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005302 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 * highlight match at end of line. If it's beyond the last
5304 * char on the screen, just overwrite that one (tricky!) Not
5305 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005306#ifdef FEAT_SEARCH_EXTRA
5307 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005308 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005309 prevcol_hl_flag = TRUE;
5310 else
5311 {
5312 cur = wp->w_match_head;
5313 while (cur != NULL)
5314 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005315 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005316 {
5317 prevcol_hl_flag = TRUE;
5318 break;
5319 }
5320 cur = cur->next;
5321 }
5322 }
5323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005325 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005326 && (VIsual_mode != Ctrl_V
5327 || lnum == VIsual.lnum
5328 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005329 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330#ifdef FEAT_SEARCH_EXTRA
5331 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005332 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005333# ifdef FEAT_SYN_HL
5334 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5335 && !(wp == curwin && VIsual_active))
5336# endif
5337# ifdef FEAT_DIFF
5338 && diff_hlf == (hlf_T)0
5339# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005340# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005341 && did_line_attr <= 1
5342# endif
5343 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344#endif
5345 ))
5346 {
5347 int n = 0;
5348
5349#ifdef FEAT_RIGHTLEFT
5350 if (wp->w_p_rl)
5351 {
5352 if (col < 0)
5353 n = 1;
5354 }
5355 else
5356#endif
5357 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005358 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 n = -1;
5360 }
5361 if (n != 0)
5362 {
5363 /* At the window boundary, highlight the last character
5364 * instead (better than nothing). */
5365 off += n;
5366 col += n;
5367 }
5368 else
5369 {
5370 /* Add a blank character to highlight. */
5371 ScreenLines[off] = ' ';
5372#ifdef FEAT_MBYTE
5373 if (enc_utf8)
5374 ScreenLinesUC[off] = 0;
5375#endif
5376 }
5377#ifdef FEAT_SEARCH_EXTRA
5378 if (area_attr == 0)
5379 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005380 /* Use attributes from match with highest priority among
5381 * 'search_hl' and the match list. */
5382 char_attr = search_hl.attr;
5383 cur = wp->w_match_head;
5384 shl_flag = FALSE;
5385 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005386 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005387 if (shl_flag == FALSE
5388 && ((cur != NULL
5389 && cur->priority > SEARCH_HL_PRIORITY)
5390 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005391 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005392 shl = &search_hl;
5393 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005394 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005395 else
5396 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005397 if ((ptr - line) - 1 == (long)shl->startcol
5398 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005399 char_attr = shl->attr;
5400 if (shl != &search_hl && cur != NULL)
5401 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
5404#endif
5405 ScreenAttrs[off] = char_attr;
5406#ifdef FEAT_RIGHTLEFT
5407 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005408 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005410 --off;
5411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 else
5413#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005416 ++off;
5417 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005418 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005419#ifdef FEAT_SYN_HL
5420 eol_hl_off = 1;
5421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
Bram Moolenaar91170f82006-05-05 21:15:17 +00005425 /*
5426 * At end of the text line.
5427 */
5428 if (c == NUL)
5429 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005430#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005431 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5432 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005433 {
5434 /* highlight last char after line */
5435 --col;
5436 --off;
5437 --vcol;
5438 }
5439
Bram Moolenaar1a384422010-07-14 19:53:30 +02005440 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005441 if (wp->w_p_wrap)
5442 v = wp->w_skipcol;
5443 else
5444 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005445
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005446 /* check if line ends before left margin */
5447 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005448 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005449#ifdef FEAT_CONCEAL
5450 /* Get rid of the boguscols now, we want to draw until the right
5451 * edge for 'cursorcolumn'. */
5452 col -= boguscols;
5453 boguscols = 0;
5454#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005455
5456 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005457 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005458
5459 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005460 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5461 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005462 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005463 && lnum != wp->w_cursor.lnum)
5464 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005465# ifdef FEAT_RIGHTLEFT
5466 && !wp->w_p_rl
5467# endif
5468 )
5469 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005470 int rightmost_vcol = 0;
5471 int i;
5472
5473 if (wp->w_p_cuc)
5474 rightmost_vcol = wp->w_virtcol;
5475 if (draw_color_col)
5476 /* determine rightmost colorcolumn to possibly draw */
5477 for (i = 0; color_cols[i] >= 0; ++i)
5478 if (rightmost_vcol < color_cols[i])
5479 rightmost_vcol = color_cols[i];
5480
Bram Moolenaar02631462017-09-22 15:20:32 +02005481 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005482 {
5483 ScreenLines[off] = ' ';
5484#ifdef FEAT_MBYTE
5485 if (enc_utf8)
5486 ScreenLinesUC[off] = 0;
5487#endif
5488 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005489 if (draw_color_col)
5490 draw_color_col = advance_color_col(VCOL_HLC,
5491 &color_cols);
5492
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005493 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005494 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005495 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005496 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005497 else
5498 ScreenAttrs[off++] = 0;
5499
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005500 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005501 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005502
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005503 ++vcol;
5504 }
5505 }
5506#endif
5507
Bram Moolenaar53f81742017-09-22 14:35:51 +02005508 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005509 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 row++;
5511
5512 /*
5513 * Update w_cline_height and w_cline_folded if the cursor line was
5514 * updated (saves a call to plines() later).
5515 */
5516 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5517 {
5518 curwin->w_cline_row = startrow;
5519 curwin->w_cline_height = row - startrow;
5520#ifdef FEAT_FOLDING
5521 curwin->w_cline_folded = FALSE;
5522#endif
5523 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5524 }
5525
5526 break;
5527 }
5528
5529 /* line continues beyond line end */
5530 if (lcs_ext
5531 && !wp->w_p_wrap
5532#ifdef FEAT_DIFF
5533 && filler_todo <= 0
5534#endif
5535 && (
5536#ifdef FEAT_RIGHTLEFT
5537 wp->w_p_rl ? col == 0 :
5538#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005539 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005541 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5543 {
5544 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005545 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546#ifdef FEAT_MBYTE
5547 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005548 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
5550 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005551 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005552 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
5554 else
5555 mb_utf8 = FALSE;
5556#endif
5557 }
5558
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005559#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005560 /* advance to the next 'colorcolumn' */
5561 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005562 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005563
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005564 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005565 * highlight the cursor position itself.
5566 * Also highlight the 'colorcolumn' if it is different than
5567 * 'cursorcolumn' */
5568 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005569 if (draw_state == WL_LINE && !lnum_in_visual_area
5570 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005571 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005572 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005573 && lnum != wp->w_cursor.lnum)
5574 {
5575 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005576 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005577 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005578 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005579 {
5580 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005581 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005582 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005583 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005584#endif
5585
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 /*
5587 * Store character to be displayed.
5588 * Skip characters that are left of the screen for 'nowrap'.
5589 */
5590 vcol_prev = vcol;
5591 if (draw_state < WL_LINE || n_skip <= 0)
5592 {
5593 /*
5594 * Store the character.
5595 */
5596#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5597 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5598 {
5599 /* A double-wide character is: put first halve in left cell. */
5600 --off;
5601 --col;
5602 }
5603#endif
5604 ScreenLines[off] = c;
5605#ifdef FEAT_MBYTE
5606 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005607 {
5608 if ((mb_c & 0xff00) == 0x8e00)
5609 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 else if (enc_utf8)
5613 {
5614 if (mb_utf8)
5615 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005616 int i;
5617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005619 if ((c & 0xff) == 0)
5620 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005621 for (i = 0; i < Screen_mco; ++i)
5622 {
5623 ScreenLinesC[i][off] = u8cc[i];
5624 if (u8cc[i] == 0)
5625 break;
5626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 }
5628 else
5629 ScreenLinesUC[off] = 0;
5630 }
5631 if (multi_attr)
5632 {
5633 ScreenAttrs[off] = multi_attr;
5634 multi_attr = 0;
5635 }
5636 else
5637#endif
5638 ScreenAttrs[off] = char_attr;
5639
5640#ifdef FEAT_MBYTE
5641 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5642 {
5643 /* Need to fill two screen columns. */
5644 ++off;
5645 ++col;
5646 if (enc_utf8)
5647 /* UTF-8: Put a 0 in the second screen char. */
5648 ScreenLines[off] = 0;
5649 else
5650 /* DBCS: Put second byte in the second screen char. */
5651 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005652 if (draw_state > WL_NR
5653#ifdef FEAT_DIFF
5654 && filler_todo <= 0
5655#endif
5656 )
5657 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 /* When "tocol" is halfway a character, set it to the end of
5659 * the character, otherwise highlighting won't stop. */
5660 if (tocol == vcol)
5661 ++tocol;
5662#ifdef FEAT_RIGHTLEFT
5663 if (wp->w_p_rl)
5664 {
5665 /* now it's time to backup one cell */
5666 --off;
5667 --col;
5668 }
5669#endif
5670 }
5671#endif
5672#ifdef FEAT_RIGHTLEFT
5673 if (wp->w_p_rl)
5674 {
5675 --off;
5676 --col;
5677 }
5678 else
5679#endif
5680 {
5681 ++off;
5682 ++col;
5683 }
5684 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005685#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005686 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005687 {
5688 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005689 ++vcol_off;
5690 if (n_extra > 0)
5691 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005692 if (wp->w_p_wrap)
5693 {
5694 /*
5695 * Special voodoo required if 'wrap' is on.
5696 *
5697 * Advance the column indicator to force the line
5698 * drawing to wrap early. This will make the line
5699 * take up the same screen space when parts are concealed,
5700 * so that cursor line computations aren't messed up.
5701 *
5702 * To avoid the fictitious advance of 'col' causing
5703 * trailing junk to be written out of the screen line
5704 * we are building, 'boguscols' keeps track of the number
5705 * of bad columns we have advanced.
5706 */
5707 if (n_extra > 0)
5708 {
5709 vcol += n_extra;
5710# ifdef FEAT_RIGHTLEFT
5711 if (wp->w_p_rl)
5712 {
5713 col -= n_extra;
5714 boguscols -= n_extra;
5715 }
5716 else
5717# endif
5718 {
5719 col += n_extra;
5720 boguscols += n_extra;
5721 }
5722 n_extra = 0;
5723 n_attr = 0;
5724 }
5725
5726
5727# ifdef FEAT_MBYTE
5728 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5729 {
5730 /* Need to fill two screen columns. */
5731# ifdef FEAT_RIGHTLEFT
5732 if (wp->w_p_rl)
5733 {
5734 --boguscols;
5735 --col;
5736 }
5737 else
5738# endif
5739 {
5740 ++boguscols;
5741 ++col;
5742 }
5743 }
5744# endif
5745
5746# ifdef FEAT_RIGHTLEFT
5747 if (wp->w_p_rl)
5748 {
5749 --boguscols;
5750 --col;
5751 }
5752 else
5753# endif
5754 {
5755 ++boguscols;
5756 ++col;
5757 }
5758 }
5759 else
5760 {
5761 if (n_extra > 0)
5762 {
5763 vcol += n_extra;
5764 n_extra = 0;
5765 n_attr = 0;
5766 }
5767 }
5768
5769 }
5770#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 else
5772 --n_skip;
5773
Bram Moolenaar64486672010-05-16 15:46:46 +02005774 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5775 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005776 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777#ifdef FEAT_DIFF
5778 && filler_todo <= 0
5779#endif
5780 )
5781 ++vcol;
5782
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005783#ifdef FEAT_SYN_HL
5784 if (vcol_save_attr >= 0)
5785 char_attr = vcol_save_attr;
5786#endif
5787
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 /* restore attributes after "predeces" in 'listchars' */
5789 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5790 char_attr = saved_attr3;
5791
5792 /* restore attributes after last 'listchars' or 'number' char */
5793 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5794 char_attr = saved_attr2;
5795
5796 /*
5797 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005798 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 */
5800 if ((
5801#ifdef FEAT_RIGHTLEFT
5802 wp->w_p_rl ? (col < 0) :
5803#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005804 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 && (*ptr != NUL
5806#ifdef FEAT_DIFF
5807 || filler_todo > 0
5808#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005809 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5811 )
5812 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005813#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005814 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005815 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005816 boguscols = 0;
5817#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005818 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005819 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 ++row;
5822 ++screen_row;
5823
5824 /* When not wrapping and finished diff lines, or when displayed
5825 * '$' and highlighting until last column, break here. */
5826 if ((!wp->w_p_wrap
5827#ifdef FEAT_DIFF
5828 && filler_todo <= 0
5829#endif
5830 ) || lcs_eol_one == -1)
5831 break;
5832
5833 /* When the window is too narrow draw all "@" lines. */
5834 if (draw_state != WL_LINE
5835#ifdef FEAT_DIFF
5836 && filler_todo <= 0
5837#endif
5838 )
5839 {
5840 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 row = endrow;
5843 }
5844
5845 /* When line got too long for screen break here. */
5846 if (row == endrow)
5847 {
5848 ++row;
5849 break;
5850 }
5851
5852 if (screen_cur_row == screen_row - 1
5853#ifdef FEAT_DIFF
5854 && filler_todo <= 0
5855#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005856 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 {
5858 /* Remember that the line wraps, used for modeless copy. */
5859 LineWraps[screen_row - 1] = TRUE;
5860
5861 /*
5862 * Special trick to make copy/paste of wrapped lines work with
5863 * xterm/screen: write an extra character beyond the end of
5864 * the line. This will work with all terminal types
5865 * (regardless of the xn,am settings).
5866 * Only do this on a fast tty.
5867 * Only do this if the cursor is on the current line
5868 * (something has been written in it).
5869 * Don't do this for the GUI.
5870 * Don't do this for double-width characters.
5871 * Don't do this for a window not at the right screen border.
5872 */
5873 if (p_tf
5874#ifdef FEAT_GUI
5875 && !gui.in_use
5876#endif
5877#ifdef FEAT_MBYTE
5878 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005879 && ((*mb_off2cells)(LineOffset[screen_row],
5880 LineOffset[screen_row] + screen_Columns)
5881 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005883 + (int)Columns - 2,
5884 LineOffset[screen_row] + screen_Columns)
5885 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886#endif
5887 )
5888 {
5889 /* First make sure we are at the end of the screen line,
5890 * then output the same character again to let the
5891 * terminal know about the wrap. If the terminal doesn't
5892 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005893 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 screen_char(LineOffset[screen_row - 1]
5895 + (unsigned)Columns - 1,
5896 screen_row - 1, (int)(Columns - 1));
5897
5898#ifdef FEAT_MBYTE
5899 /* When there is a multi-byte character, just output a
5900 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005901 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5902 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 out_char(' ');
5904 else
5905#endif
5906 out_char(ScreenLines[LineOffset[screen_row - 1]
5907 + (Columns - 1)]);
5908 /* force a redraw of the first char on the next line */
5909 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5910 screen_start(); /* don't know where cursor is now */
5911 }
5912 }
5913
5914 col = 0;
5915 off = (unsigned)(current_ScreenLine - ScreenLines);
5916#ifdef FEAT_RIGHTLEFT
5917 if (wp->w_p_rl)
5918 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005919 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 off += col;
5921 }
5922#endif
5923
5924 /* reset the drawing state for the start of a wrapped line */
5925 draw_state = WL_START;
5926 saved_n_extra = n_extra;
5927 saved_p_extra = p_extra;
5928 saved_c_extra = c_extra;
5929 saved_char_attr = char_attr;
5930 n_extra = 0;
5931 lcs_prec_todo = lcs_prec;
5932#ifdef FEAT_LINEBREAK
5933# ifdef FEAT_DIFF
5934 if (filler_todo <= 0)
5935# endif
5936 need_showbreak = TRUE;
5937#endif
5938#ifdef FEAT_DIFF
5939 --filler_todo;
5940 /* When the filler lines are actually below the last line of the
5941 * file, don't draw the line itself, break here. */
5942 if (filler_todo == 0 && wp->w_botfill)
5943 break;
5944#endif
5945 }
5946
5947 } /* for every character in the line */
5948
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005949#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005950 /* After an empty line check first word for capital. */
5951 if (*skipwhite(line) == NUL)
5952 {
5953 capcol_lnum = lnum + 1;
5954 cap_col = 0;
5955 }
5956#endif
5957
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005958 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 return row;
5960}
5961
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005962#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005963static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005964
5965/*
5966 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005967 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005968 */
5969 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005970comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005971{
5972 int i;
5973
5974 for (i = 0; i < Screen_mco; ++i)
5975 {
5976 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5977 return TRUE;
5978 if (ScreenLinesC[i][off_from] == 0)
5979 break;
5980 }
5981 return FALSE;
5982}
5983#endif
5984
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985/*
5986 * Check whether the given character needs redrawing:
5987 * - the (first byte of the) character is different
5988 * - the attributes are different
5989 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005990 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 */
5992 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005993char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 if (cols > 0
5996 && ((ScreenLines[off_from] != ScreenLines[off_to]
5997 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5998
5999#ifdef FEAT_MBYTE
6000 || (enc_dbcs != 0
6001 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6002 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6003 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6004 : (cols > 1 && ScreenLines[off_from + 1]
6005 != ScreenLines[off_to + 1])))
6006 || (enc_utf8
6007 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6008 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006009 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006010 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6011 && ScreenLines[off_from + 1]
6012 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013#endif
6014 ))
6015 return TRUE;
6016 return FALSE;
6017}
6018
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006019#if defined(FEAT_TERMINAL) || defined(PROTO)
6020/*
6021 * Return the index in ScreenLines[] for the current screen line.
6022 */
6023 int
6024screen_get_current_line_off()
6025{
6026 return (int)(current_ScreenLine - ScreenLines);
6027}
6028#endif
6029
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030/*
6031 * Move one "cooked" screen line to the screen, but only the characters that
6032 * have actually changed. Handle insert/delete character.
6033 * "coloff" gives the first column on the screen for this line.
6034 * "endcol" gives the columns where valid characters are.
6035 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6036 * needs to be cleared, negative otherwise.
6037 * "rlflag" is TRUE in a rightleft window:
6038 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6039 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6040 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006041 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006042screen_line(
6043 int row,
6044 int coloff,
6045 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006046 int clear_width,
6047 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048{
6049 unsigned off_from;
6050 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006051#ifdef FEAT_MBYTE
6052 unsigned max_off_from;
6053 unsigned max_off_to;
6054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 int force = FALSE; /* force update rest of the line */
6058 int redraw_this /* bool: does character need redraw? */
6059#ifdef FEAT_GUI
6060 = TRUE /* For GUI when while-loop empty */
6061#endif
6062 ;
6063 int redraw_next; /* redraw_this for next character */
6064#ifdef FEAT_MBYTE
6065 int clear_next = FALSE;
6066 int char_cells; /* 1: normal char */
6067 /* 2: occupies two display cells */
6068# define CHAR_CELLS char_cells
6069#else
6070# define CHAR_CELLS 1
6071#endif
6072
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006073 /* Check for illegal row and col, just in case. */
6074 if (row >= Rows)
6075 row = Rows - 1;
6076 if (endcol > Columns)
6077 endcol = Columns;
6078
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079# ifdef FEAT_CLIPBOARD
6080 clip_may_clear_selection(row, row);
6081# endif
6082
6083 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6084 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006085#ifdef FEAT_MBYTE
6086 max_off_from = off_from + screen_Columns;
6087 max_off_to = LineOffset[row] + screen_Columns;
6088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089
6090#ifdef FEAT_RIGHTLEFT
6091 if (rlflag)
6092 {
6093 /* Clear rest first, because it's left of the text. */
6094 if (clear_width > 0)
6095 {
6096 while (col <= endcol && ScreenLines[off_to] == ' '
6097 && ScreenAttrs[off_to] == 0
6098# ifdef FEAT_MBYTE
6099 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6100# endif
6101 )
6102 {
6103 ++off_to;
6104 ++col;
6105 }
6106 if (col <= endcol)
6107 screen_fill(row, row + 1, col + coloff,
6108 endcol + coloff + 1, ' ', ' ', 0);
6109 }
6110 col = endcol + 1;
6111 off_to = LineOffset[row] + col + coloff;
6112 off_from += col;
6113 endcol = (clear_width > 0 ? clear_width : -clear_width);
6114 }
6115#endif /* FEAT_RIGHTLEFT */
6116
6117 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6118
6119 while (col < endcol)
6120 {
6121#ifdef FEAT_MBYTE
6122 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006123 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 else
6125 char_cells = 1;
6126#endif
6127
6128 redraw_this = redraw_next;
6129 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6130 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6131
6132#ifdef FEAT_GUI
6133 /* If the next character was bold, then redraw the current character to
6134 * remove any pixels that might have spilt over into us. This only
6135 * happens in the GUI.
6136 */
6137 if (redraw_next && gui.in_use)
6138 {
6139 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006140 if (hl > HL_ALL)
6141 hl = syn_attr2attr(hl);
6142 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 redraw_this = TRUE;
6144 }
6145#endif
6146
6147 if (redraw_this)
6148 {
6149 /*
6150 * Special handling when 'xs' termcap flag set (hpterm):
6151 * Attributes for characters are stored at the position where the
6152 * cursor is when writing the highlighting code. The
6153 * start-highlighting code must be written with the cursor on the
6154 * first highlighted character. The stop-highlighting code must
6155 * be written with the cursor just after the last highlighted
6156 * character.
6157 * Overwriting a character doesn't remove it's highlighting. Need
6158 * to clear the rest of the line, and force redrawing it
6159 * completely.
6160 */
6161 if ( p_wiv
6162 && !force
6163#ifdef FEAT_GUI
6164 && !gui.in_use
6165#endif
6166 && ScreenAttrs[off_to] != 0
6167 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6168 {
6169 /*
6170 * Need to remove highlighting attributes here.
6171 */
6172 windgoto(row, col + coloff);
6173 out_str(T_CE); /* clear rest of this screen line */
6174 screen_start(); /* don't know where cursor is now */
6175 force = TRUE; /* force redraw of rest of the line */
6176 redraw_next = TRUE; /* or else next char would miss out */
6177
6178 /*
6179 * If the previous character was highlighted, need to stop
6180 * highlighting at this character.
6181 */
6182 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6183 {
6184 screen_attr = ScreenAttrs[off_to - 1];
6185 term_windgoto(row, col + coloff);
6186 screen_stop_highlight();
6187 }
6188 else
6189 screen_attr = 0; /* highlighting has stopped */
6190 }
6191#ifdef FEAT_MBYTE
6192 if (enc_dbcs != 0)
6193 {
6194 /* Check if overwriting a double-byte with a single-byte or
6195 * the other way around requires another character to be
6196 * redrawn. For UTF-8 this isn't needed, because comparing
6197 * ScreenLinesUC[] is sufficient. */
6198 if (char_cells == 1
6199 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006200 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 {
6202 /* Writing a single-cell character over a double-cell
6203 * character: need to redraw the next cell. */
6204 ScreenLines[off_to + 1] = 0;
6205 redraw_next = TRUE;
6206 }
6207 else if (char_cells == 2
6208 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006209 && (*mb_off2cells)(off_to, max_off_to) == 1
6210 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 {
6212 /* Writing the second half of a double-cell character over
6213 * a double-cell character: need to redraw the second
6214 * cell. */
6215 ScreenLines[off_to + 2] = 0;
6216 redraw_next = TRUE;
6217 }
6218
6219 if (enc_dbcs == DBCS_JPNU)
6220 ScreenLines2[off_to] = ScreenLines2[off_from];
6221 }
6222 /* When writing a single-width character over a double-width
6223 * character and at the end of the redrawn text, need to clear out
6224 * the right halve of the old character.
6225 * Also required when writing the right halve of a double-width
6226 * char over the left halve of an existing one. */
6227 if (has_mbyte && col + char_cells == endcol
6228 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006229 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006231 && (*mb_off2cells)(off_to, max_off_to) == 1
6232 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 clear_next = TRUE;
6234#endif
6235
6236 ScreenLines[off_to] = ScreenLines[off_from];
6237#ifdef FEAT_MBYTE
6238 if (enc_utf8)
6239 {
6240 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6241 if (ScreenLinesUC[off_from] != 0)
6242 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006243 int i;
6244
6245 for (i = 0; i < Screen_mco; ++i)
6246 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 }
6248 }
6249 if (char_cells == 2)
6250 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6251#endif
6252
6253#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006254 /* The bold trick makes a single column of pixels appear in the
6255 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 * character should be redrawn too. This happens for our own GUI
6257 * and for some xterms. */
6258 if (
6259# ifdef FEAT_GUI
6260 gui.in_use
6261# endif
6262# if defined(FEAT_GUI) && defined(UNIX)
6263 ||
6264# endif
6265# ifdef UNIX
6266 term_is_xterm
6267# endif
6268 )
6269 {
6270 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006271 if (hl > HL_ALL)
6272 hl = syn_attr2attr(hl);
6273 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 redraw_next = TRUE;
6275 }
6276#endif
6277 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6278#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006279 /* For simplicity set the attributes of second half of a
6280 * double-wide character equal to the first half. */
6281 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006283
6284 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 else
6287#endif
6288 screen_char(off_to, row, col + coloff);
6289 }
6290 else if ( p_wiv
6291#ifdef FEAT_GUI
6292 && !gui.in_use
6293#endif
6294 && col + coloff > 0)
6295 {
6296 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6297 {
6298 /*
6299 * Don't output stop-highlight when moving the cursor, it will
6300 * stop the highlighting when it should continue.
6301 */
6302 screen_attr = 0;
6303 }
6304 else if (screen_attr != 0)
6305 screen_stop_highlight();
6306 }
6307
6308 off_to += CHAR_CELLS;
6309 off_from += CHAR_CELLS;
6310 col += CHAR_CELLS;
6311 }
6312
6313#ifdef FEAT_MBYTE
6314 if (clear_next)
6315 {
6316 /* Clear the second half of a double-wide character of which the left
6317 * half was overwritten with a single-wide character. */
6318 ScreenLines[off_to] = ' ';
6319 if (enc_utf8)
6320 ScreenLinesUC[off_to] = 0;
6321 screen_char(off_to, row, col + coloff);
6322 }
6323#endif
6324
6325 if (clear_width > 0
6326#ifdef FEAT_RIGHTLEFT
6327 && !rlflag
6328#endif
6329 )
6330 {
6331#ifdef FEAT_GUI
6332 int startCol = col;
6333#endif
6334
6335 /* blank out the rest of the line */
6336 while (col < clear_width && ScreenLines[off_to] == ' '
6337 && ScreenAttrs[off_to] == 0
6338#ifdef FEAT_MBYTE
6339 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6340#endif
6341 )
6342 {
6343 ++off_to;
6344 ++col;
6345 }
6346 if (col < clear_width)
6347 {
6348#ifdef FEAT_GUI
6349 /*
6350 * In the GUI, clearing the rest of the line may leave pixels
6351 * behind if the first character cleared was bold. Some bold
6352 * fonts spill over the left. In this case we redraw the previous
6353 * character too. If we didn't skip any blanks above, then we
6354 * only redraw if the character wasn't already redrawn anyway.
6355 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006356 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 {
6358 hl = ScreenAttrs[off_to];
6359 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006360 {
6361 int prev_cells = 1;
6362# ifdef FEAT_MBYTE
6363 if (enc_utf8)
6364 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6365 * that its width is 2. */
6366 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6367 else if (enc_dbcs != 0)
6368 {
6369 /* find previous character by counting from first
6370 * column and get its width. */
6371 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006372 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006373
6374 while (off < off_to)
6375 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006376 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006377 off += prev_cells;
6378 }
6379 }
6380
6381 if (enc_dbcs != 0 && prev_cells > 1)
6382 screen_char_2(off_to - prev_cells, row,
6383 col + coloff - prev_cells);
6384 else
6385# endif
6386 screen_char(off_to - prev_cells, row,
6387 col + coloff - prev_cells);
6388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 }
6390#endif
6391 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6392 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 off_to += clear_width - col;
6394 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 }
6396 }
6397
6398 if (clear_width > 0)
6399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 /* For a window that's left of another, draw the separator char. */
6401 if (col + coloff < Columns)
6402 {
6403 int c;
6404
6405 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006406 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006407#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006408 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6409 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 || ScreenAttrs[off_to] != hl)
6412 {
6413 ScreenLines[off_to] = c;
6414 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006415#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 if (enc_utf8)
6417 {
6418 if (c >= 0x80)
6419 {
6420 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006421 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 }
6423 else
6424 ScreenLinesUC[off_to] = 0;
6425 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 screen_char(off_to, row, col + coloff);
6428 }
6429 }
6430 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 LineWraps[row] = FALSE;
6432 }
6433}
6434
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006435#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006437 * Mirror text "str" for right-left displaying.
6438 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006440 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006441rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442{
6443 char_u *p1, *p2;
6444 int t;
6445
6446 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6447 {
6448 t = *p1;
6449 *p1 = *p2;
6450 *p2 = t;
6451 }
6452}
6453#endif
6454
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455/*
6456 * mark all status lines for redraw; used after first :cd
6457 */
6458 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006459status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460{
6461 win_T *wp;
6462
Bram Moolenaar29323592016-07-24 22:04:11 +02006463 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 if (wp->w_status_height)
6465 {
6466 wp->w_redr_status = TRUE;
6467 redraw_later(VALID);
6468 }
6469}
6470
6471/*
6472 * mark all status lines of the current buffer for redraw
6473 */
6474 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006475status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476{
6477 win_T *wp;
6478
Bram Moolenaar29323592016-07-24 22:04:11 +02006479 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6481 {
6482 wp->w_redr_status = TRUE;
6483 redraw_later(VALID);
6484 }
6485}
6486
6487/*
6488 * Redraw all status lines that need to be redrawn.
6489 */
6490 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006491redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
6493 win_T *wp;
6494
Bram Moolenaar29323592016-07-24 22:04:11 +02006495 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 if (wp->w_redr_status)
6497 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006498 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006499 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501
Bram Moolenaar4033c552017-09-16 20:54:51 +02006502#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503/*
6504 * Redraw all status lines at the bottom of frame "frp".
6505 */
6506 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006507win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
6509 if (frp->fr_layout == FR_LEAF)
6510 frp->fr_win->w_redr_status = TRUE;
6511 else if (frp->fr_layout == FR_ROW)
6512 {
6513 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6514 win_redraw_last_status(frp);
6515 }
6516 else /* frp->fr_layout == FR_COL */
6517 {
6518 frp = frp->fr_child;
6519 while (frp->fr_next != NULL)
6520 frp = frp->fr_next;
6521 win_redraw_last_status(frp);
6522 }
6523}
6524#endif
6525
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526/*
6527 * Draw the verticap separator right of window "wp" starting with line "row".
6528 */
6529 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006530draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
6532 int hl;
6533 int c;
6534
6535 if (wp->w_vsep_width)
6536 {
6537 /* draw the vertical separator right of this window */
6538 c = fillchar_vsep(&hl);
6539 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6540 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6541 c, ' ', hl);
6542 }
6543}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544
6545#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006546static int status_match_len(expand_T *xp, char_u *s);
6547static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
6549/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006550 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 */
6552 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006553status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
6555 int len = 0;
6556
6557#ifdef FEAT_MENU
6558 int emenu = (xp->xp_context == EXPAND_MENUS
6559 || xp->xp_context == EXPAND_MENUNAMES);
6560
6561 /* Check for menu separators - replace with '|'. */
6562 if (emenu && menu_is_separator(s))
6563 return 1;
6564#endif
6565
6566 while (*s != NUL)
6567 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006568 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006569 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006570 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 }
6572
6573 return len;
6574}
6575
6576/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006577 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006578 * These are backslashes used for escaping. Do show backslashes in help tags.
6579 */
6580 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006581skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006582{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006583 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006584#ifdef FEAT_MENU
6585 || ((xp->xp_context == EXPAND_MENUS
6586 || xp->xp_context == EXPAND_MENUNAMES)
6587 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6588#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006589 )
6590 {
6591#ifndef BACKSLASH_IN_FILENAME
6592 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6593 return 2;
6594#endif
6595 return 1;
6596 }
6597 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006598}
6599
6600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 * Show wildchar matches in the status line.
6602 * Show at least the "match" item.
6603 * We start at item 'first_match' in the list and show all matches that fit.
6604 *
6605 * If inversion is possible we use it. Else '=' characters are used.
6606 */
6607 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006608win_redr_status_matches(
6609 expand_T *xp,
6610 int num_matches,
6611 char_u **matches, /* list of matches */
6612 int match,
6613 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614{
6615#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6616 int row;
6617 char_u *buf;
6618 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006619 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 int fillchar;
6621 int attr;
6622 int i;
6623 int highlight = TRUE;
6624 char_u *selstart = NULL;
6625 int selstart_col = 0;
6626 char_u *selend = NULL;
6627 static int first_match = 0;
6628 int add_left = FALSE;
6629 char_u *s;
6630#ifdef FEAT_MENU
6631 int emenu;
6632#endif
6633#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6634 int l;
6635#endif
6636
6637 if (matches == NULL) /* interrupted completion? */
6638 return;
6639
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006640#ifdef FEAT_MBYTE
6641 if (has_mbyte)
6642 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6643 else
6644#endif
6645 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 if (buf == NULL)
6647 return;
6648
6649 if (match == -1) /* don't show match but original text */
6650 {
6651 match = 0;
6652 highlight = FALSE;
6653 }
6654 /* count 1 for the ending ">" */
6655 clen = status_match_len(xp, L_MATCH(match)) + 3;
6656 if (match == 0)
6657 first_match = 0;
6658 else if (match < first_match)
6659 {
6660 /* jumping left, as far as we can go */
6661 first_match = match;
6662 add_left = TRUE;
6663 }
6664 else
6665 {
6666 /* check if match fits on the screen */
6667 for (i = first_match; i < match; ++i)
6668 clen += status_match_len(xp, L_MATCH(i)) + 2;
6669 if (first_match > 0)
6670 clen += 2;
6671 /* jumping right, put match at the left */
6672 if ((long)clen > Columns)
6673 {
6674 first_match = match;
6675 /* if showing the last match, we can add some on the left */
6676 clen = 2;
6677 for (i = match; i < num_matches; ++i)
6678 {
6679 clen += status_match_len(xp, L_MATCH(i)) + 2;
6680 if ((long)clen >= Columns)
6681 break;
6682 }
6683 if (i == num_matches)
6684 add_left = TRUE;
6685 }
6686 }
6687 if (add_left)
6688 while (first_match > 0)
6689 {
6690 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6691 if ((long)clen >= Columns)
6692 break;
6693 --first_match;
6694 }
6695
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006696 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697
6698 if (first_match == 0)
6699 {
6700 *buf = NUL;
6701 len = 0;
6702 }
6703 else
6704 {
6705 STRCPY(buf, "< ");
6706 len = 2;
6707 }
6708 clen = len;
6709
6710 i = first_match;
6711 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6712 {
6713 if (i == match)
6714 {
6715 selstart = buf + len;
6716 selstart_col = clen;
6717 }
6718
6719 s = L_MATCH(i);
6720 /* Check for menu separators - replace with '|' */
6721#ifdef FEAT_MENU
6722 emenu = (xp->xp_context == EXPAND_MENUS
6723 || xp->xp_context == EXPAND_MENUNAMES);
6724 if (emenu && menu_is_separator(s))
6725 {
6726 STRCPY(buf + len, transchar('|'));
6727 l = (int)STRLEN(buf + len);
6728 len += l;
6729 clen += l;
6730 }
6731 else
6732#endif
6733 for ( ; *s != NUL; ++s)
6734 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006735 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 clen += ptr2cells(s);
6737#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006738 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {
6740 STRNCPY(buf + len, s, l);
6741 s += l - 1;
6742 len += l;
6743 }
6744 else
6745#endif
6746 {
6747 STRCPY(buf + len, transchar_byte(*s));
6748 len += (int)STRLEN(buf + len);
6749 }
6750 }
6751 if (i == match)
6752 selend = buf + len;
6753
6754 *(buf + len++) = ' ';
6755 *(buf + len++) = ' ';
6756 clen += 2;
6757 if (++i == num_matches)
6758 break;
6759 }
6760
6761 if (i != num_matches)
6762 {
6763 *(buf + len++) = '>';
6764 ++clen;
6765 }
6766
6767 buf[len] = NUL;
6768
6769 row = cmdline_row - 1;
6770 if (row >= 0)
6771 {
6772 if (wild_menu_showing == 0)
6773 {
6774 if (msg_scrolled > 0)
6775 {
6776 /* Put the wildmenu just above the command line. If there is
6777 * no room, scroll the screen one line up. */
6778 if (cmdline_row == Rows - 1)
6779 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006780 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 ++msg_scrolled;
6782 }
6783 else
6784 {
6785 ++cmdline_row;
6786 ++row;
6787 }
6788 wild_menu_showing = WM_SCROLLED;
6789 }
6790 else
6791 {
6792 /* Create status line if needed by setting 'laststatus' to 2.
6793 * Set 'winminheight' to zero to avoid that the window is
6794 * resized. */
6795 if (lastwin->w_status_height == 0)
6796 {
6797 save_p_ls = p_ls;
6798 save_p_wmh = p_wmh;
6799 p_ls = 2;
6800 p_wmh = 0;
6801 last_status(FALSE);
6802 }
6803 wild_menu_showing = WM_SHOWN;
6804 }
6805 }
6806
6807 screen_puts(buf, row, 0, attr);
6808 if (selstart != NULL && highlight)
6809 {
6810 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006811 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 }
6813
6814 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6815 }
6816
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 vim_free(buf);
6819}
6820#endif
6821
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822/*
6823 * Redraw the status line of window wp.
6824 *
6825 * If inversion is possible we use it. Else '=' characters are used.
6826 */
6827 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006828win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829{
6830 int row;
6831 char_u *p;
6832 int len;
6833 int fillchar;
6834 int attr;
6835 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006836 static int busy = FALSE;
6837
6838 /* It's possible to get here recursively when 'statusline' (indirectly)
6839 * invokes ":redrawstatus". Simply ignore the call then. */
6840 if (busy)
6841 return;
6842 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843
6844 wp->w_redr_status = FALSE;
6845 if (wp->w_status_height == 0)
6846 {
6847 /* no status line, can only be last window */
6848 redraw_cmdline = TRUE;
6849 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006850 else if (!redrawing()
6851#ifdef FEAT_INS_EXPAND
6852 /* don't update status line when popup menu is visible and may be
6853 * drawn over it */
6854 || pum_visible()
6855#endif
6856 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 {
6858 /* Don't redraw right now, do it later. */
6859 wp->w_redr_status = TRUE;
6860 }
6861#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006862 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 {
6864 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006865 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 }
6867#endif
6868 else
6869 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006870 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006872 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 p = NameBuff;
6874 len = (int)STRLEN(p);
6875
Bram Moolenaard85f2712017-07-28 21:51:57 +02006876 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877#ifdef FEAT_QUICKFIX
6878 || wp->w_p_pvw
6879#endif
6880 || bufIsChanged(wp->w_buffer)
6881 || wp->w_buffer->b_p_ro)
6882 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006883 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006885 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 len += (int)STRLEN(p + len);
6887 }
6888#ifdef FEAT_QUICKFIX
6889 if (wp->w_p_pvw)
6890 {
6891 STRCPY(p + len, _("[Preview]"));
6892 len += (int)STRLEN(p + len);
6893 }
6894#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006895 if (bufIsChanged(wp->w_buffer)
6896#ifdef FEAT_TERMINAL
6897 && !bt_terminal(wp->w_buffer)
6898#endif
6899 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {
6901 STRCPY(p + len, "[+]");
6902 len += 3;
6903 }
6904 if (wp->w_buffer->b_p_ro)
6905 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006906 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006907 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 }
6909
Bram Moolenaar02631462017-09-22 15:20:32 +02006910 this_ru_col = ru_col - (Columns - wp->w_width);
6911 if (this_ru_col < (wp->w_width + 1) / 2)
6912 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 if (this_ru_col <= 1)
6914 {
6915 p = (char_u *)"<"; /* No room for file name! */
6916 len = 1;
6917 }
6918 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919#ifdef FEAT_MBYTE
6920 if (has_mbyte)
6921 {
6922 int clen = 0, i;
6923
6924 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006925 clen = mb_string2cells(p, -1);
6926
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 /* Find first character that will fit.
6928 * Going from start to end is much faster for DBCS. */
6929 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006930 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 clen -= (*mb_ptr2cells)(p + i);
6932 len = clen;
6933 if (i > 0)
6934 {
6935 p = p + i - 1;
6936 *p = '<';
6937 ++len;
6938 }
6939
6940 }
6941 else
6942#endif
6943 if (len > this_ru_col - 1)
6944 {
6945 p += len - (this_ru_col - 1);
6946 *p = '<';
6947 len = this_ru_col - 1;
6948 }
6949
6950 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006951 screen_puts(p, row, wp->w_wincol, attr);
6952 screen_fill(row, row + 1, len + wp->w_wincol,
6953 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006955 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6957 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006958 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
6960#ifdef FEAT_CMDL_INFO
6961 win_redr_ruler(wp, TRUE);
6962#endif
6963 }
6964
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 /*
6966 * May need to draw the character below the vertical separator.
6967 */
6968 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6969 {
6970 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006971 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 else
6973 fillchar = fillchar_vsep(&attr);
6974 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6975 attr);
6976 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006977 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978}
6979
Bram Moolenaar238a5642006-02-21 22:12:05 +00006980#ifdef FEAT_STL_OPT
6981/*
6982 * Redraw the status line according to 'statusline' and take care of any
6983 * errors encountered.
6984 */
6985 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006986redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006987{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006988 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006989 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006990
6991 /* When called recursively return. This can happen when the statusline
6992 * contains an expression that triggers a redraw. */
6993 if (entered)
6994 return;
6995 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006996
Bram Moolenaara742e082016-04-05 21:10:38 +02006997 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006998 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006999 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007000 {
7001 /* When there is an error disable the statusline, otherwise the
7002 * display is messed up with errors and a redraw triggers the problem
7003 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007004 set_string_option_direct((char_u *)"statusline", -1,
7005 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007006 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007007 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007008 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007009 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007010}
7011#endif
7012
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013/*
7014 * Return TRUE if the status line of window "wp" is connected to the status
7015 * line of the window right of it. If not, then it's a vertical separator.
7016 * Only call if (wp->w_vsep_width != 0).
7017 */
7018 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007019stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
7021 frame_T *fr;
7022
7023 fr = wp->w_frame;
7024 while (fr->fr_parent != NULL)
7025 {
7026 if (fr->fr_parent->fr_layout == FR_COL)
7027 {
7028 if (fr->fr_next != NULL)
7029 break;
7030 }
7031 else
7032 {
7033 if (fr->fr_next != NULL)
7034 return TRUE;
7035 }
7036 fr = fr->fr_parent;
7037 }
7038 return FALSE;
7039}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042/*
7043 * Get the value to show for the language mappings, active 'keymap'.
7044 */
7045 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007046get_keymap_str(
7047 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007048 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007049 char_u *buf, /* buffer for the result */
7050 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 char_u *p;
7053
7054 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7055 return FALSE;
7056
7057 {
7058#ifdef FEAT_EVAL
7059 buf_T *old_curbuf = curbuf;
7060 win_T *old_curwin = curwin;
7061 char_u *s;
7062
7063 curbuf = wp->w_buffer;
7064 curwin = wp;
7065 STRCPY(buf, "b:keymap_name"); /* must be writable */
7066 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007067 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 --emsg_skip;
7069 curbuf = old_curbuf;
7070 curwin = old_curwin;
7071 if (p == NULL || *p == NUL)
7072#endif
7073 {
7074#ifdef FEAT_KEYMAP
7075 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7076 p = wp->w_buffer->b_p_keymap;
7077 else
7078#endif
7079 p = (char_u *)"lang";
7080 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007081 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 buf[0] = NUL;
7083#ifdef FEAT_EVAL
7084 vim_free(s);
7085#endif
7086 }
7087 return buf[0] != NUL;
7088}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
7090#if defined(FEAT_STL_OPT) || defined(PROTO)
7091/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007092 * Redraw the status line or ruler of window "wp".
7093 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 */
7095 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007096win_redr_custom(
7097 win_T *wp,
7098 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007100 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 int attr;
7102 int curattr;
7103 int row;
7104 int col = 0;
7105 int maxwidth;
7106 int width;
7107 int n;
7108 int len;
7109 int fillchar;
7110 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007111 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007113 struct stl_hlrec hltab[STL_MAX_ITEM];
7114 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007115 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007116 win_T *ewp;
7117 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
Bram Moolenaar1d633412013-12-11 15:52:01 +01007119 /* There is a tiny chance that this gets called recursively: When
7120 * redrawing a status line triggers redrawing the ruler or tabline.
7121 * Avoid trouble by not allowing recursion. */
7122 if (entered)
7123 return;
7124 entered = TRUE;
7125
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007127 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007129 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007130 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007131 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007132 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007133 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007134 maxwidth = Columns;
7135# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007136 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007137# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007139 else
7140 {
7141 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007142 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007143 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007144
7145 if (draw_ruler)
7146 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007147 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007148 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007149 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007150 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007151 if (*++stl == '-')
7152 stl++;
7153 if (atoi((char *)stl))
7154 while (VIM_ISDIGIT(*stl))
7155 stl++;
7156 if (*stl++ != '(')
7157 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007158 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007159 col = ru_col - (Columns - wp->w_width);
7160 if (col < (wp->w_width + 1) / 2)
7161 col = (wp->w_width + 1) / 2;
7162 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007163 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007164 {
7165 row = Rows - 1;
7166 --maxwidth; /* writing in last column may cause scrolling */
7167 fillchar = ' ';
7168 attr = 0;
7169 }
7170
7171# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007172 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007173# endif
7174 }
7175 else
7176 {
7177 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007178 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007179 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007180 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007181# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007182 use_sandbox = was_set_insecurely((char_u *)"statusline",
7183 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007184# endif
7185 }
7186
Bram Moolenaar53f81742017-09-22 14:35:51 +02007187 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007188 }
7189
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007191 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
Bram Moolenaar61452852011-02-01 18:01:11 +01007193 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7194 * the cursor away and back. */
7195 ewp = wp == NULL ? curwin : wp;
7196 p_crb_save = ewp->w_p_crb;
7197 ewp->w_p_crb = FALSE;
7198
Bram Moolenaar362f3562009-11-03 16:20:34 +00007199 /* Make a copy, because the statusline may include a function call that
7200 * might change the option value and free the memory. */
7201 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007202 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007203 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007204 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007205 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007206 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007208 /* Make all characters printable. */
7209 p = transstr(buf);
7210 if (p != NULL)
7211 {
7212 vim_strncpy(buf, p, sizeof(buf) - 1);
7213 vim_free(p);
7214 }
7215
7216 /* fill up with "fillchar" */
7217 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007218 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
7220#ifdef FEAT_MBYTE
7221 len += (*mb_char2bytes)(fillchar, buf + len);
7222#else
7223 buf[len++] = fillchar;
7224#endif
7225 ++width;
7226 }
7227 buf[len] = NUL;
7228
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007229 /*
7230 * Draw each snippet with the specified highlighting.
7231 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 curattr = attr;
7233 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007234 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007236 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 screen_puts_len(p, len, row, col, curattr);
7238 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007239 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007241 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007243 else if (hltab[n].userhl < 0)
7244 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007245#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007246 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7247 && wp->w_status_height != 0)
7248 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007249 else if (wp != NULL && bt_terminal(wp->w_buffer)
7250 && wp->w_status_height != 0)
7251 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007252#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007253 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007254 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007256 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 }
7258 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007259
7260 if (wp == NULL)
7261 {
7262 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7263 col = 0;
7264 len = 0;
7265 p = buf;
7266 fillchar = 0;
7267 for (n = 0; tabtab[n].start != NULL; n++)
7268 {
7269 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7270 while (col < len)
7271 TabPageIdxs[col++] = fillchar;
7272 p = tabtab[n].start;
7273 fillchar = tabtab[n].userhl;
7274 }
7275 while (col < Columns)
7276 TabPageIdxs[col++] = fillchar;
7277 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007278
7279theend:
7280 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281}
7282
7283#endif /* FEAT_STL_OPT */
7284
7285/*
7286 * Output a single character directly to the screen and update ScreenLines.
7287 */
7288 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007289screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 char_u buf[MB_MAXBYTES + 1];
7292
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007293#ifdef FEAT_MBYTE
7294 if (has_mbyte)
7295 buf[(*mb_char2bytes)(c, buf)] = NUL;
7296 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007298 {
7299 buf[0] = c;
7300 buf[1] = NUL;
7301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 screen_puts(buf, row, col, attr);
7303}
7304
7305/*
7306 * Get a single character directly from ScreenLines into "bytes[]".
7307 * Also return its attribute in *attrp;
7308 */
7309 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007310screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311{
7312 unsigned off;
7313
7314 /* safety check */
7315 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7316 {
7317 off = LineOffset[row] + col;
7318 *attrp = ScreenAttrs[off];
7319 bytes[0] = ScreenLines[off];
7320 bytes[1] = NUL;
7321
7322#ifdef FEAT_MBYTE
7323 if (enc_utf8 && ScreenLinesUC[off] != 0)
7324 bytes[utfc_char2bytes(off, bytes)] = NUL;
7325 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7326 {
7327 bytes[0] = ScreenLines[off];
7328 bytes[1] = ScreenLines2[off];
7329 bytes[2] = NUL;
7330 }
7331 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7332 {
7333 bytes[1] = ScreenLines[off + 1];
7334 bytes[2] = NUL;
7335 }
7336#endif
7337 }
7338}
7339
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007340#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007341static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007342
7343/*
7344 * Return TRUE if composing characters for screen posn "off" differs from
7345 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007346 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007347 */
7348 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007349screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007350{
7351 int i;
7352
7353 for (i = 0; i < Screen_mco; ++i)
7354 {
7355 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7356 return TRUE;
7357 if (u8cc[i] == 0)
7358 break;
7359 }
7360 return FALSE;
7361}
7362#endif
7363
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364/*
7365 * Put string '*text' on the screen at position 'row' and 'col', with
7366 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7367 * Note: only outputs within one row, message is truncated at screen boundary!
7368 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7369 */
7370 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007371screen_puts(
7372 char_u *text,
7373 int row,
7374 int col,
7375 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376{
7377 screen_puts_len(text, -1, row, col, attr);
7378}
7379
7380/*
7381 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7382 * a NUL.
7383 */
7384 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007385screen_puts_len(
7386 char_u *text,
7387 int textlen,
7388 int row,
7389 int col,
7390 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
7392 unsigned off;
7393 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007394 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 int c;
7396#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007397 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 int mbyte_blen = 1;
7399 int mbyte_cells = 1;
7400 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007401 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 int clear_next_cell = FALSE;
7403# ifdef FEAT_ARABIC
7404 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007405 int pc, nc, nc1;
7406 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407# endif
7408#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007409#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7410 int force_redraw_this;
7411 int force_redraw_next = FALSE;
7412#endif
7413 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
7415 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7416 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007417 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418
Bram Moolenaarc236c162008-07-13 17:41:49 +00007419#ifdef FEAT_MBYTE
7420 /* When drawing over the right halve of a double-wide char clear out the
7421 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007422 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007423# ifdef FEAT_GUI
7424 && !gui.in_use
7425# endif
7426 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007427 {
7428 ScreenLines[off - 1] = ' ';
7429 ScreenAttrs[off - 1] = 0;
7430 if (enc_utf8)
7431 {
7432 ScreenLinesUC[off - 1] = 0;
7433 ScreenLinesC[0][off - 1] = 0;
7434 }
7435 /* redraw the previous cell, make it empty */
7436 screen_char(off - 1, row, col - 1);
7437 /* force the cell at "col" to be redrawn */
7438 force_redraw_next = TRUE;
7439 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007440#endif
7441
Bram Moolenaar367329b2007-08-30 11:53:22 +00007442#ifdef FEAT_MBYTE
7443 max_off = LineOffset[row] + screen_Columns;
7444#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007445 while (col < screen_Columns
7446 && (len < 0 || (int)(ptr - text) < len)
7447 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 {
7449 c = *ptr;
7450#ifdef FEAT_MBYTE
7451 /* check if this is the first byte of a multibyte */
7452 if (has_mbyte)
7453 {
7454 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007455 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007457 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7459 mbyte_cells = 1;
7460 else if (enc_dbcs != 0)
7461 mbyte_cells = mbyte_blen;
7462 else /* enc_utf8 */
7463 {
7464 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007465 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 (int)((text + len) - ptr));
7467 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007468 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007470# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 /* Non-BMP character: display as ? or fullwidth ?. */
7472 if (u8c >= 0x10000)
7473 {
7474 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7475 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007476 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007478# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479# ifdef FEAT_ARABIC
7480 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7481 {
7482 /* Do Arabic shaping. */
7483 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7484 {
7485 /* Past end of string to be displayed. */
7486 nc = NUL;
7487 nc1 = NUL;
7488 }
7489 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007490 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007491 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7492 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007493 nc1 = pcc[0];
7494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 pc = prev_c;
7496 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007497 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 }
7499 else
7500 prev_c = u8c;
7501# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007502 if (col + mbyte_cells > screen_Columns)
7503 {
7504 /* Only 1 cell left, but character requires 2 cells:
7505 * display a '>' in the last column to avoid wrapping. */
7506 c = '>';
7507 mbyte_cells = 1;
7508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 }
7510 }
7511#endif
7512
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007513#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7514 force_redraw_this = force_redraw_next;
7515 force_redraw_next = FALSE;
7516#endif
7517
7518 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519#ifdef FEAT_MBYTE
7520 || (mbyte_cells == 2
7521 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7522 || (enc_dbcs == DBCS_JPNU
7523 && c == 0x8e
7524 && ScreenLines2[off] != ptr[1])
7525 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007526 && (ScreenLinesUC[off] !=
7527 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7528 || (ScreenLinesUC[off] != 0
7529 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530#endif
7531 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007532 || exmode_active;
7533
7534 if (need_redraw
7535#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7536 || force_redraw_this
7537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 )
7539 {
7540#if defined(FEAT_GUI) || defined(UNIX)
7541 /* The bold trick makes a single row of pixels appear in the next
7542 * character. When a bold character is removed, the next
7543 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007544 * and for some xterms. */
7545 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546# ifdef FEAT_GUI
7547 gui.in_use
7548# endif
7549# if defined(FEAT_GUI) && defined(UNIX)
7550 ||
7551# endif
7552# ifdef UNIX
7553 term_is_xterm
7554# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007555 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007557 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007559 if (n > HL_ALL)
7560 n = syn_attr2attr(n);
7561 if (n & HL_BOLD)
7562 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 }
7564#endif
7565#ifdef FEAT_MBYTE
7566 /* When at the end of the text and overwriting a two-cell
7567 * character with a one-cell character, need to clear the next
7568 * cell. Also when overwriting the left halve of a two-cell char
7569 * with the right halve of a two-cell char. Do this only once
7570 * (mb_off2cells() may return 2 on the right halve). */
7571 if (clear_next_cell)
7572 clear_next_cell = FALSE;
7573 else if (has_mbyte
7574 && (len < 0 ? ptr[mbyte_blen] == NUL
7575 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007576 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007578 && (*mb_off2cells)(off, max_off) == 1
7579 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 clear_next_cell = TRUE;
7581
7582 /* Make sure we never leave a second byte of a double-byte behind,
7583 * it confuses mb_off2cells(). */
7584 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007585 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007587 && (*mb_off2cells)(off, max_off) == 1
7588 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 ScreenLines[off + mbyte_blen] = 0;
7590#endif
7591 ScreenLines[off] = c;
7592 ScreenAttrs[off] = attr;
7593#ifdef FEAT_MBYTE
7594 if (enc_utf8)
7595 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007596 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 ScreenLinesUC[off] = 0;
7598 else
7599 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007600 int i;
7601
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007603 for (i = 0; i < Screen_mco; ++i)
7604 {
7605 ScreenLinesC[i][off] = u8cc[i];
7606 if (u8cc[i] == 0)
7607 break;
7608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 }
7610 if (mbyte_cells == 2)
7611 {
7612 ScreenLines[off + 1] = 0;
7613 ScreenAttrs[off + 1] = attr;
7614 }
7615 screen_char(off, row, col);
7616 }
7617 else if (mbyte_cells == 2)
7618 {
7619 ScreenLines[off + 1] = ptr[1];
7620 ScreenAttrs[off + 1] = attr;
7621 screen_char_2(off, row, col);
7622 }
7623 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7624 {
7625 ScreenLines2[off] = ptr[1];
7626 screen_char(off, row, col);
7627 }
7628 else
7629#endif
7630 screen_char(off, row, col);
7631 }
7632#ifdef FEAT_MBYTE
7633 if (has_mbyte)
7634 {
7635 off += mbyte_cells;
7636 col += mbyte_cells;
7637 ptr += mbyte_blen;
7638 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007639 {
7640 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007642 len = -1;
7643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 }
7645 else
7646#endif
7647 {
7648 ++off;
7649 ++col;
7650 ++ptr;
7651 }
7652 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007653
7654#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7655 /* If we detected the next character needs to be redrawn, but the text
7656 * doesn't extend up to there, update the character here. */
7657 if (force_redraw_next && col < screen_Columns)
7658 {
7659# ifdef FEAT_MBYTE
7660 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7661 screen_char_2(off, row, col);
7662 else
7663# endif
7664 screen_char(off, row, col);
7665 }
7666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667}
7668
7669#ifdef FEAT_SEARCH_EXTRA
7670/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007671 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 */
7673 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007674start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675{
7676 if (p_hls && !no_hlsearch)
7677 {
7678 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007679 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007680# ifdef FEAT_RELTIME
7681 /* Set the time limit to 'redrawtime'. */
7682 profile_setlimit(p_rdt, &search_hl.tm);
7683# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 }
7685}
7686
7687/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007688 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 */
7690 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007691end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
7693 if (search_hl.rm.regprog != NULL)
7694 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007695 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 search_hl.rm.regprog = NULL;
7697 }
7698}
7699
7700/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007701 * Init for calling prepare_search_hl().
7702 */
7703 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007704init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007705{
7706 matchitem_T *cur;
7707
7708 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7709 * match */
7710 cur = wp->w_match_head;
7711 while (cur != NULL)
7712 {
7713 cur->hl.rm = cur->match;
7714 if (cur->hlg_id == 0)
7715 cur->hl.attr = 0;
7716 else
7717 cur->hl.attr = syn_id2attr(cur->hlg_id);
7718 cur->hl.buf = wp->w_buffer;
7719 cur->hl.lnum = 0;
7720 cur->hl.first_lnum = 0;
7721# ifdef FEAT_RELTIME
7722 /* Set the time limit to 'redrawtime'. */
7723 profile_setlimit(p_rdt, &(cur->hl.tm));
7724# endif
7725 cur = cur->next;
7726 }
7727 search_hl.buf = wp->w_buffer;
7728 search_hl.lnum = 0;
7729 search_hl.first_lnum = 0;
7730 /* time limit is set at the toplevel, for all windows */
7731}
7732
7733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 * Advance to the match in window "wp" line "lnum" or past it.
7735 */
7736 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007737prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007739 matchitem_T *cur; /* points to the match list */
7740 match_T *shl; /* points to search_hl or a match */
7741 int shl_flag; /* flag to indicate whether search_hl
7742 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007743 int pos_inprogress; /* marks that position match search is
7744 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 int n;
7746
7747 /*
7748 * When using a multi-line pattern, start searching at the top
7749 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007750 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007752 cur = wp->w_match_head;
7753 shl_flag = FALSE;
7754 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007756 if (shl_flag == FALSE)
7757 {
7758 shl = &search_hl;
7759 shl_flag = TRUE;
7760 }
7761 else
7762 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if (shl->rm.regprog != NULL
7764 && shl->lnum == 0
7765 && re_multiline(shl->rm.regprog))
7766 {
7767 if (shl->first_lnum == 0)
7768 {
7769# ifdef FEAT_FOLDING
7770 for (shl->first_lnum = lnum;
7771 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7772 if (hasFoldingWin(wp, shl->first_lnum - 1,
7773 NULL, NULL, TRUE, NULL))
7774 break;
7775# else
7776 shl->first_lnum = wp->w_topline;
7777# endif
7778 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007779 if (cur != NULL)
7780 cur->pos.cur = 0;
7781 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007783 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7784 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007786 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7787 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007788 pos_inprogress = cur == NULL || cur->pos.cur == 0
7789 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 if (shl->lnum != 0)
7791 {
7792 shl->first_lnum = shl->lnum
7793 + shl->rm.endpos[0].lnum
7794 - shl->rm.startpos[0].lnum;
7795 n = shl->rm.endpos[0].col;
7796 }
7797 else
7798 {
7799 ++shl->first_lnum;
7800 n = 0;
7801 }
7802 }
7803 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007804 if (shl != &search_hl && cur != NULL)
7805 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 }
7807}
7808
7809/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007810 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 * Uses shl->buf.
7812 * Sets shl->lnum and shl->rm contents.
7813 * Note: Assumes a previous match is always before "lnum", unless
7814 * shl->lnum is zero.
7815 * Careful: Any pointers for buffer lines will become invalid.
7816 */
7817 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007818next_search_hl(
7819 win_T *win,
7820 match_T *shl, /* points to search_hl or a match */
7821 linenr_T lnum,
7822 colnr_T mincol, /* minimal column for a match */
7823 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824{
7825 linenr_T l;
7826 colnr_T matchcol;
7827 long nmatched;
7828
7829 if (shl->lnum != 0)
7830 {
7831 /* Check for three situations:
7832 * 1. If the "lnum" is below a previous match, start a new search.
7833 * 2. If the previous match includes "mincol", use it.
7834 * 3. Continue after the previous match.
7835 */
7836 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7837 if (lnum > l)
7838 shl->lnum = 0;
7839 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7840 return;
7841 }
7842
7843 /*
7844 * Repeat searching for a match until one is found that includes "mincol"
7845 * or none is found in this line.
7846 */
7847 called_emsg = FALSE;
7848 for (;;)
7849 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007850#ifdef FEAT_RELTIME
7851 /* Stop searching after passing the time limit. */
7852 if (profile_passed_limit(&(shl->tm)))
7853 {
7854 shl->lnum = 0; /* no match found in time */
7855 break;
7856 }
7857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 /* Three situations:
7859 * 1. No useful previous match: search from start of line.
7860 * 2. Not Vi compatible or empty match: continue at next character.
7861 * Break the loop if this is beyond the end of the line.
7862 * 3. Vi compatible searching: continue at end of previous match.
7863 */
7864 if (shl->lnum == 0)
7865 matchcol = 0;
7866 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7867 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007868 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007870 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007871
7872 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007873 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007874 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007876 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 shl->lnum = 0;
7878 break;
7879 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007880#ifdef FEAT_MBYTE
7881 if (has_mbyte)
7882 matchcol += mb_ptr2len(ml);
7883 else
7884#endif
7885 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 }
7887 else
7888 matchcol = shl->rm.endpos[0].col;
7889
7890 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007891 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007893 /* Remember whether shl->rm is using a copy of the regprog in
7894 * cur->match. */
7895 int regprog_is_copy = (shl != &search_hl && cur != NULL
7896 && shl == &cur->hl
7897 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007898 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007899
Bram Moolenaarb3414592014-06-17 17:48:32 +02007900 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7901 matchcol,
7902#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007903 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007904#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007905 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007906#endif
7907 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007908 /* Copy the regprog, in case it got freed and recompiled. */
7909 if (regprog_is_copy)
7910 cur->match.regprog = cur->hl.rm.regprog;
7911
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007912 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007913 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007914 /* Error while handling regexp: stop using this regexp. */
7915 if (shl == &search_hl)
7916 {
7917 /* don't free regprog in the match list, it's a copy */
7918 vim_regfree(shl->rm.regprog);
7919 SET_NO_HLSEARCH(TRUE);
7920 }
7921 shl->rm.regprog = NULL;
7922 shl->lnum = 0;
7923 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7924 message */
7925 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007926 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007927 }
7928 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007929 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007930 else
7931 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 if (nmatched == 0)
7933 {
7934 shl->lnum = 0; /* no match found */
7935 break;
7936 }
7937 if (shl->rm.startpos[0].lnum > 0
7938 || shl->rm.startpos[0].col >= mincol
7939 || nmatched > 1
7940 || shl->rm.endpos[0].col > mincol)
7941 {
7942 shl->lnum += shl->rm.startpos[0].lnum;
7943 break; /* useful match found */
7944 }
7945 }
7946}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947
Bram Moolenaar85077472016-10-16 14:35:48 +02007948/*
7949 * If there is a match fill "shl" and return one.
7950 * Return zero otherwise.
7951 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007952 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007953next_search_hl_pos(
7954 match_T *shl, /* points to a match */
7955 linenr_T lnum,
7956 posmatch_T *posmatch, /* match positions */
7957 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007958{
7959 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007960 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961
Bram Moolenaarb3414592014-06-17 17:48:32 +02007962 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7963 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007964 llpos_T *pos = &posmatch->pos[i];
7965
7966 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007967 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007968 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007969 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007970 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007971 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007972 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007973 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007974 /* if this match comes before the one at "found" then swap
7975 * them */
7976 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007977 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007978 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007979
Bram Moolenaar85077472016-10-16 14:35:48 +02007980 *pos = posmatch->pos[found];
7981 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007982 }
7983 }
7984 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007985 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007986 }
7987 }
7988 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007989 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007991 colnr_T start = posmatch->pos[found].col == 0
7992 ? 0 : posmatch->pos[found].col - 1;
7993 colnr_T end = posmatch->pos[found].col == 0
7994 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007995
Bram Moolenaar85077472016-10-16 14:35:48 +02007996 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007997 shl->rm.startpos[0].lnum = 0;
7998 shl->rm.startpos[0].col = start;
7999 shl->rm.endpos[0].lnum = 0;
8000 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008001 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008002 posmatch->cur = found + 1;
8003 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008004 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008005 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008006}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008007#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008008
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008010screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011{
8012 attrentry_T *aep = NULL;
8013
8014 screen_attr = attr;
8015 if (full_screen
8016#ifdef WIN3264
8017 && termcap_active
8018#endif
8019 )
8020 {
8021#ifdef FEAT_GUI
8022 if (gui.in_use)
8023 {
8024 char buf[20];
8025
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008026 /* The GUI handles this internally. */
8027 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 OUT_STR(buf);
8029 }
8030 else
8031#endif
8032 {
8033 if (attr > HL_ALL) /* special HL attr. */
8034 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008035 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 aep = syn_cterm_attr2entry(attr);
8037 else
8038 aep = syn_term_attr2entry(attr);
8039 if (aep == NULL) /* did ":syntax clear" */
8040 attr = 0;
8041 else
8042 attr = aep->ae_attr;
8043 }
8044 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8045 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008046 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008047#ifdef FEAT_TERMGUICOLORS
8048 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008049 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008050#endif
8051 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008052#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008053 )
8054#endif
8055 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008056 /* If the Normal FG color has BOLD attribute and the new HL
8057 * has a FG color defined, clear BOLD. */
8058 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8060 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008061 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8062 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 out_str(T_US);
8064 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8065 out_str(T_CZH);
8066 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8067 out_str(T_MR);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008068 if ((attr & HL_STRIKETHROUGH) && T_STS != NULL) /* strike */
8069 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070
8071 /*
8072 * Output the color or start string after bold etc., in case the
8073 * bold etc. override the color setting.
8074 */
8075 if (aep != NULL)
8076 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008077#ifdef FEAT_TERMGUICOLORS
8078 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008080 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008081 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008082 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008083 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 }
8085 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008088 if (t_colors > 1)
8089 {
8090 if (aep->ae_u.cterm.fg_color)
8091 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8092 if (aep->ae_u.cterm.bg_color)
8093 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8094 }
8095 else
8096 {
8097 if (aep->ae_u.term.start != NULL)
8098 out_str(aep->ae_u.term.start);
8099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 }
8101 }
8102 }
8103 }
8104}
8105
8106 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008107screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
8109 int do_ME = FALSE; /* output T_ME code */
8110
8111 if (screen_attr != 0
8112#ifdef WIN3264
8113 && termcap_active
8114#endif
8115 )
8116 {
8117#ifdef FEAT_GUI
8118 if (gui.in_use)
8119 {
8120 char buf[20];
8121
8122 /* use internal GUI code */
8123 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8124 OUT_STR(buf);
8125 }
8126 else
8127#endif
8128 {
8129 if (screen_attr > HL_ALL) /* special HL attr. */
8130 {
8131 attrentry_T *aep;
8132
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008133 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {
8135 /*
8136 * Assume that t_me restores the original colors!
8137 */
8138 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008139 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008140#ifdef FEAT_TERMGUICOLORS
8141 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008142 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8143 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008144#endif
8145 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008146#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008147 )
8148#endif
8149 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 do_ME = TRUE;
8151 }
8152 else
8153 {
8154 aep = syn_term_attr2entry(screen_attr);
8155 if (aep != NULL && aep->ae_u.term.stop != NULL)
8156 {
8157 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8158 do_ME = TRUE;
8159 else
8160 out_str(aep->ae_u.term.stop);
8161 }
8162 }
8163 if (aep == NULL) /* did ":syntax clear" */
8164 screen_attr = 0;
8165 else
8166 screen_attr = aep->ae_attr;
8167 }
8168
8169 /*
8170 * Often all ending-codes are equal to T_ME. Avoid outputting the
8171 * same sequence several times.
8172 */
8173 if (screen_attr & HL_STANDOUT)
8174 {
8175 if (STRCMP(T_SE, T_ME) == 0)
8176 do_ME = TRUE;
8177 else
8178 out_str(T_SE);
8179 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008180 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {
8182 if (STRCMP(T_UE, T_ME) == 0)
8183 do_ME = TRUE;
8184 else
8185 out_str(T_UE);
8186 }
8187 if (screen_attr & HL_ITALIC)
8188 {
8189 if (STRCMP(T_CZR, T_ME) == 0)
8190 do_ME = TRUE;
8191 else
8192 out_str(T_CZR);
8193 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008194 if (screen_attr & HL_STRIKETHROUGH)
8195 {
8196 if (STRCMP(T_STE, T_ME) == 0)
8197 do_ME = TRUE;
8198 else
8199 out_str(T_STE);
8200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8202 out_str(T_ME);
8203
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008204#ifdef FEAT_TERMGUICOLORS
8205 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008207 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008208 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008209 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008210 term_bg_rgb_color(cterm_normal_bg_gui_color);
8211 }
8212 else
8213#endif
8214 {
8215 if (t_colors > 1)
8216 {
8217 /* set Normal cterm colors */
8218 if (cterm_normal_fg_color != 0)
8219 term_fg_color(cterm_normal_fg_color - 1);
8220 if (cterm_normal_bg_color != 0)
8221 term_bg_color(cterm_normal_bg_color - 1);
8222 if (cterm_normal_fg_bold)
8223 out_str(T_MD);
8224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 }
8226 }
8227 }
8228 screen_attr = 0;
8229}
8230
8231/*
8232 * Reset the colors for a cterm. Used when leaving Vim.
8233 * The machine specific code may override this again.
8234 */
8235 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008236reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008238 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {
8240 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008241#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008242 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8243 || cterm_normal_bg_gui_color != INVALCOLOR)
8244 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008245#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {
8249 out_str(T_OP);
8250 screen_attr = -1;
8251 }
8252 if (cterm_normal_fg_bold)
8253 {
8254 out_str(T_ME);
8255 screen_attr = -1;
8256 }
8257 }
8258}
8259
8260/*
8261 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8262 * using the attributes from ScreenAttrs["off"].
8263 */
8264 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008265screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266{
8267 int attr;
8268
8269 /* Check for illegal values, just in case (could happen just after
8270 * resizing). */
8271 if (row >= screen_Rows || col >= screen_Columns)
8272 return;
8273
Bram Moolenaar494838a2015-02-10 19:20:37 +01008274 /* Outputting a character in the last cell on the screen may scroll the
8275 * screen up. Only do it when the "xn" termcap property is set, otherwise
8276 * mark the character invalid (update it when scrolled up). */
8277 if (*T_XN == NUL
8278 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279#ifdef FEAT_RIGHTLEFT
8280 /* account for first command-line character in rightleft mode */
8281 && !cmdmsg_rl
8282#endif
8283 )
8284 {
8285 ScreenAttrs[off] = (sattr_T)-1;
8286 return;
8287 }
8288
8289 /*
8290 * Stop highlighting first, so it's easier to move the cursor.
8291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 if (screen_char_attr != 0)
8293 attr = screen_char_attr;
8294 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 attr = ScreenAttrs[off];
8296 if (screen_attr != attr)
8297 screen_stop_highlight();
8298
8299 windgoto(row, col);
8300
8301 if (screen_attr != attr)
8302 screen_start_highlight(attr);
8303
8304#ifdef FEAT_MBYTE
8305 if (enc_utf8 && ScreenLinesUC[off] != 0)
8306 {
8307 char_u buf[MB_MAXBYTES + 1];
8308
8309 /* Convert UTF-8 character to bytes and write it. */
8310
8311 buf[utfc_char2bytes(off, buf)] = NUL;
8312
8313 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008314 if (utf_ambiguous_width(ScreenLinesUC[off]))
8315 screen_cur_col = 9999;
8316 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 ++screen_cur_col;
8318 }
8319 else
8320#endif
8321 {
8322#ifdef FEAT_MBYTE
8323 out_flush_check();
8324#endif
8325 out_char(ScreenLines[off]);
8326#ifdef FEAT_MBYTE
8327 /* double-byte character in single-width cell */
8328 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8329 out_char(ScreenLines2[off]);
8330#endif
8331 }
8332
8333 screen_cur_col++;
8334}
8335
8336#ifdef FEAT_MBYTE
8337
8338/*
8339 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8340 * on the screen at position 'row' and 'col'.
8341 * The attributes of the first byte is used for all. This is required to
8342 * output the two bytes of a double-byte character with nothing in between.
8343 */
8344 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008345screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346{
8347 /* Check for illegal values (could be wrong when screen was resized). */
8348 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8349 return;
8350
8351 /* Outputting the last character on the screen may scrollup the screen.
8352 * Don't to it! Mark the character invalid (update it when scrolled up) */
8353 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8354 {
8355 ScreenAttrs[off] = (sattr_T)-1;
8356 return;
8357 }
8358
8359 /* Output the first byte normally (positions the cursor), then write the
8360 * second byte directly. */
8361 screen_char(off, row, col);
8362 out_char(ScreenLines[off + 1]);
8363 ++screen_cur_col;
8364}
8365#endif
8366
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367/*
8368 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8369 * This uses the contents of ScreenLines[] and doesn't change it.
8370 */
8371 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008372screen_draw_rectangle(
8373 int row,
8374 int col,
8375 int height,
8376 int width,
8377 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378{
8379 int r, c;
8380 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008381#ifdef FEAT_MBYTE
8382 int max_off;
8383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008385 /* Can't use ScreenLines unless initialized */
8386 if (ScreenLines == NULL)
8387 return;
8388
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 if (invert)
8390 screen_char_attr = HL_INVERSE;
8391 for (r = row; r < row + height; ++r)
8392 {
8393 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008394#ifdef FEAT_MBYTE
8395 max_off = off + screen_Columns;
8396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 for (c = col; c < col + width; ++c)
8398 {
8399#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008400 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 {
8402 screen_char_2(off + c, r, c);
8403 ++c;
8404 }
8405 else
8406#endif
8407 {
8408 screen_char(off + c, r, c);
8409#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008410 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 ++c;
8412#endif
8413 }
8414 }
8415 }
8416 screen_char_attr = 0;
8417}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419/*
8420 * Redraw the characters for a vertically split window.
8421 */
8422 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008423redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424{
8425 int col;
8426 int width;
8427
8428# ifdef FEAT_CLIPBOARD
8429 clip_may_clear_selection(row, end - 1);
8430# endif
8431
8432 if (wp == NULL)
8433 {
8434 col = 0;
8435 width = Columns;
8436 }
8437 else
8438 {
8439 col = wp->w_wincol;
8440 width = wp->w_width;
8441 }
8442 screen_draw_rectangle(row, col, end - row, width, FALSE);
8443}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008445 static void
8446space_to_screenline(int off, int attr)
8447{
8448 ScreenLines[off] = ' ';
8449 ScreenAttrs[off] = attr;
8450# ifdef FEAT_MBYTE
8451 if (enc_utf8)
8452 ScreenLinesUC[off] = 0;
8453# endif
8454}
8455
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456/*
8457 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8458 * with character 'c1' in first column followed by 'c2' in the other columns.
8459 * Use attributes 'attr'.
8460 */
8461 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008462screen_fill(
8463 int start_row,
8464 int end_row,
8465 int start_col,
8466 int end_col,
8467 int c1,
8468 int c2,
8469 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
8471 int row;
8472 int col;
8473 int off;
8474 int end_off;
8475 int did_delete;
8476 int c;
8477 int norm_term;
8478#if defined(FEAT_GUI) || defined(UNIX)
8479 int force_next = FALSE;
8480#endif
8481
8482 if (end_row > screen_Rows) /* safety check */
8483 end_row = screen_Rows;
8484 if (end_col > screen_Columns) /* safety check */
8485 end_col = screen_Columns;
8486 if (ScreenLines == NULL
8487 || start_row >= end_row
8488 || start_col >= end_col) /* nothing to do */
8489 return;
8490
8491 /* it's a "normal" terminal when not in a GUI or cterm */
8492 norm_term = (
8493#ifdef FEAT_GUI
8494 !gui.in_use &&
8495#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008496 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 for (row = start_row; row < end_row; ++row)
8498 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008499#ifdef FEAT_MBYTE
8500 if (has_mbyte
8501# ifdef FEAT_GUI
8502 && !gui.in_use
8503# endif
8504 )
8505 {
8506 /* When drawing over the right halve of a double-wide char clear
8507 * out the left halve. When drawing over the left halve of a
8508 * double wide-char clear out the right halve. Only needed in a
8509 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008510 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008511 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008512 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008513 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008514 }
8515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 /*
8517 * Try to use delete-line termcap code, when no attributes or in a
8518 * "normal" terminal, where a bold/italic space is just a
8519 * space.
8520 */
8521 did_delete = FALSE;
8522 if (c2 == ' '
8523 && end_col == Columns
8524 && can_clear(T_CE)
8525 && (attr == 0
8526 || (norm_term
8527 && attr <= HL_ALL
8528 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8529 {
8530 /*
8531 * check if we really need to clear something
8532 */
8533 col = start_col;
8534 if (c1 != ' ') /* don't clear first char */
8535 ++col;
8536
8537 off = LineOffset[row] + col;
8538 end_off = LineOffset[row] + end_col;
8539
8540 /* skip blanks (used often, keep it fast!) */
8541#ifdef FEAT_MBYTE
8542 if (enc_utf8)
8543 while (off < end_off && ScreenLines[off] == ' '
8544 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8545 ++off;
8546 else
8547#endif
8548 while (off < end_off && ScreenLines[off] == ' '
8549 && ScreenAttrs[off] == 0)
8550 ++off;
8551 if (off < end_off) /* something to be cleared */
8552 {
8553 col = off - LineOffset[row];
8554 screen_stop_highlight();
8555 term_windgoto(row, col);/* clear rest of this screen line */
8556 out_str(T_CE);
8557 screen_start(); /* don't know where cursor is now */
8558 col = end_col - col;
8559 while (col--) /* clear chars in ScreenLines */
8560 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008561 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 ++off;
8563 }
8564 }
8565 did_delete = TRUE; /* the chars are cleared now */
8566 }
8567
8568 off = LineOffset[row] + start_col;
8569 c = c1;
8570 for (col = start_col; col < end_col; ++col)
8571 {
8572 if (ScreenLines[off] != c
8573#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008574 || (enc_utf8 && (int)ScreenLinesUC[off]
8575 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576#endif
8577 || ScreenAttrs[off] != attr
8578#if defined(FEAT_GUI) || defined(UNIX)
8579 || force_next
8580#endif
8581 )
8582 {
8583#if defined(FEAT_GUI) || defined(UNIX)
8584 /* The bold trick may make a single row of pixels appear in
8585 * the next character. When a bold character is removed, the
8586 * next character should be redrawn too. This happens for our
8587 * own GUI and for some xterms. */
8588 if (
8589# ifdef FEAT_GUI
8590 gui.in_use
8591# endif
8592# if defined(FEAT_GUI) && defined(UNIX)
8593 ||
8594# endif
8595# ifdef UNIX
8596 term_is_xterm
8597# endif
8598 )
8599 {
8600 if (ScreenLines[off] != ' '
8601 && (ScreenAttrs[off] > HL_ALL
8602 || ScreenAttrs[off] & HL_BOLD))
8603 force_next = TRUE;
8604 else
8605 force_next = FALSE;
8606 }
8607#endif
8608 ScreenLines[off] = c;
8609#ifdef FEAT_MBYTE
8610 if (enc_utf8)
8611 {
8612 if (c >= 0x80)
8613 {
8614 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008615 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 }
8617 else
8618 ScreenLinesUC[off] = 0;
8619 }
8620#endif
8621 ScreenAttrs[off] = attr;
8622 if (!did_delete || c != ' ')
8623 screen_char(off, row, col);
8624 }
8625 ++off;
8626 if (col == start_col)
8627 {
8628 if (did_delete)
8629 break;
8630 c = c2;
8631 }
8632 }
8633 if (end_col == Columns)
8634 LineWraps[row] = FALSE;
8635 if (row == Rows - 1) /* overwritten the command line */
8636 {
8637 redraw_cmdline = TRUE;
8638 if (c1 == ' ' && c2 == ' ')
8639 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008640 if (start_col == 0)
8641 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 }
8643 }
8644}
8645
8646/*
8647 * Check if there should be a delay. Used before clearing or redrawing the
8648 * screen or the command line.
8649 */
8650 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008651check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652{
8653 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8654 && !did_wait_return
8655 && emsg_silent == 0)
8656 {
8657 out_flush();
8658 ui_delay(1000L, TRUE);
8659 emsg_on_display = FALSE;
8660 if (check_msg_scroll)
8661 msg_scroll = FALSE;
8662 }
8663}
8664
8665/*
8666 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008667 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 * Returns TRUE if there is a valid screen to write to.
8669 * Returns FALSE when starting up and screen not initialized yet.
8670 */
8671 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008672screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008674 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 return (ScreenLines != NULL);
8676}
8677
8678/*
8679 * Resize the shell to Rows and Columns.
8680 * Allocate ScreenLines[] and associated items.
8681 *
8682 * There may be some time between setting Rows and Columns and (re)allocating
8683 * ScreenLines[]. This happens when starting up and when (manually) changing
8684 * the shell size. Always use screen_Rows and screen_Columns to access items
8685 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8686 * final size of the shell is needed.
8687 */
8688 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008689screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690{
8691 int new_row, old_row;
8692#ifdef FEAT_GUI
8693 int old_Rows;
8694#endif
8695 win_T *wp;
8696 int outofmem = FALSE;
8697 int len;
8698 schar_T *new_ScreenLines;
8699#ifdef FEAT_MBYTE
8700 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008701 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008703 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704#endif
8705 sattr_T *new_ScreenAttrs;
8706 unsigned *new_LineOffset;
8707 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008708 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008709 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008711 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008712#ifdef FEAT_AUTOCMD
8713 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008715retry:
8716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 /*
8718 * Allocation of the screen buffers is done only when the size changes and
8719 * when Rows and Columns have been set and we have started doing full
8720 * screen stuff.
8721 */
8722 if ((ScreenLines != NULL
8723 && Rows == screen_Rows
8724 && Columns == screen_Columns
8725#ifdef FEAT_MBYTE
8726 && enc_utf8 == (ScreenLinesUC != NULL)
8727 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008728 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729#endif
8730 )
8731 || Rows == 0
8732 || Columns == 0
8733 || (!full_screen && ScreenLines == NULL))
8734 return;
8735
8736 /*
8737 * It's possible that we produce an out-of-memory message below, which
8738 * will cause this function to be called again. To break the loop, just
8739 * return here.
8740 */
8741 if (entered)
8742 return;
8743 entered = TRUE;
8744
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008745 /*
8746 * Note that the window sizes are updated before reallocating the arrays,
8747 * thus we must not redraw here!
8748 */
8749 ++RedrawingDisabled;
8750
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 win_new_shellsize(); /* fit the windows in the new sized shell */
8752
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 comp_col(); /* recompute columns for shown command and ruler */
8754
8755 /*
8756 * We're changing the size of the screen.
8757 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8758 * - Move lines from the old arrays into the new arrays, clear extra
8759 * lines (unless the screen is going to be cleared).
8760 * - Free the old arrays.
8761 *
8762 * If anything fails, make ScreenLines NULL, so we don't do anything!
8763 * Continuing with the old ScreenLines may result in a crash, because the
8764 * size is wrong.
8765 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008766 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008768#ifdef FEAT_AUTOCMD
8769 if (aucmd_win != NULL)
8770 win_free_lsize(aucmd_win);
8771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772
8773 new_ScreenLines = (schar_T *)lalloc((long_u)(
8774 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8775#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008776 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 if (enc_utf8)
8778 {
8779 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8780 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008781 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008782 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8784 }
8785 if (enc_dbcs == DBCS_JPNU)
8786 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8787 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8788#endif
8789 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8790 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8791 new_LineOffset = (unsigned *)lalloc((long_u)(
8792 Rows * sizeof(unsigned)), FALSE);
8793 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008794 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008796 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 {
8798 if (win_alloc_lines(wp) == FAIL)
8799 {
8800 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008801 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 }
8803 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008804#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008805 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8806 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008807 outofmem = TRUE;
8808#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008809give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008811#ifdef FEAT_MBYTE
8812 for (i = 0; i < p_mco; ++i)
8813 if (new_ScreenLinesC[i] == NULL)
8814 break;
8815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 if (new_ScreenLines == NULL
8817#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008818 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8820#endif
8821 || new_ScreenAttrs == NULL
8822 || new_LineOffset == NULL
8823 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008824 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 || outofmem)
8826 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008827 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008828 {
8829 /* guess the size */
8830 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8831
8832 /* Remember we did this to avoid getting outofmem messages over
8833 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008834 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 vim_free(new_ScreenLines);
8837 new_ScreenLines = NULL;
8838#ifdef FEAT_MBYTE
8839 vim_free(new_ScreenLinesUC);
8840 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008841 for (i = 0; i < p_mco; ++i)
8842 {
8843 vim_free(new_ScreenLinesC[i]);
8844 new_ScreenLinesC[i] = NULL;
8845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 vim_free(new_ScreenLines2);
8847 new_ScreenLines2 = NULL;
8848#endif
8849 vim_free(new_ScreenAttrs);
8850 new_ScreenAttrs = NULL;
8851 vim_free(new_LineOffset);
8852 new_LineOffset = NULL;
8853 vim_free(new_LineWraps);
8854 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008855 vim_free(new_TabPageIdxs);
8856 new_TabPageIdxs = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 }
8858 else
8859 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008860 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008861
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 for (new_row = 0; new_row < Rows; ++new_row)
8863 {
8864 new_LineOffset[new_row] = new_row * Columns;
8865 new_LineWraps[new_row] = FALSE;
8866
8867 /*
8868 * If the screen is not going to be cleared, copy as much as
8869 * possible from the old screen to the new one and clear the rest
8870 * (used when resizing the window at the "--more--" prompt or when
8871 * executing an external command, for the GUI).
8872 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008873 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 {
8875 (void)vim_memset(new_ScreenLines + new_row * Columns,
8876 ' ', (size_t)Columns * sizeof(schar_T));
8877#ifdef FEAT_MBYTE
8878 if (enc_utf8)
8879 {
8880 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8881 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008882 for (i = 0; i < p_mco; ++i)
8883 (void)vim_memset(new_ScreenLinesC[i]
8884 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 0, (size_t)Columns * sizeof(u8char_T));
8886 }
8887 if (enc_dbcs == DBCS_JPNU)
8888 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8889 0, (size_t)Columns * sizeof(schar_T));
8890#endif
8891 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8892 0, (size_t)Columns * sizeof(sattr_T));
8893 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008894 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 {
8896 if (screen_Columns < Columns)
8897 len = screen_Columns;
8898 else
8899 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008900#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008901 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008902 * may be invalid now. Also when p_mco changes. */
8903 if (!(enc_utf8 && ScreenLinesUC == NULL)
8904 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008905#endif
8906 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8907 ScreenLines + LineOffset[old_row],
8908 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008910 if (enc_utf8 && ScreenLinesUC != NULL
8911 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 {
8913 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8914 ScreenLinesUC + LineOffset[old_row],
8915 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008916 for (i = 0; i < p_mco; ++i)
8917 mch_memmove(new_ScreenLinesC[i]
8918 + new_LineOffset[new_row],
8919 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 (size_t)len * sizeof(u8char_T));
8921 }
8922 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8923 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8924 ScreenLines2 + LineOffset[old_row],
8925 (size_t)len * sizeof(schar_T));
8926#endif
8927 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8928 ScreenAttrs + LineOffset[old_row],
8929 (size_t)len * sizeof(sattr_T));
8930 }
8931 }
8932 }
8933 /* Use the last line of the screen for the current line. */
8934 current_ScreenLine = new_ScreenLines + Rows * Columns;
8935 }
8936
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008937 free_screenlines();
8938
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 ScreenLines = new_ScreenLines;
8940#ifdef FEAT_MBYTE
8941 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008942 for (i = 0; i < p_mco; ++i)
8943 ScreenLinesC[i] = new_ScreenLinesC[i];
8944 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 ScreenLines2 = new_ScreenLines2;
8946#endif
8947 ScreenAttrs = new_ScreenAttrs;
8948 LineOffset = new_LineOffset;
8949 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008950 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951
8952 /* It's important that screen_Rows and screen_Columns reflect the actual
8953 * size of ScreenLines[]. Set them before calling anything. */
8954#ifdef FEAT_GUI
8955 old_Rows = screen_Rows;
8956#endif
8957 screen_Rows = Rows;
8958 screen_Columns = Columns;
8959
8960 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008961 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 screenclear2();
8963
8964#ifdef FEAT_GUI
8965 else if (gui.in_use
8966 && !gui.starting
8967 && ScreenLines != NULL
8968 && old_Rows != Rows)
8969 {
8970 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8971 /*
8972 * Adjust the position of the cursor, for when executing an external
8973 * command.
8974 */
8975 if (msg_row >= Rows) /* Rows got smaller */
8976 msg_row = Rows - 1; /* put cursor at last row */
8977 else if (Rows > old_Rows) /* Rows got bigger */
8978 msg_row += Rows - old_Rows; /* put cursor in same place */
8979 if (msg_col >= Columns) /* Columns got smaller */
8980 msg_col = Columns - 1; /* put cursor at last column */
8981 }
8982#endif
8983
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008985 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008986
8987#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008988 /*
8989 * Do not apply autocommands more than 3 times to avoid an endless loop
8990 * in case applying autocommands always changes Rows or Columns.
8991 */
8992 if (starting == 0 && ++retry_count <= 3)
8993 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008994 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008995 /* In rare cases, autocommands may have altered Rows or Columns,
8996 * jump back to check if we need to allocate the screen again. */
8997 goto retry;
8998 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000}
9001
9002 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009003free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009004{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009005#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009006 int i;
9007
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009008 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009009 for (i = 0; i < Screen_mco; ++i)
9010 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009011 vim_free(ScreenLines2);
9012#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009013 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009014 vim_free(ScreenAttrs);
9015 vim_free(LineOffset);
9016 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009017 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009018}
9019
9020 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009021screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022{
9023 check_for_delay(FALSE);
9024 screenalloc(FALSE); /* allocate screen buffers if size changed */
9025 screenclear2(); /* clear the screen */
9026}
9027
9028 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009029screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030{
9031 int i;
9032
9033 if (starting == NO_SCREEN || ScreenLines == NULL
9034#ifdef FEAT_GUI
9035 || (gui.in_use && gui.starting)
9036#endif
9037 )
9038 return;
9039
9040#ifdef FEAT_GUI
9041 if (!gui.in_use)
9042#endif
9043 screen_attr = -1; /* force setting the Normal colors */
9044 screen_stop_highlight(); /* don't want highlighting here */
9045
9046#ifdef FEAT_CLIPBOARD
9047 /* disable selection without redrawing it */
9048 clip_scroll_selection(9999);
9049#endif
9050
9051 /* blank out ScreenLines */
9052 for (i = 0; i < Rows; ++i)
9053 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009054 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 LineWraps[i] = FALSE;
9056 }
9057
9058 if (can_clear(T_CL))
9059 {
9060 out_str(T_CL); /* clear the display */
9061 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009062 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 }
9064 else
9065 {
9066 /* can't clear the screen, mark all chars with invalid attributes */
9067 for (i = 0; i < Rows; ++i)
9068 lineinvalid(LineOffset[i], (int)Columns);
9069 clear_cmdline = TRUE;
9070 }
9071
9072 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9073
9074 win_rest_invalid(firstwin);
9075 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009076 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 if (must_redraw == CLEAR) /* no need to clear again */
9078 must_redraw = NOT_VALID;
9079 compute_cmdrow();
9080 msg_row = cmdline_row; /* put cursor on last line for messages */
9081 msg_col = 0;
9082 screen_start(); /* don't know where cursor is now */
9083 msg_scrolled = 0; /* can't scroll back */
9084 msg_didany = FALSE;
9085 msg_didout = FALSE;
9086}
9087
9088/*
9089 * Clear one line in ScreenLines.
9090 */
9091 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009092lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093{
9094 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9095#ifdef FEAT_MBYTE
9096 if (enc_utf8)
9097 (void)vim_memset(ScreenLinesUC + off, 0,
9098 (size_t)width * sizeof(u8char_T));
9099#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009100 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101}
9102
9103/*
9104 * Mark one line in ScreenLines invalid by setting the attributes to an
9105 * invalid value.
9106 */
9107 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009108lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109{
9110 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9111}
9112
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113/*
9114 * Copy part of a Screenline for vertically split window "wp".
9115 */
9116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009117linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
9119 unsigned off_to = LineOffset[to] + wp->w_wincol;
9120 unsigned off_from = LineOffset[from] + wp->w_wincol;
9121
9122 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9123 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009124#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 if (enc_utf8)
9126 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009127 int i;
9128
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9130 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009131 for (i = 0; i < p_mco; ++i)
9132 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9133 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 }
9135 if (enc_dbcs == DBCS_JPNU)
9136 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9137 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9140 wp->w_width * sizeof(sattr_T));
9141}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142
9143/*
9144 * Return TRUE if clearing with term string "p" would work.
9145 * It can't work when the string is empty or it won't set the right background.
9146 */
9147 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009148can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150 return (*p != NUL && (t_colors <= 1
9151#ifdef FEAT_GUI
9152 || gui.in_use
9153#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009154#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009155 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009156 || (!p_tgc && cterm_normal_bg_color == 0)
9157#else
9158 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009159#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009160 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161}
9162
9163/*
9164 * Reset cursor position. Use whenever cursor was moved because of outputting
9165 * something directly to the screen (shell commands) or a terminal control
9166 * code.
9167 */
9168 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009169screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170{
9171 screen_cur_row = screen_cur_col = 9999;
9172}
9173
9174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 * Move the cursor to position "row","col" in the screen.
9176 * This tries to find the most efficient way to move, minimizing the number of
9177 * characters sent to the terminal.
9178 */
9179 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009180windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009182 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 int i;
9184 int plan;
9185 int cost;
9186 int wouldbe_col;
9187 int noinvcurs;
9188 char_u *bs;
9189 int goto_cost;
9190 int attr;
9191
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009192#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9194
9195#define PLAN_LE 1
9196#define PLAN_CR 2
9197#define PLAN_NL 3
9198#define PLAN_WRITE 4
9199 /* Can't use ScreenLines unless initialized */
9200 if (ScreenLines == NULL)
9201 return;
9202
9203 if (col != screen_cur_col || row != screen_cur_row)
9204 {
9205 /* Check for valid position. */
9206 if (row < 0) /* window without text lines? */
9207 row = 0;
9208 if (row >= screen_Rows)
9209 row = screen_Rows - 1;
9210 if (col >= screen_Columns)
9211 col = screen_Columns - 1;
9212
9213 /* check if no cursor movement is allowed in highlight mode */
9214 if (screen_attr && *T_MS == NUL)
9215 noinvcurs = HIGHL_COST;
9216 else
9217 noinvcurs = 0;
9218 goto_cost = GOTO_COST + noinvcurs;
9219
9220 /*
9221 * Plan how to do the positioning:
9222 * 1. Use CR to move it to column 0, same row.
9223 * 2. Use T_LE to move it a few columns to the left.
9224 * 3. Use NL to move a few lines down, column 0.
9225 * 4. Move a few columns to the right with T_ND or by writing chars.
9226 *
9227 * Don't do this if the cursor went beyond the last column, the cursor
9228 * position is unknown then (some terminals wrap, some don't )
9229 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009230 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 * characters to move the cursor to the right.
9232 */
9233 if (row >= screen_cur_row && screen_cur_col < Columns)
9234 {
9235 /*
9236 * If the cursor is in the same row, bigger col, we can use CR
9237 * or T_LE.
9238 */
9239 bs = NULL; /* init for GCC */
9240 attr = screen_attr;
9241 if (row == screen_cur_row && col < screen_cur_col)
9242 {
9243 /* "le" is preferred over "bc", because "bc" is obsolete */
9244 if (*T_LE)
9245 bs = T_LE; /* "cursor left" */
9246 else
9247 bs = T_BC; /* "backspace character (old) */
9248 if (*bs)
9249 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9250 else
9251 cost = 999;
9252 if (col + 1 < cost) /* using CR is less characters */
9253 {
9254 plan = PLAN_CR;
9255 wouldbe_col = 0;
9256 cost = 1; /* CR is just one character */
9257 }
9258 else
9259 {
9260 plan = PLAN_LE;
9261 wouldbe_col = col;
9262 }
9263 if (noinvcurs) /* will stop highlighting */
9264 {
9265 cost += noinvcurs;
9266 attr = 0;
9267 }
9268 }
9269
9270 /*
9271 * If the cursor is above where we want to be, we can use CR LF.
9272 */
9273 else if (row > screen_cur_row)
9274 {
9275 plan = PLAN_NL;
9276 wouldbe_col = 0;
9277 cost = (row - screen_cur_row) * 2; /* CR LF */
9278 if (noinvcurs) /* will stop highlighting */
9279 {
9280 cost += noinvcurs;
9281 attr = 0;
9282 }
9283 }
9284
9285 /*
9286 * If the cursor is in the same row, smaller col, just use write.
9287 */
9288 else
9289 {
9290 plan = PLAN_WRITE;
9291 wouldbe_col = screen_cur_col;
9292 cost = 0;
9293 }
9294
9295 /*
9296 * Check if any characters that need to be written have the
9297 * correct attributes. Also avoid UTF-8 characters.
9298 */
9299 i = col - wouldbe_col;
9300 if (i > 0)
9301 cost += i;
9302 if (cost < goto_cost && i > 0)
9303 {
9304 /*
9305 * Check if the attributes are correct without additionally
9306 * stopping highlighting.
9307 */
9308 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9309 while (i && *p++ == attr)
9310 --i;
9311 if (i != 0)
9312 {
9313 /*
9314 * Try if it works when highlighting is stopped here.
9315 */
9316 if (*--p == 0)
9317 {
9318 cost += noinvcurs;
9319 while (i && *p++ == 0)
9320 --i;
9321 }
9322 if (i != 0)
9323 cost = 999; /* different attributes, don't do it */
9324 }
9325#ifdef FEAT_MBYTE
9326 if (enc_utf8)
9327 {
9328 /* Don't use an UTF-8 char for positioning, it's slow. */
9329 for (i = wouldbe_col; i < col; ++i)
9330 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9331 {
9332 cost = 999;
9333 break;
9334 }
9335 }
9336#endif
9337 }
9338
9339 /*
9340 * We can do it without term_windgoto()!
9341 */
9342 if (cost < goto_cost)
9343 {
9344 if (plan == PLAN_LE)
9345 {
9346 if (noinvcurs)
9347 screen_stop_highlight();
9348 while (screen_cur_col > col)
9349 {
9350 out_str(bs);
9351 --screen_cur_col;
9352 }
9353 }
9354 else if (plan == PLAN_CR)
9355 {
9356 if (noinvcurs)
9357 screen_stop_highlight();
9358 out_char('\r');
9359 screen_cur_col = 0;
9360 }
9361 else if (plan == PLAN_NL)
9362 {
9363 if (noinvcurs)
9364 screen_stop_highlight();
9365 while (screen_cur_row < row)
9366 {
9367 out_char('\n');
9368 ++screen_cur_row;
9369 }
9370 screen_cur_col = 0;
9371 }
9372
9373 i = col - screen_cur_col;
9374 if (i > 0)
9375 {
9376 /*
9377 * Use cursor-right if it's one character only. Avoids
9378 * removing a line of pixels from the last bold char, when
9379 * using the bold trick in the GUI.
9380 */
9381 if (T_ND[0] != NUL && T_ND[1] == NUL)
9382 {
9383 while (i-- > 0)
9384 out_char(*T_ND);
9385 }
9386 else
9387 {
9388 int off;
9389
9390 off = LineOffset[row] + screen_cur_col;
9391 while (i-- > 0)
9392 {
9393 if (ScreenAttrs[off] != screen_attr)
9394 screen_stop_highlight();
9395#ifdef FEAT_MBYTE
9396 out_flush_check();
9397#endif
9398 out_char(ScreenLines[off]);
9399#ifdef FEAT_MBYTE
9400 if (enc_dbcs == DBCS_JPNU
9401 && ScreenLines[off] == 0x8e)
9402 out_char(ScreenLines2[off]);
9403#endif
9404 ++off;
9405 }
9406 }
9407 }
9408 }
9409 }
9410 else
9411 cost = 999;
9412
9413 if (cost >= goto_cost)
9414 {
9415 if (noinvcurs)
9416 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009417 if (row == screen_cur_row && (col > screen_cur_col)
9418 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 term_cursor_right(col - screen_cur_col);
9420 else
9421 term_windgoto(row, col);
9422 }
9423 screen_cur_row = row;
9424 screen_cur_col = col;
9425 }
9426}
9427
9428/*
9429 * Set cursor to its position in the current window.
9430 */
9431 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009432setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433{
9434 if (redrawing())
9435 {
9436 validate_cursor();
9437 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009438 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009440 /* With 'rightleft' set and the cursor on a double-wide
9441 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009442 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009444 (has_mbyte
9445 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9446 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447# endif
9448 1)) :
9449#endif
9450 curwin->w_wcol));
9451 }
9452}
9453
9454
9455/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009456 * Insert 'line_count' lines at 'row' in window 'wp'.
9457 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9458 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 * scrolling.
9460 * Returns FAIL if the lines are not inserted, OK for success.
9461 */
9462 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009463win_ins_lines(
9464 win_T *wp,
9465 int row,
9466 int line_count,
9467 int invalid,
9468 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470 int did_delete;
9471 int nextrow;
9472 int lastrow;
9473 int retval;
9474
9475 if (invalid)
9476 wp->w_lines_valid = 0;
9477
9478 if (wp->w_height < 5)
9479 return FAIL;
9480
9481 if (line_count > wp->w_height - row)
9482 line_count = wp->w_height - row;
9483
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009484 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 if (retval != MAYBE)
9486 return retval;
9487
9488 /*
9489 * If there is a next window or a status line, we first try to delete the
9490 * lines at the bottom to avoid messing what is after the window.
9491 * If this fails and there are following windows, don't do anything to avoid
9492 * messing up those windows, better just redraw.
9493 */
9494 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 if (wp->w_next != NULL || wp->w_status_height)
9496 {
9497 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009498 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 did_delete = TRUE;
9500 else if (wp->w_next)
9501 return FAIL;
9502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 /*
9504 * if no lines deleted, blank the lines that will end up below the window
9505 */
9506 if (!did_delete)
9507 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009510 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 lastrow = nextrow + line_count;
9512 if (lastrow > Rows)
9513 lastrow = Rows;
9514 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009515 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 ' ', ' ', 0);
9517 }
9518
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009519 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 == FAIL)
9521 {
9522 /* deletion will have messed up other windows */
9523 if (did_delete)
9524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 win_rest_invalid(W_NEXT(wp));
9527 }
9528 return FAIL;
9529 }
9530
9531 return OK;
9532}
9533
9534/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009535 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9537 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9538 * scrolling
9539 * Return OK for success, FAIL if the lines are not deleted.
9540 */
9541 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009542win_del_lines(
9543 win_T *wp,
9544 int row,
9545 int line_count,
9546 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009547 int mayclear,
9548 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
9550 int retval;
9551
9552 if (invalid)
9553 wp->w_lines_valid = 0;
9554
9555 if (line_count > wp->w_height - row)
9556 line_count = wp->w_height - row;
9557
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009558 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 if (retval != MAYBE)
9560 return retval;
9561
9562 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009563 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 return FAIL;
9565
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 /*
9567 * If there are windows or status lines below, try to put them at the
9568 * correct place. If we can't do that, they have to be redrawn.
9569 */
9570 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9571 {
9572 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009573 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 {
9575 wp->w_redr_status = TRUE;
9576 win_rest_invalid(wp->w_next);
9577 }
9578 }
9579 /*
9580 * If this is the last window and there is no status line, redraw the
9581 * command line later.
9582 */
9583 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 redraw_cmdline = TRUE;
9585 return OK;
9586}
9587
9588/*
9589 * Common code for win_ins_lines() and win_del_lines().
9590 * Returns OK or FAIL when the work has been done.
9591 * Returns MAYBE when not finished yet.
9592 */
9593 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009594win_do_lines(
9595 win_T *wp,
9596 int row,
9597 int line_count,
9598 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009599 int del,
9600 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
9602 int retval;
9603
9604 if (!redrawing() || line_count <= 0)
9605 return FAIL;
9606
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009607 /* When inserting lines would result in loss of command output, just redraw
9608 * the lines. */
9609 if (no_win_do_lines_ins && !del)
9610 return FAIL;
9611
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009613 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009615 if (!no_win_do_lines_ins)
9616 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 return FAIL;
9618 }
9619
9620 /*
9621 * Delete all remaining lines
9622 */
9623 if (row + line_count >= wp->w_height)
9624 {
9625 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009626 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 ' ', ' ', 0);
9628 return OK;
9629 }
9630
9631 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009632 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009634 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009636 if (!no_win_do_lines_ins)
9637 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638
9639 /*
9640 * If the terminal can set a scroll region, use that.
9641 * Always do this in a vertically split window. This will redraw from
9642 * ScreenLines[] when t_CV isn't defined. That's faster than using
9643 * win_line().
9644 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009645 * a character in the lower right corner of the scroll region may cause a
9646 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009648 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 scroll_region_set(wp, row);
9652 if (del)
9653 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009654 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 else
9656 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009657 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 scroll_region_reset();
9660 return retval;
9661 }
9662
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9664 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665
9666 return MAYBE;
9667}
9668
9669/*
9670 * window 'wp' and everything after it is messed up, mark it for redraw
9671 */
9672 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009673win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 {
9677 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 wp->w_redr_status = TRUE;
9679 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 }
9681 redraw_cmdline = TRUE;
9682}
9683
9684/*
9685 * The rest of the routines in this file perform screen manipulations. The
9686 * given operation is performed physically on the screen. The corresponding
9687 * change is also made to the internal screen image. In this way, the editor
9688 * anticipates the effect of editing changes on the appearance of the screen.
9689 * That way, when we call screenupdate a complete redraw isn't usually
9690 * necessary. Another advantage is that we can keep adding code to anticipate
9691 * screen changes, and in the meantime, everything still works.
9692 */
9693
9694/*
9695 * types for inserting or deleting lines
9696 */
9697#define USE_T_CAL 1
9698#define USE_T_CDL 2
9699#define USE_T_AL 3
9700#define USE_T_CE 4
9701#define USE_T_DL 5
9702#define USE_T_SR 6
9703#define USE_NL 7
9704#define USE_T_CD 8
9705#define USE_REDRAW 9
9706
9707/*
9708 * insert lines on the screen and update ScreenLines[]
9709 * 'end' is the line after the scrolled part. Normally it is Rows.
9710 * When scrolling region used 'off' is the offset from the top for the region.
9711 * 'row' and 'end' are relative to the start of the region.
9712 *
9713 * return FAIL for failure, OK for success.
9714 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009715 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009716screen_ins_lines(
9717 int off,
9718 int row,
9719 int line_count,
9720 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009721 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009722 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723{
9724 int i;
9725 int j;
9726 unsigned temp;
9727 int cursor_row;
9728 int type;
9729 int result_empty;
9730 int can_ce = can_clear(T_CE);
9731
9732 /*
9733 * FAIL if
9734 * - there is no valid screen
9735 * - the screen has to be redrawn completely
9736 * - the line count is less than one
9737 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009738 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009740 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9741#ifdef FEAT_CLIPBOARD
9742 || (clip_star.state != SELECT_CLEARED
9743 && redrawing_for_callback > 0)
9744#endif
9745 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 return FAIL;
9747
9748 /*
9749 * There are seven ways to insert lines:
9750 * 0. When in a vertically split window and t_CV isn't set, redraw the
9751 * characters from ScreenLines[].
9752 * 1. Use T_CD (clear to end of display) if it exists and the result of
9753 * the insert is just empty lines
9754 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9755 * present or line_count > 1. It looks better if we do all the inserts
9756 * at once.
9757 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9758 * insert is just empty lines and T_CE is not present or line_count >
9759 * 1.
9760 * 4. Use T_AL (insert line) if it exists.
9761 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9762 * just empty lines.
9763 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9764 * just empty lines.
9765 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9766 * the 'da' flag is not set or we have clear line capability.
9767 * 8. redraw the characters from ScreenLines[].
9768 *
9769 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9770 * the scrollbar for the window. It does have insert line, use that if it
9771 * exists.
9772 */
9773 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9775 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009776 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 type = USE_T_CD;
9778 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9779 type = USE_T_CAL;
9780 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9781 type = USE_T_CDL;
9782 else if (*T_AL != NUL)
9783 type = USE_T_AL;
9784 else if (can_ce && result_empty)
9785 type = USE_T_CE;
9786 else if (*T_DL != NUL && result_empty)
9787 type = USE_T_DL;
9788 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9789 type = USE_T_SR;
9790 else
9791 return FAIL;
9792
9793 /*
9794 * For clearing the lines screen_del_lines() is used. This will also take
9795 * care of t_db if necessary.
9796 */
9797 if (type == USE_T_CD || type == USE_T_CDL ||
9798 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009799 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800
9801 /*
9802 * If text is retained below the screen, first clear or delete as many
9803 * lines at the bottom of the window as are about to be inserted so that
9804 * the deleted lines won't later surface during a screen_del_lines.
9805 */
9806 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009807 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808
9809#ifdef FEAT_CLIPBOARD
9810 /* Remove a modeless selection when inserting lines halfway the screen
9811 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009812 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009813 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 else
9815 clip_scroll_selection(-line_count);
9816#endif
9817
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818#ifdef FEAT_GUI
9819 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9820 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009821 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822#endif
9823
9824 if (*T_CCS != NUL) /* cursor relative to region */
9825 cursor_row = row;
9826 else
9827 cursor_row = row + off;
9828
9829 /*
9830 * Shift LineOffset[] line_count down to reflect the inserted lines.
9831 * Clear the inserted lines in ScreenLines[].
9832 */
9833 row += off;
9834 end += off;
9835 for (i = 0; i < line_count; ++i)
9836 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 if (wp != NULL && wp->w_width != Columns)
9838 {
9839 /* need to copy part of a line */
9840 j = end - 1 - i;
9841 while ((j -= line_count) >= row)
9842 linecopy(j + line_count, j, wp);
9843 j += line_count;
9844 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009845 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9846 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 else
9848 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9849 LineWraps[j] = FALSE;
9850 }
9851 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 {
9853 j = end - 1 - i;
9854 temp = LineOffset[j];
9855 while ((j -= line_count) >= row)
9856 {
9857 LineOffset[j + line_count] = LineOffset[j];
9858 LineWraps[j + line_count] = LineWraps[j];
9859 }
9860 LineOffset[j + line_count] = temp;
9861 LineWraps[j + line_count] = FALSE;
9862 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009863 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 else
9865 lineinvalid(temp, (int)Columns);
9866 }
9867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868
9869 screen_stop_highlight();
9870 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009871 if (clear_attr != 0)
9872 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 /* redraw the characters */
9875 if (type == USE_REDRAW)
9876 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009877 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 {
9879 term_append_lines(line_count);
9880 screen_start(); /* don't know where cursor is now */
9881 }
9882 else
9883 {
9884 for (i = 0; i < line_count; i++)
9885 {
9886 if (type == USE_T_AL)
9887 {
9888 if (i && cursor_row != 0)
9889 windgoto(cursor_row, 0);
9890 out_str(T_AL);
9891 }
9892 else /* type == USE_T_SR */
9893 out_str(T_SR);
9894 screen_start(); /* don't know where cursor is now */
9895 }
9896 }
9897
9898 /*
9899 * With scroll-reverse and 'da' flag set we need to clear the lines that
9900 * have been scrolled down into the region.
9901 */
9902 if (type == USE_T_SR && *T_DA)
9903 {
9904 for (i = 0; i < line_count; ++i)
9905 {
9906 windgoto(off + i, 0);
9907 out_str(T_CE);
9908 screen_start(); /* don't know where cursor is now */
9909 }
9910 }
9911
9912#ifdef FEAT_GUI
9913 gui_can_update_cursor();
9914 if (gui.in_use)
9915 out_flush(); /* always flush after a scroll */
9916#endif
9917 return OK;
9918}
9919
9920/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009921 * Delete lines on the screen and update ScreenLines[].
9922 * "end" is the line after the scrolled part. Normally it is Rows.
9923 * When scrolling region used "off" is the offset from the top for the region.
9924 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 *
9926 * Return OK for success, FAIL if the lines are not deleted.
9927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009929screen_del_lines(
9930 int off,
9931 int row,
9932 int line_count,
9933 int end,
9934 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009935 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009936 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937{
9938 int j;
9939 int i;
9940 unsigned temp;
9941 int cursor_row;
9942 int cursor_end;
9943 int result_empty; /* result is empty until end of region */
9944 int can_delete; /* deleting line codes can be used */
9945 int type;
9946
9947 /*
9948 * FAIL if
9949 * - there is no valid screen
9950 * - the screen has to be redrawn completely
9951 * - the line count is less than one
9952 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009953 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009955 if (!screen_valid(TRUE) || line_count <= 0
9956 || (!force && line_count > p_ttyscroll)
9957#ifdef FEAT_CLIPBOARD
9958 || (clip_star.state != SELECT_CLEARED
9959 && redrawing_for_callback > 0)
9960#endif
9961 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 return FAIL;
9963
9964 /*
9965 * Check if the rest of the current region will become empty.
9966 */
9967 result_empty = row + line_count >= end;
9968
9969 /*
9970 * We can delete lines only when 'db' flag not set or when 'ce' option
9971 * available.
9972 */
9973 can_delete = (*T_DB == NUL || can_clear(T_CE));
9974
9975 /*
9976 * There are six ways to delete lines:
9977 * 0. When in a vertically split window and t_CV isn't set, redraw the
9978 * characters from ScreenLines[].
9979 * 1. Use T_CD if it exists and the result is empty.
9980 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9981 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9982 * none of the other ways work.
9983 * 4. Use T_CE (erase line) if the result is empty.
9984 * 5. Use T_DL (delete line) if it exists.
9985 * 6. redraw the characters from ScreenLines[].
9986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9988 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009989 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 type = USE_T_CD;
9991#if defined(__BEOS__) && defined(BEOS_DR8)
9992 /*
9993 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9994 * its internal termcap... this works okay for tests which test *T_DB !=
9995 * NUL. It has the disadvantage that the user cannot use any :set t_*
9996 * command to get T_DB (back) to empty_option, only :set term=... will do
9997 * the trick...
9998 * Anyway, this hack will hopefully go away with the next OS release.
9999 * (Olaf Seibert)
10000 */
10001 else if (row == 0 && T_DB == empty_option
10002 && (line_count == 1 || *T_CDL == NUL))
10003#else
10004 else if (row == 0 && (
10005#ifndef AMIGA
10006 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10007 * up, so use delete-line command */
10008 line_count == 1 ||
10009#endif
10010 *T_CDL == NUL))
10011#endif
10012 type = USE_NL;
10013 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10014 type = USE_T_CDL;
10015 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010016 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 type = USE_T_CE;
10018 else if (*T_DL != NUL && can_delete)
10019 type = USE_T_DL;
10020 else if (*T_CDL != NUL && can_delete)
10021 type = USE_T_CDL;
10022 else
10023 return FAIL;
10024
10025#ifdef FEAT_CLIPBOARD
10026 /* Remove a modeless selection when deleting lines halfway the screen or
10027 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010028 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010029 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 else
10031 clip_scroll_selection(line_count);
10032#endif
10033
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034#ifdef FEAT_GUI
10035 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10036 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010037 gui_dont_update_cursor(gui.cursor_row >= row + off
10038 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039#endif
10040
10041 if (*T_CCS != NUL) /* cursor relative to region */
10042 {
10043 cursor_row = row;
10044 cursor_end = end;
10045 }
10046 else
10047 {
10048 cursor_row = row + off;
10049 cursor_end = end + off;
10050 }
10051
10052 /*
10053 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10054 * Clear the inserted lines in ScreenLines[].
10055 */
10056 row += off;
10057 end += off;
10058 for (i = 0; i < line_count; ++i)
10059 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 if (wp != NULL && wp->w_width != Columns)
10061 {
10062 /* need to copy part of a line */
10063 j = row + i;
10064 while ((j += line_count) <= end - 1)
10065 linecopy(j - line_count, j, wp);
10066 j -= line_count;
10067 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010068 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10069 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 else
10071 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10072 LineWraps[j] = FALSE;
10073 }
10074 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 {
10076 /* whole width, moving the line pointers is faster */
10077 j = row + i;
10078 temp = LineOffset[j];
10079 while ((j += line_count) <= end - 1)
10080 {
10081 LineOffset[j - line_count] = LineOffset[j];
10082 LineWraps[j - line_count] = LineWraps[j];
10083 }
10084 LineOffset[j - line_count] = temp;
10085 LineWraps[j - line_count] = FALSE;
10086 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010087 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 else
10089 lineinvalid(temp, (int)Columns);
10090 }
10091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010093 if (screen_attr != clear_attr)
10094 screen_stop_highlight();
10095 if (clear_attr != 0)
10096 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 /* redraw the characters */
10099 if (type == USE_REDRAW)
10100 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010101 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 {
10103 windgoto(cursor_row, 0);
10104 out_str(T_CD);
10105 screen_start(); /* don't know where cursor is now */
10106 }
10107 else if (type == USE_T_CDL)
10108 {
10109 windgoto(cursor_row, 0);
10110 term_delete_lines(line_count);
10111 screen_start(); /* don't know where cursor is now */
10112 }
10113 /*
10114 * Deleting lines at top of the screen or scroll region: Just scroll
10115 * the whole screen (scroll region) up by outputting newlines on the
10116 * last line.
10117 */
10118 else if (type == USE_NL)
10119 {
10120 windgoto(cursor_end - 1, 0);
10121 for (i = line_count; --i >= 0; )
10122 out_char('\n'); /* cursor will remain on same line */
10123 }
10124 else
10125 {
10126 for (i = line_count; --i >= 0; )
10127 {
10128 if (type == USE_T_DL)
10129 {
10130 windgoto(cursor_row, 0);
10131 out_str(T_DL); /* delete a line */
10132 }
10133 else /* type == USE_T_CE */
10134 {
10135 windgoto(cursor_row + i, 0);
10136 out_str(T_CE); /* erase a line */
10137 }
10138 screen_start(); /* don't know where cursor is now */
10139 }
10140 }
10141
10142 /*
10143 * If the 'db' flag is set, we need to clear the lines that have been
10144 * scrolled up at the bottom of the region.
10145 */
10146 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10147 {
10148 for (i = line_count; i > 0; --i)
10149 {
10150 windgoto(cursor_end - i, 0);
10151 out_str(T_CE); /* erase a line */
10152 screen_start(); /* don't know where cursor is now */
10153 }
10154 }
10155
10156#ifdef FEAT_GUI
10157 gui_can_update_cursor();
10158 if (gui.in_use)
10159 out_flush(); /* always flush after a scroll */
10160#endif
10161
10162 return OK;
10163}
10164
10165/*
10166 * show the current mode and ruler
10167 *
10168 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10169 * If clear_cmdline is FALSE there may be a message there that needs to be
10170 * cleared only if a mode is shown.
10171 * Return the length of the message (0 if no message).
10172 */
10173 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010174showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175{
10176 int need_clear;
10177 int length = 0;
10178 int do_mode;
10179 int attr;
10180 int nwr_save;
10181#ifdef FEAT_INS_EXPAND
10182 int sub_attr;
10183#endif
10184
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010185 do_mode = ((p_smd && msg_silent == 0)
10186 && ((State & INSERT)
10187 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010188 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 if (do_mode || Recording)
10190 {
10191 /*
10192 * Don't show mode right now, when not redrawing or inside a mapping.
10193 * Call char_avail() only when we are going to show something, because
10194 * it takes a bit of time.
10195 */
10196 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10197 {
10198 redraw_cmdline = TRUE; /* show mode later */
10199 return 0;
10200 }
10201
10202 nwr_save = need_wait_return;
10203
10204 /* wait a bit before overwriting an important message */
10205 check_for_delay(FALSE);
10206
10207 /* if the cmdline is more than one line high, erase top lines */
10208 need_clear = clear_cmdline;
10209 if (clear_cmdline && cmdline_row < Rows - 1)
10210 msg_clr_cmdline(); /* will reset clear_cmdline */
10211
10212 /* Position on the last line in the window, column 0 */
10213 msg_pos_mode();
10214 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010215 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 if (do_mode)
10217 {
10218 MSG_PUTS_ATTR("--", attr);
10219#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010220 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010221# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010222 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010223# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010224 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010225# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010226 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010227# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 MSG_PUTS_ATTR(" IM", attr);
10229# else
10230 MSG_PUTS_ATTR(" XIM", attr);
10231# endif
10232#endif
10233#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10234 if (gui.in_use)
10235 {
10236 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010237 {
10238 /* HANGUL */
10239 if (enc_utf8)
10240 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10241 else
10242 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 }
10245#endif
10246#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010247 /* CTRL-X in Insert mode */
10248 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 {
10250 /* These messages can get long, avoid a wrap in a narrow
10251 * window. Prefer showing edit_submode_extra. */
10252 length = (Rows - msg_row) * Columns - 3;
10253 if (edit_submode_extra != NULL)
10254 length -= vim_strsize(edit_submode_extra);
10255 if (length > 0)
10256 {
10257 if (edit_submode_pre != NULL)
10258 length -= vim_strsize(edit_submode_pre);
10259 if (length - vim_strsize(edit_submode) > 0)
10260 {
10261 if (edit_submode_pre != NULL)
10262 msg_puts_attr(edit_submode_pre, attr);
10263 msg_puts_attr(edit_submode, attr);
10264 }
10265 if (edit_submode_extra != NULL)
10266 {
10267 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10268 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010269 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 else
10271 sub_attr = attr;
10272 msg_puts_attr(edit_submode_extra, sub_attr);
10273 }
10274 }
10275 length = 0;
10276 }
10277 else
10278#endif
10279 {
10280#ifdef FEAT_VREPLACE
10281 if (State & VREPLACE_FLAG)
10282 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10283 else
10284#endif
10285 if (State & REPLACE_FLAG)
10286 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10287 else if (State & INSERT)
10288 {
10289#ifdef FEAT_RIGHTLEFT
10290 if (p_ri)
10291 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10292#endif
10293 MSG_PUTS_ATTR(_(" INSERT"), attr);
10294 }
10295 else if (restart_edit == 'I')
10296 MSG_PUTS_ATTR(_(" (insert)"), attr);
10297 else if (restart_edit == 'R')
10298 MSG_PUTS_ATTR(_(" (replace)"), attr);
10299 else if (restart_edit == 'V')
10300 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10301#ifdef FEAT_RIGHTLEFT
10302 if (p_hkmap)
10303 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10304# ifdef FEAT_FKMAP
10305 if (p_fkmap)
10306 MSG_PUTS_ATTR(farsi_text_5, attr);
10307# endif
10308#endif
10309#ifdef FEAT_KEYMAP
10310 if (State & LANGMAP)
10311 {
10312# ifdef FEAT_ARABIC
10313 if (curwin->w_p_arab)
10314 MSG_PUTS_ATTR(_(" Arabic"), attr);
10315 else
10316# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010317 if (get_keymap_str(curwin, (char_u *)" (%s)",
10318 NameBuff, MAXPATHL))
10319 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 }
10321#endif
10322 if ((State & INSERT) && p_paste)
10323 MSG_PUTS_ATTR(_(" (paste)"), attr);
10324
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 if (VIsual_active)
10326 {
10327 char *p;
10328
10329 /* Don't concatenate separate words to avoid translation
10330 * problems. */
10331 switch ((VIsual_select ? 4 : 0)
10332 + (VIsual_mode == Ctrl_V) * 2
10333 + (VIsual_mode == 'V'))
10334 {
10335 case 0: p = N_(" VISUAL"); break;
10336 case 1: p = N_(" VISUAL LINE"); break;
10337 case 2: p = N_(" VISUAL BLOCK"); break;
10338 case 4: p = N_(" SELECT"); break;
10339 case 5: p = N_(" SELECT LINE"); break;
10340 default: p = N_(" SELECT BLOCK"); break;
10341 }
10342 MSG_PUTS_ATTR(_(p), attr);
10343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 MSG_PUTS_ATTR(" --", attr);
10345 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010346
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 need_clear = TRUE;
10348 }
10349 if (Recording
10350#ifdef FEAT_INS_EXPAND
10351 && edit_submode == NULL /* otherwise it gets too long */
10352#endif
10353 )
10354 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010355 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 need_clear = TRUE;
10357 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010358
10359 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 if (need_clear || clear_cmdline)
10361 msg_clr_eos();
10362 msg_didout = FALSE; /* overwrite this message */
10363 length = msg_col;
10364 msg_col = 0;
10365 need_wait_return = nwr_save; /* never ask for hit-return for this */
10366 }
10367 else if (clear_cmdline && msg_silent == 0)
10368 /* Clear the whole command line. Will reset "clear_cmdline". */
10369 msg_clr_cmdline();
10370
10371#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 /* In Visual mode the size of the selected area must be redrawn. */
10373 if (VIsual_active)
10374 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375
10376 /* If the last window has no status line, the ruler is after the mode
10377 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010378 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 win_redr_ruler(lastwin, TRUE);
10380#endif
10381 redraw_cmdline = FALSE;
10382 clear_cmdline = FALSE;
10383
10384 return length;
10385}
10386
10387/*
10388 * Position for a mode message.
10389 */
10390 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010391msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392{
10393 msg_col = 0;
10394 msg_row = Rows - 1;
10395}
10396
10397/*
10398 * Delete mode message. Used when ESC is typed which is expected to end
10399 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010400 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 */
10402 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010403unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404{
10405 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010406 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 */
10408 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10409 redraw_cmdline = TRUE; /* delete mode later */
10410 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010411 clearmode();
10412}
10413
10414/*
10415 * Clear the mode message.
10416 */
10417 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010418clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010419{
10420 msg_pos_mode();
10421 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010422 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010423 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424}
10425
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010426 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010427recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010428{
10429 MSG_PUTS_ATTR(_("recording"), attr);
10430 if (!shortmess(SHM_RECORDING))
10431 {
10432 char_u s[4];
10433 sprintf((char *)s, " @%c", Recording);
10434 MSG_PUTS_ATTR(s, attr);
10435 }
10436}
10437
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010438/*
10439 * Draw the tab pages line at the top of the Vim window.
10440 */
10441 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010442draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010443{
10444 int tabcount = 0;
10445 tabpage_T *tp;
10446 int tabwidth;
10447 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010448 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010449 int attr;
10450 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010451 win_T *cwp;
10452 int wincount;
10453 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010454 int c;
10455 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010456 int attr_sel = HL_ATTR(HLF_TPS);
10457 int attr_nosel = HL_ATTR(HLF_TP);
10458 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010459 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010460 int room;
10461 int use_sep_chars = (t_colors < 8
10462#ifdef FEAT_GUI
10463 && !gui.in_use
10464#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010465#ifdef FEAT_TERMGUICOLORS
10466 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010467#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010468 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010469
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010470 if (ScreenLines == NULL)
10471 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010472 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010473
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010474#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010475 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010476 if (gui_use_tabline())
10477 {
10478 gui_update_tabline();
10479 return;
10480 }
10481#endif
10482
10483 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010484 return;
10485
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010486#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010487
10488 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10489 for (scol = 0; scol < Columns; ++scol)
10490 TabPageIdxs[scol] = 0;
10491
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010492 /* Use the 'tabline' option if it's set. */
10493 if (*p_tal != NUL)
10494 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010495 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010496
10497 /* Check for an error. If there is one we would loop in redrawing the
10498 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010499 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010500 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010501 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010502 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010503 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010504 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010505 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010506 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010507#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010508 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010509 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010510 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010511
Bram Moolenaar238a5642006-02-21 22:12:05 +000010512 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10513 if (tabwidth < 6)
10514 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010515
Bram Moolenaar238a5642006-02-21 22:12:05 +000010516 attr = attr_nosel;
10517 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010518 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010519 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10520 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010521 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010522 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010523
Bram Moolenaar238a5642006-02-21 22:12:05 +000010524 if (tp->tp_topframe == topframe)
10525 attr = attr_sel;
10526 if (use_sep_chars && col > 0)
10527 screen_putchar('|', 0, col++, attr);
10528
10529 if (tp->tp_topframe != topframe)
10530 attr = attr_nosel;
10531
10532 screen_putchar(' ', 0, col++, attr);
10533
10534 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010535 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010536 cwp = curwin;
10537 wp = firstwin;
10538 }
10539 else
10540 {
10541 cwp = tp->tp_curwin;
10542 wp = tp->tp_firstwin;
10543 }
10544
10545 modified = FALSE;
10546 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10547 if (bufIsChanged(wp->w_buffer))
10548 modified = TRUE;
10549 if (modified || wincount > 1)
10550 {
10551 if (wincount > 1)
10552 {
10553 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010554 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010555 if (col + len >= Columns - 3)
10556 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010557 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010558#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010559 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010560#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010561 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010562#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010563 );
10564 col += len;
10565 }
10566 if (modified)
10567 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10568 screen_putchar(' ', 0, col++, attr);
10569 }
10570
10571 room = scol - col + tabwidth - 1;
10572 if (room > 0)
10573 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010574 /* Get buffer name in NameBuff[] */
10575 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010576 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010577 len = vim_strsize(NameBuff);
10578 p = NameBuff;
10579#ifdef FEAT_MBYTE
10580 if (has_mbyte)
10581 while (len > room)
10582 {
10583 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010584 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010585 }
10586 else
10587#endif
10588 if (len > room)
10589 {
10590 p += len - room;
10591 len = room;
10592 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010593 if (len > Columns - col - 1)
10594 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010595
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010596 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010597 col += len;
10598 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010599 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010600
10601 /* Store the tab page number in TabPageIdxs[], so that
10602 * jump_to_mouse() knows where each one is. */
10603 ++tabcount;
10604 while (scol < col)
10605 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010606 }
10607
Bram Moolenaar238a5642006-02-21 22:12:05 +000010608 if (use_sep_chars)
10609 c = '_';
10610 else
10611 c = ' ';
10612 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010613
10614 /* Put an "X" for closing the current tab if there are several. */
10615 if (first_tabpage->tp_next != NULL)
10616 {
10617 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10618 TabPageIdxs[Columns - 1] = -999;
10619 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010620 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010621
10622 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10623 * set. */
10624 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010625}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010626
10627/*
10628 * Get buffer name for "buf" into NameBuff[].
10629 * Takes care of special buffer names and translates special characters.
10630 */
10631 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010632get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010633{
10634 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010635 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010636 else
10637 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10638 trans_characters(NameBuff, MAXPATHL);
10639}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010640
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641/*
10642 * Get the character to use in a status line. Get its attributes in "*attr".
10643 */
10644 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010645fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646{
10647 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010648
10649#ifdef FEAT_TERMINAL
10650 if (bt_terminal(wp->w_buffer))
10651 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010652 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010653 {
10654 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010655 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010656 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010657 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010658 {
10659 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010660 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010661 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010662 }
10663 else
10664#endif
10665 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010667 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 fill = fill_stl;
10669 }
10670 else
10671 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010672 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 fill = fill_stlnc;
10674 }
10675 /* Use fill when there is highlighting, and highlighting of current
10676 * window differs, or the fillchars differ, or this is not the
10677 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010678 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010679 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 || (fill_stl != fill_stlnc)))
10681 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010682 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 return '^';
10684 return '=';
10685}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687/*
10688 * Get the character to use in a separator between vertically split windows.
10689 * Get its attributes in "*attr".
10690 */
10691 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010692fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010694 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 if (*attr == 0 && fill_vert == ' ')
10696 return '|';
10697 else
10698 return fill_vert;
10699}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700
10701/*
10702 * Return TRUE if redrawing should currently be done.
10703 */
10704 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010705redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010707#ifdef FEAT_EVAL
10708 if (disable_redraw_for_testing)
10709 return 0;
10710 else
10711#endif
10712 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10714}
10715
10716/*
10717 * Return TRUE if printing messages should currently be done.
10718 */
10719 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010720messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721{
10722 return (!(p_lz && char_avail() && !KeyTyped));
10723}
10724
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010725#ifdef FEAT_MENU
10726/*
10727 * Draw the window toolbar.
10728 */
10729 static void
10730redraw_win_toolbar(win_T *wp)
10731{
10732 vimmenu_T *menu;
10733 int item_idx = 0;
10734 int item_count = 0;
10735 int col = 0;
10736 int next_col;
10737 int off = (int)(current_ScreenLine - ScreenLines);
10738 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10739 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10740
10741 vim_free(wp->w_winbar_items);
10742 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10743 ++item_count;
10744 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10745 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10746
10747 /* TODO: use fewer spaces if there is not enough room */
10748 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010749 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010750 {
10751 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010752 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010753 break;
10754 if (col > 1)
10755 {
10756 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010757 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010758 break;
10759 }
10760
10761 wp->w_winbar_items[item_idx].wb_startcol = col;
10762 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010763 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010764 break;
10765
10766 next_col = text_to_screenline(wp, menu->name, col);
10767 while (col < next_col)
10768 {
10769 ScreenAttrs[off + col] = button_attr;
10770 ++col;
10771 }
10772 wp->w_winbar_items[item_idx].wb_endcol = col;
10773 wp->w_winbar_items[item_idx].wb_menu = menu;
10774 ++item_idx;
10775
Bram Moolenaar02631462017-09-22 15:20:32 +020010776 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010777 break;
10778 space_to_screenline(off + col, button_attr);
10779 ++col;
10780 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010781 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010782 {
10783 space_to_screenline(off + col, fill_attr);
10784 ++col;
10785 }
10786 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10787
Bram Moolenaar02631462017-09-22 15:20:32 +020010788 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10789 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010790}
10791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792/*
10793 * Show current status info in ruler and various other places
10794 * If always is FALSE, only show ruler if position has changed.
10795 */
10796 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010797showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798{
10799 if (!always && !redrawing())
10800 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010801#ifdef FEAT_INS_EXPAND
10802 if (pum_visible())
10803 {
10804 /* Don't redraw right now, do it later. */
10805 curwin->w_redr_status = TRUE;
10806 return;
10807 }
10808#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010809#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010810 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010811 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010812 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 else
10815#endif
10816#ifdef FEAT_CMDL_INFO
10817 win_redr_ruler(curwin, always);
10818#endif
10819
10820#ifdef FEAT_TITLE
10821 if (need_maketitle
10822# ifdef FEAT_STL_OPT
10823 || (p_icon && (stl_syntax & STL_IN_ICON))
10824 || (p_title && (stl_syntax & STL_IN_TITLE))
10825# endif
10826 )
10827 maketitle();
10828#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010829 /* Redraw the tab pages line if needed. */
10830 if (redraw_tabline)
10831 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832}
10833
10834#ifdef FEAT_CMDL_INFO
10835 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010836win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010838#define RULER_BUF_LEN 70
10839 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 int row;
10841 int fillchar;
10842 int attr;
10843 int empty_line = FALSE;
10844 colnr_T virtcol;
10845 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010846 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 int this_ru_col;
10849 int off = 0;
10850 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851
10852 /* If 'ruler' off or redrawing disabled, don't do anything */
10853 if (!p_ru)
10854 return;
10855
10856 /*
10857 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10858 * after deleting lines, before cursor.lnum is corrected.
10859 */
10860 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10861 return;
10862
10863#ifdef FEAT_INS_EXPAND
10864 /* Don't draw the ruler while doing insert-completion, it might overwrite
10865 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 if (edit_submode != NULL)
10868 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010869 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10870 if (pum_visible())
10871 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872#endif
10873
10874#ifdef FEAT_STL_OPT
10875 if (*p_ruf)
10876 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010877 int save_called_emsg = called_emsg;
10878
10879 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010881 if (called_emsg)
10882 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010883 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010884 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 return;
10886 }
10887#endif
10888
10889 /*
10890 * Check if not in Insert mode and the line is empty (will show "0-1").
10891 */
10892 if (!(State & INSERT)
10893 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10894 empty_line = TRUE;
10895
10896 /*
10897 * Only draw the ruler when something changed.
10898 */
10899 validate_virtcol_win(wp);
10900 if ( redraw_cmdline
10901 || always
10902 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10903 || wp->w_cursor.col != wp->w_ru_cursor.col
10904 || wp->w_virtcol != wp->w_ru_virtcol
10905#ifdef FEAT_VIRTUALEDIT
10906 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10907#endif
10908 || wp->w_topline != wp->w_ru_topline
10909 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10910#ifdef FEAT_DIFF
10911 || wp->w_topfill != wp->w_ru_topfill
10912#endif
10913 || empty_line != wp->w_ru_empty)
10914 {
10915 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 if (wp->w_status_height)
10917 {
10918 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010919 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010920 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010921 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 }
10923 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 {
10925 row = Rows - 1;
10926 fillchar = ' ';
10927 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 width = Columns;
10929 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 }
10931
10932 /* In list mode virtcol needs to be recomputed */
10933 virtcol = wp->w_virtcol;
10934 if (wp->w_p_list && lcs_tab1 == NUL)
10935 {
10936 wp->w_p_list = FALSE;
10937 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10938 wp->w_p_list = TRUE;
10939 }
10940
10941 /*
10942 * Some sprintfs return the length, some return a pointer.
10943 * To avoid portability problems we use strlen() here.
10944 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010945 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10947 ? 0L
10948 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010949 len = STRLEN(buffer);
10950 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10952 (int)virtcol + 1);
10953
10954 /*
10955 * Add a "50%" if there is room for it.
10956 * On the last line, don't print in the last column (scrolls the
10957 * screen up on some terminals).
10958 */
10959 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010960 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 this_ru_col = ru_col - (Columns - width);
10965 if (this_ru_col < 0)
10966 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 /* Never use more than half the window/screen width, leave the other
10968 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010969 if (this_ru_col < (width + 1) / 2)
10970 this_ru_col = (width + 1) / 2;
10971 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010973 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010974 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 {
10976#ifdef FEAT_MBYTE
10977 if (has_mbyte)
10978 i += (*mb_char2bytes)(fillchar, buffer + i);
10979 else
10980#endif
10981 buffer[i++] = fillchar;
10982 ++o;
10983 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010984 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 }
10986 /* Truncate at window boundary. */
10987#ifdef FEAT_MBYTE
10988 if (has_mbyte)
10989 {
10990 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010991 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 {
10993 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010994 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 {
10996 buffer[i] = NUL;
10997 break;
10998 }
10999 }
11000 }
11001 else
11002#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011003 if (this_ru_col + (int)STRLEN(buffer) > width)
11004 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005
Bram Moolenaar4033c552017-09-16 20:54:51 +020011006 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 i = redraw_cmdline;
11008 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011009 this_ru_col + off + (int)STRLEN(buffer),
11010 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 fillchar, fillchar, attr);
11012 /* don't redraw the cmdline because of showing the ruler */
11013 redraw_cmdline = i;
11014 wp->w_ru_cursor = wp->w_cursor;
11015 wp->w_ru_virtcol = wp->w_virtcol;
11016 wp->w_ru_empty = empty_line;
11017 wp->w_ru_topline = wp->w_topline;
11018 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11019#ifdef FEAT_DIFF
11020 wp->w_ru_topfill = wp->w_topfill;
11021#endif
11022 }
11023}
11024#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011025
11026#if defined(FEAT_LINEBREAK) || defined(PROTO)
11027/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011028 * Return the width of the 'number' and 'relativenumber' column.
11029 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011030 * Otherwise it depends on 'numberwidth' and the line count.
11031 */
11032 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011033number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011034{
11035 int n;
11036 linenr_T lnum;
11037
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011038 if (wp->w_p_rnu && !wp->w_p_nu)
11039 /* cursor line shows "0" */
11040 lnum = wp->w_height;
11041 else
11042 /* cursor line shows absolute line number */
11043 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011044
Bram Moolenaar6b314672015-03-20 15:42:10 +010011045 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011046 return wp->w_nrwidth_width;
11047 wp->w_nrwidth_line_count = lnum;
11048
11049 n = 0;
11050 do
11051 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011052 lnum /= 10;
11053 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011054 } while (lnum > 0);
11055
11056 /* 'numberwidth' gives the minimal width plus one */
11057 if (n < wp->w_p_nuw - 1)
11058 n = wp->w_p_nuw - 1;
11059
11060 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011061 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011062 return n;
11063}
11064#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011065
11066/*
11067 * Return the current cursor column. This is the actual position on the
11068 * screen. First column is 0.
11069 */
11070 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011071screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011072{
11073 return screen_cur_col;
11074}
11075
11076/*
11077 * Return the current cursor row. This is the actual position on the screen.
11078 * First row is 0.
11079 */
11080 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011081screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011082{
11083 return screen_cur_row;
11084}