blob: 5ab2e85fc8109e04181664da7cbd5ac2e3247dbb [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();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100471#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100472 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200473 /* Don't update the cursor when it is blinking and off to avoid
474 * flicker. */
Bram Moolenaara338adc2018-01-31 20:51:47 +0100475 out_flush_cursor(FALSE, FALSE);
476 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100478 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200479
480 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100481}
482
483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 * Changed something in the current window, at buffer line "lnum", that
485 * requires that line and possibly other lines to be redrawn.
486 * Used when entering/leaving Insert mode with the cursor on a folded line.
487 * Used to remove the "$" from a change command.
488 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
489 * may become invalid and the whole window will have to be redrawn.
490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100492redrawWinline(
493 linenr_T lnum,
494 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496#ifdef FEAT_FOLDING
497 int i;
498#endif
499
500 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
501 curwin->w_redraw_top = lnum;
502 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
503 curwin->w_redraw_bot = lnum;
504 redraw_later(VALID);
505
506#ifdef FEAT_FOLDING
507 if (invalid)
508 {
509 /* A w_lines[] entry for this lnum has become invalid. */
510 i = find_wl_entry(curwin, lnum);
511 if (i >= 0)
512 curwin->w_lines[i].wl_valid = FALSE;
513 }
514#endif
515}
516
517/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200518 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 */
520 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100521update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
523 redraw_curbuf_later(type);
524 update_screen(type);
525}
526
527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 * Based on the current value of curwin->w_topline, transfer a screenfull
529 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200530 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200532 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200533update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200535 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 win_T *wp;
537 static int did_intro = FALSE;
538#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
539 int did_one;
540#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200541#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200542 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200543 int gui_cursor_col;
544 int gui_cursor_row;
545#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000548 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200550 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200552 if (type == VALID_NO_UPDATE)
553 {
554 no_update = TRUE;
555 type = 0;
556 }
557
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 if (must_redraw)
559 {
560 if (type < must_redraw) /* use maximal type */
561 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000562
563 /* must_redraw is reset here, so that when we run into some weird
564 * reason to redraw while busy redrawing (e.g., asynchronous
565 * scrolling), or update_topline() in win_update() will cause a
566 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 must_redraw = 0;
568 }
569
570 /* Need to update w_lines[]. */
571 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
572 type = NOT_VALID;
573
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000574 /* Postpone the redrawing when it's not needed and when being called
575 * recursively. */
576 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {
578 redraw_later(type); /* remember type for next time */
579 must_redraw = type;
580 if (type > INVERTED_ALL)
581 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200582 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 }
584
585 updating_screen = TRUE;
586#ifdef FEAT_SYN_HL
587 ++display_tick; /* let syntax code know we're in a next round of
588 * display updating */
589#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200590 if (no_update)
591 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 /*
594 * if the screen was scrolled up when displaying a message, scroll it down
595 */
596 if (msg_scrolled)
597 {
598 clear_cmdline = TRUE;
599 if (msg_scrolled > Rows - 5) /* clearing is faster */
600 type = CLEAR;
601 else if (type != CLEAR)
602 {
603 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200604 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
605 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 type = CLEAR;
607 FOR_ALL_WINDOWS(wp)
608 {
609 if (W_WINROW(wp) < msg_scrolled)
610 {
611 if (W_WINROW(wp) + wp->w_height > msg_scrolled
612 && wp->w_redr_type < REDRAW_TOP
613 && wp->w_lines_valid > 0
614 && wp->w_topline == wp->w_lines[0].wl_lnum)
615 {
616 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
617 wp->w_redr_type = REDRAW_TOP;
618 }
619 else
620 {
621 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200622 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
623 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 }
626 }
627 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200628 if (!no_update)
629 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000630 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 }
632 msg_scrolled = 0;
633 need_wait_return = FALSE;
634 }
635
636 /* reset cmdline_row now (may have been changed temporarily) */
637 compute_cmdrow();
638
639 /* Check for changed highlighting */
640 if (need_highlight_changed)
641 highlight_changed();
642
643 if (type == CLEAR) /* first clear screen */
644 {
645 screenclear(); /* will reset clear_cmdline */
646 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200647 /* must_redraw may be set indirectly, avoid another redraw later */
648 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 }
650
651 if (clear_cmdline) /* going to clear cmdline (done below) */
652 check_for_delay(FALSE);
653
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000654#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200655 /* Force redraw when width of 'number' or 'relativenumber' column
656 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000657 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200658 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
659 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000660 curwin->w_redr_type = NOT_VALID;
661#endif
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 /*
664 * Only start redrawing if there is really something to do.
665 */
666 if (type == INVERTED)
667 update_curswant();
668 if (curwin->w_redr_type < type
669 && !((type == VALID
670 && curwin->w_lines[0].wl_valid
671#ifdef FEAT_DIFF
672 && curwin->w_topfill == curwin->w_old_topfill
673 && curwin->w_botfill == curwin->w_old_botfill
674#endif
675 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000677 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
679 && curwin->w_old_visual_mode == VIsual_mode
680 && (curwin->w_valid & VALID_VIRTCOL)
681 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 ))
683 curwin->w_redr_type = type;
684
Bram Moolenaar5a305422006-04-28 22:38:25 +0000685 /* Redraw the tab pages line if needed. */
686 if (redraw_tabline || type >= NOT_VALID)
687 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#ifdef FEAT_SYN_HL
690 /*
691 * Correct stored syntax highlighting info for changes in each displayed
692 * buffer. Each buffer must only be done once.
693 */
694 FOR_ALL_WINDOWS(wp)
695 {
696 if (wp->w_buffer->b_mod_set)
697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 win_T *wwp;
699
700 /* Check if we already did this buffer. */
701 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
702 if (wwp->w_buffer == wp->w_buffer)
703 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200704 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 syn_stack_apply_changes(wp->w_buffer);
706 }
707 }
708#endif
709
710 /*
711 * Go from top to bottom through the windows, redrawing the ones that need
712 * it.
713 */
714#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
715 did_one = FALSE;
716#endif
717#ifdef FEAT_SEARCH_EXTRA
718 search_hl.rm.regprog = NULL;
719#endif
720 FOR_ALL_WINDOWS(wp)
721 {
722 if (wp->w_redr_type != 0)
723 {
724 cursor_off();
725#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
726 if (!did_one)
727 {
728 did_one = TRUE;
729# ifdef FEAT_SEARCH_EXTRA
730 start_search_hl();
731# endif
732# ifdef FEAT_CLIPBOARD
733 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200734 if (clip_star.available && clip_isautosel_star())
735 clip_update_selection(&clip_star);
736 if (clip_plus.available && clip_isautosel_plus())
737 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738# endif
739#ifdef FEAT_GUI
740 /* Remove the cursor before starting to do anything, because
741 * scrolling may make it difficult to redraw the text under
742 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200743 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200744 {
745 gui_cursor_col = gui.cursor_col;
746 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200748 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750#endif
751 }
752#endif
753 win_update(wp);
754 }
755
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 /* redraw status line after the window to minimize cursor movement */
757 if (wp->w_redr_status)
758 {
759 cursor_off();
760 win_redr_status(wp);
761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 }
763#if defined(FEAT_SEARCH_EXTRA)
764 end_search_hl();
765#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100766#ifdef FEAT_INS_EXPAND
767 /* May need to redraw the popup menu. */
768 if (pum_visible())
769 pum_redraw();
770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 /* Reset b_mod_set flags. Going through all windows is probably faster
773 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200774 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
777 updating_screen = FALSE;
778#ifdef FEAT_GUI
779 gui_may_resize_shell();
780#endif
781
782 /* Clear or redraw the command line. Done last, because scrolling may
783 * mess up the command line. */
784 if (clear_cmdline || redraw_cmdline)
785 showmode();
786
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200787 if (no_update)
788 --no_win_do_lines_ins;
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200791 if (!did_intro)
792 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 did_intro = TRUE;
794
795#ifdef FEAT_GUI
796 /* Redraw the cursor and update the scrollbars when all screen updating is
797 * done. */
798 if (gui.in_use)
799 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200800 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200801 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100802 mch_disable_flush();
803 out_flush(); /* required before updating the cursor */
804 mch_enable_flush();
805
Bram Moolenaar144445d2016-07-08 21:41:54 +0200806 /* 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 Moolenaara338adc2018-01-31 20:51:47 +0100814 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200815 screen_cur_col = gui.col;
816 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200817 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100818 else
819 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 gui_update_scrollbars(FALSE);
821 }
822#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200823 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824}
825
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100826#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
827/*
828 * Prepare for updating one or more windows.
829 * Caller must check for "updating_screen" already set to avoid recursiveness.
830 */
831 static void
832update_prepare(void)
833{
834 cursor_off();
835 updating_screen = TRUE;
836#ifdef FEAT_GUI
837 /* Remove the cursor before starting to do anything, because scrolling may
838 * make it difficult to redraw the text under it. */
839 if (gui.in_use)
840 gui_undraw_cursor();
841#endif
842#ifdef FEAT_SEARCH_EXTRA
843 start_search_hl();
844#endif
845}
846
847/*
848 * Finish updating one or more windows.
849 */
850 static void
851update_finish(void)
852{
853 if (redraw_cmdline)
854 showmode();
855
856# ifdef FEAT_SEARCH_EXTRA
857 end_search_hl();
858# endif
859
860 updating_screen = FALSE;
861
862# ifdef FEAT_GUI
863 gui_may_resize_shell();
864
865 /* Redraw the cursor and update the scrollbars when all screen updating is
866 * done. */
867 if (gui.in_use)
868 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100869 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100870 gui_update_scrollbars(FALSE);
871 }
872# endif
873}
874#endif
875
Bram Moolenaar860cae12010-06-05 23:22:07 +0200876#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200877/*
878 * Return TRUE if the cursor line in window "wp" may be concealed, according
879 * to the 'concealcursor' option.
880 */
881 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100882conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200883{
884 int c;
885
886 if (*wp->w_p_cocu == NUL)
887 return FALSE;
888 if (get_real_state() & VISUAL)
889 c = 'v';
890 else if (State & INSERT)
891 c = 'i';
892 else if (State & NORMAL)
893 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200894 else if (State & CMDLINE)
895 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200896 else
897 return FALSE;
898 return vim_strchr(wp->w_p_cocu, c) != NULL;
899}
900
901/*
902 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
903 */
904 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100905conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200906{
907 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
908 {
909 need_cursor_line_redraw = TRUE;
910 /* Need to recompute cursor column, e.g., when starting Visual mode
911 * without concealing. */
912 curs_columns(TRUE);
913 }
914}
915
Bram Moolenaar860cae12010-06-05 23:22:07 +0200916 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100917update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200918{
919 int row;
920 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200921#ifdef SYN_TIME_LIMIT
922 proftime_T syntax_tm;
923#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200924
Bram Moolenaar908be432016-05-24 10:51:30 +0200925 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100926 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200927 return;
928
Bram Moolenaar860cae12010-06-05 23:22:07 +0200929 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200930 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200931 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200932#ifdef SYN_TIME_LIMIT
933 /* Set the time limit to 'redrawtime'. */
934 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200935 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200936#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100937 update_prepare();
938
Bram Moolenaar860cae12010-06-05 23:22:07 +0200939 row = 0;
940 for (j = 0; j < wp->w_lines_valid; ++j)
941 {
942 if (lnum == wp->w_lines[j].wl_lnum)
943 {
944 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200945# ifdef FEAT_SEARCH_EXTRA
946 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200947 start_search_hl();
948 prepare_search_hl(wp, lnum);
949# endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200950 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200951# if defined(FEAT_SEARCH_EXTRA)
952 end_search_hl();
953# endif
954 break;
955 }
956 row += wp->w_lines[j].wl_size;
957 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100958
959 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200960
961#ifdef SYN_TIME_LIMIT
962 syn_set_timeout(NULL);
963#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200964 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200965 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967#endif
968
969#if defined(FEAT_SIGNS) || defined(PROTO)
970 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100971update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 win_T *wp;
974 int doit = FALSE;
975
976# ifdef FEAT_FOLDING
977 win_foldinfo.fi_level = 0;
978# endif
979
980 /* update/delete a specific mark */
981 FOR_ALL_WINDOWS(wp)
982 {
983 if (buf != NULL && lnum > 0)
984 {
985 if (wp->w_buffer == buf && lnum >= wp->w_topline
986 && lnum < wp->w_botline)
987 {
988 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
989 wp->w_redraw_top = lnum;
990 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
991 wp->w_redraw_bot = lnum;
992 redraw_win_later(wp, VALID);
993 }
994 }
995 else
996 redraw_win_later(wp, VALID);
997 if (wp->w_redr_type != 0)
998 doit = TRUE;
999 }
1000
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001001 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001002 * happening (recursive call), messages on the screen or still starting up.
1003 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001004 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001005 || State == ASKMORE || State == HITRETURN
1006 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001007#ifdef FEAT_GUI
1008 || gui.starting
1009#endif
1010 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 return;
1012
1013 /* update all windows that need updating */
1014 update_prepare();
1015
Bram Moolenaar29323592016-07-24 22:04:11 +02001016 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 {
1018 if (wp->w_redr_type != 0)
1019 win_update(wp);
1020 if (wp->w_redr_status)
1021 win_redr_status(wp);
1022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 update_finish();
1025}
1026#endif
1027
1028
1029#if defined(FEAT_GUI) || defined(PROTO)
1030/*
1031 * Update a single window, its status line and maybe the command line msg.
1032 * Used for the GUI scrollbar.
1033 */
1034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001035updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001037 /* return if already busy updating */
1038 if (updating_screen)
1039 return;
1040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 update_prepare();
1042
1043#ifdef FEAT_CLIPBOARD
1044 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001045 if (clip_star.available && clip_isautosel_star())
1046 clip_update_selection(&clip_star);
1047 if (clip_plus.available && clip_isautosel_plus())
1048 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001050
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001052
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001053 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001054 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001055 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (wp->w_redr_status
1058# ifdef FEAT_CMDL_INFO
1059 || p_ru
1060# endif
1061# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001062 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063# endif
1064 )
1065 win_redr_status(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
1067 update_finish();
1068}
1069#endif
1070
1071/*
1072 * Update a single window.
1073 *
1074 * This may cause the windows below it also to be redrawn (when clearing the
1075 * screen or scrolling lines).
1076 *
1077 * How the window is redrawn depends on wp->w_redr_type. Each type also
1078 * implies the one below it.
1079 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001080 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1082 * INVERTED redraw the changed part of the Visual area
1083 * INVERTED_ALL redraw the whole Visual area
1084 * VALID 1. scroll up/down to adjust for a changed w_topline
1085 * 2. update lines at the top when scrolled down
1086 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001087 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 * b_mod_top and b_mod_bot.
1089 * - if wp->w_redraw_top non-zero, redraw lines between
1090 * wp->w_redraw_top and wp->w_redr_bot.
1091 * - continue redrawing when syntax status is invalid.
1092 * 4. if scrolled up, update lines at the bottom.
1093 * This results in three areas that may need updating:
1094 * top: from first row to top_end (when scrolled down)
1095 * mid: from mid_start to mid_end (update inversion or changed text)
1096 * bot: from bot_start to last row (when scrolled up)
1097 */
1098 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001099win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100{
1101 buf_T *buf = wp->w_buffer;
1102 int type;
1103 int top_end = 0; /* Below last row of the top area that needs
1104 updating. 0 when no top area updating. */
1105 int mid_start = 999;/* first row of the mid area that needs
1106 updating. 999 when no mid area updating. */
1107 int mid_end = 0; /* Below last row of the mid area that needs
1108 updating. 0 when no mid area updating. */
1109 int bot_start = 999;/* first row of the bot area that needs
1110 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 int scrolled_down = FALSE; /* TRUE when scrolled down when
1112 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001114 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 int top_to_mod = FALSE; /* redraw above mod_top */
1116#endif
1117
1118 int row; /* current window row to display */
1119 linenr_T lnum; /* current buffer lnum to display */
1120 int idx; /* current index in w_lines[] */
1121 int srow; /* starting row of the current line */
1122
1123 int eof = FALSE; /* if TRUE, we hit the end of the file */
1124 int didline = FALSE; /* if TRUE, we finished the last line */
1125 int i;
1126 long j;
1127 static int recursive = FALSE; /* being called recursively */
1128 int old_botline = wp->w_botline;
1129#ifdef FEAT_FOLDING
1130 long fold_count;
1131#endif
1132#ifdef FEAT_SYN_HL
1133 /* remember what happened to the previous line, to know if
1134 * check_visual_highlight() can be used */
1135#define DID_NONE 1 /* didn't update a line */
1136#define DID_LINE 2 /* updated a normal line */
1137#define DID_FOLD 3 /* updated a folded line */
1138 int did_update = DID_NONE;
1139 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1140#endif
1141 linenr_T mod_top = 0;
1142 linenr_T mod_bot = 0;
1143#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1144 int save_got_int;
1145#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001146#ifdef SYN_TIME_LIMIT
1147 proftime_T syntax_tm;
1148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
1150 type = wp->w_redr_type;
1151
1152 if (type == NOT_VALID)
1153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 wp->w_lines_valid = 0;
1156 }
1157
1158 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001159 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
1161 wp->w_redr_type = 0;
1162 return;
1163 }
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 /* Window is zero-width: Only need to draw the separator. */
1166 if (wp->w_width == 0)
1167 {
1168 /* draw the vertical separator right of this window */
1169 draw_vsep_win(wp, 0);
1170 wp->w_redr_type = 0;
1171 return;
1172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001174#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001175 /* If this window contains a terminal, redraw works completely differently.
1176 */
1177 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001178 {
Bram Moolenaar181ca992018-02-13 21:19:21 +01001179# ifdef FEAT_MENU
1180 /* Draw the window toolbar, if there is one. */
1181 if (winbar_height(wp) > 0)
1182 redraw_win_toolbar(wp);
1183# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001184 wp->w_redr_type = 0;
1185 return;
1186 }
1187#endif
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001190 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191#endif
1192
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001193#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001194 /* Force redraw when width of 'number' or 'relativenumber' column
1195 * changes. */
1196 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001197 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001198 {
1199 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001200 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001201 }
1202 else
1203#endif
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1206 {
1207 /*
1208 * When there are both inserted/deleted lines and specific lines to be
1209 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1210 * everything (only happens when redrawing is off for while).
1211 */
1212 type = NOT_VALID;
1213 }
1214 else
1215 {
1216 /*
1217 * Set mod_top to the first line that needs displaying because of
1218 * changes. Set mod_bot to the first line after the changes.
1219 */
1220 mod_top = wp->w_redraw_top;
1221 if (wp->w_redraw_bot != 0)
1222 mod_bot = wp->w_redraw_bot + 1;
1223 else
1224 mod_bot = 0;
1225 wp->w_redraw_top = 0; /* reset for next time */
1226 wp->w_redraw_bot = 0;
1227 if (buf->b_mod_set)
1228 {
1229 if (mod_top == 0 || mod_top > buf->b_mod_top)
1230 {
1231 mod_top = buf->b_mod_top;
1232#ifdef FEAT_SYN_HL
1233 /* Need to redraw lines above the change that may be included
1234 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001235 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001237 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 if (mod_top < 1)
1239 mod_top = 1;
1240 }
1241#endif
1242 }
1243 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1244 mod_bot = buf->b_mod_bot;
1245
1246#ifdef FEAT_SEARCH_EXTRA
1247 /* When 'hlsearch' is on and using a multi-line search pattern, a
1248 * change in one line may make the Search highlighting in a
1249 * previous line invalid. Simple solution: redraw all visible
1250 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001251 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001253 if (search_hl.rm.regprog != NULL
1254 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001256 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001257 {
1258 cur = wp->w_match_head;
1259 while (cur != NULL)
1260 {
1261 if (cur->match.regprog != NULL
1262 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001263 {
1264 top_to_mod = TRUE;
1265 break;
1266 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001267 cur = cur->next;
1268 }
1269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270#endif
1271 }
1272#ifdef FEAT_FOLDING
1273 if (mod_top != 0 && hasAnyFolding(wp))
1274 {
1275 linenr_T lnumt, lnumb;
1276
1277 /*
1278 * A change in a line can cause lines above it to become folded or
1279 * unfolded. Find the top most buffer line that may be affected.
1280 * If the line was previously folded and displayed, get the first
1281 * line of that fold. If the line is folded now, get the first
1282 * folded line. Use the minimum of these two.
1283 */
1284
1285 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1286 * the line below it. If there is no valid entry, use w_topline.
1287 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1288 * to this line. If there is no valid entry, use MAXLNUM. */
1289 lnumt = wp->w_topline;
1290 lnumb = MAXLNUM;
1291 for (i = 0; i < wp->w_lines_valid; ++i)
1292 if (wp->w_lines[i].wl_valid)
1293 {
1294 if (wp->w_lines[i].wl_lastlnum < mod_top)
1295 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1296 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1297 {
1298 lnumb = wp->w_lines[i].wl_lnum;
1299 /* When there is a fold column it might need updating
1300 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001301 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 ++lnumb;
1303 }
1304 }
1305
1306 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1307 if (mod_top > lnumt)
1308 mod_top = lnumt;
1309
1310 /* Now do the same for the bottom line (one above mod_bot). */
1311 --mod_bot;
1312 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1313 ++mod_bot;
1314 if (mod_bot < lnumb)
1315 mod_bot = lnumb;
1316 }
1317#endif
1318
1319 /* When a change starts above w_topline and the end is below
1320 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001321 * If the end of the change is above w_topline: do like no change was
1322 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (mod_top != 0 && mod_top < wp->w_topline)
1324 {
1325 if (mod_bot > wp->w_topline)
1326 mod_top = wp->w_topline;
1327#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001328 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 top_end = 1;
1330#endif
1331 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001332
1333 /* When line numbers are displayed need to redraw all lines below
1334 * inserted/deleted lines. */
1335 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1336 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338
1339 /*
1340 * When only displaying the lines at the top, set top_end. Used when
1341 * window has scrolled down for msg_scrolled.
1342 */
1343 if (type == REDRAW_TOP)
1344 {
1345 j = 0;
1346 for (i = 0; i < wp->w_lines_valid; ++i)
1347 {
1348 j += wp->w_lines[i].wl_size;
1349 if (j >= wp->w_upd_rows)
1350 {
1351 top_end = j;
1352 break;
1353 }
1354 }
1355 if (top_end == 0)
1356 /* not found (cannot happen?): redraw everything */
1357 type = NOT_VALID;
1358 else
1359 /* top area defined, the rest is VALID */
1360 type = VALID;
1361 }
1362
Bram Moolenaar367329b2007-08-30 11:53:22 +00001363 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001364 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1365 * non-zero and thus not FALSE) will indicate that screenclear() was not
1366 * called. */
1367 if (screen_cleared)
1368 screen_cleared = MAYBE;
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /*
1371 * If there are no changes on the screen that require a complete redraw,
1372 * handle three cases:
1373 * 1: we are off the top of the screen by a few lines: scroll down
1374 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1375 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1376 * w_lines[] that needs updating.
1377 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001378 if ((type == VALID || type == SOME_VALID
1379 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380#ifdef FEAT_DIFF
1381 && !wp->w_botfill && !wp->w_old_botfill
1382#endif
1383 )
1384 {
1385 if (mod_top != 0 && wp->w_topline == mod_top)
1386 {
1387 /*
1388 * w_topline is the first changed line, the scrolling will be done
1389 * further down.
1390 */
1391 }
1392 else if (wp->w_lines[0].wl_valid
1393 && (wp->w_topline < wp->w_lines[0].wl_lnum
1394#ifdef FEAT_DIFF
1395 || (wp->w_topline == wp->w_lines[0].wl_lnum
1396 && wp->w_topfill > wp->w_old_topfill)
1397#endif
1398 ))
1399 {
1400 /*
1401 * New topline is above old topline: May scroll down.
1402 */
1403#ifdef FEAT_FOLDING
1404 if (hasAnyFolding(wp))
1405 {
1406 linenr_T ln;
1407
1408 /* count the number of lines we are off, counting a sequence
1409 * of folded lines as one */
1410 j = 0;
1411 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1412 {
1413 ++j;
1414 if (j >= wp->w_height - 2)
1415 break;
1416 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1417 }
1418 }
1419 else
1420#endif
1421 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1422 if (j < wp->w_height - 2) /* not too far off */
1423 {
1424 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1425#ifdef FEAT_DIFF
1426 /* insert extra lines for previously invisible filler lines */
1427 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1428 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1429 - wp->w_old_topfill;
1430#endif
1431 if (i < wp->w_height - 2) /* less than a screen off */
1432 {
1433 /*
1434 * Try to insert the correct number of lines.
1435 * If not the last window, delete the lines at the bottom.
1436 * win_ins_lines may fail when the terminal can't do it.
1437 */
1438 if (i > 0)
1439 check_for_delay(FALSE);
1440 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1441 {
1442 if (wp->w_lines_valid != 0)
1443 {
1444 /* Need to update rows that are new, stop at the
1445 * first one that scrolled down. */
1446 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
1449 /* Move the entries that were scrolled, disable
1450 * the entries for the lines to be redrawn. */
1451 if ((wp->w_lines_valid += j) > wp->w_height)
1452 wp->w_lines_valid = wp->w_height;
1453 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1454 wp->w_lines[idx] = wp->w_lines[idx - j];
1455 while (idx >= 0)
1456 wp->w_lines[idx--].wl_valid = FALSE;
1457 }
1458 }
1459 else
1460 mid_start = 0; /* redraw all lines */
1461 }
1462 else
1463 mid_start = 0; /* redraw all lines */
1464 }
1465 else
1466 mid_start = 0; /* redraw all lines */
1467 }
1468 else
1469 {
1470 /*
1471 * New topline is at or below old topline: May scroll up.
1472 * When topline didn't change, find first entry in w_lines[] that
1473 * needs updating.
1474 */
1475
1476 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1477 j = -1;
1478 row = 0;
1479 for (i = 0; i < wp->w_lines_valid; i++)
1480 {
1481 if (wp->w_lines[i].wl_valid
1482 && wp->w_lines[i].wl_lnum == wp->w_topline)
1483 {
1484 j = i;
1485 break;
1486 }
1487 row += wp->w_lines[i].wl_size;
1488 }
1489 if (j == -1)
1490 {
1491 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1492 * lines */
1493 mid_start = 0;
1494 }
1495 else
1496 {
1497 /*
1498 * Try to delete the correct number of lines.
1499 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1500 */
1501#ifdef FEAT_DIFF
1502 /* If the topline didn't change, delete old filler lines,
1503 * otherwise delete filler lines of the new topline... */
1504 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1505 row += wp->w_old_topfill;
1506 else
1507 row += diff_check_fill(wp, wp->w_topline);
1508 /* ... but don't delete new filler lines. */
1509 row -= wp->w_topfill;
1510#endif
1511 if (row > 0)
1512 {
1513 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001514 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1515 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 bot_start = wp->w_height - row;
1517 else
1518 mid_start = 0; /* redraw all lines */
1519 }
1520 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1521 {
1522 /*
1523 * Skip the lines (below the deleted lines) that are still
1524 * valid and don't need redrawing. Copy their info
1525 * upwards, to compensate for the deleted lines. Set
1526 * bot_start to the first row that needs redrawing.
1527 */
1528 bot_start = 0;
1529 idx = 0;
1530 for (;;)
1531 {
1532 wp->w_lines[idx] = wp->w_lines[j];
1533 /* stop at line that didn't fit, unless it is still
1534 * valid (no lines deleted) */
1535 if (row > 0 && bot_start + row
1536 + (int)wp->w_lines[j].wl_size > wp->w_height)
1537 {
1538 wp->w_lines_valid = idx + 1;
1539 break;
1540 }
1541 bot_start += wp->w_lines[idx++].wl_size;
1542
1543 /* stop at the last valid entry in w_lines[].wl_size */
1544 if (++j >= wp->w_lines_valid)
1545 {
1546 wp->w_lines_valid = idx;
1547 break;
1548 }
1549 }
1550#ifdef FEAT_DIFF
1551 /* Correct the first entry for filler lines at the top
1552 * when it won't get updated below. */
1553 if (wp->w_p_diff && bot_start > 0)
1554 wp->w_lines[0].wl_size =
1555 plines_win_nofill(wp, wp->w_topline, TRUE)
1556 + wp->w_topfill;
1557#endif
1558 }
1559 }
1560 }
1561
1562 /* When starting redraw in the first line, redraw all lines. When
1563 * there is only one window it's probably faster to clear the screen
1564 * first. */
1565 if (mid_start == 0)
1566 {
1567 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001568 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001569 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001570 /* Clear the screen when it was not done by win_del_lines() or
1571 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1572 * then. */
1573 if (screen_cleared != TRUE)
1574 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001575 /* The screen was cleared, redraw the tab pages line. */
1576 if (redraw_tabline)
1577 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001580
1581 /* When win_del_lines() or win_ins_lines() caused the screen to be
1582 * cleared (only happens for the first window) or when screenclear()
1583 * was called directly above, "must_redraw" will have been set to
1584 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1585 if (screen_cleared == TRUE)
1586 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
1588 else
1589 {
1590 /* Not VALID or INVERTED: redraw all lines. */
1591 mid_start = 0;
1592 mid_end = wp->w_height;
1593 }
1594
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001595 if (type == SOME_VALID)
1596 {
1597 /* SOME_VALID: redraw all lines. */
1598 mid_start = 0;
1599 mid_end = wp->w_height;
1600 type = NOT_VALID;
1601 }
1602
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 /* check if we are updating or removing the inverted part */
1604 if ((VIsual_active && buf == curwin->w_buffer)
1605 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1606 {
1607 linenr_T from, to;
1608
1609 if (VIsual_active)
1610 {
1611 if (VIsual_active
1612 && (VIsual_mode != wp->w_old_visual_mode
1613 || type == INVERTED_ALL))
1614 {
1615 /*
1616 * If the type of Visual selection changed, redraw the whole
1617 * selection. Also when the ownership of the X selection is
1618 * gained or lost.
1619 */
1620 if (curwin->w_cursor.lnum < VIsual.lnum)
1621 {
1622 from = curwin->w_cursor.lnum;
1623 to = VIsual.lnum;
1624 }
1625 else
1626 {
1627 from = VIsual.lnum;
1628 to = curwin->w_cursor.lnum;
1629 }
1630 /* redraw more when the cursor moved as well */
1631 if (wp->w_old_cursor_lnum < from)
1632 from = wp->w_old_cursor_lnum;
1633 if (wp->w_old_cursor_lnum > to)
1634 to = wp->w_old_cursor_lnum;
1635 if (wp->w_old_visual_lnum < from)
1636 from = wp->w_old_visual_lnum;
1637 if (wp->w_old_visual_lnum > to)
1638 to = wp->w_old_visual_lnum;
1639 }
1640 else
1641 {
1642 /*
1643 * Find the line numbers that need to be updated: The lines
1644 * between the old cursor position and the current cursor
1645 * position. Also check if the Visual position changed.
1646 */
1647 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1648 {
1649 from = curwin->w_cursor.lnum;
1650 to = wp->w_old_cursor_lnum;
1651 }
1652 else
1653 {
1654 from = wp->w_old_cursor_lnum;
1655 to = curwin->w_cursor.lnum;
1656 if (from == 0) /* Visual mode just started */
1657 from = to;
1658 }
1659
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001660 if (VIsual.lnum != wp->w_old_visual_lnum
1661 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 {
1663 if (wp->w_old_visual_lnum < from
1664 && wp->w_old_visual_lnum != 0)
1665 from = wp->w_old_visual_lnum;
1666 if (wp->w_old_visual_lnum > to)
1667 to = wp->w_old_visual_lnum;
1668 if (VIsual.lnum < from)
1669 from = VIsual.lnum;
1670 if (VIsual.lnum > to)
1671 to = VIsual.lnum;
1672 }
1673 }
1674
1675 /*
1676 * If in block mode and changed column or curwin->w_curswant:
1677 * update all lines.
1678 * First compute the actual start and end column.
1679 */
1680 if (VIsual_mode == Ctrl_V)
1681 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001682 colnr_T fromc, toc;
1683#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1684 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaar404406a2014-10-09 13:24:43 +02001686 if (curwin->w_p_lbr)
1687 ve_flags = VE_ALL;
1688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001690#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1691 ve_flags = save_ve_flags;
1692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 ++toc;
1694 if (curwin->w_curswant == MAXCOL)
1695 toc = MAXCOL;
1696
1697 if (fromc != wp->w_old_cursor_fcol
1698 || toc != wp->w_old_cursor_lcol)
1699 {
1700 if (from > VIsual.lnum)
1701 from = VIsual.lnum;
1702 if (to < VIsual.lnum)
1703 to = VIsual.lnum;
1704 }
1705 wp->w_old_cursor_fcol = fromc;
1706 wp->w_old_cursor_lcol = toc;
1707 }
1708 }
1709 else
1710 {
1711 /* Use the line numbers of the old Visual area. */
1712 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1713 {
1714 from = wp->w_old_cursor_lnum;
1715 to = wp->w_old_visual_lnum;
1716 }
1717 else
1718 {
1719 from = wp->w_old_visual_lnum;
1720 to = wp->w_old_cursor_lnum;
1721 }
1722 }
1723
1724 /*
1725 * There is no need to update lines above the top of the window.
1726 */
1727 if (from < wp->w_topline)
1728 from = wp->w_topline;
1729
1730 /*
1731 * If we know the value of w_botline, use it to restrict the update to
1732 * the lines that are visible in the window.
1733 */
1734 if (wp->w_valid & VALID_BOTLINE)
1735 {
1736 if (from >= wp->w_botline)
1737 from = wp->w_botline - 1;
1738 if (to >= wp->w_botline)
1739 to = wp->w_botline - 1;
1740 }
1741
1742 /*
1743 * Find the minimal part to be updated.
1744 * Watch out for scrolling that made entries in w_lines[] invalid.
1745 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1746 * top_end; need to redraw from top_end to the "to" line.
1747 * A middle mouse click with a Visual selection may change the text
1748 * above the Visual area and reset wl_valid, do count these for
1749 * mid_end (in srow).
1750 */
1751 if (mid_start > 0)
1752 {
1753 lnum = wp->w_topline;
1754 idx = 0;
1755 srow = 0;
1756 if (scrolled_down)
1757 mid_start = top_end;
1758 else
1759 mid_start = 0;
1760 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1761 {
1762 if (wp->w_lines[idx].wl_valid)
1763 mid_start += wp->w_lines[idx].wl_size;
1764 else if (!scrolled_down)
1765 srow += wp->w_lines[idx].wl_size;
1766 ++idx;
1767# ifdef FEAT_FOLDING
1768 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1769 lnum = wp->w_lines[idx].wl_lnum;
1770 else
1771# endif
1772 ++lnum;
1773 }
1774 srow += mid_start;
1775 mid_end = wp->w_height;
1776 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1777 {
1778 if (wp->w_lines[idx].wl_valid
1779 && wp->w_lines[idx].wl_lnum >= to + 1)
1780 {
1781 /* Only update until first row of this line */
1782 mid_end = srow;
1783 break;
1784 }
1785 srow += wp->w_lines[idx].wl_size;
1786 }
1787 }
1788 }
1789
1790 if (VIsual_active && buf == curwin->w_buffer)
1791 {
1792 wp->w_old_visual_mode = VIsual_mode;
1793 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1794 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001795 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 wp->w_old_curswant = curwin->w_curswant;
1797 }
1798 else
1799 {
1800 wp->w_old_visual_mode = 0;
1801 wp->w_old_cursor_lnum = 0;
1802 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001803 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
1806#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1807 /* reset got_int, otherwise regexp won't work */
1808 save_got_int = got_int;
1809 got_int = 0;
1810#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001811#ifdef SYN_TIME_LIMIT
1812 /* Set the time limit to 'redrawtime'. */
1813 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001814 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816#ifdef FEAT_FOLDING
1817 win_foldinfo.fi_level = 0;
1818#endif
1819
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001820#ifdef FEAT_MENU
1821 /*
1822 * Draw the window toolbar, if there is one.
1823 * TODO: only when needed.
1824 */
1825 if (winbar_height(wp) > 0)
1826 redraw_win_toolbar(wp);
1827#endif
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 /*
1830 * Update all the window rows.
1831 */
1832 idx = 0; /* first entry in w_lines[].wl_size */
1833 row = 0;
1834 srow = 0;
1835 lnum = wp->w_topline; /* first line shown in window */
1836 for (;;)
1837 {
1838 /* stop updating when reached the end of the window (check for _past_
1839 * the end of the window is at the end of the loop) */
1840 if (row == wp->w_height)
1841 {
1842 didline = TRUE;
1843 break;
1844 }
1845
1846 /* stop updating when hit the end of the file */
1847 if (lnum > buf->b_ml.ml_line_count)
1848 {
1849 eof = TRUE;
1850 break;
1851 }
1852
1853 /* Remember the starting row of the line that is going to be dealt
1854 * with. It is used further down when the line doesn't fit. */
1855 srow = row;
1856
1857 /*
1858 * Update a line when it is in an area that needs updating, when it
1859 * has changes or w_lines[idx] is invalid.
1860 * bot_start may be halfway a wrapped line after using
1861 * win_del_lines(), check if the current line includes it.
1862 * When syntax folding is being used, the saved syntax states will
1863 * already have been updated, we can't see where the syntax state is
1864 * the same again, just update until the end of the window.
1865 */
1866 if (row < top_end
1867 || (row >= mid_start && row < mid_end)
1868#ifdef FEAT_SEARCH_EXTRA
1869 || top_to_mod
1870#endif
1871 || idx >= wp->w_lines_valid
1872 || (row + wp->w_lines[idx].wl_size > bot_start)
1873 || (mod_top != 0
1874 && (lnum == mod_top
1875 || (lnum >= mod_top
1876 && (lnum < mod_bot
1877#ifdef FEAT_SYN_HL
1878 || did_update == DID_FOLD
1879 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001880 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 && (
1882# ifdef FEAT_FOLDING
1883 (foldmethodIsSyntax(wp)
1884 && hasAnyFolding(wp)) ||
1885# endif
1886 syntax_check_changed(lnum)))
1887#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001888#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001889 /* match in fixed position might need redraw
1890 * if lines were inserted or deleted */
1891 || (wp->w_match_head != NULL
1892 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 )))))
1895 {
1896#ifdef FEAT_SEARCH_EXTRA
1897 if (lnum == mod_top)
1898 top_to_mod = FALSE;
1899#endif
1900
1901 /*
1902 * When at start of changed lines: May scroll following lines
1903 * up or down to minimize redrawing.
1904 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001905 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 */
1907 if (lnum == mod_top
1908 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001909 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 {
1911 int old_rows = 0;
1912 int new_rows = 0;
1913 int xtra_rows;
1914 linenr_T l;
1915
1916 /* Count the old number of window rows, using w_lines[], which
1917 * should still contain the sizes for the lines as they are
1918 * currently displayed. */
1919 for (i = idx; i < wp->w_lines_valid; ++i)
1920 {
1921 /* Only valid lines have a meaningful wl_lnum. Invalid
1922 * lines are part of the changed area. */
1923 if (wp->w_lines[i].wl_valid
1924 && wp->w_lines[i].wl_lnum == mod_bot)
1925 break;
1926 old_rows += wp->w_lines[i].wl_size;
1927#ifdef FEAT_FOLDING
1928 if (wp->w_lines[i].wl_valid
1929 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1930 {
1931 /* Must have found the last valid entry above mod_bot.
1932 * Add following invalid entries. */
1933 ++i;
1934 while (i < wp->w_lines_valid
1935 && !wp->w_lines[i].wl_valid)
1936 old_rows += wp->w_lines[i++].wl_size;
1937 break;
1938 }
1939#endif
1940 }
1941
1942 if (i >= wp->w_lines_valid)
1943 {
1944 /* We can't find a valid line below the changed lines,
1945 * need to redraw until the end of the window.
1946 * Inserting/deleting lines has no use. */
1947 bot_start = 0;
1948 }
1949 else
1950 {
1951 /* Able to count old number of rows: Count new window
1952 * rows, and may insert/delete lines */
1953 j = idx;
1954 for (l = lnum; l < mod_bot; ++l)
1955 {
1956#ifdef FEAT_FOLDING
1957 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1958 ++new_rows;
1959 else
1960#endif
1961#ifdef FEAT_DIFF
1962 if (l == wp->w_topline)
1963 new_rows += plines_win_nofill(wp, l, TRUE)
1964 + wp->w_topfill;
1965 else
1966#endif
1967 new_rows += plines_win(wp, l, TRUE);
1968 ++j;
1969 if (new_rows > wp->w_height - row - 2)
1970 {
1971 /* it's getting too much, must redraw the rest */
1972 new_rows = 9999;
1973 break;
1974 }
1975 }
1976 xtra_rows = new_rows - old_rows;
1977 if (xtra_rows < 0)
1978 {
1979 /* May scroll text up. If there is not enough
1980 * remaining text or scrolling fails, must redraw the
1981 * rest. If scrolling works, must redraw the text
1982 * below the scrolled text. */
1983 if (row - xtra_rows >= wp->w_height - 2)
1984 mod_bot = MAXLNUM;
1985 else
1986 {
1987 check_for_delay(FALSE);
1988 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001989 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 mod_bot = MAXLNUM;
1991 else
1992 bot_start = wp->w_height + xtra_rows;
1993 }
1994 }
1995 else if (xtra_rows > 0)
1996 {
1997 /* May scroll text down. If there is not enough
1998 * remaining text of scrolling fails, must redraw the
1999 * rest. */
2000 if (row + xtra_rows >= wp->w_height - 2)
2001 mod_bot = MAXLNUM;
2002 else
2003 {
2004 check_for_delay(FALSE);
2005 if (win_ins_lines(wp, row + old_rows,
2006 xtra_rows, FALSE, FALSE) == FAIL)
2007 mod_bot = MAXLNUM;
2008 else if (top_end > row + old_rows)
2009 /* Scrolled the part at the top that requires
2010 * updating down. */
2011 top_end += xtra_rows;
2012 }
2013 }
2014
2015 /* When not updating the rest, may need to move w_lines[]
2016 * entries. */
2017 if (mod_bot != MAXLNUM && i != j)
2018 {
2019 if (j < i)
2020 {
2021 int x = row + new_rows;
2022
2023 /* move entries in w_lines[] upwards */
2024 for (;;)
2025 {
2026 /* stop at last valid entry in w_lines[] */
2027 if (i >= wp->w_lines_valid)
2028 {
2029 wp->w_lines_valid = j;
2030 break;
2031 }
2032 wp->w_lines[j] = wp->w_lines[i];
2033 /* stop at a line that won't fit */
2034 if (x + (int)wp->w_lines[j].wl_size
2035 > wp->w_height)
2036 {
2037 wp->w_lines_valid = j + 1;
2038 break;
2039 }
2040 x += wp->w_lines[j++].wl_size;
2041 ++i;
2042 }
2043 if (bot_start > x)
2044 bot_start = x;
2045 }
2046 else /* j > i */
2047 {
2048 /* move entries in w_lines[] downwards */
2049 j -= i;
2050 wp->w_lines_valid += j;
2051 if (wp->w_lines_valid > wp->w_height)
2052 wp->w_lines_valid = wp->w_height;
2053 for (i = wp->w_lines_valid; i - j >= idx; --i)
2054 wp->w_lines[i] = wp->w_lines[i - j];
2055
2056 /* The w_lines[] entries for inserted lines are
2057 * now invalid, but wl_size may be used above.
2058 * Reset to zero. */
2059 while (i >= idx)
2060 {
2061 wp->w_lines[i].wl_size = 0;
2062 wp->w_lines[i--].wl_valid = FALSE;
2063 }
2064 }
2065 }
2066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
2068
2069#ifdef FEAT_FOLDING
2070 /*
2071 * When lines are folded, display one line for all of them.
2072 * Otherwise, display normally (can be several display lines when
2073 * 'wrap' is on).
2074 */
2075 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2076 if (fold_count != 0)
2077 {
2078 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2079 ++row;
2080 --fold_count;
2081 wp->w_lines[idx].wl_folded = TRUE;
2082 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2083# ifdef FEAT_SYN_HL
2084 did_update = DID_FOLD;
2085# endif
2086 }
2087 else
2088#endif
2089 if (idx < wp->w_lines_valid
2090 && wp->w_lines[idx].wl_valid
2091 && wp->w_lines[idx].wl_lnum == lnum
2092 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002093 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 && srow + wp->w_lines[idx].wl_size > wp->w_height
2095#ifdef FEAT_DIFF
2096 && diff_check_fill(wp, lnum) == 0
2097#endif
2098 )
2099 {
2100 /* This line is not going to fit. Don't draw anything here,
2101 * will draw "@ " lines below. */
2102 row = wp->w_height + 1;
2103 }
2104 else
2105 {
2106#ifdef FEAT_SEARCH_EXTRA
2107 prepare_search_hl(wp, lnum);
2108#endif
2109#ifdef FEAT_SYN_HL
2110 /* Let the syntax stuff know we skipped a few lines. */
2111 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002112 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 syntax_end_parsing(syntax_last_parsed + 1);
2114#endif
2115
2116 /*
2117 * Display one line.
2118 */
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002119 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
2121#ifdef FEAT_FOLDING
2122 wp->w_lines[idx].wl_folded = FALSE;
2123 wp->w_lines[idx].wl_lastlnum = lnum;
2124#endif
2125#ifdef FEAT_SYN_HL
2126 did_update = DID_LINE;
2127 syntax_last_parsed = lnum;
2128#endif
2129 }
2130
2131 wp->w_lines[idx].wl_lnum = lnum;
2132 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002133
2134 /* Past end of the window or end of the screen. Note that after
2135 * resizing wp->w_height may be end up too big. That's a problem
2136 * elsewhere, but prevent a crash here. */
2137 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
2139 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002140 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2142 ++idx;
2143 break;
2144 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002145 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 wp->w_lines[idx].wl_size = row - srow;
2147 ++idx;
2148#ifdef FEAT_FOLDING
2149 lnum += fold_count + 1;
2150#else
2151 ++lnum;
2152#endif
2153 }
2154 else
2155 {
2156 /* This line does not need updating, advance to the next one */
2157 row += wp->w_lines[idx++].wl_size;
2158 if (row > wp->w_height) /* past end of screen */
2159 break;
2160#ifdef FEAT_FOLDING
2161 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2162#else
2163 ++lnum;
2164#endif
2165#ifdef FEAT_SYN_HL
2166 did_update = DID_NONE;
2167#endif
2168 }
2169
2170 if (lnum > buf->b_ml.ml_line_count)
2171 {
2172 eof = TRUE;
2173 break;
2174 }
2175 }
2176 /*
2177 * End of loop over all window lines.
2178 */
2179
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002180#ifdef FEAT_VTP
2181 /* Rewrite the character at the end of the screen line. */
2182 if (use_vtp())
2183 {
2184 int i;
2185
2186 for (i = 0; i < Rows; ++i)
2187# ifdef FEAT_MBYTE
2188 if (enc_utf8)
2189 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2190 LineOffset[i] + screen_Columns) > 1)
2191 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2192 else
2193 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2194 else
2195# endif
2196 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2197 }
2198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199
2200 if (idx > wp->w_lines_valid)
2201 wp->w_lines_valid = idx;
2202
2203#ifdef FEAT_SYN_HL
2204 /*
2205 * Let the syntax stuff know we stop parsing here.
2206 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002207 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 syntax_end_parsing(syntax_last_parsed + 1);
2209#endif
2210
2211 /*
2212 * If we didn't hit the end of the file, and we didn't finish the last
2213 * line we were working on, then the line didn't fit.
2214 */
2215 wp->w_empty_rows = 0;
2216#ifdef FEAT_DIFF
2217 wp->w_filler_rows = 0;
2218#endif
2219 if (!eof && !didline)
2220 {
2221 if (lnum == wp->w_topline)
2222 {
2223 /*
2224 * Single line that does not fit!
2225 * Don't overwrite it, it can be edited.
2226 */
2227 wp->w_botline = lnum + 1;
2228 }
2229#ifdef FEAT_DIFF
2230 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2231 {
2232 /* Window ends in filler lines. */
2233 wp->w_botline = lnum;
2234 wp->w_filler_rows = wp->w_height - srow;
2235 }
2236#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002237 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2238 {
2239 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2240
2241 /*
2242 * Last line isn't finished: Display "@@@" in the last screen line.
2243 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002244 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002245 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002246 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002247 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002248 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002249 set_empty_rows(wp, srow);
2250 wp->w_botline = lnum;
2251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2253 {
2254 /*
2255 * Last line isn't finished: Display "@@@" at the end.
2256 */
2257 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2258 W_WINROW(wp) + wp->w_height,
2259 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002260 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 set_empty_rows(wp, srow);
2262 wp->w_botline = lnum;
2263 }
2264 else
2265 {
2266 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2267 wp->w_botline = lnum;
2268 }
2269 }
2270 else
2271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (eof) /* we hit the end of the file */
2274 {
2275 wp->w_botline = buf->b_ml.ml_line_count + 1;
2276#ifdef FEAT_DIFF
2277 j = diff_check_fill(wp, wp->w_botline);
2278 if (j > 0 && !wp->w_botfill)
2279 {
2280 /*
2281 * Display filler lines at the end of the file
2282 */
2283 if (char2cells(fill_diff) > 1)
2284 i = '-';
2285 else
2286 i = fill_diff;
2287 if (row + j > wp->w_height)
2288 j = wp->w_height - row;
2289 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2290 row += j;
2291 }
2292#endif
2293 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002294 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 wp->w_botline = lnum;
2296
2297 /* make sure the rest of the screen is blank */
2298 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002299 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002302#ifdef SYN_TIME_LIMIT
2303 syn_set_timeout(NULL);
2304#endif
2305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 /* Reset the type of redrawing required, the window has been updated. */
2307 wp->w_redr_type = 0;
2308#ifdef FEAT_DIFF
2309 wp->w_old_topfill = wp->w_topfill;
2310 wp->w_old_botfill = wp->w_botfill;
2311#endif
2312
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002313 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 /*
2316 * There is a trick with w_botline. If we invalidate it on each
2317 * change that might modify it, this will cause a lot of expensive
2318 * calls to plines() in update_topline() each time. Therefore the
2319 * value of w_botline is often approximated, and this value is used to
2320 * compute the value of w_topline. If the value of w_botline was
2321 * wrong, check that the value of w_topline is correct (cursor is on
2322 * the visible part of the text). If it's not, we need to redraw
2323 * again. Mostly this just means scrolling up a few lines, so it
2324 * doesn't look too bad. Only do this for the current window (where
2325 * changes are relevant).
2326 */
2327 wp->w_valid |= VALID_BOTLINE;
2328 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2329 {
2330 recursive = TRUE;
2331 curwin->w_valid &= ~VALID_TOPLINE;
2332 update_topline(); /* may invalidate w_botline again */
2333 if (must_redraw != 0)
2334 {
2335 /* Don't update for changes in buffer again. */
2336 i = curbuf->b_mod_set;
2337 curbuf->b_mod_set = FALSE;
2338 win_update(curwin);
2339 must_redraw = 0;
2340 curbuf->b_mod_set = i;
2341 }
2342 recursive = FALSE;
2343 }
2344 }
2345
2346#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2347 /* restore got_int, unless CTRL-C was hit while redrawing */
2348 if (!got_int)
2349 got_int = save_got_int;
2350#endif
2351}
2352
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353/*
2354 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2355 * as the filler character.
2356 */
2357 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002358win_draw_end(
2359 win_T *wp,
2360 int c1,
2361 int c2,
2362 int row,
2363 int endrow,
2364 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365{
2366#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2367 int n = 0;
2368# define FDC_OFF n
2369#else
2370# define FDC_OFF 0
2371#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002372#ifdef FEAT_FOLDING
2373 int fdc = compute_foldcolumn(wp, 0);
2374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376#ifdef FEAT_RIGHTLEFT
2377 if (wp->w_p_rl)
2378 {
2379 /* No check for cmdline window: should never be right-left. */
2380# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002381 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382
2383 if (n > 0)
2384 {
2385 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002386 if (n > wp->w_width)
2387 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2389 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002390 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392# endif
2393# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002394 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
2396 int nn = n + 2;
2397
2398 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002399 if (nn > wp->w_width)
2400 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2402 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002403 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 n = nn;
2405 }
2406# endif
2407 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002408 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002409 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2411 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002412 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 }
2414 else
2415#endif
2416 {
2417#ifdef FEAT_CMDWIN
2418 if (cmdwin_type != 0 && wp == curwin)
2419 {
2420 /* draw the cmdline character in the leftmost column */
2421 n = 1;
2422 if (n > wp->w_width)
2423 n = wp->w_width;
2424 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002425 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002426 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 }
2428#endif
2429#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002430 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002432 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
2434 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002435 if (nn > wp->w_width)
2436 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002438 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002439 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 n = nn;
2441 }
2442#endif
2443#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002444 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
2446 int nn = n + 2;
2447
2448 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002449 if (nn > wp->w_width)
2450 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002452 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002453 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 n = nn;
2455 }
2456#endif
2457 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002458 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002459 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461 set_empty_rows(wp, row);
2462}
2463
Bram Moolenaar1a384422010-07-14 19:53:30 +02002464#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002465static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002466
2467/*
2468 * Advance **color_cols and return TRUE when there are columns to draw.
2469 */
2470 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002471advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002472{
2473 while (**color_cols >= 0 && vcol > **color_cols)
2474 ++*color_cols;
2475 return (**color_cols >= 0);
2476}
2477#endif
2478
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002479#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2480/*
2481 * Copy "text" to ScreenLines using "attr".
2482 * Returns the next screen column.
2483 */
2484 static int
2485text_to_screenline(win_T *wp, char_u *text, int col)
2486{
2487 int off = (int)(current_ScreenLine - ScreenLines);
2488
2489#ifdef FEAT_MBYTE
2490 if (has_mbyte)
2491 {
2492 int cells;
2493 int u8c, u8cc[MAX_MCO];
2494 int i;
2495 int idx;
2496 int c_len;
2497 char_u *p;
2498# ifdef FEAT_ARABIC
2499 int prev_c = 0; /* previous Arabic character */
2500 int prev_c1 = 0; /* first composing char for prev_c */
2501# endif
2502
2503# ifdef FEAT_RIGHTLEFT
2504 if (wp->w_p_rl)
2505 idx = off;
2506 else
2507# endif
2508 idx = off + col;
2509
2510 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2511 for (p = text; *p != NUL; )
2512 {
2513 cells = (*mb_ptr2cells)(p);
2514 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002515 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002516# ifdef FEAT_RIGHTLEFT
2517 - (wp->w_p_rl ? col : 0)
2518# endif
2519 )
2520 break;
2521 ScreenLines[idx] = *p;
2522 if (enc_utf8)
2523 {
2524 u8c = utfc_ptr2char(p, u8cc);
2525 if (*p < 0x80 && u8cc[0] == 0)
2526 {
2527 ScreenLinesUC[idx] = 0;
2528#ifdef FEAT_ARABIC
2529 prev_c = u8c;
2530#endif
2531 }
2532 else
2533 {
2534#ifdef FEAT_ARABIC
2535 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2536 {
2537 /* Do Arabic shaping. */
2538 int pc, pc1, nc;
2539 int pcc[MAX_MCO];
2540 int firstbyte = *p;
2541
2542 /* The idea of what is the previous and next
2543 * character depends on 'rightleft'. */
2544 if (wp->w_p_rl)
2545 {
2546 pc = prev_c;
2547 pc1 = prev_c1;
2548 nc = utf_ptr2char(p + c_len);
2549 prev_c1 = u8cc[0];
2550 }
2551 else
2552 {
2553 pc = utfc_ptr2char(p + c_len, pcc);
2554 nc = prev_c;
2555 pc1 = pcc[0];
2556 }
2557 prev_c = u8c;
2558
2559 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2560 pc, pc1, nc);
2561 ScreenLines[idx] = firstbyte;
2562 }
2563 else
2564 prev_c = u8c;
2565#endif
2566 /* Non-BMP character: display as ? or fullwidth ?. */
2567#ifdef UNICODE16
2568 if (u8c >= 0x10000)
2569 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2570 else
2571#endif
2572 ScreenLinesUC[idx] = u8c;
2573 for (i = 0; i < Screen_mco; ++i)
2574 {
2575 ScreenLinesC[i][idx] = u8cc[i];
2576 if (u8cc[i] == 0)
2577 break;
2578 }
2579 }
2580 if (cells > 1)
2581 ScreenLines[idx + 1] = 0;
2582 }
2583 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2584 /* double-byte single width character */
2585 ScreenLines2[idx] = p[1];
2586 else if (cells > 1)
2587 /* double-width character */
2588 ScreenLines[idx + 1] = p[1];
2589 col += cells;
2590 idx += cells;
2591 p += c_len;
2592 }
2593 }
2594 else
2595#endif
2596 {
2597 int len = (int)STRLEN(text);
2598
Bram Moolenaar02631462017-09-22 15:20:32 +02002599 if (len > wp->w_width - col)
2600 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002601 if (len > 0)
2602 {
2603#ifdef FEAT_RIGHTLEFT
2604 if (wp->w_p_rl)
2605 STRNCPY(current_ScreenLine, text, len);
2606 else
2607#endif
2608 STRNCPY(current_ScreenLine + col, text, len);
2609 col += len;
2610 }
2611 }
2612 return col;
2613}
2614#endif
2615
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616#ifdef FEAT_FOLDING
2617/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002618 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2619 * space is available for window "wp", minus "col".
2620 */
2621 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002622compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002623{
2624 int fdc = wp->w_p_fdc;
2625 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002626 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002627
2628 if (fdc > wwidth - (col + wmw))
2629 fdc = wwidth - (col + wmw);
2630 return fdc;
2631}
2632
2633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 * Display one folded line.
2635 */
2636 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002637fold_line(
2638 win_T *wp,
2639 long fold_count,
2640 foldinfo_T *foldinfo,
2641 linenr_T lnum,
2642 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002644 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 pos_T *top, *bot;
2646 linenr_T lnume = lnum + fold_count - 1;
2647 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002648 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 int col;
2651 int txtcol;
2652 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002653 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654
2655 /* Build the fold line:
2656 * 1. Add the cmdwin_type for the command-line window
2657 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002658 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 * 4. Compose the text
2660 * 5. Add the text
2661 * 6. set highlighting for the Visual area an other text
2662 */
2663 col = 0;
2664
2665 /*
2666 * 1. Add the cmdwin_type for the command-line window
2667 * Ignores 'rightleft', this window is never right-left.
2668 */
2669#ifdef FEAT_CMDWIN
2670 if (cmdwin_type != 0 && wp == curwin)
2671 {
2672 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002673 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674#ifdef FEAT_MBYTE
2675 if (enc_utf8)
2676 ScreenLinesUC[off] = 0;
2677#endif
2678 ++col;
2679 }
2680#endif
2681
2682 /*
2683 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002684 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002686 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (fdc > 0)
2688 {
2689 fill_foldcolumn(buf, wp, TRUE, lnum);
2690#ifdef FEAT_RIGHTLEFT
2691 if (wp->w_p_rl)
2692 {
2693 int i;
2694
Bram Moolenaar02631462017-09-22 15:20:32 +02002695 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002696 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 /* reverse the fold column */
2698 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002699 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701 else
2702#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002703 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 col += fdc;
2705 }
2706
2707#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002708# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2709 for (ri = 0; ri < l; ++ri) \
Bram Moolenaar02631462017-09-22 15:20:32 +02002710 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002711 else \
2712 for (ri = 0; ri < l; ++ri) \
2713 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002715# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2716 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717#endif
2718
Bram Moolenaar64486672010-05-16 15:46:46 +02002719 /* Set all attributes of the 'number' or 'relativenumber' column and the
2720 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002721 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
2723#ifdef FEAT_SIGNS
2724 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002725 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002727 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (len > 0)
2729 {
2730 if (len > 2)
2731 len = 2;
2732# ifdef FEAT_RIGHTLEFT
2733 if (wp->w_p_rl)
2734 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002735 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002736 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 else
2738# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002739 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 col += len;
2741 }
2742 }
2743#endif
2744
2745 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002746 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002748 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002750 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (len > 0)
2752 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002753 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002754 long num;
2755 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002756
2757 if (len > w + 1)
2758 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002759
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002760 if (wp->w_p_nu && !wp->w_p_rnu)
2761 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002762 num = (long)lnum;
2763 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002764 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002765 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002766 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002767 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002768 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002769 /* 'number' + 'relativenumber': cursor line shows absolute
2770 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002771 num = lnum;
2772 fmt = "%-*ld ";
2773 }
2774 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002775
Bram Moolenaar700e7342013-01-30 12:31:36 +01002776 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777#ifdef FEAT_RIGHTLEFT
2778 if (wp->w_p_rl)
2779 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002780 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002781 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 else
2783#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002784 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 col += len;
2786 }
2787 }
2788
2789 /*
2790 * 4. Compose the folded-line string with 'foldtext', if set.
2791 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002792 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794 txtcol = col; /* remember where text starts */
2795
2796 /*
2797 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2798 * Right-left text is put in columns 0 - number-col, normal text is put
2799 * in columns number-col - window-width.
2800 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002801 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
2803 /* Fill the rest of the line with the fold filler */
2804#ifdef FEAT_RIGHTLEFT
2805 if (wp->w_p_rl)
2806 col -= txtcol;
2807#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002808 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809#ifdef FEAT_RIGHTLEFT
2810 - (wp->w_p_rl ? txtcol : 0)
2811#endif
2812 )
2813 {
2814#ifdef FEAT_MBYTE
2815 if (enc_utf8)
2816 {
2817 if (fill_fold >= 0x80)
2818 {
2819 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002820 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002821 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 }
2823 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002826 ScreenLines[off + col] = fill_fold;
2827 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002828 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002830 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002832 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 }
2834
2835 if (text != buf)
2836 vim_free(text);
2837
2838 /*
2839 * 6. set highlighting for the Visual area an other text.
2840 * If all folded lines are in the Visual area, highlight the line.
2841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2843 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002844 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 {
2846 /* Visual is after curwin->w_cursor */
2847 top = &curwin->w_cursor;
2848 bot = &VIsual;
2849 }
2850 else
2851 {
2852 /* Visual is before curwin->w_cursor */
2853 top = &VIsual;
2854 bot = &curwin->w_cursor;
2855 }
2856 if (lnum >= top->lnum
2857 && lnume <= bot->lnum
2858 && (VIsual_mode != 'v'
2859 || ((lnum > top->lnum
2860 || (lnum == top->lnum
2861 && top->col == 0))
2862 && (lnume < bot->lnum
2863 || (lnume == bot->lnum
2864 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002865 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 {
2867 if (VIsual_mode == Ctrl_V)
2868 {
2869 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002870 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002872 if (wp->w_old_cursor_lcol != MAXCOL
2873 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002874 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 len = wp->w_old_cursor_lcol;
2876 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002877 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002878 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002879 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 }
2881 }
2882 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002883 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002885 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
2888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002890#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002891 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2892 if (wp->w_p_cc_cols)
2893 {
2894 int i = 0;
2895 int j = wp->w_p_cc_cols[i];
2896 int old_txtcol = txtcol;
2897
2898 while (j > -1)
2899 {
2900 txtcol += j;
2901 if (wp->w_p_wrap)
2902 txtcol -= wp->w_skipcol;
2903 else
2904 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002905 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002906 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002907 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002908 txtcol = old_txtcol;
2909 j = wp->w_p_cc_cols[++i];
2910 }
2911 }
2912
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002913 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002914 if (wp->w_p_cuc)
2915 {
2916 txtcol += wp->w_virtcol;
2917 if (wp->w_p_wrap)
2918 txtcol -= wp->w_skipcol;
2919 else
2920 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002921 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002922 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002923 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002924 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
Bram Moolenaar02631462017-09-22 15:20:32 +02002927 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2928 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
2930 /*
2931 * Update w_cline_height and w_cline_folded if the cursor line was
2932 * updated (saves a call to plines() later).
2933 */
2934 if (wp == curwin
2935 && lnum <= curwin->w_cursor.lnum
2936 && lnume >= curwin->w_cursor.lnum)
2937 {
2938 curwin->w_cline_row = row;
2939 curwin->w_cline_height = 1;
2940 curwin->w_cline_folded = TRUE;
2941 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2942 }
2943}
2944
2945/*
2946 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2947 */
2948 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002949copy_text_attr(
2950 int off,
2951 char_u *buf,
2952 int len,
2953 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002955 int i;
2956
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 mch_memmove(ScreenLines + off, buf, (size_t)len);
2958# ifdef FEAT_MBYTE
2959 if (enc_utf8)
2960 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2961# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002962 for (i = 0; i < len; ++i)
2963 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964}
2965
2966/*
2967 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002968 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 */
2970 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002971fill_foldcolumn(
2972 char_u *p,
2973 win_T *wp,
2974 int closed, /* TRUE of FALSE */
2975 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
2977 int i = 0;
2978 int level;
2979 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002980 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002981 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
2983 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002984 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986 level = win_foldinfo.fi_level;
2987 if (level > 0)
2988 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002989 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002990 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 /* If the column is too narrow, we start at the lowest level that
2993 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002994 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 if (first_level < 1)
2996 first_level = 1;
2997
Bram Moolenaar1c934292015-01-27 16:39:29 +01002998 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 {
3000 if (win_foldinfo.fi_lnum == lnum
3001 && first_level + i >= win_foldinfo.fi_low_level)
3002 p[i] = '-';
3003 else if (first_level == 1)
3004 p[i] = '|';
3005 else if (first_level + i <= 9)
3006 p[i] = '0' + first_level + i;
3007 else
3008 p[i] = '>';
3009 if (first_level + i == level)
3010 break;
3011 }
3012 }
3013 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003014 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015}
3016#endif /* FEAT_FOLDING */
3017
3018/*
3019 * Display line "lnum" of window 'wp' on the screen.
3020 * Start at row "startrow", stop when "endrow" is reached.
3021 * wp->w_virtcol needs to be valid.
3022 *
3023 * Return the number of last row the line occupies.
3024 */
3025 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003026win_line(
3027 win_T *wp,
3028 linenr_T lnum,
3029 int startrow,
3030 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003031 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003033 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3035 int c = 0; /* init for GCC */
3036 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003037#ifdef FEAT_LINEBREAK
3038 long vcol_sbr = -1; /* virtual column after showbreak */
3039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 long vcol_prev = -1; /* "vcol" of previous character */
3041 char_u *line; /* current line */
3042 char_u *ptr; /* current position in "line" */
3043 int row; /* row in the window, excl w_winrow */
3044 int screen_row; /* row on the screen, incl w_winrow */
3045
3046 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3047 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003048 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003049 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 int c_extra = NUL; /* extra chars, all the same */
3051 int extra_attr = 0; /* attributes when n_extra != 0 */
3052 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3053 displaying lcs_eol at end-of-line */
3054 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3055 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3056
3057 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3058 int saved_n_extra = 0;
3059 char_u *saved_p_extra = NULL;
3060 int saved_c_extra = 0;
3061 int saved_char_attr = 0;
3062
3063 int n_attr = 0; /* chars with special attr */
3064 int saved_attr2 = 0; /* char_attr saved for n_attr */
3065 int n_attr3 = 0; /* chars with overruling special attr */
3066 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3067
3068 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3069
3070 int fromcol, tocol; /* start/end of inverting */
3071 int fromcol_prev = -2; /* start of inverting after cursor */
3072 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003074 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 pos_T pos;
3076 long v;
3077
3078 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003079 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3081 in this line */
3082 int attr = 0; /* attributes for area highlighting */
3083 int area_attr = 0; /* attributes desired by highlighting */
3084 int search_attr = 0; /* attributes desired by 'hlsearch' */
3085#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003086 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 int syntax_attr = 0; /* attributes desired by syntax */
3088 int has_syntax = FALSE; /* this buffer has syntax highl. */
3089 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003090 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003091 int draw_color_col = FALSE; /* highlight colorcolumn */
3092 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003093#endif
3094#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003095 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003096# define SPWORDLEN 150
3097 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003098 int nextlinecol = 0; /* column where nextline[] starts */
3099 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003100 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003101 int spell_attr = 0; /* attributes desired by spelling */
3102 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003103 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3104 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003105 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003106 static int cap_col = -1; /* column to check for Cap word */
3107 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003108 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109#endif
3110 int extra_check; /* has syntax or linebreak */
3111#ifdef FEAT_MBYTE
3112 int multi_attr = 0; /* attributes desired by multibyte */
3113 int mb_l = 1; /* multi-byte byte length */
3114 int mb_c = 0; /* decoded multi-byte character */
3115 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003116 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117#endif
3118#ifdef FEAT_DIFF
3119 int filler_lines; /* nr of filler lines to be drawn */
3120 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003121 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 int change_start = MAXCOL; /* first col of changed area */
3123 int change_end = -1; /* last col of changed area */
3124#endif
3125 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3126#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003127 int need_showbreak = FALSE; /* overlong line, skipping first x
3128 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003130#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003131 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003133 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134#endif
3135#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003136 matchitem_T *cur; /* points to the match list */
3137 match_T *shl; /* points to search_hl or a match */
3138 int shl_flag; /* flag to indicate whether search_hl
3139 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003140 int pos_inprogress; /* marks that position match search is
3141 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003142 int prevcol_hl_flag; /* flag to indicate whether prevcol
3143 equals startcol of search_hl or one
3144 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145#endif
3146#ifdef FEAT_ARABIC
3147 int prev_c = 0; /* previous Arabic character */
3148 int prev_c1 = 0; /* first composing char for prev_c */
3149#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003150#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003151 int did_line_attr = 0;
3152#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003153#ifdef FEAT_TERMINAL
3154 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003155 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158 /* draw_state: items that are drawn in sequence: */
3159#define WL_START 0 /* nothing done yet */
3160#ifdef FEAT_CMDWIN
3161# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3162#else
3163# define WL_CMDLINE WL_START
3164#endif
3165#ifdef FEAT_FOLDING
3166# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3167#else
3168# define WL_FOLD WL_CMDLINE
3169#endif
3170#ifdef FEAT_SIGNS
3171# define WL_SIGN WL_FOLD + 1 /* column for signs */
3172#else
3173# define WL_SIGN WL_FOLD /* column for signs */
3174#endif
3175#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003176#ifdef FEAT_LINEBREAK
3177# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003179# define WL_BRI WL_NR
3180#endif
3181#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3182# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3183#else
3184# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185#endif
3186#define WL_LINE WL_SBR + 1 /* text in the line */
3187 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003188#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 int feedback_col = 0;
3190 int feedback_old_attr = -1;
3191#endif
3192
Bram Moolenaar860cae12010-06-05 23:22:07 +02003193#ifdef FEAT_CONCEAL
3194 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003195 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003196 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003197 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003198 int is_concealing = FALSE;
3199 int boguscols = 0; /* nonexistent columns added to force
3200 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003201 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003202 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003203 int match_conc = 0; /* cchar for match functions */
3204 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003205 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003206# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003207# define FIX_FOR_BOGUSCOLS \
3208 { \
3209 n_extra += vcol_off; \
3210 vcol -= vcol_off; \
3211 vcol_off = 0; \
3212 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003213 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003214 boguscols = 0; \
3215 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003216#else
3217# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
3220 if (startrow > endrow) /* past the end already! */
3221 return startrow;
3222
3223 row = startrow;
3224 screen_row = row + W_WINROW(wp);
3225
3226 /*
3227 * To speed up the loop below, set extra_check when there is linebreak,
3228 * trailing white space and/or syntax processing to be done.
3229 */
3230#ifdef FEAT_LINEBREAK
3231 extra_check = wp->w_p_lbr;
3232#else
3233 extra_check = 0;
3234#endif
3235#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003236 if (syntax_present(wp) && !wp->w_s->b_syn_error
3237# ifdef SYN_TIME_LIMIT
3238 && !wp->w_s->b_syn_slow
3239# endif
3240 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 {
3242 /* Prepare for syntax highlighting in this line. When there is an
3243 * error, stop syntax highlighting. */
3244 save_did_emsg = did_emsg;
3245 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003246 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003248 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 else
3250 {
3251 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003252#ifdef SYN_TIME_LIMIT
3253 if (!wp->w_s->b_syn_slow)
3254#endif
3255 {
3256 has_syntax = TRUE;
3257 extra_check = TRUE;
3258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 }
3260 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003261
3262 /* Check for columns to display for 'colorcolumn'. */
3263 color_cols = wp->w_p_cc_cols;
3264 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003265 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003266#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003267
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003268#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003269 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003270 {
3271 extra_check = TRUE;
3272 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003273 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003274 }
3275#endif
3276
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003277#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003278 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003279 && *wp->w_s->b_p_spl != NUL
3280 && wp->w_s->b_langp.ga_len > 0
3281 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003282 {
3283 /* Prepare for spell checking. */
3284 has_spell = TRUE;
3285 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003286
3287 /* Get the start of the next line, so that words that wrap to the next
3288 * line are found too: "et<line-break>al.".
3289 * Trick: skip a few chars for C/shell/Vim comments */
3290 nextline[SPWORDLEN] = NUL;
3291 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3292 {
3293 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3294 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3295 }
3296
3297 /* When a word wrapped from the previous line the start of the current
3298 * line is valid. */
3299 if (lnum == checked_lnum)
3300 cur_checked_col = checked_col;
3301 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003302
3303 /* When there was a sentence end in the previous line may require a
3304 * word starting with capital in this line. In line 1 always check
3305 * the first word. */
3306 if (lnum != capcol_lnum)
3307 cap_col = -1;
3308 if (lnum == 1)
3309 cap_col = 0;
3310 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#endif
3313
3314 /*
3315 * handle visual active in this window
3316 */
3317 fromcol = -10;
3318 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3320 {
3321 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003322 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
3324 top = &curwin->w_cursor;
3325 bot = &VIsual;
3326 }
3327 else /* Visual is before curwin->w_cursor */
3328 {
3329 top = &VIsual;
3330 bot = &curwin->w_cursor;
3331 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003332 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 if (VIsual_mode == Ctrl_V) /* block mode */
3334 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003335 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 {
3337 fromcol = wp->w_old_cursor_fcol;
3338 tocol = wp->w_old_cursor_lcol;
3339 }
3340 }
3341 else /* non-block mode */
3342 {
3343 if (lnum > top->lnum && lnum <= bot->lnum)
3344 fromcol = 0;
3345 else if (lnum == top->lnum)
3346 {
3347 if (VIsual_mode == 'V') /* linewise */
3348 fromcol = 0;
3349 else
3350 {
3351 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3352 if (gchar_pos(top) == NUL)
3353 tocol = fromcol + 1;
3354 }
3355 }
3356 if (VIsual_mode != 'V' && lnum == bot->lnum)
3357 {
3358 if (*p_sel == 'e' && bot->col == 0
3359#ifdef FEAT_VIRTUALEDIT
3360 && bot->coladd == 0
3361#endif
3362 )
3363 {
3364 fromcol = -10;
3365 tocol = MAXCOL;
3366 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003367 else if (bot->col == MAXCOL)
3368 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 else
3370 {
3371 pos = *bot;
3372 if (*p_sel == 'e')
3373 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3374 else
3375 {
3376 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3377 ++tocol;
3378 }
3379 }
3380 }
3381 }
3382
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 /* Check if the character under the cursor should not be inverted */
3384 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003385#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 )
3389 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
3391 /* if inverting in this line set area_highlighting */
3392 if (fromcol >= 0)
3393 {
3394 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003395 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003397 if ((clip_star.available && !clip_star.owned
3398 && clip_isautosel_star())
3399 || (clip_plus.available && !clip_plus.owned
3400 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003401 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402#endif
3403 }
3404 }
3405
3406 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003407 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003409 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 && wp == curwin
3411 && lnum >= curwin->w_cursor.lnum
3412 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3413 {
3414 if (lnum == curwin->w_cursor.lnum)
3415 getvcol(curwin, &(curwin->w_cursor),
3416 (colnr_T *)&fromcol, NULL, NULL);
3417 else
3418 fromcol = 0;
3419 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3420 {
3421 pos.lnum = lnum;
3422 pos.col = search_match_endcol;
3423 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3424 }
3425 else
3426 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003427 /* do at least one character; happens when past end of line */
3428 if (fromcol == tocol)
3429 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003431 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
3433
3434#ifdef FEAT_DIFF
3435 filler_lines = diff_check(wp, lnum);
3436 if (filler_lines < 0)
3437 {
3438 if (filler_lines == -1)
3439 {
3440 if (diff_find_change(wp, lnum, &change_start, &change_end))
3441 diff_hlf = HLF_ADD; /* added line */
3442 else if (change_start == 0)
3443 diff_hlf = HLF_TXD; /* changed text */
3444 else
3445 diff_hlf = HLF_CHD; /* changed line */
3446 }
3447 else
3448 diff_hlf = HLF_ADD; /* added line */
3449 filler_lines = 0;
3450 area_highlighting = TRUE;
3451 }
3452 if (lnum == wp->w_topline)
3453 filler_lines = wp->w_topfill;
3454 filler_todo = filler_lines;
3455#endif
3456
3457#ifdef LINE_ATTR
3458# ifdef FEAT_SIGNS
3459 /* If this line has a sign with line highlighting set line_attr. */
3460 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3461 if (v != 0)
3462 line_attr = sign_get_attr((int)v, TRUE);
3463# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003464# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003466 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003467 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468# endif
3469 if (line_attr != 0)
3470 area_highlighting = TRUE;
3471#endif
3472
3473 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3474 ptr = line;
3475
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003476#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003477 if (has_spell)
3478 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003479 /* For checking first word with a capital skip white space. */
3480 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003481 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003482
Bram Moolenaar30abd282005-06-22 22:35:10 +00003483 /* To be able to spell-check over line boundaries copy the end of the
3484 * current line into nextline[]. Above the start of the next line was
3485 * copied to nextline[SPWORDLEN]. */
3486 if (nextline[SPWORDLEN] == NUL)
3487 {
3488 /* No next line or it is empty. */
3489 nextlinecol = MAXCOL;
3490 nextline_idx = 0;
3491 }
3492 else
3493 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003494 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003495 if (v < SPWORDLEN)
3496 {
3497 /* Short line, use it completely and append the start of the
3498 * next line. */
3499 nextlinecol = 0;
3500 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003501 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003502 nextline_idx = v + 1;
3503 }
3504 else
3505 {
3506 /* Long line, use only the last SPWORDLEN bytes. */
3507 nextlinecol = v - SPWORDLEN;
3508 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3509 nextline_idx = SPWORDLEN + 1;
3510 }
3511 }
3512 }
3513#endif
3514
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003515 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003517 if (lcs_space || lcs_trail)
3518 extra_check = TRUE;
3519 /* find start of trailing whitespace */
3520 if (lcs_trail)
3521 {
3522 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003523 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003524 --trailcol;
3525 trailcol += (colnr_T) (ptr - line);
3526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528
3529 /*
3530 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3531 * first character to be displayed.
3532 */
3533 if (wp->w_p_wrap)
3534 v = wp->w_skipcol;
3535 else
3536 v = wp->w_leftcol;
3537 if (v > 0)
3538 {
3539#ifdef FEAT_MBYTE
3540 char_u *prev_ptr = ptr;
3541#endif
3542 while (vcol < v && *ptr != NUL)
3543 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003544 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 vcol += c;
3546#ifdef FEAT_MBYTE
3547 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003549 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
3551
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003552 /* When:
3553 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003554 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003555 * - 'virtualedit' is set, or
3556 * - the visual mode is active,
3557 * the end of the line may be before the start of the displayed part.
3558 */
3559 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003560#ifdef FEAT_SYN_HL
3561 wp->w_p_cuc || draw_color_col ||
3562#endif
3563#ifdef FEAT_VIRTUALEDIT
3564 virtual_active() ||
3565#endif
3566 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
3571 /* Handle a character that's not completely on the screen: Put ptr at
3572 * that character but skip the first few screen characters. */
3573 if (vcol > v)
3574 {
3575 vcol -= c;
3576#ifdef FEAT_MBYTE
3577 ptr = prev_ptr;
3578#else
3579 --ptr;
3580#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003581 /* If the character fits on the screen, don't need to skip it.
3582 * Except for a TAB. */
3583 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003584#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003585 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003586#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003587 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003588 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590
3591 /*
3592 * Adjust for when the inverted text is before the screen,
3593 * and when the start of the inverted text is before the screen.
3594 */
3595 if (tocol <= vcol)
3596 fromcol = 0;
3597 else if (fromcol >= 0 && fromcol < vcol)
3598 fromcol = vcol;
3599
3600#ifdef FEAT_LINEBREAK
3601 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3602 if (wp->w_p_wrap)
3603 need_showbreak = TRUE;
3604#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003605#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003606 /* When spell checking a word we need to figure out the start of the
3607 * word and if it's badly spelled or not. */
3608 if (has_spell)
3609 {
3610 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003611 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003612 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003613
3614 pos = wp->w_cursor;
3615 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003616 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003617 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003618
3619 /* spell_move_to() may call ml_get() and make "line" invalid */
3620 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3621 ptr = line + linecol;
3622
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003623 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003624 {
3625 /* no bad word found at line start, don't check until end of a
3626 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003627 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003628 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003629 }
3630 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003631 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003632 /* bad word found, use attributes until end of word */
3633 word_end = wp->w_cursor.col + len + 1;
3634
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003635 /* Turn index into actual attributes. */
3636 if (spell_hlf != HLF_COUNT)
3637 spell_attr = highlight_attr[spell_hlf];
3638 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003639 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003640
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003641# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003642 /* Need to restart syntax highlighting for this line. */
3643 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003644 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003645# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003646 }
3647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 }
3649
3650 /*
3651 * Correct highlighting for cursor that can't be disabled.
3652 * Avoids having to check this for each character.
3653 */
3654 if (fromcol >= 0)
3655 {
3656 if (noinvcur)
3657 {
3658 if ((colnr_T)fromcol == wp->w_virtcol)
3659 {
3660 /* highlighting starts at cursor, let it start just after the
3661 * cursor */
3662 fromcol_prev = fromcol;
3663 fromcol = -1;
3664 }
3665 else if ((colnr_T)fromcol < wp->w_virtcol)
3666 /* restart highlighting after the cursor */
3667 fromcol_prev = wp->w_virtcol;
3668 }
3669 if (fromcol >= tocol)
3670 fromcol = -1;
3671 }
3672
3673#ifdef FEAT_SEARCH_EXTRA
3674 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003675 * Handle highlighting the last used search pattern and matches.
3676 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003678 cur = wp->w_match_head;
3679 shl_flag = FALSE;
3680 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003682 if (shl_flag == FALSE)
3683 {
3684 shl = &search_hl;
3685 shl_flag = TRUE;
3686 }
3687 else
3688 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003689 shl->startcol = MAXCOL;
3690 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003692 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003693 v = (long)(ptr - line);
3694 if (cur != NULL)
3695 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003696 next_search_hl(wp, shl, lnum, (colnr_T)v,
3697 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003698
3699 /* Need to get the line again, a multi-line regexp may have made it
3700 * invalid. */
3701 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3702 ptr = line + v;
3703
3704 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003706 if (shl->lnum == lnum)
3707 shl->startcol = shl->rm.startpos[0].col;
3708 else
3709 shl->startcol = 0;
3710 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3711 - shl->rm.startpos[0].lnum)
3712 shl->endcol = shl->rm.endpos[0].col;
3713 else
3714 shl->endcol = MAXCOL;
3715 /* Highlight one character for an empty match. */
3716 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003719 if (has_mbyte && line[shl->endcol] != NUL)
3720 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3721 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003723 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003725 if ((long)shl->startcol < v) /* match at leftcol */
3726 {
3727 shl->attr_cur = shl->attr;
3728 search_attr = shl->attr;
3729 }
3730 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003732 if (shl != &search_hl && cur != NULL)
3733 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 }
3735#endif
3736
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003737#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003738 /* Cursor line highlighting for 'cursorline' in the current window. Not
3739 * when Visual mode is active, because it's not clear what is selected
3740 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003741 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003742 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003743 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003744 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003745 area_highlighting = TRUE;
3746 }
3747#endif
3748
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003749 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 col = 0;
3751#ifdef FEAT_RIGHTLEFT
3752 if (wp->w_p_rl)
3753 {
3754 /* Rightleft window: process the text in the normal direction, but put
3755 * it in current_ScreenLine[] from right to left. Start at the
3756 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003757 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 off += col;
3759 }
3760#endif
3761
3762 /*
3763 * Repeat for the whole displayed line.
3764 */
3765 for (;;)
3766 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003767#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003768 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 /* Skip this quickly when working on the text. */
3771 if (draw_state != WL_LINE)
3772 {
3773#ifdef FEAT_CMDWIN
3774 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3775 {
3776 draw_state = WL_CMDLINE;
3777 if (cmdwin_type != 0 && wp == curwin)
3778 {
3779 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003781 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003782 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784 }
3785#endif
3786
3787#ifdef FEAT_FOLDING
3788 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3789 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003790 int fdc = compute_foldcolumn(wp, 0);
3791
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003793 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003795 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003796 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003797 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003798 p_extra_free = alloc(12 + 1);
3799
3800 if (p_extra_free != NULL)
3801 {
3802 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3803 n_extra = fdc;
3804 p_extra_free[n_extra] = NUL;
3805 p_extra = p_extra_free;
3806 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003807 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810 }
3811#endif
3812
3813#ifdef FEAT_SIGNS
3814 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3815 {
3816 draw_state = WL_SIGN;
3817 /* Show the sign column when there are any signs in this
3818 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003819 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003821 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003823 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824# endif
3825
3826 /* Draw two cells with the sign value or blank. */
3827 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003828 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 n_extra = 2;
3830
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003831 if (row == startrow
3832#ifdef FEAT_DIFF
3833 + filler_lines && filler_todo <= 0
3834#endif
3835 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 {
3837 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3838 SIGN_TEXT);
3839# ifdef FEAT_SIGN_ICONS
3840 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3841 SIGN_ICON);
3842 if (gui.in_use && icon_sign != 0)
3843 {
3844 /* Use the image in this position. */
3845 c_extra = SIGN_BYTE;
3846# ifdef FEAT_NETBEANS_INTG
3847 if (buf_signcount(wp->w_buffer, lnum) > 1)
3848 c_extra = MULTISIGN_BYTE;
3849# endif
3850 char_attr = icon_sign;
3851 }
3852 else
3853# endif
3854 if (text_sign != 0)
3855 {
3856 p_extra = sign_get_text(text_sign);
3857 if (p_extra != NULL)
3858 {
3859 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003860 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 }
3862 char_attr = sign_get_attr(text_sign, FALSE);
3863 }
3864 }
3865 }
3866 }
3867#endif
3868
3869 if (draw_state == WL_NR - 1 && n_extra == 0)
3870 {
3871 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003872 /* Display the absolute or relative line number. After the
3873 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3874 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 && (row == startrow
3876#ifdef FEAT_DIFF
3877 + filler_lines
3878#endif
3879 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3880 {
3881 /* Draw the line number (empty space after wrapping). */
3882 if (row == startrow
3883#ifdef FEAT_DIFF
3884 + filler_lines
3885#endif
3886 )
3887 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003888 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003889 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003890
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003891 if (wp->w_p_nu && !wp->w_p_rnu)
3892 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003893 num = (long)lnum;
3894 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003895 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003896 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003897 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003898 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003899 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003900 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003901 num = lnum;
3902 fmt = "%-*ld ";
3903 }
3904 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003905
Bram Moolenaar700e7342013-01-30 12:31:36 +01003906 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003907 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 if (wp->w_skipcol > 0)
3909 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3910 *p_extra = '-';
3911#ifdef FEAT_RIGHTLEFT
3912 if (wp->w_p_rl) /* reverse line numbers */
3913 rl_mirror(extra);
3914#endif
3915 p_extra = extra;
3916 c_extra = NUL;
3917 }
3918 else
3919 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003920 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003921 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003922#ifdef FEAT_SYN_HL
3923 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003924 * the current line differently.
3925 * TODO: Can we use CursorLine instead of CursorLineNr
3926 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003927 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003928 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003929 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932 }
3933
Bram Moolenaar597a4222014-06-25 14:39:50 +02003934#ifdef FEAT_LINEBREAK
3935 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3936 && n_extra == 0 && *p_sbr != NUL)
3937 /* draw indent after showbreak value */
3938 draw_state = WL_BRI;
3939 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3940 /* After the showbreak, draw the breakindent */
3941 draw_state = WL_BRI - 1;
3942
3943 /* draw 'breakindent': indent wrapped text accordingly */
3944 if (draw_state == WL_BRI - 1 && n_extra == 0)
3945 {
3946 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003947 /* if need_showbreak is set, breakindent also applies */
3948 if (wp->w_p_bri && n_extra == 0
3949 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003950# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003951 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003952# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003953 )
3954 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003955 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003956# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003957 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003958 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003959 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003960# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003961 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3962 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003963 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003964# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003965 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003966# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003967 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003968 c_extra = ' ';
3969 n_extra = get_breakindent_win(wp,
3970 ml_get_buf(wp->w_buffer, lnum, FALSE));
3971 /* Correct end of highlighted area for 'breakindent',
3972 * required when 'linebreak' is also set. */
3973 if (tocol == vcol)
3974 tocol += n_extra;
3975 }
3976 }
3977#endif
3978
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3980 if (draw_state == WL_SBR - 1 && n_extra == 0)
3981 {
3982 draw_state = WL_SBR;
3983# ifdef FEAT_DIFF
3984 if (filler_todo > 0)
3985 {
3986 /* Draw "deleted" diff line(s). */
3987 if (char2cells(fill_diff) > 1)
3988 c_extra = '-';
3989 else
3990 c_extra = fill_diff;
3991# ifdef FEAT_RIGHTLEFT
3992 if (wp->w_p_rl)
3993 n_extra = col + 1;
3994 else
3995# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02003996 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003997 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999# endif
4000# ifdef FEAT_LINEBREAK
4001 if (*p_sbr != NUL && need_showbreak)
4002 {
4003 /* Draw 'showbreak' at the start of each broken line. */
4004 p_extra = p_sbr;
4005 c_extra = NUL;
4006 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004007 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004009 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 /* Correct end of highlighted area for 'showbreak',
4011 * required when 'linebreak' is also set. */
4012 if (tocol == vcol)
4013 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004014#ifdef FEAT_SYN_HL
4015 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004016 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004017 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004018 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021# endif
4022 }
4023#endif
4024
4025 if (draw_state == WL_LINE - 1 && n_extra == 0)
4026 {
4027 draw_state = WL_LINE;
4028 if (saved_n_extra)
4029 {
4030 /* Continue item from end of wrapped line. */
4031 n_extra = saved_n_extra;
4032 c_extra = saved_c_extra;
4033 p_extra = saved_p_extra;
4034 char_attr = saved_char_attr;
4035 }
4036 else
4037 char_attr = 0;
4038 }
4039 }
4040
4041 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004042 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004043 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044#ifdef FEAT_DIFF
4045 && filler_todo <= 0
4046#endif
4047 )
4048 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004049 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004050 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004051 /* Pretend we have finished updating the window. Except when
4052 * 'cursorcolumn' is set. */
4053#ifdef FEAT_SYN_HL
4054 if (wp->w_p_cuc)
4055 row = wp->w_cline_row + wp->w_cline_height;
4056 else
4057#endif
4058 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 break;
4060 }
4061
4062 if (draw_state == WL_LINE && area_highlighting)
4063 {
4064 /* handle Visual or match highlighting in this line */
4065 if (vcol == fromcol
4066#ifdef FEAT_MBYTE
4067 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4068 && (*mb_ptr2cells)(ptr) > 1)
4069#endif
4070 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004071 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 && vcol < tocol))
4073 area_attr = attr; /* start highlighting */
4074 else if (area_attr != 0
4075 && (vcol == tocol
4076 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079#ifdef FEAT_SEARCH_EXTRA
4080 if (!n_extra)
4081 {
4082 /*
4083 * Check for start/end of search pattern match.
4084 * After end, check for start/end of next match.
4085 * When another match, have to check for start again.
4086 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004087 * Do this for 'search_hl' and the match list (ordered by
4088 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004090 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004091 cur = wp->w_match_head;
4092 shl_flag = FALSE;
4093 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004095 if (shl_flag == FALSE
4096 && ((cur != NULL
4097 && cur->priority > SEARCH_HL_PRIORITY)
4098 || cur == NULL))
4099 {
4100 shl = &search_hl;
4101 shl_flag = TRUE;
4102 }
4103 else
4104 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004105 if (cur != NULL)
4106 cur->pos.cur = 0;
4107 pos_inprogress = TRUE;
4108 while (shl->rm.regprog != NULL
4109 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004111 if (shl->startcol != MAXCOL
4112 && v >= (long)shl->startcol
4113 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004115#ifdef FEAT_MBYTE
4116 int tmp_col = v + MB_PTR2LEN(ptr);
4117
4118 if (shl->endcol < tmp_col)
4119 shl->endcol = tmp_col;
4120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004122#ifdef FEAT_CONCEAL
4123 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4124 == cur->hlg_id)
4125 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004126 has_match_conc =
4127 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004128 match_conc = cur->conceal_char;
4129 }
4130 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004131 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004134 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 {
4136 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004137 next_search_hl(wp, shl, lnum, (colnr_T)v,
4138 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004139 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004140 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
4142 /* Need to get the line again, a multi-line regexp
4143 * may have made it invalid. */
4144 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4145 ptr = line + v;
4146
4147 if (shl->lnum == lnum)
4148 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004149 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004151 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004153 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004155 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
4157 /* highlight empty match, try again after
4158 * it */
4159#ifdef FEAT_MBYTE
4160 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004161 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004162 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 else
4164#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004165 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167
4168 /* Loop to check if the match starts at the
4169 * current position */
4170 continue;
4171 }
4172 }
4173 break;
4174 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004175 if (shl != &search_hl && cur != NULL)
4176 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004178
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004179 /* Use attributes from match with highest priority among
4180 * 'search_hl' and the match list. */
4181 search_attr = search_hl.attr_cur;
4182 cur = wp->w_match_head;
4183 shl_flag = FALSE;
4184 while (cur != NULL || shl_flag == FALSE)
4185 {
4186 if (shl_flag == FALSE
4187 && ((cur != NULL
4188 && cur->priority > SEARCH_HL_PRIORITY)
4189 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004190 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004191 shl = &search_hl;
4192 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004193 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004194 else
4195 shl = &cur->hl;
4196 if (shl->attr_cur != 0)
4197 search_attr = shl->attr_cur;
4198 if (shl != &search_hl && cur != NULL)
4199 cur = cur->next;
4200 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004201 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004202 if (*ptr == NUL && (did_line_attr >= 1
4203 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004204 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206#endif
4207
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004209 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004211 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4212 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004214 if (diff_hlf == HLF_TXD && ptr - line > change_end
4215 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004217 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004218 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004219 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004222
4223 /* Decide which of the highlight attributes to use. */
4224 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004225#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004226 if (area_attr != 0)
4227 char_attr = hl_combine_attr(line_attr, area_attr);
4228 else if (search_attr != 0)
4229 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004230 /* Use line_attr when not in the Visual or 'incsearch' area
4231 * (area_attr may be 0 when "noinvcur" is set). */
4232 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004233 || vcol < fromcol || vcol_prev < fromcol_prev
4234 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004235 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004236#else
4237 if (area_attr != 0)
4238 char_attr = area_attr;
4239 else if (search_attr != 0)
4240 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004241#endif
4242 else
4243 {
4244 attr_pri = FALSE;
4245#ifdef FEAT_SYN_HL
4246 if (has_syntax)
4247 char_attr = syntax_attr;
4248 else
4249#endif
4250 char_attr = 0;
4251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
4253
4254 /*
4255 * Get the next character to put on the screen.
4256 */
4257 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004258 * The "p_extra" points to the extra stuff that is inserted to
4259 * represent special characters (non-printable stuff) and other
4260 * things. When all characters are the same, c_extra is used.
4261 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4262 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4264 */
4265 if (n_extra > 0)
4266 {
4267 if (c_extra != NUL)
4268 {
4269 c = c_extra;
4270#ifdef FEAT_MBYTE
4271 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004272 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 {
4274 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004275 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004276 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278 else
4279 mb_utf8 = FALSE;
4280#endif
4281 }
4282 else
4283 {
4284 c = *p_extra;
4285#ifdef FEAT_MBYTE
4286 if (has_mbyte)
4287 {
4288 mb_c = c;
4289 if (enc_utf8)
4290 {
4291 /* If the UTF-8 character is more than one byte:
4292 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004293 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 mb_utf8 = FALSE;
4295 if (mb_l > n_extra)
4296 mb_l = 1;
4297 else if (mb_l > 1)
4298 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004299 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004301 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 }
4304 else
4305 {
4306 /* if this is a DBCS character, put it in "mb_c" */
4307 mb_l = MB_BYTE2LEN(c);
4308 if (mb_l >= n_extra)
4309 mb_l = 1;
4310 else if (mb_l > 1)
4311 mb_c = (c << 8) + p_extra[1];
4312 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004313 if (mb_l == 0) /* at the NUL at end-of-line */
4314 mb_l = 1;
4315
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 /* If a double-width char doesn't fit display a '>' in the
4317 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004318 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319# ifdef FEAT_RIGHTLEFT
4320 wp->w_p_rl ? (col <= 0) :
4321# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004322 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 && (*mb_char2cells)(mb_c) == 2)
4324 {
4325 c = '>';
4326 mb_c = c;
4327 mb_l = 1;
4328 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004329 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 /* put the pointer back to output the double-width
4331 * character at the start of the next line. */
4332 ++n_extra;
4333 --p_extra;
4334 }
4335 else
4336 {
4337 n_extra -= mb_l - 1;
4338 p_extra += mb_l - 1;
4339 }
4340 }
4341#endif
4342 ++p_extra;
4343 }
4344 --n_extra;
4345 }
4346 else
4347 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004348#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004349 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004350#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004351
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004352 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004353 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 /*
4355 * Get a character from the line itself.
4356 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004357 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004358#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004359 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361#ifdef FEAT_MBYTE
4362 if (has_mbyte)
4363 {
4364 mb_c = c;
4365 if (enc_utf8)
4366 {
4367 /* If the UTF-8 character is more than one byte: Decode it
4368 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004369 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 mb_utf8 = FALSE;
4371 if (mb_l > 1)
4372 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004373 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 /* Overlong encoded ASCII or ASCII with composing char
4375 * is displayed normally, except a NUL. */
4376 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004377 {
4378 c = mb_c;
4379# ifdef FEAT_LINEBREAK
4380 c0 = mb_c;
4381# endif
4382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004384
4385 /* At start of the line we can have a composing char.
4386 * Draw it as a space with a composing char. */
4387 if (utf_iscomposing(mb_c))
4388 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004389 int i;
4390
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004391 for (i = Screen_mco - 1; i > 0; --i)
4392 u8cc[i] = u8cc[i - 1];
4393 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004394 mb_c = ' ';
4395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
4397
4398 if ((mb_l == 1 && c >= 0x80)
4399 || (mb_l >= 1 && mb_c == 0)
4400 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004401# ifdef UNICODE16
4402 || mb_c >= 0x10000
4403# endif
4404 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 {
4406 /*
4407 * Illegal UTF-8 byte: display as <xx>.
4408 * Non-BMP character : display as ? or fullwidth ?.
4409 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004410# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
4414 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004415# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 if (wp->w_p_rl) /* reverse */
4417 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004420# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 else if (utf_char2cells(mb_c) != 2)
4422 STRCPY(extra, "?");
4423 else
4424 /* 0xff1f in UTF-8: full-width '?' */
4425 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004426# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
4428 p_extra = extra;
4429 c = *p_extra;
4430 mb_c = mb_ptr2char_adv(&p_extra);
4431 mb_utf8 = (c >= 0x80);
4432 n_extra = (int)STRLEN(p_extra);
4433 c_extra = NUL;
4434 if (area_attr == 0 && search_attr == 0)
4435 {
4436 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004437 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 saved_attr2 = char_attr; /* save current attr */
4439 }
4440 }
4441 else if (mb_l == 0) /* at the NUL at end-of-line */
4442 mb_l = 1;
4443#ifdef FEAT_ARABIC
4444 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4445 {
4446 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004447 int pc, pc1, nc;
4448 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
4450 /* The idea of what is the previous and next
4451 * character depends on 'rightleft'. */
4452 if (wp->w_p_rl)
4453 {
4454 pc = prev_c;
4455 pc1 = prev_c1;
4456 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004457 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459 else
4460 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004461 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004463 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465 prev_c = mb_c;
4466
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004467 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469 else
4470 prev_c = mb_c;
4471#endif
4472 }
4473 else /* enc_dbcs */
4474 {
4475 mb_l = MB_BYTE2LEN(c);
4476 if (mb_l == 0) /* at the NUL at end-of-line */
4477 mb_l = 1;
4478 else if (mb_l > 1)
4479 {
4480 /* We assume a second byte below 32 is illegal.
4481 * Hopefully this is OK for all double-byte encodings!
4482 */
4483 if (ptr[1] >= 32)
4484 mb_c = (c << 8) + ptr[1];
4485 else
4486 {
4487 if (ptr[1] == NUL)
4488 {
4489 /* head byte at end of line */
4490 mb_l = 1;
4491 transchar_nonprint(extra, c);
4492 }
4493 else
4494 {
4495 /* illegal tail byte */
4496 mb_l = 2;
4497 STRCPY(extra, "XX");
4498 }
4499 p_extra = extra;
4500 n_extra = (int)STRLEN(extra) - 1;
4501 c_extra = NUL;
4502 c = *p_extra++;
4503 if (area_attr == 0 && search_attr == 0)
4504 {
4505 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004506 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 saved_attr2 = char_attr; /* save current attr */
4508 }
4509 mb_c = c;
4510 }
4511 }
4512 }
4513 /* If a double-width char doesn't fit display a '>' in the
4514 * last column; the character is displayed at the start of the
4515 * next line. */
4516 if ((
4517# ifdef FEAT_RIGHTLEFT
4518 wp->w_p_rl ? (col <= 0) :
4519# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004520 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 && (*mb_char2cells)(mb_c) == 2)
4522 {
4523 c = '>';
4524 mb_c = c;
4525 mb_utf8 = FALSE;
4526 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004527 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 /* Put pointer back so that the character will be
4529 * displayed at the start of the next line. */
4530 --ptr;
4531 }
4532 else if (*ptr != NUL)
4533 ptr += mb_l - 1;
4534
4535 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004536 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004537 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004538 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004541 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 c = ' ';
4543 if (area_attr == 0 && search_attr == 0)
4544 {
4545 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004546 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 saved_attr2 = char_attr; /* save current attr */
4548 }
4549 mb_c = c;
4550 mb_utf8 = FALSE;
4551 mb_l = 1;
4552 }
4553
4554 }
4555#endif
4556 ++ptr;
4557
4558 if (extra_check)
4559 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004560#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004561 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004562#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004563
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004564#ifdef FEAT_TERMINAL
4565 if (get_term_attr)
4566 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004567 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004568
4569 if (!attr_pri)
4570 char_attr = syntax_attr;
4571 else
4572 char_attr = hl_combine_attr(syntax_attr, char_attr);
4573 }
4574#endif
4575
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004576#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 /* Get syntax attribute, unless still at the start of the line
4578 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004579 v = (long)(ptr - line);
4580 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
4582 /* Get the syntax attribute for the character. If there
4583 * is an error, disable syntax highlighting. */
4584 save_did_emsg = did_emsg;
4585 did_emsg = FALSE;
4586
Bram Moolenaar217ad922005-03-20 22:37:15 +00004587 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004588# ifdef FEAT_SPELL
4589 has_spell ? &can_spell :
4590# endif
4591 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004594 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004595 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004596 has_syntax = FALSE;
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 else
4599 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004600#ifdef SYN_TIME_LIMIT
4601 if (wp->w_s->b_syn_slow)
4602 has_syntax = FALSE;
4603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
4605 /* Need to get the line again, a multi-line regexp may
4606 * have made it invalid. */
4607 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4608 ptr = line + v;
4609
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004610 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004612 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004613 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004614# ifdef FEAT_CONCEAL
4615 /* no concealing past the end of the line, it interferes
4616 * with line highlighting */
4617 if (c == NUL)
4618 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004619 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004620 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004623#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004624
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004625#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004626 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004627 * Only do this when there is no syntax highlighting, the
4628 * @Spell cluster is not used or the current syntax item
4629 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004630 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004631 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004632 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004633# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004634 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004635 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004636# endif
4637 if (c != 0 && (
4638# ifdef FEAT_SYN_HL
4639 !has_syntax ||
4640# endif
4641 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004642 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004643 char_u *prev_ptr, *p;
4644 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004645 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004646# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004647 if (has_mbyte)
4648 {
4649 prev_ptr = ptr - mb_l;
4650 v -= mb_l - 1;
4651 }
4652 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004653# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004654 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004655
4656 /* Use nextline[] if possible, it has the start of the
4657 * next line concatenated. */
4658 if ((prev_ptr - line) - nextlinecol >= 0)
4659 p = nextline + (prev_ptr - line) - nextlinecol;
4660 else
4661 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004662 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004663 len = spell_check(wp, p, &spell_hlf, &cap_col,
4664 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004665 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004666
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004667 /* In Insert mode only highlight a word that
4668 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004669 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004670 && (State & INSERT) != 0
4671 && wp->w_cursor.lnum == lnum
4672 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004673 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004674 && wp->w_cursor.col < (colnr_T)word_end)
4675 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004676 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004677 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004678 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004679
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004680 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004681 && (p - nextline) + len > nextline_idx)
4682 {
4683 /* Remember that the good word continues at the
4684 * start of the next line. */
4685 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004686 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004687 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004688
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004689 /* Turn index into actual attributes. */
4690 if (spell_hlf != HLF_COUNT)
4691 spell_attr = highlight_attr[spell_hlf];
4692
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004693 if (cap_col > 0)
4694 {
4695 if (p != prev_ptr
4696 && (p - nextline) + cap_col >= nextline_idx)
4697 {
4698 /* Remember that the word in the next line
4699 * must start with a capital. */
4700 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004701 cap_col = (int)((p - nextline) + cap_col
4702 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004703 }
4704 else
4705 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004706 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004707 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004708 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004709 }
4710 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004711 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004712 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004713 char_attr = hl_combine_attr(char_attr, spell_attr);
4714 else
4715 char_attr = hl_combine_attr(spell_attr, char_attr);
4716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717#endif
4718#ifdef FEAT_LINEBREAK
4719 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004720 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004722 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004723 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004725# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004726 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004727# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004728 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004730 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004732 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004733
Bram Moolenaar597a4222014-06-25 14:39:50 +02004734 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004735 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004736 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004737 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaara3650912014-11-19 13:21:57 +01004738 n_extra = (int)wp->w_buffer->b_p_ts
4739 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4740
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004741# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004742 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004743# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004745# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004746 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004747 {
4748#ifdef FEAT_CONCEAL
4749 if (c == TAB)
4750 /* See "Tab alignment" below. */
4751 FIX_FOR_BOGUSCOLS;
4752#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004753 if (!wp->w_p_list)
4754 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757#endif
4758
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004759 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4760 */
4761 if (wp->w_p_list
4762 && (((c == 160
4763#ifdef FEAT_MBYTE
4764 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4765#endif
4766 ) && lcs_nbsp)
4767 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4768 {
4769 c = (c == ' ') ? lcs_space : lcs_nbsp;
4770 if (area_attr == 0 && search_attr == 0)
4771 {
4772 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004773 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004774 saved_attr2 = char_attr; /* save current attr */
4775 }
4776#ifdef FEAT_MBYTE
4777 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004778 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004779 {
4780 mb_utf8 = TRUE;
4781 u8cc[0] = 0;
4782 c = 0xc0;
4783 }
4784 else
4785 mb_utf8 = FALSE;
4786#endif
4787 }
4788
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4790 {
4791 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004792 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
4794 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004795 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 saved_attr2 = char_attr; /* save current attr */
4797 }
4798#ifdef FEAT_MBYTE
4799 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004800 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 {
4802 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004803 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004804 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 }
4806 else
4807 mb_utf8 = FALSE;
4808#endif
4809 }
4810 }
4811
4812 /*
4813 * Handling of non-printable characters.
4814 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004815 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
4817 /*
4818 * when getting a character from the file, we may have to
4819 * turn it into something else on the way to putting it
4820 * into "ScreenLines".
4821 */
4822 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4823 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004824 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004825 long vcol_adjusted = vcol; /* removed showbreak length */
4826#ifdef FEAT_LINEBREAK
4827 /* only adjust the tab_len, when at the first column
4828 * after the showbreak value was drawn */
4829 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4830 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004833 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004834 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4835
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004836#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004837 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004838#endif
4839 /* tab amount depends on current column */
4840 n_extra = tab_len;
4841#ifdef FEAT_LINEBREAK
4842 else
4843 {
4844 char_u *p;
4845 int len = n_extra;
4846 int i;
4847 int saved_nextra = n_extra;
4848
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004849#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004850 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004851 /* there are characters to conceal */
4852 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004853 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4854 */
4855 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4856 && n_extra > tab_len)
4857 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004858#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004859
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004860 /* if n_extra > 0, it gives the number of chars, to
4861 * use for a tab, else we need to calculate the width
4862 * for a tab */
4863#ifdef FEAT_MBYTE
4864 len = (tab_len * mb_char2len(lcs_tab2));
4865 if (n_extra > 0)
4866 len += n_extra - tab_len;
4867#endif
4868 c = lcs_tab1;
4869 p = alloc((unsigned)(len + 1));
4870 vim_memset(p, ' ', len);
4871 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004872 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004873 p_extra_free = p;
4874 for (i = 0; i < tab_len; i++)
4875 {
4876#ifdef FEAT_MBYTE
4877 mb_char2bytes(lcs_tab2, p);
4878 p += mb_char2len(lcs_tab2);
4879 n_extra += mb_char2len(lcs_tab2)
4880 - (saved_nextra > 0 ? 1 : 0);
4881#else
4882 p[i] = lcs_tab2;
4883#endif
4884 }
4885 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004886#ifdef FEAT_CONCEAL
4887 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4888 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004889 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004890 n_extra -= vcol_off;
4891#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004892 }
4893#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004894#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004895 {
4896 int vc_saved = vcol_off;
4897
4898 /* Tab alignment should be identical regardless of
4899 * 'conceallevel' value. So tab compensates of all
4900 * previous concealed characters, and thus resets
4901 * vcol_off and boguscols accumulated so far in the
4902 * line. Note that the tab can be longer than
4903 * 'tabstop' when there are concealed characters. */
4904 FIX_FOR_BOGUSCOLS;
4905
4906 /* Make sure, the highlighting for the tab char will be
4907 * correctly set further below (effectively reverts the
4908 * FIX_FOR_BOGSUCOLS macro */
4909 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004910 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004911 tab_len += vc_saved;
4912 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914#ifdef FEAT_MBYTE
4915 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4916#endif
4917 if (wp->w_p_list)
4918 {
4919 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004920#ifdef FEAT_LINEBREAK
4921 if (wp->w_p_lbr)
4922 c_extra = NUL; /* using p_extra from above */
4923 else
4924#endif
4925 c_extra = lcs_tab2;
4926 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004927 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 saved_attr2 = char_attr; /* save current attr */
4929#ifdef FEAT_MBYTE
4930 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004931 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 {
4933 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004934 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004935 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937#endif
4938 }
4939 else
4940 {
4941 c_extra = ' ';
4942 c = ' ';
4943 }
4944 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004945 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004946 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004947 || ((fromcol >= 0 || fromcol_prev >= 0)
4948 && tocol > vcol
4949 && VIsual_mode != Ctrl_V
4950 && (
4951# ifdef FEAT_RIGHTLEFT
4952 wp->w_p_rl ? (col >= 0) :
4953# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004954 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004955 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004956 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004957 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004958 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004960 /* Display a '$' after the line or highlight an extra
4961 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4963 /* For a diff line the highlighting continues after the
4964 * "$". */
4965 if (
4966# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004967 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968# ifdef LINE_ATTR
4969 &&
4970# endif
4971# endif
4972# ifdef LINE_ATTR
4973 line_attr == 0
4974# endif
4975 )
4976#endif
4977 {
4978#ifdef FEAT_VIRTUALEDIT
4979 /* In virtualedit, visual selections may extend
4980 * beyond end of line. */
4981 if (area_highlighting && virtual_active()
4982 && tocol != MAXCOL && vcol < tocol)
4983 n_extra = 0;
4984 else
4985#endif
4986 {
4987 p_extra = at_end_str;
4988 n_extra = 1;
4989 c_extra = NUL;
4990 }
4991 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004992 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004993 c = lcs_eol;
4994 else
4995 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 lcs_eol_one = -1;
4997 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004998 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005000 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 n_attr = 1;
5002 }
5003#ifdef FEAT_MBYTE
5004 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005005 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 {
5007 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005008 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005009 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
5011 else
5012 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5013#endif
5014 }
5015 else if (c != NUL)
5016 {
5017 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005018 if (n_extra == 0)
5019 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020#ifdef FEAT_RIGHTLEFT
5021 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5022 rl_mirror(p_extra); /* reverse "<12>" */
5023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005025#ifdef FEAT_LINEBREAK
5026 if (wp->w_p_lbr)
5027 {
5028 char_u *p;
5029
5030 c = *p_extra;
5031 p = alloc((unsigned)n_extra + 1);
5032 vim_memset(p, ' ', n_extra);
5033 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5034 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005035 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005036 p_extra_free = p_extra = p;
5037 }
5038 else
5039#endif
5040 {
5041 n_extra = byte2cells(c) - 1;
5042 c = *p_extra++;
5043 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005044 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 {
5046 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005047 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 saved_attr2 = char_attr; /* save current attr */
5049 }
5050#ifdef FEAT_MBYTE
5051 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5052#endif
5053 }
5054#ifdef FEAT_VIRTUALEDIT
5055 else if (VIsual_active
5056 && (VIsual_mode == Ctrl_V
5057 || VIsual_mode == 'v')
5058 && virtual_active()
5059 && tocol != MAXCOL
5060 && vcol < tocol
5061 && (
5062# ifdef FEAT_RIGHTLEFT
5063 wp->w_p_rl ? (col >= 0) :
5064# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005065 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 c = ' ';
5068 --ptr; /* put it back at the NUL */
5069 }
5070#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005071#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 else if ((
5073# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005074 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005076# ifdef FEAT_TERMINAL
5077 term_attr != 0 ||
5078# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 ) && (
5081# ifdef FEAT_RIGHTLEFT
5082 wp->w_p_rl ? (col >= 0) :
5083# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005084 (col
5085# ifdef FEAT_CONCEAL
5086 - boguscols
5087# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005088 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
5090 /* Highlight until the right side of the window */
5091 c = ' ';
5092 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005093
5094 /* Remember we do the char for line highlighting. */
5095 ++did_line_attr;
5096
5097 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005098 if (line_attr != 0 && char_attr == search_attr
5099 && (did_line_attr > 1
5100 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005101 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102# ifdef FEAT_DIFF
5103 if (diff_hlf == HLF_TXD)
5104 {
5105 diff_hlf = HLF_CHD;
5106 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005107 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005108 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005109 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5110 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005111 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
5114# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005115# ifdef FEAT_TERMINAL
5116 if (term_attr != 0)
5117 {
5118 char_attr = term_attr;
5119 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5120 char_attr = hl_combine_attr(char_attr,
5121 HL_ATTR(HLF_CUL));
5122 }
5123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125#endif
5126 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005127
5128#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005129 if ( wp->w_p_cole > 0
5130 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005131 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005132 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005133 && !(lnum_in_visual_area
5134 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005135 {
5136 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005137 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005138 && (syn_get_sub_char() != NUL || match_conc
5139 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005140 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005141 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005142 /* First time at this concealed item: display one
5143 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005144 if (match_conc)
5145 c = match_conc;
5146 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005147 c = syn_get_sub_char();
5148 else if (lcs_conceal != NUL)
5149 c = lcs_conceal;
5150 else
5151 c = ' ';
5152
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005153 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005154
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005155 if (n_extra > 0)
5156 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005157 vcol += n_extra;
5158 if (wp->w_p_wrap && n_extra > 0)
5159 {
5160# ifdef FEAT_RIGHTLEFT
5161 if (wp->w_p_rl)
5162 {
5163 col -= n_extra;
5164 boguscols -= n_extra;
5165 }
5166 else
5167# endif
5168 {
5169 boguscols += n_extra;
5170 col += n_extra;
5171 }
5172 }
5173 n_extra = 0;
5174 n_attr = 0;
5175 }
5176 else if (n_skip == 0)
5177 {
5178 is_concealing = TRUE;
5179 n_skip = 1;
5180 }
5181# ifdef FEAT_MBYTE
5182 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005183 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005184 {
5185 mb_utf8 = TRUE;
5186 u8cc[0] = 0;
5187 c = 0xc0;
5188 }
5189 else
5190 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5191# endif
5192 }
5193 else
5194 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005195 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005196 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005197 }
5198#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005201#ifdef FEAT_CONCEAL
5202 /* In the cursor line and we may be concealing characters: correct
5203 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005204 if (!did_wcol && draw_state == WL_LINE
5205 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005206 && conceal_cursor_line(wp)
5207 && (int)wp->w_virtcol <= vcol + n_skip)
5208 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005209# ifdef FEAT_RIGHTLEFT
5210 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005211 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005212 else
5213# endif
5214 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005215 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005216 did_wcol = TRUE;
5217 }
5218#endif
5219
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 /* Don't override visual selection highlighting. */
5221 if (n_attr > 0
5222 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005223 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 char_attr = extra_attr;
5225
Bram Moolenaar81695252004-12-29 20:58:21 +00005226#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 /* XIM don't send preedit_start and preedit_end, but they send
5228 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5229 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005230 if (p_imst == IM_ON_THE_SPOT
5231 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005232 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 && (State & INSERT)
5234 && !p_imdisable
5235 && im_is_preediting()
5236 && draw_state == WL_LINE)
5237 {
5238 colnr_T tcol;
5239
5240 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005241 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 else
5243 tcol = preedit_end_col;
5244 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5245 {
5246 if (feedback_old_attr < 0)
5247 {
5248 feedback_col = 0;
5249 feedback_old_attr = char_attr;
5250 }
5251 char_attr = im_get_feedback_attr(feedback_col);
5252 if (char_attr < 0)
5253 char_attr = feedback_old_attr;
5254 feedback_col++;
5255 }
5256 else if (feedback_old_attr >= 0)
5257 {
5258 char_attr = feedback_old_attr;
5259 feedback_old_attr = -1;
5260 feedback_col = 0;
5261 }
5262 }
5263#endif
5264 /*
5265 * Handle the case where we are in column 0 but not on the first
5266 * character of the line and the user wants us to show us a
5267 * special character (via 'listchars' option "precedes:<char>".
5268 */
5269 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005270 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5272#ifdef FEAT_DIFF
5273 && filler_todo <= 0
5274#endif
5275 && draw_state > WL_NR
5276 && c != NUL)
5277 {
5278 c = lcs_prec;
5279 lcs_prec_todo = NUL;
5280#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005281 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5282 {
5283 /* Double-width character being overwritten by the "precedes"
5284 * character, need to fill up half the character. */
5285 c_extra = MB_FILLER_CHAR;
5286 n_extra = 1;
5287 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005288 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005291 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
5293 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005294 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005295 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
5297 else
5298 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5299#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005300 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {
5302 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005303 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 n_attr3 = 1;
5305 }
5306 }
5307
5308 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005309 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005311 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005312#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005313 || did_line_attr == 1
5314#endif
5315 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005317#ifdef FEAT_SEARCH_EXTRA
5318 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005319
5320 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005321 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005322 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005323#endif
5324
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005325 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 * highlight match at end of line. If it's beyond the last
5327 * char on the screen, just overwrite that one (tricky!) Not
5328 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005329#ifdef FEAT_SEARCH_EXTRA
5330 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005331 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005332 prevcol_hl_flag = TRUE;
5333 else
5334 {
5335 cur = wp->w_match_head;
5336 while (cur != NULL)
5337 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005338 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005339 {
5340 prevcol_hl_flag = TRUE;
5341 break;
5342 }
5343 cur = cur->next;
5344 }
5345 }
5346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005348 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005349 && (VIsual_mode != Ctrl_V
5350 || lnum == VIsual.lnum
5351 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005352 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353#ifdef FEAT_SEARCH_EXTRA
5354 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005355 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005356# ifdef FEAT_SYN_HL
5357 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5358 && !(wp == curwin && VIsual_active))
5359# endif
5360# ifdef FEAT_DIFF
5361 && diff_hlf == (hlf_T)0
5362# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005363# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005364 && did_line_attr <= 1
5365# endif
5366 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367#endif
5368 ))
5369 {
5370 int n = 0;
5371
5372#ifdef FEAT_RIGHTLEFT
5373 if (wp->w_p_rl)
5374 {
5375 if (col < 0)
5376 n = 1;
5377 }
5378 else
5379#endif
5380 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005381 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 n = -1;
5383 }
5384 if (n != 0)
5385 {
5386 /* At the window boundary, highlight the last character
5387 * instead (better than nothing). */
5388 off += n;
5389 col += n;
5390 }
5391 else
5392 {
5393 /* Add a blank character to highlight. */
5394 ScreenLines[off] = ' ';
5395#ifdef FEAT_MBYTE
5396 if (enc_utf8)
5397 ScreenLinesUC[off] = 0;
5398#endif
5399 }
5400#ifdef FEAT_SEARCH_EXTRA
5401 if (area_attr == 0)
5402 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005403 /* Use attributes from match with highest priority among
5404 * 'search_hl' and the match list. */
5405 char_attr = search_hl.attr;
5406 cur = wp->w_match_head;
5407 shl_flag = FALSE;
5408 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005409 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005410 if (shl_flag == FALSE
5411 && ((cur != NULL
5412 && cur->priority > SEARCH_HL_PRIORITY)
5413 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005414 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005415 shl = &search_hl;
5416 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005417 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005418 else
5419 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005420 if ((ptr - line) - 1 == (long)shl->startcol
5421 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005422 char_attr = shl->attr;
5423 if (shl != &search_hl && cur != NULL)
5424 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 }
5427#endif
5428 ScreenAttrs[off] = char_attr;
5429#ifdef FEAT_RIGHTLEFT
5430 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005433 --off;
5434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 else
5436#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005439 ++off;
5440 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005441 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005442#ifdef FEAT_SYN_HL
5443 eol_hl_off = 1;
5444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447
Bram Moolenaar91170f82006-05-05 21:15:17 +00005448 /*
5449 * At end of the text line.
5450 */
5451 if (c == NUL)
5452 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005453#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005454 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5455 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005456 {
5457 /* highlight last char after line */
5458 --col;
5459 --off;
5460 --vcol;
5461 }
5462
Bram Moolenaar1a384422010-07-14 19:53:30 +02005463 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005464 if (wp->w_p_wrap)
5465 v = wp->w_skipcol;
5466 else
5467 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005468
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005469 /* check if line ends before left margin */
5470 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005471 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005472#ifdef FEAT_CONCEAL
5473 /* Get rid of the boguscols now, we want to draw until the right
5474 * edge for 'cursorcolumn'. */
5475 col -= boguscols;
5476 boguscols = 0;
5477#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005478
5479 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005480 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005481
5482 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005483 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5484 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005485 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005486 && lnum != wp->w_cursor.lnum)
5487 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005488# ifdef FEAT_RIGHTLEFT
5489 && !wp->w_p_rl
5490# endif
5491 )
5492 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005493 int rightmost_vcol = 0;
5494 int i;
5495
5496 if (wp->w_p_cuc)
5497 rightmost_vcol = wp->w_virtcol;
5498 if (draw_color_col)
5499 /* determine rightmost colorcolumn to possibly draw */
5500 for (i = 0; color_cols[i] >= 0; ++i)
5501 if (rightmost_vcol < color_cols[i])
5502 rightmost_vcol = color_cols[i];
5503
Bram Moolenaar02631462017-09-22 15:20:32 +02005504 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005505 {
5506 ScreenLines[off] = ' ';
5507#ifdef FEAT_MBYTE
5508 if (enc_utf8)
5509 ScreenLinesUC[off] = 0;
5510#endif
5511 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005512 if (draw_color_col)
5513 draw_color_col = advance_color_col(VCOL_HLC,
5514 &color_cols);
5515
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005516 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005517 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005518 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005519 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005520 else
5521 ScreenAttrs[off++] = 0;
5522
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005523 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005524 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005525
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005526 ++vcol;
5527 }
5528 }
5529#endif
5530
Bram Moolenaar53f81742017-09-22 14:35:51 +02005531 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005532 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 row++;
5534
5535 /*
5536 * Update w_cline_height and w_cline_folded if the cursor line was
5537 * updated (saves a call to plines() later).
5538 */
5539 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5540 {
5541 curwin->w_cline_row = startrow;
5542 curwin->w_cline_height = row - startrow;
5543#ifdef FEAT_FOLDING
5544 curwin->w_cline_folded = FALSE;
5545#endif
5546 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5547 }
5548
5549 break;
5550 }
5551
5552 /* line continues beyond line end */
5553 if (lcs_ext
5554 && !wp->w_p_wrap
5555#ifdef FEAT_DIFF
5556 && filler_todo <= 0
5557#endif
5558 && (
5559#ifdef FEAT_RIGHTLEFT
5560 wp->w_p_rl ? col == 0 :
5561#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005562 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005564 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5566 {
5567 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005568 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569#ifdef FEAT_MBYTE
5570 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005571 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
5573 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005574 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005575 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
5577 else
5578 mb_utf8 = FALSE;
5579#endif
5580 }
5581
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005582#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005583 /* advance to the next 'colorcolumn' */
5584 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005585 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005586
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005587 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005588 * highlight the cursor position itself.
5589 * Also highlight the 'colorcolumn' if it is different than
5590 * 'cursorcolumn' */
5591 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005592 if (draw_state == WL_LINE && !lnum_in_visual_area
5593 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005594 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005595 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005596 && lnum != wp->w_cursor.lnum)
5597 {
5598 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005599 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005600 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005601 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005602 {
5603 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005604 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005605 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005606 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005607#endif
5608
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 /*
5610 * Store character to be displayed.
5611 * Skip characters that are left of the screen for 'nowrap'.
5612 */
5613 vcol_prev = vcol;
5614 if (draw_state < WL_LINE || n_skip <= 0)
5615 {
5616 /*
5617 * Store the character.
5618 */
5619#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5620 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5621 {
5622 /* A double-wide character is: put first halve in left cell. */
5623 --off;
5624 --col;
5625 }
5626#endif
5627 ScreenLines[off] = c;
5628#ifdef FEAT_MBYTE
5629 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005630 {
5631 if ((mb_c & 0xff00) == 0x8e00)
5632 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 else if (enc_utf8)
5636 {
5637 if (mb_utf8)
5638 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005639 int i;
5640
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005642 if ((c & 0xff) == 0)
5643 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005644 for (i = 0; i < Screen_mco; ++i)
5645 {
5646 ScreenLinesC[i][off] = u8cc[i];
5647 if (u8cc[i] == 0)
5648 break;
5649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 }
5651 else
5652 ScreenLinesUC[off] = 0;
5653 }
5654 if (multi_attr)
5655 {
5656 ScreenAttrs[off] = multi_attr;
5657 multi_attr = 0;
5658 }
5659 else
5660#endif
5661 ScreenAttrs[off] = char_attr;
5662
5663#ifdef FEAT_MBYTE
5664 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5665 {
5666 /* Need to fill two screen columns. */
5667 ++off;
5668 ++col;
5669 if (enc_utf8)
5670 /* UTF-8: Put a 0 in the second screen char. */
5671 ScreenLines[off] = 0;
5672 else
5673 /* DBCS: Put second byte in the second screen char. */
5674 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005675 if (draw_state > WL_NR
5676#ifdef FEAT_DIFF
5677 && filler_todo <= 0
5678#endif
5679 )
5680 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 /* When "tocol" is halfway a character, set it to the end of
5682 * the character, otherwise highlighting won't stop. */
5683 if (tocol == vcol)
5684 ++tocol;
5685#ifdef FEAT_RIGHTLEFT
5686 if (wp->w_p_rl)
5687 {
5688 /* now it's time to backup one cell */
5689 --off;
5690 --col;
5691 }
5692#endif
5693 }
5694#endif
5695#ifdef FEAT_RIGHTLEFT
5696 if (wp->w_p_rl)
5697 {
5698 --off;
5699 --col;
5700 }
5701 else
5702#endif
5703 {
5704 ++off;
5705 ++col;
5706 }
5707 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005708#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005709 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005710 {
5711 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005712 ++vcol_off;
5713 if (n_extra > 0)
5714 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005715 if (wp->w_p_wrap)
5716 {
5717 /*
5718 * Special voodoo required if 'wrap' is on.
5719 *
5720 * Advance the column indicator to force the line
5721 * drawing to wrap early. This will make the line
5722 * take up the same screen space when parts are concealed,
5723 * so that cursor line computations aren't messed up.
5724 *
5725 * To avoid the fictitious advance of 'col' causing
5726 * trailing junk to be written out of the screen line
5727 * we are building, 'boguscols' keeps track of the number
5728 * of bad columns we have advanced.
5729 */
5730 if (n_extra > 0)
5731 {
5732 vcol += n_extra;
5733# ifdef FEAT_RIGHTLEFT
5734 if (wp->w_p_rl)
5735 {
5736 col -= n_extra;
5737 boguscols -= n_extra;
5738 }
5739 else
5740# endif
5741 {
5742 col += n_extra;
5743 boguscols += n_extra;
5744 }
5745 n_extra = 0;
5746 n_attr = 0;
5747 }
5748
5749
5750# ifdef FEAT_MBYTE
5751 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5752 {
5753 /* Need to fill two screen columns. */
5754# ifdef FEAT_RIGHTLEFT
5755 if (wp->w_p_rl)
5756 {
5757 --boguscols;
5758 --col;
5759 }
5760 else
5761# endif
5762 {
5763 ++boguscols;
5764 ++col;
5765 }
5766 }
5767# endif
5768
5769# ifdef FEAT_RIGHTLEFT
5770 if (wp->w_p_rl)
5771 {
5772 --boguscols;
5773 --col;
5774 }
5775 else
5776# endif
5777 {
5778 ++boguscols;
5779 ++col;
5780 }
5781 }
5782 else
5783 {
5784 if (n_extra > 0)
5785 {
5786 vcol += n_extra;
5787 n_extra = 0;
5788 n_attr = 0;
5789 }
5790 }
5791
5792 }
5793#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 else
5795 --n_skip;
5796
Bram Moolenaar64486672010-05-16 15:46:46 +02005797 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5798 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005799 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800#ifdef FEAT_DIFF
5801 && filler_todo <= 0
5802#endif
5803 )
5804 ++vcol;
5805
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005806#ifdef FEAT_SYN_HL
5807 if (vcol_save_attr >= 0)
5808 char_attr = vcol_save_attr;
5809#endif
5810
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 /* restore attributes after "predeces" in 'listchars' */
5812 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5813 char_attr = saved_attr3;
5814
5815 /* restore attributes after last 'listchars' or 'number' char */
5816 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5817 char_attr = saved_attr2;
5818
5819 /*
5820 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005821 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 */
5823 if ((
5824#ifdef FEAT_RIGHTLEFT
5825 wp->w_p_rl ? (col < 0) :
5826#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005827 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 && (*ptr != NUL
5829#ifdef FEAT_DIFF
5830 || filler_todo > 0
5831#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005832 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5834 )
5835 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005836#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005837 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005838 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005839 boguscols = 0;
5840#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005841 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005842 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 ++row;
5845 ++screen_row;
5846
5847 /* When not wrapping and finished diff lines, or when displayed
5848 * '$' and highlighting until last column, break here. */
5849 if ((!wp->w_p_wrap
5850#ifdef FEAT_DIFF
5851 && filler_todo <= 0
5852#endif
5853 ) || lcs_eol_one == -1)
5854 break;
5855
5856 /* When the window is too narrow draw all "@" lines. */
5857 if (draw_state != WL_LINE
5858#ifdef FEAT_DIFF
5859 && filler_todo <= 0
5860#endif
5861 )
5862 {
5863 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 row = endrow;
5866 }
5867
5868 /* When line got too long for screen break here. */
5869 if (row == endrow)
5870 {
5871 ++row;
5872 break;
5873 }
5874
5875 if (screen_cur_row == screen_row - 1
5876#ifdef FEAT_DIFF
5877 && filler_todo <= 0
5878#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005879 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
5881 /* Remember that the line wraps, used for modeless copy. */
5882 LineWraps[screen_row - 1] = TRUE;
5883
5884 /*
5885 * Special trick to make copy/paste of wrapped lines work with
5886 * xterm/screen: write an extra character beyond the end of
5887 * the line. This will work with all terminal types
5888 * (regardless of the xn,am settings).
5889 * Only do this on a fast tty.
5890 * Only do this if the cursor is on the current line
5891 * (something has been written in it).
5892 * Don't do this for the GUI.
5893 * Don't do this for double-width characters.
5894 * Don't do this for a window not at the right screen border.
5895 */
5896 if (p_tf
5897#ifdef FEAT_GUI
5898 && !gui.in_use
5899#endif
5900#ifdef FEAT_MBYTE
5901 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005902 && ((*mb_off2cells)(LineOffset[screen_row],
5903 LineOffset[screen_row] + screen_Columns)
5904 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005906 + (int)Columns - 2,
5907 LineOffset[screen_row] + screen_Columns)
5908 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909#endif
5910 )
5911 {
5912 /* First make sure we are at the end of the screen line,
5913 * then output the same character again to let the
5914 * terminal know about the wrap. If the terminal doesn't
5915 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005916 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 screen_char(LineOffset[screen_row - 1]
5918 + (unsigned)Columns - 1,
5919 screen_row - 1, (int)(Columns - 1));
5920
5921#ifdef FEAT_MBYTE
5922 /* When there is a multi-byte character, just output a
5923 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005924 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5925 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 out_char(' ');
5927 else
5928#endif
5929 out_char(ScreenLines[LineOffset[screen_row - 1]
5930 + (Columns - 1)]);
5931 /* force a redraw of the first char on the next line */
5932 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5933 screen_start(); /* don't know where cursor is now */
5934 }
5935 }
5936
5937 col = 0;
5938 off = (unsigned)(current_ScreenLine - ScreenLines);
5939#ifdef FEAT_RIGHTLEFT
5940 if (wp->w_p_rl)
5941 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005942 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 off += col;
5944 }
5945#endif
5946
5947 /* reset the drawing state for the start of a wrapped line */
5948 draw_state = WL_START;
5949 saved_n_extra = n_extra;
5950 saved_p_extra = p_extra;
5951 saved_c_extra = c_extra;
5952 saved_char_attr = char_attr;
5953 n_extra = 0;
5954 lcs_prec_todo = lcs_prec;
5955#ifdef FEAT_LINEBREAK
5956# ifdef FEAT_DIFF
5957 if (filler_todo <= 0)
5958# endif
5959 need_showbreak = TRUE;
5960#endif
5961#ifdef FEAT_DIFF
5962 --filler_todo;
5963 /* When the filler lines are actually below the last line of the
5964 * file, don't draw the line itself, break here. */
5965 if (filler_todo == 0 && wp->w_botfill)
5966 break;
5967#endif
5968 }
5969
5970 } /* for every character in the line */
5971
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005972#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005973 /* After an empty line check first word for capital. */
5974 if (*skipwhite(line) == NUL)
5975 {
5976 capcol_lnum = lnum + 1;
5977 cap_col = 0;
5978 }
5979#endif
5980
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005981 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 return row;
5983}
5984
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005985#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005986static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005987
5988/*
5989 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005990 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005991 */
5992 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005993comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005994{
5995 int i;
5996
5997 for (i = 0; i < Screen_mco; ++i)
5998 {
5999 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6000 return TRUE;
6001 if (ScreenLinesC[i][off_from] == 0)
6002 break;
6003 }
6004 return FALSE;
6005}
6006#endif
6007
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008/*
6009 * Check whether the given character needs redrawing:
6010 * - the (first byte of the) character is different
6011 * - the attributes are different
6012 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006013 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 */
6015 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006016char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017{
6018 if (cols > 0
6019 && ((ScreenLines[off_from] != ScreenLines[off_to]
6020 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6021
6022#ifdef FEAT_MBYTE
6023 || (enc_dbcs != 0
6024 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6025 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6026 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6027 : (cols > 1 && ScreenLines[off_from + 1]
6028 != ScreenLines[off_to + 1])))
6029 || (enc_utf8
6030 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6031 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006032 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006033 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6034 && ScreenLines[off_from + 1]
6035 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036#endif
6037 ))
6038 return TRUE;
6039 return FALSE;
6040}
6041
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006042#if defined(FEAT_TERMINAL) || defined(PROTO)
6043/*
6044 * Return the index in ScreenLines[] for the current screen line.
6045 */
6046 int
6047screen_get_current_line_off()
6048{
6049 return (int)(current_ScreenLine - ScreenLines);
6050}
6051#endif
6052
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053/*
6054 * Move one "cooked" screen line to the screen, but only the characters that
6055 * have actually changed. Handle insert/delete character.
6056 * "coloff" gives the first column on the screen for this line.
6057 * "endcol" gives the columns where valid characters are.
6058 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6059 * needs to be cleared, negative otherwise.
6060 * "rlflag" is TRUE in a rightleft window:
6061 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6062 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6063 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006064 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006065screen_line(
6066 int row,
6067 int coloff,
6068 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006069 int clear_width,
6070 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071{
6072 unsigned off_from;
6073 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006074#ifdef FEAT_MBYTE
6075 unsigned max_off_from;
6076 unsigned max_off_to;
6077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 int force = FALSE; /* force update rest of the line */
6081 int redraw_this /* bool: does character need redraw? */
6082#ifdef FEAT_GUI
6083 = TRUE /* For GUI when while-loop empty */
6084#endif
6085 ;
6086 int redraw_next; /* redraw_this for next character */
6087#ifdef FEAT_MBYTE
6088 int clear_next = FALSE;
6089 int char_cells; /* 1: normal char */
6090 /* 2: occupies two display cells */
6091# define CHAR_CELLS char_cells
6092#else
6093# define CHAR_CELLS 1
6094#endif
6095
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006096 /* Check for illegal row and col, just in case. */
6097 if (row >= Rows)
6098 row = Rows - 1;
6099 if (endcol > Columns)
6100 endcol = Columns;
6101
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102# ifdef FEAT_CLIPBOARD
6103 clip_may_clear_selection(row, row);
6104# endif
6105
6106 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6107 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006108#ifdef FEAT_MBYTE
6109 max_off_from = off_from + screen_Columns;
6110 max_off_to = LineOffset[row] + screen_Columns;
6111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112
6113#ifdef FEAT_RIGHTLEFT
6114 if (rlflag)
6115 {
6116 /* Clear rest first, because it's left of the text. */
6117 if (clear_width > 0)
6118 {
6119 while (col <= endcol && ScreenLines[off_to] == ' '
6120 && ScreenAttrs[off_to] == 0
6121# ifdef FEAT_MBYTE
6122 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6123# endif
6124 )
6125 {
6126 ++off_to;
6127 ++col;
6128 }
6129 if (col <= endcol)
6130 screen_fill(row, row + 1, col + coloff,
6131 endcol + coloff + 1, ' ', ' ', 0);
6132 }
6133 col = endcol + 1;
6134 off_to = LineOffset[row] + col + coloff;
6135 off_from += col;
6136 endcol = (clear_width > 0 ? clear_width : -clear_width);
6137 }
6138#endif /* FEAT_RIGHTLEFT */
6139
6140 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6141
6142 while (col < endcol)
6143 {
6144#ifdef FEAT_MBYTE
6145 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006146 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 else
6148 char_cells = 1;
6149#endif
6150
6151 redraw_this = redraw_next;
6152 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6153 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6154
6155#ifdef FEAT_GUI
6156 /* If the next character was bold, then redraw the current character to
6157 * remove any pixels that might have spilt over into us. This only
6158 * happens in the GUI.
6159 */
6160 if (redraw_next && gui.in_use)
6161 {
6162 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006163 if (hl > HL_ALL)
6164 hl = syn_attr2attr(hl);
6165 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 redraw_this = TRUE;
6167 }
6168#endif
6169
6170 if (redraw_this)
6171 {
6172 /*
6173 * Special handling when 'xs' termcap flag set (hpterm):
6174 * Attributes for characters are stored at the position where the
6175 * cursor is when writing the highlighting code. The
6176 * start-highlighting code must be written with the cursor on the
6177 * first highlighted character. The stop-highlighting code must
6178 * be written with the cursor just after the last highlighted
6179 * character.
6180 * Overwriting a character doesn't remove it's highlighting. Need
6181 * to clear the rest of the line, and force redrawing it
6182 * completely.
6183 */
6184 if ( p_wiv
6185 && !force
6186#ifdef FEAT_GUI
6187 && !gui.in_use
6188#endif
6189 && ScreenAttrs[off_to] != 0
6190 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6191 {
6192 /*
6193 * Need to remove highlighting attributes here.
6194 */
6195 windgoto(row, col + coloff);
6196 out_str(T_CE); /* clear rest of this screen line */
6197 screen_start(); /* don't know where cursor is now */
6198 force = TRUE; /* force redraw of rest of the line */
6199 redraw_next = TRUE; /* or else next char would miss out */
6200
6201 /*
6202 * If the previous character was highlighted, need to stop
6203 * highlighting at this character.
6204 */
6205 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6206 {
6207 screen_attr = ScreenAttrs[off_to - 1];
6208 term_windgoto(row, col + coloff);
6209 screen_stop_highlight();
6210 }
6211 else
6212 screen_attr = 0; /* highlighting has stopped */
6213 }
6214#ifdef FEAT_MBYTE
6215 if (enc_dbcs != 0)
6216 {
6217 /* Check if overwriting a double-byte with a single-byte or
6218 * the other way around requires another character to be
6219 * redrawn. For UTF-8 this isn't needed, because comparing
6220 * ScreenLinesUC[] is sufficient. */
6221 if (char_cells == 1
6222 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006223 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 {
6225 /* Writing a single-cell character over a double-cell
6226 * character: need to redraw the next cell. */
6227 ScreenLines[off_to + 1] = 0;
6228 redraw_next = TRUE;
6229 }
6230 else if (char_cells == 2
6231 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006232 && (*mb_off2cells)(off_to, max_off_to) == 1
6233 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 {
6235 /* Writing the second half of a double-cell character over
6236 * a double-cell character: need to redraw the second
6237 * cell. */
6238 ScreenLines[off_to + 2] = 0;
6239 redraw_next = TRUE;
6240 }
6241
6242 if (enc_dbcs == DBCS_JPNU)
6243 ScreenLines2[off_to] = ScreenLines2[off_from];
6244 }
6245 /* When writing a single-width character over a double-width
6246 * character and at the end of the redrawn text, need to clear out
6247 * the right halve of the old character.
6248 * Also required when writing the right halve of a double-width
6249 * char over the left halve of an existing one. */
6250 if (has_mbyte && col + char_cells == endcol
6251 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006252 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006254 && (*mb_off2cells)(off_to, max_off_to) == 1
6255 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 clear_next = TRUE;
6257#endif
6258
6259 ScreenLines[off_to] = ScreenLines[off_from];
6260#ifdef FEAT_MBYTE
6261 if (enc_utf8)
6262 {
6263 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6264 if (ScreenLinesUC[off_from] != 0)
6265 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006266 int i;
6267
6268 for (i = 0; i < Screen_mco; ++i)
6269 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 }
6271 }
6272 if (char_cells == 2)
6273 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6274#endif
6275
6276#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006277 /* The bold trick makes a single column of pixels appear in the
6278 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 * character should be redrawn too. This happens for our own GUI
6280 * and for some xterms. */
6281 if (
6282# ifdef FEAT_GUI
6283 gui.in_use
6284# endif
6285# if defined(FEAT_GUI) && defined(UNIX)
6286 ||
6287# endif
6288# ifdef UNIX
6289 term_is_xterm
6290# endif
6291 )
6292 {
6293 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006294 if (hl > HL_ALL)
6295 hl = syn_attr2attr(hl);
6296 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 redraw_next = TRUE;
6298 }
6299#endif
6300 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6301#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006302 /* For simplicity set the attributes of second half of a
6303 * double-wide character equal to the first half. */
6304 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006306
6307 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 else
6310#endif
6311 screen_char(off_to, row, col + coloff);
6312 }
6313 else if ( p_wiv
6314#ifdef FEAT_GUI
6315 && !gui.in_use
6316#endif
6317 && col + coloff > 0)
6318 {
6319 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6320 {
6321 /*
6322 * Don't output stop-highlight when moving the cursor, it will
6323 * stop the highlighting when it should continue.
6324 */
6325 screen_attr = 0;
6326 }
6327 else if (screen_attr != 0)
6328 screen_stop_highlight();
6329 }
6330
6331 off_to += CHAR_CELLS;
6332 off_from += CHAR_CELLS;
6333 col += CHAR_CELLS;
6334 }
6335
6336#ifdef FEAT_MBYTE
6337 if (clear_next)
6338 {
6339 /* Clear the second half of a double-wide character of which the left
6340 * half was overwritten with a single-wide character. */
6341 ScreenLines[off_to] = ' ';
6342 if (enc_utf8)
6343 ScreenLinesUC[off_to] = 0;
6344 screen_char(off_to, row, col + coloff);
6345 }
6346#endif
6347
6348 if (clear_width > 0
6349#ifdef FEAT_RIGHTLEFT
6350 && !rlflag
6351#endif
6352 )
6353 {
6354#ifdef FEAT_GUI
6355 int startCol = col;
6356#endif
6357
6358 /* blank out the rest of the line */
6359 while (col < clear_width && ScreenLines[off_to] == ' '
6360 && ScreenAttrs[off_to] == 0
6361#ifdef FEAT_MBYTE
6362 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6363#endif
6364 )
6365 {
6366 ++off_to;
6367 ++col;
6368 }
6369 if (col < clear_width)
6370 {
6371#ifdef FEAT_GUI
6372 /*
6373 * In the GUI, clearing the rest of the line may leave pixels
6374 * behind if the first character cleared was bold. Some bold
6375 * fonts spill over the left. In this case we redraw the previous
6376 * character too. If we didn't skip any blanks above, then we
6377 * only redraw if the character wasn't already redrawn anyway.
6378 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006379 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
6381 hl = ScreenAttrs[off_to];
6382 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006383 {
6384 int prev_cells = 1;
6385# ifdef FEAT_MBYTE
6386 if (enc_utf8)
6387 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6388 * that its width is 2. */
6389 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6390 else if (enc_dbcs != 0)
6391 {
6392 /* find previous character by counting from first
6393 * column and get its width. */
6394 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006395 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006396
6397 while (off < off_to)
6398 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006399 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006400 off += prev_cells;
6401 }
6402 }
6403
6404 if (enc_dbcs != 0 && prev_cells > 1)
6405 screen_char_2(off_to - prev_cells, row,
6406 col + coloff - prev_cells);
6407 else
6408# endif
6409 screen_char(off_to - prev_cells, row,
6410 col + coloff - prev_cells);
6411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 }
6413#endif
6414 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6415 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 off_to += clear_width - col;
6417 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419 }
6420
6421 if (clear_width > 0)
6422 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 /* For a window that's left of another, draw the separator char. */
6424 if (col + coloff < Columns)
6425 {
6426 int c;
6427
6428 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006429 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006430#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006431 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6432 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 || ScreenAttrs[off_to] != hl)
6435 {
6436 ScreenLines[off_to] = c;
6437 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006438#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 if (enc_utf8)
6440 {
6441 if (c >= 0x80)
6442 {
6443 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006444 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 }
6446 else
6447 ScreenLinesUC[off_to] = 0;
6448 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 screen_char(off_to, row, col + coloff);
6451 }
6452 }
6453 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 LineWraps[row] = FALSE;
6455 }
6456}
6457
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006458#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006460 * Mirror text "str" for right-left displaying.
6461 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006463 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006464rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465{
6466 char_u *p1, *p2;
6467 int t;
6468
6469 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6470 {
6471 t = *p1;
6472 *p1 = *p2;
6473 *p2 = t;
6474 }
6475}
6476#endif
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478/*
6479 * mark all status lines for redraw; used after first :cd
6480 */
6481 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006482status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483{
6484 win_T *wp;
6485
Bram Moolenaar29323592016-07-24 22:04:11 +02006486 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 if (wp->w_status_height)
6488 {
6489 wp->w_redr_status = TRUE;
6490 redraw_later(VALID);
6491 }
6492}
6493
6494/*
6495 * mark all status lines of the current buffer for redraw
6496 */
6497 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006498status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
6500 win_T *wp;
6501
Bram Moolenaar29323592016-07-24 22:04:11 +02006502 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6504 {
6505 wp->w_redr_status = TRUE;
6506 redraw_later(VALID);
6507 }
6508}
6509
6510/*
6511 * Redraw all status lines that need to be redrawn.
6512 */
6513 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006514redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
6516 win_T *wp;
6517
Bram Moolenaar29323592016-07-24 22:04:11 +02006518 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 if (wp->w_redr_status)
6520 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006521 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006522 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524
Bram Moolenaar4033c552017-09-16 20:54:51 +02006525#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526/*
6527 * Redraw all status lines at the bottom of frame "frp".
6528 */
6529 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006530win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
6532 if (frp->fr_layout == FR_LEAF)
6533 frp->fr_win->w_redr_status = TRUE;
6534 else if (frp->fr_layout == FR_ROW)
6535 {
6536 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6537 win_redraw_last_status(frp);
6538 }
6539 else /* frp->fr_layout == FR_COL */
6540 {
6541 frp = frp->fr_child;
6542 while (frp->fr_next != NULL)
6543 frp = frp->fr_next;
6544 win_redraw_last_status(frp);
6545 }
6546}
6547#endif
6548
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549/*
6550 * Draw the verticap separator right of window "wp" starting with line "row".
6551 */
6552 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006553draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
6555 int hl;
6556 int c;
6557
6558 if (wp->w_vsep_width)
6559 {
6560 /* draw the vertical separator right of this window */
6561 c = fillchar_vsep(&hl);
6562 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6563 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6564 c, ' ', hl);
6565 }
6566}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
6568#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006569static int status_match_len(expand_T *xp, char_u *s);
6570static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
6572/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006573 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 */
6575 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006576status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
6578 int len = 0;
6579
6580#ifdef FEAT_MENU
6581 int emenu = (xp->xp_context == EXPAND_MENUS
6582 || xp->xp_context == EXPAND_MENUNAMES);
6583
6584 /* Check for menu separators - replace with '|'. */
6585 if (emenu && menu_is_separator(s))
6586 return 1;
6587#endif
6588
6589 while (*s != NUL)
6590 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006591 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006592 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006593 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 }
6595
6596 return len;
6597}
6598
6599/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006600 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006601 * These are backslashes used for escaping. Do show backslashes in help tags.
6602 */
6603 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006604skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006605{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006606 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006607#ifdef FEAT_MENU
6608 || ((xp->xp_context == EXPAND_MENUS
6609 || xp->xp_context == EXPAND_MENUNAMES)
6610 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6611#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006612 )
6613 {
6614#ifndef BACKSLASH_IN_FILENAME
6615 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6616 return 2;
6617#endif
6618 return 1;
6619 }
6620 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006621}
6622
6623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 * Show wildchar matches in the status line.
6625 * Show at least the "match" item.
6626 * We start at item 'first_match' in the list and show all matches that fit.
6627 *
6628 * If inversion is possible we use it. Else '=' characters are used.
6629 */
6630 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006631win_redr_status_matches(
6632 expand_T *xp,
6633 int num_matches,
6634 char_u **matches, /* list of matches */
6635 int match,
6636 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
6638#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6639 int row;
6640 char_u *buf;
6641 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006642 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 int fillchar;
6644 int attr;
6645 int i;
6646 int highlight = TRUE;
6647 char_u *selstart = NULL;
6648 int selstart_col = 0;
6649 char_u *selend = NULL;
6650 static int first_match = 0;
6651 int add_left = FALSE;
6652 char_u *s;
6653#ifdef FEAT_MENU
6654 int emenu;
6655#endif
6656#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6657 int l;
6658#endif
6659
6660 if (matches == NULL) /* interrupted completion? */
6661 return;
6662
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006663#ifdef FEAT_MBYTE
6664 if (has_mbyte)
6665 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6666 else
6667#endif
6668 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 if (buf == NULL)
6670 return;
6671
6672 if (match == -1) /* don't show match but original text */
6673 {
6674 match = 0;
6675 highlight = FALSE;
6676 }
6677 /* count 1 for the ending ">" */
6678 clen = status_match_len(xp, L_MATCH(match)) + 3;
6679 if (match == 0)
6680 first_match = 0;
6681 else if (match < first_match)
6682 {
6683 /* jumping left, as far as we can go */
6684 first_match = match;
6685 add_left = TRUE;
6686 }
6687 else
6688 {
6689 /* check if match fits on the screen */
6690 for (i = first_match; i < match; ++i)
6691 clen += status_match_len(xp, L_MATCH(i)) + 2;
6692 if (first_match > 0)
6693 clen += 2;
6694 /* jumping right, put match at the left */
6695 if ((long)clen > Columns)
6696 {
6697 first_match = match;
6698 /* if showing the last match, we can add some on the left */
6699 clen = 2;
6700 for (i = match; i < num_matches; ++i)
6701 {
6702 clen += status_match_len(xp, L_MATCH(i)) + 2;
6703 if ((long)clen >= Columns)
6704 break;
6705 }
6706 if (i == num_matches)
6707 add_left = TRUE;
6708 }
6709 }
6710 if (add_left)
6711 while (first_match > 0)
6712 {
6713 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6714 if ((long)clen >= Columns)
6715 break;
6716 --first_match;
6717 }
6718
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006719 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
6721 if (first_match == 0)
6722 {
6723 *buf = NUL;
6724 len = 0;
6725 }
6726 else
6727 {
6728 STRCPY(buf, "< ");
6729 len = 2;
6730 }
6731 clen = len;
6732
6733 i = first_match;
6734 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6735 {
6736 if (i == match)
6737 {
6738 selstart = buf + len;
6739 selstart_col = clen;
6740 }
6741
6742 s = L_MATCH(i);
6743 /* Check for menu separators - replace with '|' */
6744#ifdef FEAT_MENU
6745 emenu = (xp->xp_context == EXPAND_MENUS
6746 || xp->xp_context == EXPAND_MENUNAMES);
6747 if (emenu && menu_is_separator(s))
6748 {
6749 STRCPY(buf + len, transchar('|'));
6750 l = (int)STRLEN(buf + len);
6751 len += l;
6752 clen += l;
6753 }
6754 else
6755#endif
6756 for ( ; *s != NUL; ++s)
6757 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006758 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 clen += ptr2cells(s);
6760#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006761 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
6763 STRNCPY(buf + len, s, l);
6764 s += l - 1;
6765 len += l;
6766 }
6767 else
6768#endif
6769 {
6770 STRCPY(buf + len, transchar_byte(*s));
6771 len += (int)STRLEN(buf + len);
6772 }
6773 }
6774 if (i == match)
6775 selend = buf + len;
6776
6777 *(buf + len++) = ' ';
6778 *(buf + len++) = ' ';
6779 clen += 2;
6780 if (++i == num_matches)
6781 break;
6782 }
6783
6784 if (i != num_matches)
6785 {
6786 *(buf + len++) = '>';
6787 ++clen;
6788 }
6789
6790 buf[len] = NUL;
6791
6792 row = cmdline_row - 1;
6793 if (row >= 0)
6794 {
6795 if (wild_menu_showing == 0)
6796 {
6797 if (msg_scrolled > 0)
6798 {
6799 /* Put the wildmenu just above the command line. If there is
6800 * no room, scroll the screen one line up. */
6801 if (cmdline_row == Rows - 1)
6802 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006803 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 ++msg_scrolled;
6805 }
6806 else
6807 {
6808 ++cmdline_row;
6809 ++row;
6810 }
6811 wild_menu_showing = WM_SCROLLED;
6812 }
6813 else
6814 {
6815 /* Create status line if needed by setting 'laststatus' to 2.
6816 * Set 'winminheight' to zero to avoid that the window is
6817 * resized. */
6818 if (lastwin->w_status_height == 0)
6819 {
6820 save_p_ls = p_ls;
6821 save_p_wmh = p_wmh;
6822 p_ls = 2;
6823 p_wmh = 0;
6824 last_status(FALSE);
6825 }
6826 wild_menu_showing = WM_SHOWN;
6827 }
6828 }
6829
6830 screen_puts(buf, row, 0, attr);
6831 if (selstart != NULL && highlight)
6832 {
6833 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006834 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 }
6836
6837 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6838 }
6839
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 vim_free(buf);
6842}
6843#endif
6844
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845/*
6846 * Redraw the status line of window wp.
6847 *
6848 * If inversion is possible we use it. Else '=' characters are used.
6849 */
6850 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006851win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
6853 int row;
6854 char_u *p;
6855 int len;
6856 int fillchar;
6857 int attr;
6858 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006859 static int busy = FALSE;
6860
6861 /* It's possible to get here recursively when 'statusline' (indirectly)
6862 * invokes ":redrawstatus". Simply ignore the call then. */
6863 if (busy)
6864 return;
6865 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866
6867 wp->w_redr_status = FALSE;
6868 if (wp->w_status_height == 0)
6869 {
6870 /* no status line, can only be last window */
6871 redraw_cmdline = TRUE;
6872 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006873 else if (!redrawing()
6874#ifdef FEAT_INS_EXPAND
6875 /* don't update status line when popup menu is visible and may be
6876 * drawn over it */
6877 || pum_visible()
6878#endif
6879 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
6881 /* Don't redraw right now, do it later. */
6882 wp->w_redr_status = TRUE;
6883 }
6884#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006885 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
6887 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006888 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 }
6890#endif
6891 else
6892 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006893 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006895 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 p = NameBuff;
6897 len = (int)STRLEN(p);
6898
Bram Moolenaard85f2712017-07-28 21:51:57 +02006899 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900#ifdef FEAT_QUICKFIX
6901 || wp->w_p_pvw
6902#endif
6903 || bufIsChanged(wp->w_buffer)
6904 || wp->w_buffer->b_p_ro)
6905 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006906 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006908 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 len += (int)STRLEN(p + len);
6910 }
6911#ifdef FEAT_QUICKFIX
6912 if (wp->w_p_pvw)
6913 {
6914 STRCPY(p + len, _("[Preview]"));
6915 len += (int)STRLEN(p + len);
6916 }
6917#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006918 if (bufIsChanged(wp->w_buffer)
6919#ifdef FEAT_TERMINAL
6920 && !bt_terminal(wp->w_buffer)
6921#endif
6922 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 {
6924 STRCPY(p + len, "[+]");
6925 len += 3;
6926 }
6927 if (wp->w_buffer->b_p_ro)
6928 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006929 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006930 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
6932
Bram Moolenaar02631462017-09-22 15:20:32 +02006933 this_ru_col = ru_col - (Columns - wp->w_width);
6934 if (this_ru_col < (wp->w_width + 1) / 2)
6935 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 if (this_ru_col <= 1)
6937 {
6938 p = (char_u *)"<"; /* No room for file name! */
6939 len = 1;
6940 }
6941 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942#ifdef FEAT_MBYTE
6943 if (has_mbyte)
6944 {
6945 int clen = 0, i;
6946
6947 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006948 clen = mb_string2cells(p, -1);
6949
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 /* Find first character that will fit.
6951 * Going from start to end is much faster for DBCS. */
6952 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006953 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 clen -= (*mb_ptr2cells)(p + i);
6955 len = clen;
6956 if (i > 0)
6957 {
6958 p = p + i - 1;
6959 *p = '<';
6960 ++len;
6961 }
6962
6963 }
6964 else
6965#endif
6966 if (len > this_ru_col - 1)
6967 {
6968 p += len - (this_ru_col - 1);
6969 *p = '<';
6970 len = this_ru_col - 1;
6971 }
6972
6973 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006974 screen_puts(p, row, wp->w_wincol, attr);
6975 screen_fill(row, row + 1, len + wp->w_wincol,
6976 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006978 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6980 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006981 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982
6983#ifdef FEAT_CMDL_INFO
6984 win_redr_ruler(wp, TRUE);
6985#endif
6986 }
6987
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 /*
6989 * May need to draw the character below the vertical separator.
6990 */
6991 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6992 {
6993 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006994 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 else
6996 fillchar = fillchar_vsep(&attr);
6997 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6998 attr);
6999 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007000 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001}
7002
Bram Moolenaar238a5642006-02-21 22:12:05 +00007003#ifdef FEAT_STL_OPT
7004/*
7005 * Redraw the status line according to 'statusline' and take care of any
7006 * errors encountered.
7007 */
7008 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007009redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007010{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007011 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007012 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007013
7014 /* When called recursively return. This can happen when the statusline
7015 * contains an expression that triggers a redraw. */
7016 if (entered)
7017 return;
7018 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007019
Bram Moolenaara742e082016-04-05 21:10:38 +02007020 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007021 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007022 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007023 {
7024 /* When there is an error disable the statusline, otherwise the
7025 * display is messed up with errors and a redraw triggers the problem
7026 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007027 set_string_option_direct((char_u *)"statusline", -1,
7028 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007029 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007030 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007031 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007032 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007033}
7034#endif
7035
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036/*
7037 * Return TRUE if the status line of window "wp" is connected to the status
7038 * line of the window right of it. If not, then it's a vertical separator.
7039 * Only call if (wp->w_vsep_width != 0).
7040 */
7041 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007042stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043{
7044 frame_T *fr;
7045
7046 fr = wp->w_frame;
7047 while (fr->fr_parent != NULL)
7048 {
7049 if (fr->fr_parent->fr_layout == FR_COL)
7050 {
7051 if (fr->fr_next != NULL)
7052 break;
7053 }
7054 else
7055 {
7056 if (fr->fr_next != NULL)
7057 return TRUE;
7058 }
7059 fr = fr->fr_parent;
7060 }
7061 return FALSE;
7062}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065/*
7066 * Get the value to show for the language mappings, active 'keymap'.
7067 */
7068 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007069get_keymap_str(
7070 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007071 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007072 char_u *buf, /* buffer for the result */
7073 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074{
7075 char_u *p;
7076
7077 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7078 return FALSE;
7079
7080 {
7081#ifdef FEAT_EVAL
7082 buf_T *old_curbuf = curbuf;
7083 win_T *old_curwin = curwin;
7084 char_u *s;
7085
7086 curbuf = wp->w_buffer;
7087 curwin = wp;
7088 STRCPY(buf, "b:keymap_name"); /* must be writable */
7089 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007090 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 --emsg_skip;
7092 curbuf = old_curbuf;
7093 curwin = old_curwin;
7094 if (p == NULL || *p == NUL)
7095#endif
7096 {
7097#ifdef FEAT_KEYMAP
7098 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7099 p = wp->w_buffer->b_p_keymap;
7100 else
7101#endif
7102 p = (char_u *)"lang";
7103 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007104 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 buf[0] = NUL;
7106#ifdef FEAT_EVAL
7107 vim_free(s);
7108#endif
7109 }
7110 return buf[0] != NUL;
7111}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112
7113#if defined(FEAT_STL_OPT) || defined(PROTO)
7114/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007115 * Redraw the status line or ruler of window "wp".
7116 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 */
7118 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007119win_redr_custom(
7120 win_T *wp,
7121 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007123 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 int attr;
7125 int curattr;
7126 int row;
7127 int col = 0;
7128 int maxwidth;
7129 int width;
7130 int n;
7131 int len;
7132 int fillchar;
7133 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007134 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007136 struct stl_hlrec hltab[STL_MAX_ITEM];
7137 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007138 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007139 win_T *ewp;
7140 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141
Bram Moolenaar1d633412013-12-11 15:52:01 +01007142 /* There is a tiny chance that this gets called recursively: When
7143 * redrawing a status line triggers redrawing the ruler or tabline.
7144 * Avoid trouble by not allowing recursion. */
7145 if (entered)
7146 return;
7147 entered = TRUE;
7148
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007150 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007152 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007153 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007154 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007155 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007156 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007157 maxwidth = Columns;
7158# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007159 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007162 else
7163 {
7164 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007165 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007166 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007167
7168 if (draw_ruler)
7169 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007170 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007171 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007172 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007173 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007174 if (*++stl == '-')
7175 stl++;
7176 if (atoi((char *)stl))
7177 while (VIM_ISDIGIT(*stl))
7178 stl++;
7179 if (*stl++ != '(')
7180 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007181 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007182 col = ru_col - (Columns - wp->w_width);
7183 if (col < (wp->w_width + 1) / 2)
7184 col = (wp->w_width + 1) / 2;
7185 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007186 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007187 {
7188 row = Rows - 1;
7189 --maxwidth; /* writing in last column may cause scrolling */
7190 fillchar = ' ';
7191 attr = 0;
7192 }
7193
7194# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007195 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007196# endif
7197 }
7198 else
7199 {
7200 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007201 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007202 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007203 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007204# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007205 use_sandbox = was_set_insecurely((char_u *)"statusline",
7206 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007207# endif
7208 }
7209
Bram Moolenaar53f81742017-09-22 14:35:51 +02007210 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007211 }
7212
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007214 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
Bram Moolenaar61452852011-02-01 18:01:11 +01007216 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7217 * the cursor away and back. */
7218 ewp = wp == NULL ? curwin : wp;
7219 p_crb_save = ewp->w_p_crb;
7220 ewp->w_p_crb = FALSE;
7221
Bram Moolenaar362f3562009-11-03 16:20:34 +00007222 /* Make a copy, because the statusline may include a function call that
7223 * might change the option value and free the memory. */
7224 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007225 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007226 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007227 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007228 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007229 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007231 /* Make all characters printable. */
7232 p = transstr(buf);
7233 if (p != NULL)
7234 {
7235 vim_strncpy(buf, p, sizeof(buf) - 1);
7236 vim_free(p);
7237 }
7238
7239 /* fill up with "fillchar" */
7240 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007241 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
7243#ifdef FEAT_MBYTE
7244 len += (*mb_char2bytes)(fillchar, buf + len);
7245#else
7246 buf[len++] = fillchar;
7247#endif
7248 ++width;
7249 }
7250 buf[len] = NUL;
7251
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007252 /*
7253 * Draw each snippet with the specified highlighting.
7254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 curattr = attr;
7256 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007257 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007259 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 screen_puts_len(p, len, row, col, curattr);
7261 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007262 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007264 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007266 else if (hltab[n].userhl < 0)
7267 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007268#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007269 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7270 && wp->w_status_height != 0)
7271 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007272 else if (wp != NULL && bt_terminal(wp->w_buffer)
7273 && wp->w_status_height != 0)
7274 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007275#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007276 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007277 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007279 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 }
7281 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007282
7283 if (wp == NULL)
7284 {
7285 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7286 col = 0;
7287 len = 0;
7288 p = buf;
7289 fillchar = 0;
7290 for (n = 0; tabtab[n].start != NULL; n++)
7291 {
7292 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7293 while (col < len)
7294 TabPageIdxs[col++] = fillchar;
7295 p = tabtab[n].start;
7296 fillchar = tabtab[n].userhl;
7297 }
7298 while (col < Columns)
7299 TabPageIdxs[col++] = fillchar;
7300 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007301
7302theend:
7303 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304}
7305
7306#endif /* FEAT_STL_OPT */
7307
7308/*
7309 * Output a single character directly to the screen and update ScreenLines.
7310 */
7311 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007312screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 char_u buf[MB_MAXBYTES + 1];
7315
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007316#ifdef FEAT_MBYTE
7317 if (has_mbyte)
7318 buf[(*mb_char2bytes)(c, buf)] = NUL;
7319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007321 {
7322 buf[0] = c;
7323 buf[1] = NUL;
7324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 screen_puts(buf, row, col, attr);
7326}
7327
7328/*
7329 * Get a single character directly from ScreenLines into "bytes[]".
7330 * Also return its attribute in *attrp;
7331 */
7332 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007333screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334{
7335 unsigned off;
7336
7337 /* safety check */
7338 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7339 {
7340 off = LineOffset[row] + col;
7341 *attrp = ScreenAttrs[off];
7342 bytes[0] = ScreenLines[off];
7343 bytes[1] = NUL;
7344
7345#ifdef FEAT_MBYTE
7346 if (enc_utf8 && ScreenLinesUC[off] != 0)
7347 bytes[utfc_char2bytes(off, bytes)] = NUL;
7348 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7349 {
7350 bytes[0] = ScreenLines[off];
7351 bytes[1] = ScreenLines2[off];
7352 bytes[2] = NUL;
7353 }
7354 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7355 {
7356 bytes[1] = ScreenLines[off + 1];
7357 bytes[2] = NUL;
7358 }
7359#endif
7360 }
7361}
7362
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007363#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007364static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007365
7366/*
7367 * Return TRUE if composing characters for screen posn "off" differs from
7368 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007369 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007370 */
7371 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007372screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007373{
7374 int i;
7375
7376 for (i = 0; i < Screen_mco; ++i)
7377 {
7378 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7379 return TRUE;
7380 if (u8cc[i] == 0)
7381 break;
7382 }
7383 return FALSE;
7384}
7385#endif
7386
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387/*
7388 * Put string '*text' on the screen at position 'row' and 'col', with
7389 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7390 * Note: only outputs within one row, message is truncated at screen boundary!
7391 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7392 */
7393 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007394screen_puts(
7395 char_u *text,
7396 int row,
7397 int col,
7398 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399{
7400 screen_puts_len(text, -1, row, col, attr);
7401}
7402
7403/*
7404 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7405 * a NUL.
7406 */
7407 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007408screen_puts_len(
7409 char_u *text,
7410 int textlen,
7411 int row,
7412 int col,
7413 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414{
7415 unsigned off;
7416 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007417 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 int c;
7419#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007420 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 int mbyte_blen = 1;
7422 int mbyte_cells = 1;
7423 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007424 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 int clear_next_cell = FALSE;
7426# ifdef FEAT_ARABIC
7427 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007428 int pc, nc, nc1;
7429 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430# endif
7431#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007432#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7433 int force_redraw_this;
7434 int force_redraw_next = FALSE;
7435#endif
7436 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437
7438 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7439 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007440 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441
Bram Moolenaarc236c162008-07-13 17:41:49 +00007442#ifdef FEAT_MBYTE
7443 /* When drawing over the right halve of a double-wide char clear out the
7444 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007445 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007446# ifdef FEAT_GUI
7447 && !gui.in_use
7448# endif
7449 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007450 {
7451 ScreenLines[off - 1] = ' ';
7452 ScreenAttrs[off - 1] = 0;
7453 if (enc_utf8)
7454 {
7455 ScreenLinesUC[off - 1] = 0;
7456 ScreenLinesC[0][off - 1] = 0;
7457 }
7458 /* redraw the previous cell, make it empty */
7459 screen_char(off - 1, row, col - 1);
7460 /* force the cell at "col" to be redrawn */
7461 force_redraw_next = TRUE;
7462 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007463#endif
7464
Bram Moolenaar367329b2007-08-30 11:53:22 +00007465#ifdef FEAT_MBYTE
7466 max_off = LineOffset[row] + screen_Columns;
7467#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007468 while (col < screen_Columns
7469 && (len < 0 || (int)(ptr - text) < len)
7470 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 {
7472 c = *ptr;
7473#ifdef FEAT_MBYTE
7474 /* check if this is the first byte of a multibyte */
7475 if (has_mbyte)
7476 {
7477 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007478 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007480 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7482 mbyte_cells = 1;
7483 else if (enc_dbcs != 0)
7484 mbyte_cells = mbyte_blen;
7485 else /* enc_utf8 */
7486 {
7487 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007488 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 (int)((text + len) - ptr));
7490 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007491 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007493# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 /* Non-BMP character: display as ? or fullwidth ?. */
7495 if (u8c >= 0x10000)
7496 {
7497 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7498 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007499 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502# ifdef FEAT_ARABIC
7503 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7504 {
7505 /* Do Arabic shaping. */
7506 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7507 {
7508 /* Past end of string to be displayed. */
7509 nc = NUL;
7510 nc1 = NUL;
7511 }
7512 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007513 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007514 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7515 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007516 nc1 = pcc[0];
7517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 pc = prev_c;
7519 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007520 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
7522 else
7523 prev_c = u8c;
7524# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007525 if (col + mbyte_cells > screen_Columns)
7526 {
7527 /* Only 1 cell left, but character requires 2 cells:
7528 * display a '>' in the last column to avoid wrapping. */
7529 c = '>';
7530 mbyte_cells = 1;
7531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 }
7533 }
7534#endif
7535
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007536#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7537 force_redraw_this = force_redraw_next;
7538 force_redraw_next = FALSE;
7539#endif
7540
7541 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542#ifdef FEAT_MBYTE
7543 || (mbyte_cells == 2
7544 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7545 || (enc_dbcs == DBCS_JPNU
7546 && c == 0x8e
7547 && ScreenLines2[off] != ptr[1])
7548 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007549 && (ScreenLinesUC[off] !=
7550 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7551 || (ScreenLinesUC[off] != 0
7552 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553#endif
7554 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007555 || exmode_active;
7556
7557 if (need_redraw
7558#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7559 || force_redraw_this
7560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 )
7562 {
7563#if defined(FEAT_GUI) || defined(UNIX)
7564 /* The bold trick makes a single row of pixels appear in the next
7565 * character. When a bold character is removed, the next
7566 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007567 * and for some xterms. */
7568 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569# ifdef FEAT_GUI
7570 gui.in_use
7571# endif
7572# if defined(FEAT_GUI) && defined(UNIX)
7573 ||
7574# endif
7575# ifdef UNIX
7576 term_is_xterm
7577# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007578 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007580 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007582 if (n > HL_ALL)
7583 n = syn_attr2attr(n);
7584 if (n & HL_BOLD)
7585 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 }
7587#endif
7588#ifdef FEAT_MBYTE
7589 /* When at the end of the text and overwriting a two-cell
7590 * character with a one-cell character, need to clear the next
7591 * cell. Also when overwriting the left halve of a two-cell char
7592 * with the right halve of a two-cell char. Do this only once
7593 * (mb_off2cells() may return 2 on the right halve). */
7594 if (clear_next_cell)
7595 clear_next_cell = FALSE;
7596 else if (has_mbyte
7597 && (len < 0 ? ptr[mbyte_blen] == NUL
7598 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007599 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007601 && (*mb_off2cells)(off, max_off) == 1
7602 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 clear_next_cell = TRUE;
7604
7605 /* Make sure we never leave a second byte of a double-byte behind,
7606 * it confuses mb_off2cells(). */
7607 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007608 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007610 && (*mb_off2cells)(off, max_off) == 1
7611 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 ScreenLines[off + mbyte_blen] = 0;
7613#endif
7614 ScreenLines[off] = c;
7615 ScreenAttrs[off] = attr;
7616#ifdef FEAT_MBYTE
7617 if (enc_utf8)
7618 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007619 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 ScreenLinesUC[off] = 0;
7621 else
7622 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007623 int i;
7624
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007626 for (i = 0; i < Screen_mco; ++i)
7627 {
7628 ScreenLinesC[i][off] = u8cc[i];
7629 if (u8cc[i] == 0)
7630 break;
7631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 }
7633 if (mbyte_cells == 2)
7634 {
7635 ScreenLines[off + 1] = 0;
7636 ScreenAttrs[off + 1] = attr;
7637 }
7638 screen_char(off, row, col);
7639 }
7640 else if (mbyte_cells == 2)
7641 {
7642 ScreenLines[off + 1] = ptr[1];
7643 ScreenAttrs[off + 1] = attr;
7644 screen_char_2(off, row, col);
7645 }
7646 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7647 {
7648 ScreenLines2[off] = ptr[1];
7649 screen_char(off, row, col);
7650 }
7651 else
7652#endif
7653 screen_char(off, row, col);
7654 }
7655#ifdef FEAT_MBYTE
7656 if (has_mbyte)
7657 {
7658 off += mbyte_cells;
7659 col += mbyte_cells;
7660 ptr += mbyte_blen;
7661 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007662 {
7663 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007665 len = -1;
7666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 }
7668 else
7669#endif
7670 {
7671 ++off;
7672 ++col;
7673 ++ptr;
7674 }
7675 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007676
7677#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7678 /* If we detected the next character needs to be redrawn, but the text
7679 * doesn't extend up to there, update the character here. */
7680 if (force_redraw_next && col < screen_Columns)
7681 {
7682# ifdef FEAT_MBYTE
7683 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7684 screen_char_2(off, row, col);
7685 else
7686# endif
7687 screen_char(off, row, col);
7688 }
7689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690}
7691
7692#ifdef FEAT_SEARCH_EXTRA
7693/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007694 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 */
7696 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007697start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
7699 if (p_hls && !no_hlsearch)
7700 {
7701 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007702 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007703# ifdef FEAT_RELTIME
7704 /* Set the time limit to 'redrawtime'. */
7705 profile_setlimit(p_rdt, &search_hl.tm);
7706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 }
7708}
7709
7710/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007711 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 */
7713 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007714end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
7716 if (search_hl.rm.regprog != NULL)
7717 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007718 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 search_hl.rm.regprog = NULL;
7720 }
7721}
7722
7723/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007724 * Init for calling prepare_search_hl().
7725 */
7726 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007727init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007728{
7729 matchitem_T *cur;
7730
7731 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7732 * match */
7733 cur = wp->w_match_head;
7734 while (cur != NULL)
7735 {
7736 cur->hl.rm = cur->match;
7737 if (cur->hlg_id == 0)
7738 cur->hl.attr = 0;
7739 else
7740 cur->hl.attr = syn_id2attr(cur->hlg_id);
7741 cur->hl.buf = wp->w_buffer;
7742 cur->hl.lnum = 0;
7743 cur->hl.first_lnum = 0;
7744# ifdef FEAT_RELTIME
7745 /* Set the time limit to 'redrawtime'. */
7746 profile_setlimit(p_rdt, &(cur->hl.tm));
7747# endif
7748 cur = cur->next;
7749 }
7750 search_hl.buf = wp->w_buffer;
7751 search_hl.lnum = 0;
7752 search_hl.first_lnum = 0;
7753 /* time limit is set at the toplevel, for all windows */
7754}
7755
7756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 * Advance to the match in window "wp" line "lnum" or past it.
7758 */
7759 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007760prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007762 matchitem_T *cur; /* points to the match list */
7763 match_T *shl; /* points to search_hl or a match */
7764 int shl_flag; /* flag to indicate whether search_hl
7765 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007766 int pos_inprogress; /* marks that position match search is
7767 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 int n;
7769
7770 /*
7771 * When using a multi-line pattern, start searching at the top
7772 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007773 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007775 cur = wp->w_match_head;
7776 shl_flag = FALSE;
7777 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007779 if (shl_flag == FALSE)
7780 {
7781 shl = &search_hl;
7782 shl_flag = TRUE;
7783 }
7784 else
7785 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 if (shl->rm.regprog != NULL
7787 && shl->lnum == 0
7788 && re_multiline(shl->rm.regprog))
7789 {
7790 if (shl->first_lnum == 0)
7791 {
7792# ifdef FEAT_FOLDING
7793 for (shl->first_lnum = lnum;
7794 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7795 if (hasFoldingWin(wp, shl->first_lnum - 1,
7796 NULL, NULL, TRUE, NULL))
7797 break;
7798# else
7799 shl->first_lnum = wp->w_topline;
7800# endif
7801 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007802 if (cur != NULL)
7803 cur->pos.cur = 0;
7804 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007806 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7807 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007809 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7810 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007811 pos_inprogress = cur == NULL || cur->pos.cur == 0
7812 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 if (shl->lnum != 0)
7814 {
7815 shl->first_lnum = shl->lnum
7816 + shl->rm.endpos[0].lnum
7817 - shl->rm.startpos[0].lnum;
7818 n = shl->rm.endpos[0].col;
7819 }
7820 else
7821 {
7822 ++shl->first_lnum;
7823 n = 0;
7824 }
7825 }
7826 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007827 if (shl != &search_hl && cur != NULL)
7828 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830}
7831
7832/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007833 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 * Uses shl->buf.
7835 * Sets shl->lnum and shl->rm contents.
7836 * Note: Assumes a previous match is always before "lnum", unless
7837 * shl->lnum is zero.
7838 * Careful: Any pointers for buffer lines will become invalid.
7839 */
7840 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007841next_search_hl(
7842 win_T *win,
7843 match_T *shl, /* points to search_hl or a match */
7844 linenr_T lnum,
7845 colnr_T mincol, /* minimal column for a match */
7846 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 linenr_T l;
7849 colnr_T matchcol;
7850 long nmatched;
7851
7852 if (shl->lnum != 0)
7853 {
7854 /* Check for three situations:
7855 * 1. If the "lnum" is below a previous match, start a new search.
7856 * 2. If the previous match includes "mincol", use it.
7857 * 3. Continue after the previous match.
7858 */
7859 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7860 if (lnum > l)
7861 shl->lnum = 0;
7862 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7863 return;
7864 }
7865
7866 /*
7867 * Repeat searching for a match until one is found that includes "mincol"
7868 * or none is found in this line.
7869 */
7870 called_emsg = FALSE;
7871 for (;;)
7872 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007873#ifdef FEAT_RELTIME
7874 /* Stop searching after passing the time limit. */
7875 if (profile_passed_limit(&(shl->tm)))
7876 {
7877 shl->lnum = 0; /* no match found in time */
7878 break;
7879 }
7880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 /* Three situations:
7882 * 1. No useful previous match: search from start of line.
7883 * 2. Not Vi compatible or empty match: continue at next character.
7884 * Break the loop if this is beyond the end of the line.
7885 * 3. Vi compatible searching: continue at end of previous match.
7886 */
7887 if (shl->lnum == 0)
7888 matchcol = 0;
7889 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7890 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007891 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007893 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007894
7895 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007896 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007897 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007899 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 shl->lnum = 0;
7901 break;
7902 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007903#ifdef FEAT_MBYTE
7904 if (has_mbyte)
7905 matchcol += mb_ptr2len(ml);
7906 else
7907#endif
7908 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 }
7910 else
7911 matchcol = shl->rm.endpos[0].col;
7912
7913 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007914 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007916 /* Remember whether shl->rm is using a copy of the regprog in
7917 * cur->match. */
7918 int regprog_is_copy = (shl != &search_hl && cur != NULL
7919 && shl == &cur->hl
7920 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007921 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007922
Bram Moolenaarb3414592014-06-17 17:48:32 +02007923 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7924 matchcol,
7925#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007926 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007927#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007928 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007929#endif
7930 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007931 /* Copy the regprog, in case it got freed and recompiled. */
7932 if (regprog_is_copy)
7933 cur->match.regprog = cur->hl.rm.regprog;
7934
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007935 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007936 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007937 /* Error while handling regexp: stop using this regexp. */
7938 if (shl == &search_hl)
7939 {
7940 /* don't free regprog in the match list, it's a copy */
7941 vim_regfree(shl->rm.regprog);
7942 SET_NO_HLSEARCH(TRUE);
7943 }
7944 shl->rm.regprog = NULL;
7945 shl->lnum = 0;
7946 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7947 message */
7948 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007949 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007950 }
7951 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007952 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007953 else
7954 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 if (nmatched == 0)
7956 {
7957 shl->lnum = 0; /* no match found */
7958 break;
7959 }
7960 if (shl->rm.startpos[0].lnum > 0
7961 || shl->rm.startpos[0].col >= mincol
7962 || nmatched > 1
7963 || shl->rm.endpos[0].col > mincol)
7964 {
7965 shl->lnum += shl->rm.startpos[0].lnum;
7966 break; /* useful match found */
7967 }
7968 }
7969}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970
Bram Moolenaar85077472016-10-16 14:35:48 +02007971/*
7972 * If there is a match fill "shl" and return one.
7973 * Return zero otherwise.
7974 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007975 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007976next_search_hl_pos(
7977 match_T *shl, /* points to a match */
7978 linenr_T lnum,
7979 posmatch_T *posmatch, /* match positions */
7980 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007981{
7982 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007983 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007984
Bram Moolenaarb3414592014-06-17 17:48:32 +02007985 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7986 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007987 llpos_T *pos = &posmatch->pos[i];
7988
7989 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007991 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007992 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007993 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007994 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007995 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007996 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007997 /* if this match comes before the one at "found" then swap
7998 * them */
7999 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008000 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008001 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008002
Bram Moolenaar85077472016-10-16 14:35:48 +02008003 *pos = posmatch->pos[found];
8004 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008005 }
8006 }
8007 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008008 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008009 }
8010 }
8011 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008012 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008013 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008014 colnr_T start = posmatch->pos[found].col == 0
8015 ? 0 : posmatch->pos[found].col - 1;
8016 colnr_T end = posmatch->pos[found].col == 0
8017 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008018
Bram Moolenaar85077472016-10-16 14:35:48 +02008019 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008020 shl->rm.startpos[0].lnum = 0;
8021 shl->rm.startpos[0].col = start;
8022 shl->rm.endpos[0].lnum = 0;
8023 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008024 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008025 posmatch->cur = found + 1;
8026 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008027 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008028 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008029}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008030#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008031
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008033screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034{
8035 attrentry_T *aep = NULL;
8036
8037 screen_attr = attr;
8038 if (full_screen
8039#ifdef WIN3264
8040 && termcap_active
8041#endif
8042 )
8043 {
8044#ifdef FEAT_GUI
8045 if (gui.in_use)
8046 {
8047 char buf[20];
8048
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008049 /* The GUI handles this internally. */
8050 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 OUT_STR(buf);
8052 }
8053 else
8054#endif
8055 {
8056 if (attr > HL_ALL) /* special HL attr. */
8057 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008058 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 aep = syn_cterm_attr2entry(attr);
8060 else
8061 aep = syn_term_attr2entry(attr);
8062 if (aep == NULL) /* did ":syntax clear" */
8063 attr = 0;
8064 else
8065 attr = aep->ae_attr;
8066 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008067 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008069 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008070#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008071 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8072 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8073 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008074#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008075 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008076 /* If the Normal FG color has BOLD attribute and the new HL
8077 * has a FG color defined, clear BOLD. */
8078 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008079 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008081 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008082 out_str(T_UCS);
8083 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008084 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8085 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008087 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008089 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008091 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008092 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093
8094 /*
8095 * Output the color or start string after bold etc., in case the
8096 * bold etc. override the color setting.
8097 */
8098 if (aep != NULL)
8099 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008100#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008101 /* When 'termguicolors' is set but fg or bg is unset,
8102 * fall back to the cterm colors. This helps for SpellBad,
8103 * where the GUI uses a red undercurl. */
8104 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008106 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008107 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008108 }
8109 else
8110#endif
8111 if (t_colors > 1)
8112 {
8113 if (aep->ae_u.cterm.fg_color)
8114 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8115 }
8116#ifdef FEAT_TERMGUICOLORS
8117 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8118 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008119 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008120 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 }
8122 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008123#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008124 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008126 if (aep->ae_u.cterm.bg_color)
8127 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8128 }
8129
8130 if (t_colors <= 1)
8131 {
8132 if (aep->ae_u.term.start != NULL)
8133 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 }
8135 }
8136 }
8137 }
8138}
8139
8140 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008141screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142{
8143 int do_ME = FALSE; /* output T_ME code */
8144
8145 if (screen_attr != 0
8146#ifdef WIN3264
8147 && termcap_active
8148#endif
8149 )
8150 {
8151#ifdef FEAT_GUI
8152 if (gui.in_use)
8153 {
8154 char buf[20];
8155
8156 /* use internal GUI code */
8157 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8158 OUT_STR(buf);
8159 }
8160 else
8161#endif
8162 {
8163 if (screen_attr > HL_ALL) /* special HL attr. */
8164 {
8165 attrentry_T *aep;
8166
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008167 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {
8169 /*
8170 * Assume that t_me restores the original colors!
8171 */
8172 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008173 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008174#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008175 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8176 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8177 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008178#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008179 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008180#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008181 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8182 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8183 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008184#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008185 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 do_ME = TRUE;
8187 }
8188 else
8189 {
8190 aep = syn_term_attr2entry(screen_attr);
8191 if (aep != NULL && aep->ae_u.term.stop != NULL)
8192 {
8193 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8194 do_ME = TRUE;
8195 else
8196 out_str(aep->ae_u.term.stop);
8197 }
8198 }
8199 if (aep == NULL) /* did ":syntax clear" */
8200 screen_attr = 0;
8201 else
8202 screen_attr = aep->ae_attr;
8203 }
8204
8205 /*
8206 * Often all ending-codes are equal to T_ME. Avoid outputting the
8207 * same sequence several times.
8208 */
8209 if (screen_attr & HL_STANDOUT)
8210 {
8211 if (STRCMP(T_SE, T_ME) == 0)
8212 do_ME = TRUE;
8213 else
8214 out_str(T_SE);
8215 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008216 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008217 {
8218 if (STRCMP(T_UCE, T_ME) == 0)
8219 do_ME = TRUE;
8220 else
8221 out_str(T_UCE);
8222 }
8223 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008224 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {
8226 if (STRCMP(T_UE, T_ME) == 0)
8227 do_ME = TRUE;
8228 else
8229 out_str(T_UE);
8230 }
8231 if (screen_attr & HL_ITALIC)
8232 {
8233 if (STRCMP(T_CZR, T_ME) == 0)
8234 do_ME = TRUE;
8235 else
8236 out_str(T_CZR);
8237 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008238 if (screen_attr & HL_STRIKETHROUGH)
8239 {
8240 if (STRCMP(T_STE, T_ME) == 0)
8241 do_ME = TRUE;
8242 else
8243 out_str(T_STE);
8244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8246 out_str(T_ME);
8247
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008248#ifdef FEAT_TERMGUICOLORS
8249 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008251 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008252 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008253 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008254 term_bg_rgb_color(cterm_normal_bg_gui_color);
8255 }
8256 else
8257#endif
8258 {
8259 if (t_colors > 1)
8260 {
8261 /* set Normal cterm colors */
8262 if (cterm_normal_fg_color != 0)
8263 term_fg_color(cterm_normal_fg_color - 1);
8264 if (cterm_normal_bg_color != 0)
8265 term_bg_color(cterm_normal_bg_color - 1);
8266 if (cterm_normal_fg_bold)
8267 out_str(T_MD);
8268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 }
8270 }
8271 }
8272 screen_attr = 0;
8273}
8274
8275/*
8276 * Reset the colors for a cterm. Used when leaving Vim.
8277 * The machine specific code may override this again.
8278 */
8279 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008280reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008282 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {
8284 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008285#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008286 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8287 || cterm_normal_bg_gui_color != INVALCOLOR)
8288 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008289#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {
8293 out_str(T_OP);
8294 screen_attr = -1;
8295 }
8296 if (cterm_normal_fg_bold)
8297 {
8298 out_str(T_ME);
8299 screen_attr = -1;
8300 }
8301 }
8302}
8303
8304/*
8305 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8306 * using the attributes from ScreenAttrs["off"].
8307 */
8308 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008309screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310{
8311 int attr;
8312
8313 /* Check for illegal values, just in case (could happen just after
8314 * resizing). */
8315 if (row >= screen_Rows || col >= screen_Columns)
8316 return;
8317
Bram Moolenaar494838a2015-02-10 19:20:37 +01008318 /* Outputting a character in the last cell on the screen may scroll the
8319 * screen up. Only do it when the "xn" termcap property is set, otherwise
8320 * mark the character invalid (update it when scrolled up). */
8321 if (*T_XN == NUL
8322 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323#ifdef FEAT_RIGHTLEFT
8324 /* account for first command-line character in rightleft mode */
8325 && !cmdmsg_rl
8326#endif
8327 )
8328 {
8329 ScreenAttrs[off] = (sattr_T)-1;
8330 return;
8331 }
8332
8333 /*
8334 * Stop highlighting first, so it's easier to move the cursor.
8335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 if (screen_char_attr != 0)
8337 attr = screen_char_attr;
8338 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 attr = ScreenAttrs[off];
8340 if (screen_attr != attr)
8341 screen_stop_highlight();
8342
8343 windgoto(row, col);
8344
8345 if (screen_attr != attr)
8346 screen_start_highlight(attr);
8347
8348#ifdef FEAT_MBYTE
8349 if (enc_utf8 && ScreenLinesUC[off] != 0)
8350 {
8351 char_u buf[MB_MAXBYTES + 1];
8352
Bram Moolenaarcb070082016-04-02 22:14:51 +02008353 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008354 {
8355 if (*p_ambw == 'd'
8356# ifdef FEAT_GUI
8357 && !gui.in_use
8358# endif
8359 )
8360 {
8361 /* Clear the two screen cells. If the character is actually
8362 * single width it won't change the second cell. */
8363 out_str((char_u *)" ");
8364 term_windgoto(row, col);
8365 }
8366 /* not sure where the cursor is after drawing the ambiguous width
8367 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008368 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008369 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008370 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008372
8373 /* Convert the UTF-8 character to bytes and write it. */
8374 buf[utfc_char2bytes(off, buf)] = NUL;
8375 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 }
8377 else
8378#endif
8379 {
8380#ifdef FEAT_MBYTE
8381 out_flush_check();
8382#endif
8383 out_char(ScreenLines[off]);
8384#ifdef FEAT_MBYTE
8385 /* double-byte character in single-width cell */
8386 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8387 out_char(ScreenLines2[off]);
8388#endif
8389 }
8390
8391 screen_cur_col++;
8392}
8393
8394#ifdef FEAT_MBYTE
8395
8396/*
8397 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8398 * on the screen at position 'row' and 'col'.
8399 * The attributes of the first byte is used for all. This is required to
8400 * output the two bytes of a double-byte character with nothing in between.
8401 */
8402 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008403screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
8405 /* Check for illegal values (could be wrong when screen was resized). */
8406 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8407 return;
8408
8409 /* Outputting the last character on the screen may scrollup the screen.
8410 * Don't to it! Mark the character invalid (update it when scrolled up) */
8411 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8412 {
8413 ScreenAttrs[off] = (sattr_T)-1;
8414 return;
8415 }
8416
8417 /* Output the first byte normally (positions the cursor), then write the
8418 * second byte directly. */
8419 screen_char(off, row, col);
8420 out_char(ScreenLines[off + 1]);
8421 ++screen_cur_col;
8422}
8423#endif
8424
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425/*
8426 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8427 * This uses the contents of ScreenLines[] and doesn't change it.
8428 */
8429 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008430screen_draw_rectangle(
8431 int row,
8432 int col,
8433 int height,
8434 int width,
8435 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436{
8437 int r, c;
8438 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008439#ifdef FEAT_MBYTE
8440 int max_off;
8441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008443 /* Can't use ScreenLines unless initialized */
8444 if (ScreenLines == NULL)
8445 return;
8446
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 if (invert)
8448 screen_char_attr = HL_INVERSE;
8449 for (r = row; r < row + height; ++r)
8450 {
8451 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008452#ifdef FEAT_MBYTE
8453 max_off = off + screen_Columns;
8454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 for (c = col; c < col + width; ++c)
8456 {
8457#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008458 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 {
8460 screen_char_2(off + c, r, c);
8461 ++c;
8462 }
8463 else
8464#endif
8465 {
8466 screen_char(off + c, r, c);
8467#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008468 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 ++c;
8470#endif
8471 }
8472 }
8473 }
8474 screen_char_attr = 0;
8475}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477/*
8478 * Redraw the characters for a vertically split window.
8479 */
8480 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008481redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482{
8483 int col;
8484 int width;
8485
8486# ifdef FEAT_CLIPBOARD
8487 clip_may_clear_selection(row, end - 1);
8488# endif
8489
8490 if (wp == NULL)
8491 {
8492 col = 0;
8493 width = Columns;
8494 }
8495 else
8496 {
8497 col = wp->w_wincol;
8498 width = wp->w_width;
8499 }
8500 screen_draw_rectangle(row, col, end - row, width, FALSE);
8501}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008503 static void
8504space_to_screenline(int off, int attr)
8505{
8506 ScreenLines[off] = ' ';
8507 ScreenAttrs[off] = attr;
8508# ifdef FEAT_MBYTE
8509 if (enc_utf8)
8510 ScreenLinesUC[off] = 0;
8511# endif
8512}
8513
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514/*
8515 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8516 * with character 'c1' in first column followed by 'c2' in the other columns.
8517 * Use attributes 'attr'.
8518 */
8519 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008520screen_fill(
8521 int start_row,
8522 int end_row,
8523 int start_col,
8524 int end_col,
8525 int c1,
8526 int c2,
8527 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528{
8529 int row;
8530 int col;
8531 int off;
8532 int end_off;
8533 int did_delete;
8534 int c;
8535 int norm_term;
8536#if defined(FEAT_GUI) || defined(UNIX)
8537 int force_next = FALSE;
8538#endif
8539
8540 if (end_row > screen_Rows) /* safety check */
8541 end_row = screen_Rows;
8542 if (end_col > screen_Columns) /* safety check */
8543 end_col = screen_Columns;
8544 if (ScreenLines == NULL
8545 || start_row >= end_row
8546 || start_col >= end_col) /* nothing to do */
8547 return;
8548
8549 /* it's a "normal" terminal when not in a GUI or cterm */
8550 norm_term = (
8551#ifdef FEAT_GUI
8552 !gui.in_use &&
8553#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008554 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 for (row = start_row; row < end_row; ++row)
8556 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008557#ifdef FEAT_MBYTE
8558 if (has_mbyte
8559# ifdef FEAT_GUI
8560 && !gui.in_use
8561# endif
8562 )
8563 {
8564 /* When drawing over the right halve of a double-wide char clear
8565 * out the left halve. When drawing over the left halve of a
8566 * double wide-char clear out the right halve. Only needed in a
8567 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008568 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008569 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008570 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008571 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008572 }
8573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 /*
8575 * Try to use delete-line termcap code, when no attributes or in a
8576 * "normal" terminal, where a bold/italic space is just a
8577 * space.
8578 */
8579 did_delete = FALSE;
8580 if (c2 == ' '
8581 && end_col == Columns
8582 && can_clear(T_CE)
8583 && (attr == 0
8584 || (norm_term
8585 && attr <= HL_ALL
8586 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8587 {
8588 /*
8589 * check if we really need to clear something
8590 */
8591 col = start_col;
8592 if (c1 != ' ') /* don't clear first char */
8593 ++col;
8594
8595 off = LineOffset[row] + col;
8596 end_off = LineOffset[row] + end_col;
8597
8598 /* skip blanks (used often, keep it fast!) */
8599#ifdef FEAT_MBYTE
8600 if (enc_utf8)
8601 while (off < end_off && ScreenLines[off] == ' '
8602 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8603 ++off;
8604 else
8605#endif
8606 while (off < end_off && ScreenLines[off] == ' '
8607 && ScreenAttrs[off] == 0)
8608 ++off;
8609 if (off < end_off) /* something to be cleared */
8610 {
8611 col = off - LineOffset[row];
8612 screen_stop_highlight();
8613 term_windgoto(row, col);/* clear rest of this screen line */
8614 out_str(T_CE);
8615 screen_start(); /* don't know where cursor is now */
8616 col = end_col - col;
8617 while (col--) /* clear chars in ScreenLines */
8618 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008619 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 ++off;
8621 }
8622 }
8623 did_delete = TRUE; /* the chars are cleared now */
8624 }
8625
8626 off = LineOffset[row] + start_col;
8627 c = c1;
8628 for (col = start_col; col < end_col; ++col)
8629 {
8630 if (ScreenLines[off] != c
8631#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008632 || (enc_utf8 && (int)ScreenLinesUC[off]
8633 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634#endif
8635 || ScreenAttrs[off] != attr
8636#if defined(FEAT_GUI) || defined(UNIX)
8637 || force_next
8638#endif
8639 )
8640 {
8641#if defined(FEAT_GUI) || defined(UNIX)
8642 /* The bold trick may make a single row of pixels appear in
8643 * the next character. When a bold character is removed, the
8644 * next character should be redrawn too. This happens for our
8645 * own GUI and for some xterms. */
8646 if (
8647# ifdef FEAT_GUI
8648 gui.in_use
8649# endif
8650# if defined(FEAT_GUI) && defined(UNIX)
8651 ||
8652# endif
8653# ifdef UNIX
8654 term_is_xterm
8655# endif
8656 )
8657 {
8658 if (ScreenLines[off] != ' '
8659 && (ScreenAttrs[off] > HL_ALL
8660 || ScreenAttrs[off] & HL_BOLD))
8661 force_next = TRUE;
8662 else
8663 force_next = FALSE;
8664 }
8665#endif
8666 ScreenLines[off] = c;
8667#ifdef FEAT_MBYTE
8668 if (enc_utf8)
8669 {
8670 if (c >= 0x80)
8671 {
8672 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008673 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 }
8675 else
8676 ScreenLinesUC[off] = 0;
8677 }
8678#endif
8679 ScreenAttrs[off] = attr;
8680 if (!did_delete || c != ' ')
8681 screen_char(off, row, col);
8682 }
8683 ++off;
8684 if (col == start_col)
8685 {
8686 if (did_delete)
8687 break;
8688 c = c2;
8689 }
8690 }
8691 if (end_col == Columns)
8692 LineWraps[row] = FALSE;
8693 if (row == Rows - 1) /* overwritten the command line */
8694 {
8695 redraw_cmdline = TRUE;
8696 if (c1 == ' ' && c2 == ' ')
8697 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008698 if (start_col == 0)
8699 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 }
8701 }
8702}
8703
8704/*
8705 * Check if there should be a delay. Used before clearing or redrawing the
8706 * screen or the command line.
8707 */
8708 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008709check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710{
8711 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8712 && !did_wait_return
8713 && emsg_silent == 0)
8714 {
8715 out_flush();
8716 ui_delay(1000L, TRUE);
8717 emsg_on_display = FALSE;
8718 if (check_msg_scroll)
8719 msg_scroll = FALSE;
8720 }
8721}
8722
8723/*
8724 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008725 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 * Returns TRUE if there is a valid screen to write to.
8727 * Returns FALSE when starting up and screen not initialized yet.
8728 */
8729 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008730screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008732 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 return (ScreenLines != NULL);
8734}
8735
8736/*
8737 * Resize the shell to Rows and Columns.
8738 * Allocate ScreenLines[] and associated items.
8739 *
8740 * There may be some time between setting Rows and Columns and (re)allocating
8741 * ScreenLines[]. This happens when starting up and when (manually) changing
8742 * the shell size. Always use screen_Rows and screen_Columns to access items
8743 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8744 * final size of the shell is needed.
8745 */
8746 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008747screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
8749 int new_row, old_row;
8750#ifdef FEAT_GUI
8751 int old_Rows;
8752#endif
8753 win_T *wp;
8754 int outofmem = FALSE;
8755 int len;
8756 schar_T *new_ScreenLines;
8757#ifdef FEAT_MBYTE
8758 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008759 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008761 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762#endif
8763 sattr_T *new_ScreenAttrs;
8764 unsigned *new_LineOffset;
8765 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008766 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008767 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008769 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008770 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008772retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 /*
8774 * Allocation of the screen buffers is done only when the size changes and
8775 * when Rows and Columns have been set and we have started doing full
8776 * screen stuff.
8777 */
8778 if ((ScreenLines != NULL
8779 && Rows == screen_Rows
8780 && Columns == screen_Columns
8781#ifdef FEAT_MBYTE
8782 && enc_utf8 == (ScreenLinesUC != NULL)
8783 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008784 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785#endif
8786 )
8787 || Rows == 0
8788 || Columns == 0
8789 || (!full_screen && ScreenLines == NULL))
8790 return;
8791
8792 /*
8793 * It's possible that we produce an out-of-memory message below, which
8794 * will cause this function to be called again. To break the loop, just
8795 * return here.
8796 */
8797 if (entered)
8798 return;
8799 entered = TRUE;
8800
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008801 /*
8802 * Note that the window sizes are updated before reallocating the arrays,
8803 * thus we must not redraw here!
8804 */
8805 ++RedrawingDisabled;
8806
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 win_new_shellsize(); /* fit the windows in the new sized shell */
8808
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 comp_col(); /* recompute columns for shown command and ruler */
8810
8811 /*
8812 * We're changing the size of the screen.
8813 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8814 * - Move lines from the old arrays into the new arrays, clear extra
8815 * lines (unless the screen is going to be cleared).
8816 * - Free the old arrays.
8817 *
8818 * If anything fails, make ScreenLines NULL, so we don't do anything!
8819 * Continuing with the old ScreenLines may result in a crash, because the
8820 * size is wrong.
8821 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008822 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008824 if (aucmd_win != NULL)
8825 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826
8827 new_ScreenLines = (schar_T *)lalloc((long_u)(
8828 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8829#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008830 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 if (enc_utf8)
8832 {
8833 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8834 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008835 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008836 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8838 }
8839 if (enc_dbcs == DBCS_JPNU)
8840 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8841 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8842#endif
8843 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8844 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8845 new_LineOffset = (unsigned *)lalloc((long_u)(
8846 Rows * sizeof(unsigned)), FALSE);
8847 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008848 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008850 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 {
8852 if (win_alloc_lines(wp) == FAIL)
8853 {
8854 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008855 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 }
8857 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008858 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8859 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008860 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008861give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008863#ifdef FEAT_MBYTE
8864 for (i = 0; i < p_mco; ++i)
8865 if (new_ScreenLinesC[i] == NULL)
8866 break;
8867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 if (new_ScreenLines == NULL
8869#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008870 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8872#endif
8873 || new_ScreenAttrs == NULL
8874 || new_LineOffset == NULL
8875 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008876 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 || outofmem)
8878 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008879 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008880 {
8881 /* guess the size */
8882 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8883
8884 /* Remember we did this to avoid getting outofmem messages over
8885 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008886 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008887 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008888 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008890 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008891 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008892 VIM_CLEAR(new_ScreenLinesC[i]);
8893 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008895 VIM_CLEAR(new_ScreenAttrs);
8896 VIM_CLEAR(new_LineOffset);
8897 VIM_CLEAR(new_LineWraps);
8898 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 }
8900 else
8901 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008902 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008903
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 for (new_row = 0; new_row < Rows; ++new_row)
8905 {
8906 new_LineOffset[new_row] = new_row * Columns;
8907 new_LineWraps[new_row] = FALSE;
8908
8909 /*
8910 * If the screen is not going to be cleared, copy as much as
8911 * possible from the old screen to the new one and clear the rest
8912 * (used when resizing the window at the "--more--" prompt or when
8913 * executing an external command, for the GUI).
8914 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008915 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 {
8917 (void)vim_memset(new_ScreenLines + new_row * Columns,
8918 ' ', (size_t)Columns * sizeof(schar_T));
8919#ifdef FEAT_MBYTE
8920 if (enc_utf8)
8921 {
8922 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8923 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008924 for (i = 0; i < p_mco; ++i)
8925 (void)vim_memset(new_ScreenLinesC[i]
8926 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 0, (size_t)Columns * sizeof(u8char_T));
8928 }
8929 if (enc_dbcs == DBCS_JPNU)
8930 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8931 0, (size_t)Columns * sizeof(schar_T));
8932#endif
8933 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8934 0, (size_t)Columns * sizeof(sattr_T));
8935 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008936 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 {
8938 if (screen_Columns < Columns)
8939 len = screen_Columns;
8940 else
8941 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008942#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008943 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008944 * may be invalid now. Also when p_mco changes. */
8945 if (!(enc_utf8 && ScreenLinesUC == NULL)
8946 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008947#endif
8948 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8949 ScreenLines + LineOffset[old_row],
8950 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008952 if (enc_utf8 && ScreenLinesUC != NULL
8953 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 {
8955 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8956 ScreenLinesUC + LineOffset[old_row],
8957 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008958 for (i = 0; i < p_mco; ++i)
8959 mch_memmove(new_ScreenLinesC[i]
8960 + new_LineOffset[new_row],
8961 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 (size_t)len * sizeof(u8char_T));
8963 }
8964 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8965 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8966 ScreenLines2 + LineOffset[old_row],
8967 (size_t)len * sizeof(schar_T));
8968#endif
8969 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8970 ScreenAttrs + LineOffset[old_row],
8971 (size_t)len * sizeof(sattr_T));
8972 }
8973 }
8974 }
8975 /* Use the last line of the screen for the current line. */
8976 current_ScreenLine = new_ScreenLines + Rows * Columns;
8977 }
8978
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008979 free_screenlines();
8980
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 ScreenLines = new_ScreenLines;
8982#ifdef FEAT_MBYTE
8983 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008984 for (i = 0; i < p_mco; ++i)
8985 ScreenLinesC[i] = new_ScreenLinesC[i];
8986 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 ScreenLines2 = new_ScreenLines2;
8988#endif
8989 ScreenAttrs = new_ScreenAttrs;
8990 LineOffset = new_LineOffset;
8991 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008992 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993
8994 /* It's important that screen_Rows and screen_Columns reflect the actual
8995 * size of ScreenLines[]. Set them before calling anything. */
8996#ifdef FEAT_GUI
8997 old_Rows = screen_Rows;
8998#endif
8999 screen_Rows = Rows;
9000 screen_Columns = Columns;
9001
9002 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009003 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 screenclear2();
9005
9006#ifdef FEAT_GUI
9007 else if (gui.in_use
9008 && !gui.starting
9009 && ScreenLines != NULL
9010 && old_Rows != Rows)
9011 {
9012 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9013 /*
9014 * Adjust the position of the cursor, for when executing an external
9015 * command.
9016 */
9017 if (msg_row >= Rows) /* Rows got smaller */
9018 msg_row = Rows - 1; /* put cursor at last row */
9019 else if (Rows > old_Rows) /* Rows got bigger */
9020 msg_row += Rows - old_Rows; /* put cursor in same place */
9021 if (msg_col >= Columns) /* Columns got smaller */
9022 msg_col = Columns - 1; /* put cursor at last column */
9023 }
9024#endif
9025
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009027 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009028
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009029 /*
9030 * Do not apply autocommands more than 3 times to avoid an endless loop
9031 * in case applying autocommands always changes Rows or Columns.
9032 */
9033 if (starting == 0 && ++retry_count <= 3)
9034 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009035 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009036 /* In rare cases, autocommands may have altered Rows or Columns,
9037 * jump back to check if we need to allocate the screen again. */
9038 goto retry;
9039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040}
9041
9042 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009043free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009044{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009045#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009046 int i;
9047
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009048 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009049 for (i = 0; i < Screen_mco; ++i)
9050 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009051 vim_free(ScreenLines2);
9052#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009053 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009054 vim_free(ScreenAttrs);
9055 vim_free(LineOffset);
9056 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009057 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009058}
9059
9060 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009061screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062{
9063 check_for_delay(FALSE);
9064 screenalloc(FALSE); /* allocate screen buffers if size changed */
9065 screenclear2(); /* clear the screen */
9066}
9067
9068 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009069screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
9071 int i;
9072
9073 if (starting == NO_SCREEN || ScreenLines == NULL
9074#ifdef FEAT_GUI
9075 || (gui.in_use && gui.starting)
9076#endif
9077 )
9078 return;
9079
9080#ifdef FEAT_GUI
9081 if (!gui.in_use)
9082#endif
9083 screen_attr = -1; /* force setting the Normal colors */
9084 screen_stop_highlight(); /* don't want highlighting here */
9085
9086#ifdef FEAT_CLIPBOARD
9087 /* disable selection without redrawing it */
9088 clip_scroll_selection(9999);
9089#endif
9090
9091 /* blank out ScreenLines */
9092 for (i = 0; i < Rows; ++i)
9093 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009094 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 LineWraps[i] = FALSE;
9096 }
9097
9098 if (can_clear(T_CL))
9099 {
9100 out_str(T_CL); /* clear the display */
9101 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009102 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 }
9104 else
9105 {
9106 /* can't clear the screen, mark all chars with invalid attributes */
9107 for (i = 0; i < Rows; ++i)
9108 lineinvalid(LineOffset[i], (int)Columns);
9109 clear_cmdline = TRUE;
9110 }
9111
9112 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9113
9114 win_rest_invalid(firstwin);
9115 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009116 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 if (must_redraw == CLEAR) /* no need to clear again */
9118 must_redraw = NOT_VALID;
9119 compute_cmdrow();
9120 msg_row = cmdline_row; /* put cursor on last line for messages */
9121 msg_col = 0;
9122 screen_start(); /* don't know where cursor is now */
9123 msg_scrolled = 0; /* can't scroll back */
9124 msg_didany = FALSE;
9125 msg_didout = FALSE;
9126}
9127
9128/*
9129 * Clear one line in ScreenLines.
9130 */
9131 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009132lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133{
9134 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9135#ifdef FEAT_MBYTE
9136 if (enc_utf8)
9137 (void)vim_memset(ScreenLinesUC + off, 0,
9138 (size_t)width * sizeof(u8char_T));
9139#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009140 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141}
9142
9143/*
9144 * Mark one line in ScreenLines invalid by setting the attributes to an
9145 * invalid value.
9146 */
9147 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009148lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9151}
9152
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153/*
9154 * Copy part of a Screenline for vertically split window "wp".
9155 */
9156 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009157linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158{
9159 unsigned off_to = LineOffset[to] + wp->w_wincol;
9160 unsigned off_from = LineOffset[from] + wp->w_wincol;
9161
9162 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9163 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009164#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 if (enc_utf8)
9166 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009167 int i;
9168
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9170 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009171 for (i = 0; i < p_mco; ++i)
9172 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9173 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 }
9175 if (enc_dbcs == DBCS_JPNU)
9176 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9177 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9180 wp->w_width * sizeof(sattr_T));
9181}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182
9183/*
9184 * Return TRUE if clearing with term string "p" would work.
9185 * It can't work when the string is empty or it won't set the right background.
9186 */
9187 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009188can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189{
9190 return (*p != NUL && (t_colors <= 1
9191#ifdef FEAT_GUI
9192 || gui.in_use
9193#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009194#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009195 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009196 || (!p_tgc && cterm_normal_bg_color == 0)
9197#else
9198 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009199#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009200 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201}
9202
9203/*
9204 * Reset cursor position. Use whenever cursor was moved because of outputting
9205 * something directly to the screen (shell commands) or a terminal control
9206 * code.
9207 */
9208 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009209screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
9211 screen_cur_row = screen_cur_col = 9999;
9212}
9213
9214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 * Move the cursor to position "row","col" in the screen.
9216 * This tries to find the most efficient way to move, minimizing the number of
9217 * characters sent to the terminal.
9218 */
9219 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009220windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009222 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 int i;
9224 int plan;
9225 int cost;
9226 int wouldbe_col;
9227 int noinvcurs;
9228 char_u *bs;
9229 int goto_cost;
9230 int attr;
9231
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009232#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9234
9235#define PLAN_LE 1
9236#define PLAN_CR 2
9237#define PLAN_NL 3
9238#define PLAN_WRITE 4
9239 /* Can't use ScreenLines unless initialized */
9240 if (ScreenLines == NULL)
9241 return;
9242
9243 if (col != screen_cur_col || row != screen_cur_row)
9244 {
9245 /* Check for valid position. */
9246 if (row < 0) /* window without text lines? */
9247 row = 0;
9248 if (row >= screen_Rows)
9249 row = screen_Rows - 1;
9250 if (col >= screen_Columns)
9251 col = screen_Columns - 1;
9252
9253 /* check if no cursor movement is allowed in highlight mode */
9254 if (screen_attr && *T_MS == NUL)
9255 noinvcurs = HIGHL_COST;
9256 else
9257 noinvcurs = 0;
9258 goto_cost = GOTO_COST + noinvcurs;
9259
9260 /*
9261 * Plan how to do the positioning:
9262 * 1. Use CR to move it to column 0, same row.
9263 * 2. Use T_LE to move it a few columns to the left.
9264 * 3. Use NL to move a few lines down, column 0.
9265 * 4. Move a few columns to the right with T_ND or by writing chars.
9266 *
9267 * Don't do this if the cursor went beyond the last column, the cursor
9268 * position is unknown then (some terminals wrap, some don't )
9269 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009270 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 * characters to move the cursor to the right.
9272 */
9273 if (row >= screen_cur_row && screen_cur_col < Columns)
9274 {
9275 /*
9276 * If the cursor is in the same row, bigger col, we can use CR
9277 * or T_LE.
9278 */
9279 bs = NULL; /* init for GCC */
9280 attr = screen_attr;
9281 if (row == screen_cur_row && col < screen_cur_col)
9282 {
9283 /* "le" is preferred over "bc", because "bc" is obsolete */
9284 if (*T_LE)
9285 bs = T_LE; /* "cursor left" */
9286 else
9287 bs = T_BC; /* "backspace character (old) */
9288 if (*bs)
9289 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9290 else
9291 cost = 999;
9292 if (col + 1 < cost) /* using CR is less characters */
9293 {
9294 plan = PLAN_CR;
9295 wouldbe_col = 0;
9296 cost = 1; /* CR is just one character */
9297 }
9298 else
9299 {
9300 plan = PLAN_LE;
9301 wouldbe_col = col;
9302 }
9303 if (noinvcurs) /* will stop highlighting */
9304 {
9305 cost += noinvcurs;
9306 attr = 0;
9307 }
9308 }
9309
9310 /*
9311 * If the cursor is above where we want to be, we can use CR LF.
9312 */
9313 else if (row > screen_cur_row)
9314 {
9315 plan = PLAN_NL;
9316 wouldbe_col = 0;
9317 cost = (row - screen_cur_row) * 2; /* CR LF */
9318 if (noinvcurs) /* will stop highlighting */
9319 {
9320 cost += noinvcurs;
9321 attr = 0;
9322 }
9323 }
9324
9325 /*
9326 * If the cursor is in the same row, smaller col, just use write.
9327 */
9328 else
9329 {
9330 plan = PLAN_WRITE;
9331 wouldbe_col = screen_cur_col;
9332 cost = 0;
9333 }
9334
9335 /*
9336 * Check if any characters that need to be written have the
9337 * correct attributes. Also avoid UTF-8 characters.
9338 */
9339 i = col - wouldbe_col;
9340 if (i > 0)
9341 cost += i;
9342 if (cost < goto_cost && i > 0)
9343 {
9344 /*
9345 * Check if the attributes are correct without additionally
9346 * stopping highlighting.
9347 */
9348 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9349 while (i && *p++ == attr)
9350 --i;
9351 if (i != 0)
9352 {
9353 /*
9354 * Try if it works when highlighting is stopped here.
9355 */
9356 if (*--p == 0)
9357 {
9358 cost += noinvcurs;
9359 while (i && *p++ == 0)
9360 --i;
9361 }
9362 if (i != 0)
9363 cost = 999; /* different attributes, don't do it */
9364 }
9365#ifdef FEAT_MBYTE
9366 if (enc_utf8)
9367 {
9368 /* Don't use an UTF-8 char for positioning, it's slow. */
9369 for (i = wouldbe_col; i < col; ++i)
9370 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9371 {
9372 cost = 999;
9373 break;
9374 }
9375 }
9376#endif
9377 }
9378
9379 /*
9380 * We can do it without term_windgoto()!
9381 */
9382 if (cost < goto_cost)
9383 {
9384 if (plan == PLAN_LE)
9385 {
9386 if (noinvcurs)
9387 screen_stop_highlight();
9388 while (screen_cur_col > col)
9389 {
9390 out_str(bs);
9391 --screen_cur_col;
9392 }
9393 }
9394 else if (plan == PLAN_CR)
9395 {
9396 if (noinvcurs)
9397 screen_stop_highlight();
9398 out_char('\r');
9399 screen_cur_col = 0;
9400 }
9401 else if (plan == PLAN_NL)
9402 {
9403 if (noinvcurs)
9404 screen_stop_highlight();
9405 while (screen_cur_row < row)
9406 {
9407 out_char('\n');
9408 ++screen_cur_row;
9409 }
9410 screen_cur_col = 0;
9411 }
9412
9413 i = col - screen_cur_col;
9414 if (i > 0)
9415 {
9416 /*
9417 * Use cursor-right if it's one character only. Avoids
9418 * removing a line of pixels from the last bold char, when
9419 * using the bold trick in the GUI.
9420 */
9421 if (T_ND[0] != NUL && T_ND[1] == NUL)
9422 {
9423 while (i-- > 0)
9424 out_char(*T_ND);
9425 }
9426 else
9427 {
9428 int off;
9429
9430 off = LineOffset[row] + screen_cur_col;
9431 while (i-- > 0)
9432 {
9433 if (ScreenAttrs[off] != screen_attr)
9434 screen_stop_highlight();
9435#ifdef FEAT_MBYTE
9436 out_flush_check();
9437#endif
9438 out_char(ScreenLines[off]);
9439#ifdef FEAT_MBYTE
9440 if (enc_dbcs == DBCS_JPNU
9441 && ScreenLines[off] == 0x8e)
9442 out_char(ScreenLines2[off]);
9443#endif
9444 ++off;
9445 }
9446 }
9447 }
9448 }
9449 }
9450 else
9451 cost = 999;
9452
9453 if (cost >= goto_cost)
9454 {
9455 if (noinvcurs)
9456 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009457 if (row == screen_cur_row && (col > screen_cur_col)
9458 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 term_cursor_right(col - screen_cur_col);
9460 else
9461 term_windgoto(row, col);
9462 }
9463 screen_cur_row = row;
9464 screen_cur_col = col;
9465 }
9466}
9467
9468/*
9469 * Set cursor to its position in the current window.
9470 */
9471 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009472setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009474 setcursor_mayforce(FALSE);
9475}
9476
9477/*
9478 * Set cursor to its position in the current window.
9479 * When "force" is TRUE also when not redrawing.
9480 */
9481 void
9482setcursor_mayforce(int force)
9483{
9484 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 {
9486 validate_cursor();
9487 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009488 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009490 /* With 'rightleft' set and the cursor on a double-wide
9491 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009492 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009494 (has_mbyte
9495 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9496 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497# endif
9498 1)) :
9499#endif
9500 curwin->w_wcol));
9501 }
9502}
9503
9504
9505/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009506 * Insert 'line_count' lines at 'row' in window 'wp'.
9507 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9508 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 * scrolling.
9510 * Returns FAIL if the lines are not inserted, OK for success.
9511 */
9512 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009513win_ins_lines(
9514 win_T *wp,
9515 int row,
9516 int line_count,
9517 int invalid,
9518 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519{
9520 int did_delete;
9521 int nextrow;
9522 int lastrow;
9523 int retval;
9524
9525 if (invalid)
9526 wp->w_lines_valid = 0;
9527
9528 if (wp->w_height < 5)
9529 return FAIL;
9530
9531 if (line_count > wp->w_height - row)
9532 line_count = wp->w_height - row;
9533
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009534 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 if (retval != MAYBE)
9536 return retval;
9537
9538 /*
9539 * If there is a next window or a status line, we first try to delete the
9540 * lines at the bottom to avoid messing what is after the window.
9541 * If this fails and there are following windows, don't do anything to avoid
9542 * messing up those windows, better just redraw.
9543 */
9544 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 if (wp->w_next != NULL || wp->w_status_height)
9546 {
9547 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009548 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 did_delete = TRUE;
9550 else if (wp->w_next)
9551 return FAIL;
9552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 /*
9554 * if no lines deleted, blank the lines that will end up below the window
9555 */
9556 if (!did_delete)
9557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009560 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 lastrow = nextrow + line_count;
9562 if (lastrow > Rows)
9563 lastrow = Rows;
9564 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009565 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 ' ', ' ', 0);
9567 }
9568
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009569 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 == FAIL)
9571 {
9572 /* deletion will have messed up other windows */
9573 if (did_delete)
9574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 win_rest_invalid(W_NEXT(wp));
9577 }
9578 return FAIL;
9579 }
9580
9581 return OK;
9582}
9583
9584/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009585 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9587 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9588 * scrolling
9589 * Return OK for success, FAIL if the lines are not deleted.
9590 */
9591 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009592win_del_lines(
9593 win_T *wp,
9594 int row,
9595 int line_count,
9596 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009597 int mayclear,
9598 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599{
9600 int retval;
9601
9602 if (invalid)
9603 wp->w_lines_valid = 0;
9604
9605 if (line_count > wp->w_height - row)
9606 line_count = wp->w_height - row;
9607
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009608 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 if (retval != MAYBE)
9610 return retval;
9611
9612 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009613 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 return FAIL;
9615
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 /*
9617 * If there are windows or status lines below, try to put them at the
9618 * correct place. If we can't do that, they have to be redrawn.
9619 */
9620 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9621 {
9622 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009623 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 {
9625 wp->w_redr_status = TRUE;
9626 win_rest_invalid(wp->w_next);
9627 }
9628 }
9629 /*
9630 * If this is the last window and there is no status line, redraw the
9631 * command line later.
9632 */
9633 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 redraw_cmdline = TRUE;
9635 return OK;
9636}
9637
9638/*
9639 * Common code for win_ins_lines() and win_del_lines().
9640 * Returns OK or FAIL when the work has been done.
9641 * Returns MAYBE when not finished yet.
9642 */
9643 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009644win_do_lines(
9645 win_T *wp,
9646 int row,
9647 int line_count,
9648 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009649 int del,
9650 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651{
9652 int retval;
9653
9654 if (!redrawing() || line_count <= 0)
9655 return FAIL;
9656
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009657 /* When inserting lines would result in loss of command output, just redraw
9658 * the lines. */
9659 if (no_win_do_lines_ins && !del)
9660 return FAIL;
9661
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009663 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009665 if (!no_win_do_lines_ins)
9666 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 return FAIL;
9668 }
9669
9670 /*
9671 * Delete all remaining lines
9672 */
9673 if (row + line_count >= wp->w_height)
9674 {
9675 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009676 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 ' ', ' ', 0);
9678 return OK;
9679 }
9680
9681 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009682 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009684 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009686 if (!no_win_do_lines_ins)
9687 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688
9689 /*
9690 * If the terminal can set a scroll region, use that.
9691 * Always do this in a vertically split window. This will redraw from
9692 * ScreenLines[] when t_CV isn't defined. That's faster than using
9693 * win_line().
9694 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009695 * a character in the lower right corner of the scroll region may cause a
9696 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009698 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 scroll_region_set(wp, row);
9702 if (del)
9703 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009704 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 else
9706 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009707 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 scroll_region_reset();
9710 return retval;
9711 }
9712
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9714 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715
9716 return MAYBE;
9717}
9718
9719/*
9720 * window 'wp' and everything after it is messed up, mark it for redraw
9721 */
9722 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009723win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 {
9727 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 wp->w_redr_status = TRUE;
9729 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 }
9731 redraw_cmdline = TRUE;
9732}
9733
9734/*
9735 * The rest of the routines in this file perform screen manipulations. The
9736 * given operation is performed physically on the screen. The corresponding
9737 * change is also made to the internal screen image. In this way, the editor
9738 * anticipates the effect of editing changes on the appearance of the screen.
9739 * That way, when we call screenupdate a complete redraw isn't usually
9740 * necessary. Another advantage is that we can keep adding code to anticipate
9741 * screen changes, and in the meantime, everything still works.
9742 */
9743
9744/*
9745 * types for inserting or deleting lines
9746 */
9747#define USE_T_CAL 1
9748#define USE_T_CDL 2
9749#define USE_T_AL 3
9750#define USE_T_CE 4
9751#define USE_T_DL 5
9752#define USE_T_SR 6
9753#define USE_NL 7
9754#define USE_T_CD 8
9755#define USE_REDRAW 9
9756
9757/*
9758 * insert lines on the screen and update ScreenLines[]
9759 * 'end' is the line after the scrolled part. Normally it is Rows.
9760 * When scrolling region used 'off' is the offset from the top for the region.
9761 * 'row' and 'end' are relative to the start of the region.
9762 *
9763 * return FAIL for failure, OK for success.
9764 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009765 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009766screen_ins_lines(
9767 int off,
9768 int row,
9769 int line_count,
9770 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009771 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009772 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773{
9774 int i;
9775 int j;
9776 unsigned temp;
9777 int cursor_row;
9778 int type;
9779 int result_empty;
9780 int can_ce = can_clear(T_CE);
9781
9782 /*
9783 * FAIL if
9784 * - there is no valid screen
9785 * - the screen has to be redrawn completely
9786 * - the line count is less than one
9787 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009788 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009790 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9791#ifdef FEAT_CLIPBOARD
9792 || (clip_star.state != SELECT_CLEARED
9793 && redrawing_for_callback > 0)
9794#endif
9795 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 return FAIL;
9797
9798 /*
9799 * There are seven ways to insert lines:
9800 * 0. When in a vertically split window and t_CV isn't set, redraw the
9801 * characters from ScreenLines[].
9802 * 1. Use T_CD (clear to end of display) if it exists and the result of
9803 * the insert is just empty lines
9804 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9805 * present or line_count > 1. It looks better if we do all the inserts
9806 * at once.
9807 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9808 * insert is just empty lines and T_CE is not present or line_count >
9809 * 1.
9810 * 4. Use T_AL (insert line) if it exists.
9811 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9812 * just empty lines.
9813 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9814 * just empty lines.
9815 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9816 * the 'da' flag is not set or we have clear line capability.
9817 * 8. redraw the characters from ScreenLines[].
9818 *
9819 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9820 * the scrollbar for the window. It does have insert line, use that if it
9821 * exists.
9822 */
9823 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9825 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009826 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 type = USE_T_CD;
9828 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9829 type = USE_T_CAL;
9830 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9831 type = USE_T_CDL;
9832 else if (*T_AL != NUL)
9833 type = USE_T_AL;
9834 else if (can_ce && result_empty)
9835 type = USE_T_CE;
9836 else if (*T_DL != NUL && result_empty)
9837 type = USE_T_DL;
9838 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9839 type = USE_T_SR;
9840 else
9841 return FAIL;
9842
9843 /*
9844 * For clearing the lines screen_del_lines() is used. This will also take
9845 * care of t_db if necessary.
9846 */
9847 if (type == USE_T_CD || type == USE_T_CDL ||
9848 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009849 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850
9851 /*
9852 * If text is retained below the screen, first clear or delete as many
9853 * lines at the bottom of the window as are about to be inserted so that
9854 * the deleted lines won't later surface during a screen_del_lines.
9855 */
9856 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009857 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858
9859#ifdef FEAT_CLIPBOARD
9860 /* Remove a modeless selection when inserting lines halfway the screen
9861 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009862 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009863 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 else
9865 clip_scroll_selection(-line_count);
9866#endif
9867
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868#ifdef FEAT_GUI
9869 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9870 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009871 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872#endif
9873
9874 if (*T_CCS != NUL) /* cursor relative to region */
9875 cursor_row = row;
9876 else
9877 cursor_row = row + off;
9878
9879 /*
9880 * Shift LineOffset[] line_count down to reflect the inserted lines.
9881 * Clear the inserted lines in ScreenLines[].
9882 */
9883 row += off;
9884 end += off;
9885 for (i = 0; i < line_count; ++i)
9886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 if (wp != NULL && wp->w_width != Columns)
9888 {
9889 /* need to copy part of a line */
9890 j = end - 1 - i;
9891 while ((j -= line_count) >= row)
9892 linecopy(j + line_count, j, wp);
9893 j += line_count;
9894 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009895 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9896 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 else
9898 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9899 LineWraps[j] = FALSE;
9900 }
9901 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 {
9903 j = end - 1 - i;
9904 temp = LineOffset[j];
9905 while ((j -= line_count) >= row)
9906 {
9907 LineOffset[j + line_count] = LineOffset[j];
9908 LineWraps[j + line_count] = LineWraps[j];
9909 }
9910 LineOffset[j + line_count] = temp;
9911 LineWraps[j + line_count] = FALSE;
9912 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009913 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 else
9915 lineinvalid(temp, (int)Columns);
9916 }
9917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918
9919 screen_stop_highlight();
9920 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009921 if (clear_attr != 0)
9922 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 /* redraw the characters */
9925 if (type == USE_REDRAW)
9926 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009927 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 {
9929 term_append_lines(line_count);
9930 screen_start(); /* don't know where cursor is now */
9931 }
9932 else
9933 {
9934 for (i = 0; i < line_count; i++)
9935 {
9936 if (type == USE_T_AL)
9937 {
9938 if (i && cursor_row != 0)
9939 windgoto(cursor_row, 0);
9940 out_str(T_AL);
9941 }
9942 else /* type == USE_T_SR */
9943 out_str(T_SR);
9944 screen_start(); /* don't know where cursor is now */
9945 }
9946 }
9947
9948 /*
9949 * With scroll-reverse and 'da' flag set we need to clear the lines that
9950 * have been scrolled down into the region.
9951 */
9952 if (type == USE_T_SR && *T_DA)
9953 {
9954 for (i = 0; i < line_count; ++i)
9955 {
9956 windgoto(off + i, 0);
9957 out_str(T_CE);
9958 screen_start(); /* don't know where cursor is now */
9959 }
9960 }
9961
9962#ifdef FEAT_GUI
9963 gui_can_update_cursor();
9964 if (gui.in_use)
9965 out_flush(); /* always flush after a scroll */
9966#endif
9967 return OK;
9968}
9969
9970/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009971 * Delete lines on the screen and update ScreenLines[].
9972 * "end" is the line after the scrolled part. Normally it is Rows.
9973 * When scrolling region used "off" is the offset from the top for the region.
9974 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 *
9976 * Return OK for success, FAIL if the lines are not deleted.
9977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009979screen_del_lines(
9980 int off,
9981 int row,
9982 int line_count,
9983 int end,
9984 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009985 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009986 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987{
9988 int j;
9989 int i;
9990 unsigned temp;
9991 int cursor_row;
9992 int cursor_end;
9993 int result_empty; /* result is empty until end of region */
9994 int can_delete; /* deleting line codes can be used */
9995 int type;
9996
9997 /*
9998 * FAIL if
9999 * - there is no valid screen
10000 * - the screen has to be redrawn completely
10001 * - the line count is less than one
10002 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010003 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010005 if (!screen_valid(TRUE) || line_count <= 0
10006 || (!force && line_count > p_ttyscroll)
10007#ifdef FEAT_CLIPBOARD
10008 || (clip_star.state != SELECT_CLEARED
10009 && redrawing_for_callback > 0)
10010#endif
10011 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 return FAIL;
10013
10014 /*
10015 * Check if the rest of the current region will become empty.
10016 */
10017 result_empty = row + line_count >= end;
10018
10019 /*
10020 * We can delete lines only when 'db' flag not set or when 'ce' option
10021 * available.
10022 */
10023 can_delete = (*T_DB == NUL || can_clear(T_CE));
10024
10025 /*
10026 * There are six ways to delete lines:
10027 * 0. When in a vertically split window and t_CV isn't set, redraw the
10028 * characters from ScreenLines[].
10029 * 1. Use T_CD if it exists and the result is empty.
10030 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10031 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10032 * none of the other ways work.
10033 * 4. Use T_CE (erase line) if the result is empty.
10034 * 5. Use T_DL (delete line) if it exists.
10035 * 6. redraw the characters from ScreenLines[].
10036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10038 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010039 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 type = USE_T_CD;
10041#if defined(__BEOS__) && defined(BEOS_DR8)
10042 /*
10043 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10044 * its internal termcap... this works okay for tests which test *T_DB !=
10045 * NUL. It has the disadvantage that the user cannot use any :set t_*
10046 * command to get T_DB (back) to empty_option, only :set term=... will do
10047 * the trick...
10048 * Anyway, this hack will hopefully go away with the next OS release.
10049 * (Olaf Seibert)
10050 */
10051 else if (row == 0 && T_DB == empty_option
10052 && (line_count == 1 || *T_CDL == NUL))
10053#else
10054 else if (row == 0 && (
10055#ifndef AMIGA
10056 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10057 * up, so use delete-line command */
10058 line_count == 1 ||
10059#endif
10060 *T_CDL == NUL))
10061#endif
10062 type = USE_NL;
10063 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10064 type = USE_T_CDL;
10065 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010066 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 type = USE_T_CE;
10068 else if (*T_DL != NUL && can_delete)
10069 type = USE_T_DL;
10070 else if (*T_CDL != NUL && can_delete)
10071 type = USE_T_CDL;
10072 else
10073 return FAIL;
10074
10075#ifdef FEAT_CLIPBOARD
10076 /* Remove a modeless selection when deleting lines halfway the screen or
10077 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010078 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010079 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 else
10081 clip_scroll_selection(line_count);
10082#endif
10083
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084#ifdef FEAT_GUI
10085 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10086 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010087 gui_dont_update_cursor(gui.cursor_row >= row + off
10088 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089#endif
10090
10091 if (*T_CCS != NUL) /* cursor relative to region */
10092 {
10093 cursor_row = row;
10094 cursor_end = end;
10095 }
10096 else
10097 {
10098 cursor_row = row + off;
10099 cursor_end = end + off;
10100 }
10101
10102 /*
10103 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10104 * Clear the inserted lines in ScreenLines[].
10105 */
10106 row += off;
10107 end += off;
10108 for (i = 0; i < line_count; ++i)
10109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 if (wp != NULL && wp->w_width != Columns)
10111 {
10112 /* need to copy part of a line */
10113 j = row + i;
10114 while ((j += line_count) <= end - 1)
10115 linecopy(j - line_count, j, wp);
10116 j -= line_count;
10117 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010118 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10119 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 else
10121 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10122 LineWraps[j] = FALSE;
10123 }
10124 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 {
10126 /* whole width, moving the line pointers is faster */
10127 j = row + i;
10128 temp = LineOffset[j];
10129 while ((j += line_count) <= end - 1)
10130 {
10131 LineOffset[j - line_count] = LineOffset[j];
10132 LineWraps[j - line_count] = LineWraps[j];
10133 }
10134 LineOffset[j - line_count] = temp;
10135 LineWraps[j - line_count] = FALSE;
10136 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010137 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 else
10139 lineinvalid(temp, (int)Columns);
10140 }
10141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010143 if (screen_attr != clear_attr)
10144 screen_stop_highlight();
10145 if (clear_attr != 0)
10146 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 /* redraw the characters */
10149 if (type == USE_REDRAW)
10150 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010151 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 {
10153 windgoto(cursor_row, 0);
10154 out_str(T_CD);
10155 screen_start(); /* don't know where cursor is now */
10156 }
10157 else if (type == USE_T_CDL)
10158 {
10159 windgoto(cursor_row, 0);
10160 term_delete_lines(line_count);
10161 screen_start(); /* don't know where cursor is now */
10162 }
10163 /*
10164 * Deleting lines at top of the screen or scroll region: Just scroll
10165 * the whole screen (scroll region) up by outputting newlines on the
10166 * last line.
10167 */
10168 else if (type == USE_NL)
10169 {
10170 windgoto(cursor_end - 1, 0);
10171 for (i = line_count; --i >= 0; )
10172 out_char('\n'); /* cursor will remain on same line */
10173 }
10174 else
10175 {
10176 for (i = line_count; --i >= 0; )
10177 {
10178 if (type == USE_T_DL)
10179 {
10180 windgoto(cursor_row, 0);
10181 out_str(T_DL); /* delete a line */
10182 }
10183 else /* type == USE_T_CE */
10184 {
10185 windgoto(cursor_row + i, 0);
10186 out_str(T_CE); /* erase a line */
10187 }
10188 screen_start(); /* don't know where cursor is now */
10189 }
10190 }
10191
10192 /*
10193 * If the 'db' flag is set, we need to clear the lines that have been
10194 * scrolled up at the bottom of the region.
10195 */
10196 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10197 {
10198 for (i = line_count; i > 0; --i)
10199 {
10200 windgoto(cursor_end - i, 0);
10201 out_str(T_CE); /* erase a line */
10202 screen_start(); /* don't know where cursor is now */
10203 }
10204 }
10205
10206#ifdef FEAT_GUI
10207 gui_can_update_cursor();
10208 if (gui.in_use)
10209 out_flush(); /* always flush after a scroll */
10210#endif
10211
10212 return OK;
10213}
10214
10215/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010216 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 *
10218 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10219 * If clear_cmdline is FALSE there may be a message there that needs to be
10220 * cleared only if a mode is shown.
10221 * Return the length of the message (0 if no message).
10222 */
10223 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010224showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225{
10226 int need_clear;
10227 int length = 0;
10228 int do_mode;
10229 int attr;
10230 int nwr_save;
10231#ifdef FEAT_INS_EXPAND
10232 int sub_attr;
10233#endif
10234
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010235 do_mode = ((p_smd && msg_silent == 0)
10236 && ((State & INSERT)
10237 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010238 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 if (do_mode || Recording)
10240 {
10241 /*
10242 * Don't show mode right now, when not redrawing or inside a mapping.
10243 * Call char_avail() only when we are going to show something, because
10244 * it takes a bit of time.
10245 */
10246 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10247 {
10248 redraw_cmdline = TRUE; /* show mode later */
10249 return 0;
10250 }
10251
10252 nwr_save = need_wait_return;
10253
10254 /* wait a bit before overwriting an important message */
10255 check_for_delay(FALSE);
10256
10257 /* if the cmdline is more than one line high, erase top lines */
10258 need_clear = clear_cmdline;
10259 if (clear_cmdline && cmdline_row < Rows - 1)
10260 msg_clr_cmdline(); /* will reset clear_cmdline */
10261
10262 /* Position on the last line in the window, column 0 */
10263 msg_pos_mode();
10264 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010265 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 if (do_mode)
10267 {
10268 MSG_PUTS_ATTR("--", attr);
10269#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010270 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010271# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010272 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010273# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010274 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010275# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010276 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010277# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 MSG_PUTS_ATTR(" IM", attr);
10279# else
10280 MSG_PUTS_ATTR(" XIM", attr);
10281# endif
10282#endif
10283#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10284 if (gui.in_use)
10285 {
10286 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010287 {
10288 /* HANGUL */
10289 if (enc_utf8)
10290 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10291 else
10292 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 }
10295#endif
10296#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010297 /* CTRL-X in Insert mode */
10298 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 {
10300 /* These messages can get long, avoid a wrap in a narrow
10301 * window. Prefer showing edit_submode_extra. */
10302 length = (Rows - msg_row) * Columns - 3;
10303 if (edit_submode_extra != NULL)
10304 length -= vim_strsize(edit_submode_extra);
10305 if (length > 0)
10306 {
10307 if (edit_submode_pre != NULL)
10308 length -= vim_strsize(edit_submode_pre);
10309 if (length - vim_strsize(edit_submode) > 0)
10310 {
10311 if (edit_submode_pre != NULL)
10312 msg_puts_attr(edit_submode_pre, attr);
10313 msg_puts_attr(edit_submode, attr);
10314 }
10315 if (edit_submode_extra != NULL)
10316 {
10317 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10318 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010319 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 else
10321 sub_attr = attr;
10322 msg_puts_attr(edit_submode_extra, sub_attr);
10323 }
10324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 }
10326 else
10327#endif
10328 {
10329#ifdef FEAT_VREPLACE
10330 if (State & VREPLACE_FLAG)
10331 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10332 else
10333#endif
10334 if (State & REPLACE_FLAG)
10335 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10336 else if (State & INSERT)
10337 {
10338#ifdef FEAT_RIGHTLEFT
10339 if (p_ri)
10340 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10341#endif
10342 MSG_PUTS_ATTR(_(" INSERT"), attr);
10343 }
10344 else if (restart_edit == 'I')
10345 MSG_PUTS_ATTR(_(" (insert)"), attr);
10346 else if (restart_edit == 'R')
10347 MSG_PUTS_ATTR(_(" (replace)"), attr);
10348 else if (restart_edit == 'V')
10349 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10350#ifdef FEAT_RIGHTLEFT
10351 if (p_hkmap)
10352 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10353# ifdef FEAT_FKMAP
10354 if (p_fkmap)
10355 MSG_PUTS_ATTR(farsi_text_5, attr);
10356# endif
10357#endif
10358#ifdef FEAT_KEYMAP
10359 if (State & LANGMAP)
10360 {
10361# ifdef FEAT_ARABIC
10362 if (curwin->w_p_arab)
10363 MSG_PUTS_ATTR(_(" Arabic"), attr);
10364 else
10365# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010366 if (get_keymap_str(curwin, (char_u *)" (%s)",
10367 NameBuff, MAXPATHL))
10368 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 }
10370#endif
10371 if ((State & INSERT) && p_paste)
10372 MSG_PUTS_ATTR(_(" (paste)"), attr);
10373
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 if (VIsual_active)
10375 {
10376 char *p;
10377
10378 /* Don't concatenate separate words to avoid translation
10379 * problems. */
10380 switch ((VIsual_select ? 4 : 0)
10381 + (VIsual_mode == Ctrl_V) * 2
10382 + (VIsual_mode == 'V'))
10383 {
10384 case 0: p = N_(" VISUAL"); break;
10385 case 1: p = N_(" VISUAL LINE"); break;
10386 case 2: p = N_(" VISUAL BLOCK"); break;
10387 case 4: p = N_(" SELECT"); break;
10388 case 5: p = N_(" SELECT LINE"); break;
10389 default: p = N_(" SELECT BLOCK"); break;
10390 }
10391 MSG_PUTS_ATTR(_(p), attr);
10392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 MSG_PUTS_ATTR(" --", attr);
10394 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010395
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 need_clear = TRUE;
10397 }
10398 if (Recording
10399#ifdef FEAT_INS_EXPAND
10400 && edit_submode == NULL /* otherwise it gets too long */
10401#endif
10402 )
10403 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010404 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 need_clear = TRUE;
10406 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010407
10408 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 if (need_clear || clear_cmdline)
10410 msg_clr_eos();
10411 msg_didout = FALSE; /* overwrite this message */
10412 length = msg_col;
10413 msg_col = 0;
10414 need_wait_return = nwr_save; /* never ask for hit-return for this */
10415 }
10416 else if (clear_cmdline && msg_silent == 0)
10417 /* Clear the whole command line. Will reset "clear_cmdline". */
10418 msg_clr_cmdline();
10419
10420#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 /* In Visual mode the size of the selected area must be redrawn. */
10422 if (VIsual_active)
10423 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424
10425 /* If the last window has no status line, the ruler is after the mode
10426 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010427 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 win_redr_ruler(lastwin, TRUE);
10429#endif
10430 redraw_cmdline = FALSE;
10431 clear_cmdline = FALSE;
10432
10433 return length;
10434}
10435
10436/*
10437 * Position for a mode message.
10438 */
10439 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010440msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441{
10442 msg_col = 0;
10443 msg_row = Rows - 1;
10444}
10445
10446/*
10447 * Delete mode message. Used when ESC is typed which is expected to end
10448 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010449 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 */
10451 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010452unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453{
10454 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010455 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 */
10457 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10458 redraw_cmdline = TRUE; /* delete mode later */
10459 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010460 clearmode();
10461}
10462
10463/*
10464 * Clear the mode message.
10465 */
10466 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010467clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010468{
10469 msg_pos_mode();
10470 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010471 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010472 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473}
10474
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010475 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010476recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010477{
10478 MSG_PUTS_ATTR(_("recording"), attr);
10479 if (!shortmess(SHM_RECORDING))
10480 {
10481 char_u s[4];
10482 sprintf((char *)s, " @%c", Recording);
10483 MSG_PUTS_ATTR(s, attr);
10484 }
10485}
10486
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010487/*
10488 * Draw the tab pages line at the top of the Vim window.
10489 */
10490 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010491draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010492{
10493 int tabcount = 0;
10494 tabpage_T *tp;
10495 int tabwidth;
10496 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010497 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010498 int attr;
10499 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010500 win_T *cwp;
10501 int wincount;
10502 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010503 int c;
10504 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010505 int attr_sel = HL_ATTR(HLF_TPS);
10506 int attr_nosel = HL_ATTR(HLF_TP);
10507 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010508 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010509 int room;
10510 int use_sep_chars = (t_colors < 8
10511#ifdef FEAT_GUI
10512 && !gui.in_use
10513#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010514#ifdef FEAT_TERMGUICOLORS
10515 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010516#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010517 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010518
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010519 if (ScreenLines == NULL)
10520 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010521 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010522
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010523#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010524 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010525 if (gui_use_tabline())
10526 {
10527 gui_update_tabline();
10528 return;
10529 }
10530#endif
10531
10532 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010533 return;
10534
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010535#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010536
10537 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10538 for (scol = 0; scol < Columns; ++scol)
10539 TabPageIdxs[scol] = 0;
10540
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010541 /* Use the 'tabline' option if it's set. */
10542 if (*p_tal != NUL)
10543 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010544 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010545
10546 /* Check for an error. If there is one we would loop in redrawing the
10547 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010548 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010549 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010550 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010551 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010552 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010553 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010554 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010555 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010556#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010557 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010558 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010559 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010560
Bram Moolenaar238a5642006-02-21 22:12:05 +000010561 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10562 if (tabwidth < 6)
10563 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010564
Bram Moolenaar238a5642006-02-21 22:12:05 +000010565 attr = attr_nosel;
10566 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010567 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010568 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10569 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010570 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010571 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010572
Bram Moolenaar238a5642006-02-21 22:12:05 +000010573 if (tp->tp_topframe == topframe)
10574 attr = attr_sel;
10575 if (use_sep_chars && col > 0)
10576 screen_putchar('|', 0, col++, attr);
10577
10578 if (tp->tp_topframe != topframe)
10579 attr = attr_nosel;
10580
10581 screen_putchar(' ', 0, col++, attr);
10582
10583 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010584 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010585 cwp = curwin;
10586 wp = firstwin;
10587 }
10588 else
10589 {
10590 cwp = tp->tp_curwin;
10591 wp = tp->tp_firstwin;
10592 }
10593
10594 modified = FALSE;
10595 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10596 if (bufIsChanged(wp->w_buffer))
10597 modified = TRUE;
10598 if (modified || wincount > 1)
10599 {
10600 if (wincount > 1)
10601 {
10602 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010603 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010604 if (col + len >= Columns - 3)
10605 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010606 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010607#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010608 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010609#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010610 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010611#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010612 );
10613 col += len;
10614 }
10615 if (modified)
10616 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10617 screen_putchar(' ', 0, col++, attr);
10618 }
10619
10620 room = scol - col + tabwidth - 1;
10621 if (room > 0)
10622 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010623 /* Get buffer name in NameBuff[] */
10624 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010625 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010626 len = vim_strsize(NameBuff);
10627 p = NameBuff;
10628#ifdef FEAT_MBYTE
10629 if (has_mbyte)
10630 while (len > room)
10631 {
10632 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010633 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010634 }
10635 else
10636#endif
10637 if (len > room)
10638 {
10639 p += len - room;
10640 len = room;
10641 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010642 if (len > Columns - col - 1)
10643 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010644
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010645 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010646 col += len;
10647 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010648 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010649
10650 /* Store the tab page number in TabPageIdxs[], so that
10651 * jump_to_mouse() knows where each one is. */
10652 ++tabcount;
10653 while (scol < col)
10654 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010655 }
10656
Bram Moolenaar238a5642006-02-21 22:12:05 +000010657 if (use_sep_chars)
10658 c = '_';
10659 else
10660 c = ' ';
10661 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010662
10663 /* Put an "X" for closing the current tab if there are several. */
10664 if (first_tabpage->tp_next != NULL)
10665 {
10666 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10667 TabPageIdxs[Columns - 1] = -999;
10668 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010669 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010670
10671 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10672 * set. */
10673 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010674}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010675
10676/*
10677 * Get buffer name for "buf" into NameBuff[].
10678 * Takes care of special buffer names and translates special characters.
10679 */
10680 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010681get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010682{
10683 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010684 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010685 else
10686 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10687 trans_characters(NameBuff, MAXPATHL);
10688}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010689
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690/*
10691 * Get the character to use in a status line. Get its attributes in "*attr".
10692 */
10693 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010694fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695{
10696 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010697
10698#ifdef FEAT_TERMINAL
10699 if (bt_terminal(wp->w_buffer))
10700 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010701 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010702 {
10703 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010704 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010705 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010706 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010707 {
10708 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010709 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010710 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010711 }
10712 else
10713#endif
10714 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010716 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 fill = fill_stl;
10718 }
10719 else
10720 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010721 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 fill = fill_stlnc;
10723 }
10724 /* Use fill when there is highlighting, and highlighting of current
10725 * window differs, or the fillchars differ, or this is not the
10726 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010727 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010728 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 || (fill_stl != fill_stlnc)))
10730 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010731 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 return '^';
10733 return '=';
10734}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736/*
10737 * Get the character to use in a separator between vertically split windows.
10738 * Get its attributes in "*attr".
10739 */
10740 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010741fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010743 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 if (*attr == 0 && fill_vert == ' ')
10745 return '|';
10746 else
10747 return fill_vert;
10748}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749
10750/*
10751 * Return TRUE if redrawing should currently be done.
10752 */
10753 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010754redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010756#ifdef FEAT_EVAL
10757 if (disable_redraw_for_testing)
10758 return 0;
10759 else
10760#endif
10761 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10763}
10764
10765/*
10766 * Return TRUE if printing messages should currently be done.
10767 */
10768 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010769messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770{
10771 return (!(p_lz && char_avail() && !KeyTyped));
10772}
10773
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010774#ifdef FEAT_MENU
10775/*
10776 * Draw the window toolbar.
10777 */
10778 static void
10779redraw_win_toolbar(win_T *wp)
10780{
10781 vimmenu_T *menu;
10782 int item_idx = 0;
10783 int item_count = 0;
10784 int col = 0;
10785 int next_col;
10786 int off = (int)(current_ScreenLine - ScreenLines);
10787 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10788 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10789
10790 vim_free(wp->w_winbar_items);
10791 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10792 ++item_count;
10793 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10794 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10795
10796 /* TODO: use fewer spaces if there is not enough room */
10797 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010798 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010799 {
10800 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010801 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010802 break;
10803 if (col > 1)
10804 {
10805 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010806 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010807 break;
10808 }
10809
10810 wp->w_winbar_items[item_idx].wb_startcol = col;
10811 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010812 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010813 break;
10814
10815 next_col = text_to_screenline(wp, menu->name, col);
10816 while (col < next_col)
10817 {
10818 ScreenAttrs[off + col] = button_attr;
10819 ++col;
10820 }
10821 wp->w_winbar_items[item_idx].wb_endcol = col;
10822 wp->w_winbar_items[item_idx].wb_menu = menu;
10823 ++item_idx;
10824
Bram Moolenaar02631462017-09-22 15:20:32 +020010825 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010826 break;
10827 space_to_screenline(off + col, button_attr);
10828 ++col;
10829 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010830 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010831 {
10832 space_to_screenline(off + col, fill_attr);
10833 ++col;
10834 }
10835 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10836
Bram Moolenaar02631462017-09-22 15:20:32 +020010837 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10838 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010839}
10840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841/*
10842 * Show current status info in ruler and various other places
10843 * If always is FALSE, only show ruler if position has changed.
10844 */
10845 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010846showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847{
10848 if (!always && !redrawing())
10849 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010850#ifdef FEAT_INS_EXPAND
10851 if (pum_visible())
10852 {
10853 /* Don't redraw right now, do it later. */
10854 curwin->w_redr_status = TRUE;
10855 return;
10856 }
10857#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010858#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010859 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010860 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010861 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 else
10864#endif
10865#ifdef FEAT_CMDL_INFO
10866 win_redr_ruler(curwin, always);
10867#endif
10868
10869#ifdef FEAT_TITLE
10870 if (need_maketitle
10871# ifdef FEAT_STL_OPT
10872 || (p_icon && (stl_syntax & STL_IN_ICON))
10873 || (p_title && (stl_syntax & STL_IN_TITLE))
10874# endif
10875 )
10876 maketitle();
10877#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010878 /* Redraw the tab pages line if needed. */
10879 if (redraw_tabline)
10880 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881}
10882
10883#ifdef FEAT_CMDL_INFO
10884 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010885win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010887#define RULER_BUF_LEN 70
10888 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 int row;
10890 int fillchar;
10891 int attr;
10892 int empty_line = FALSE;
10893 colnr_T virtcol;
10894 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010895 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 int this_ru_col;
10898 int off = 0;
10899 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900
10901 /* If 'ruler' off or redrawing disabled, don't do anything */
10902 if (!p_ru)
10903 return;
10904
10905 /*
10906 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10907 * after deleting lines, before cursor.lnum is corrected.
10908 */
10909 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10910 return;
10911
10912#ifdef FEAT_INS_EXPAND
10913 /* Don't draw the ruler while doing insert-completion, it might overwrite
10914 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 if (edit_submode != NULL)
10917 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010918 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10919 if (pum_visible())
10920 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921#endif
10922
10923#ifdef FEAT_STL_OPT
10924 if (*p_ruf)
10925 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010926 int save_called_emsg = called_emsg;
10927
10928 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010930 if (called_emsg)
10931 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010932 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010933 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 return;
10935 }
10936#endif
10937
10938 /*
10939 * Check if not in Insert mode and the line is empty (will show "0-1").
10940 */
10941 if (!(State & INSERT)
10942 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10943 empty_line = TRUE;
10944
10945 /*
10946 * Only draw the ruler when something changed.
10947 */
10948 validate_virtcol_win(wp);
10949 if ( redraw_cmdline
10950 || always
10951 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10952 || wp->w_cursor.col != wp->w_ru_cursor.col
10953 || wp->w_virtcol != wp->w_ru_virtcol
10954#ifdef FEAT_VIRTUALEDIT
10955 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10956#endif
10957 || wp->w_topline != wp->w_ru_topline
10958 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10959#ifdef FEAT_DIFF
10960 || wp->w_topfill != wp->w_ru_topfill
10961#endif
10962 || empty_line != wp->w_ru_empty)
10963 {
10964 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 if (wp->w_status_height)
10966 {
10967 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010968 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010969 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010970 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 }
10972 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 {
10974 row = Rows - 1;
10975 fillchar = ' ';
10976 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 width = Columns;
10978 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 }
10980
10981 /* In list mode virtcol needs to be recomputed */
10982 virtcol = wp->w_virtcol;
10983 if (wp->w_p_list && lcs_tab1 == NUL)
10984 {
10985 wp->w_p_list = FALSE;
10986 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10987 wp->w_p_list = TRUE;
10988 }
10989
10990 /*
10991 * Some sprintfs return the length, some return a pointer.
10992 * To avoid portability problems we use strlen() here.
10993 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010994 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10996 ? 0L
10997 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010998 len = STRLEN(buffer);
10999 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11001 (int)virtcol + 1);
11002
11003 /*
11004 * Add a "50%" if there is room for it.
11005 * On the last line, don't print in the last column (scrolls the
11006 * screen up on some terminals).
11007 */
11008 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011009 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 this_ru_col = ru_col - (Columns - width);
11014 if (this_ru_col < 0)
11015 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 /* Never use more than half the window/screen width, leave the other
11017 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011018 if (this_ru_col < (width + 1) / 2)
11019 this_ru_col = (width + 1) / 2;
11020 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011022 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011023 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 {
11025#ifdef FEAT_MBYTE
11026 if (has_mbyte)
11027 i += (*mb_char2bytes)(fillchar, buffer + i);
11028 else
11029#endif
11030 buffer[i++] = fillchar;
11031 ++o;
11032 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011033 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 }
11035 /* Truncate at window boundary. */
11036#ifdef FEAT_MBYTE
11037 if (has_mbyte)
11038 {
11039 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011040 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 {
11042 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011043 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 {
11045 buffer[i] = NUL;
11046 break;
11047 }
11048 }
11049 }
11050 else
11051#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011052 if (this_ru_col + (int)STRLEN(buffer) > width)
11053 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054
Bram Moolenaar4033c552017-09-16 20:54:51 +020011055 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 i = redraw_cmdline;
11057 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011058 this_ru_col + off + (int)STRLEN(buffer),
11059 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 fillchar, fillchar, attr);
11061 /* don't redraw the cmdline because of showing the ruler */
11062 redraw_cmdline = i;
11063 wp->w_ru_cursor = wp->w_cursor;
11064 wp->w_ru_virtcol = wp->w_virtcol;
11065 wp->w_ru_empty = empty_line;
11066 wp->w_ru_topline = wp->w_topline;
11067 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11068#ifdef FEAT_DIFF
11069 wp->w_ru_topfill = wp->w_topfill;
11070#endif
11071 }
11072}
11073#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011074
11075#if defined(FEAT_LINEBREAK) || defined(PROTO)
11076/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011077 * Return the width of the 'number' and 'relativenumber' column.
11078 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011079 * Otherwise it depends on 'numberwidth' and the line count.
11080 */
11081 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011082number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011083{
11084 int n;
11085 linenr_T lnum;
11086
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011087 if (wp->w_p_rnu && !wp->w_p_nu)
11088 /* cursor line shows "0" */
11089 lnum = wp->w_height;
11090 else
11091 /* cursor line shows absolute line number */
11092 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011093
Bram Moolenaar6b314672015-03-20 15:42:10 +010011094 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011095 return wp->w_nrwidth_width;
11096 wp->w_nrwidth_line_count = lnum;
11097
11098 n = 0;
11099 do
11100 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011101 lnum /= 10;
11102 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011103 } while (lnum > 0);
11104
11105 /* 'numberwidth' gives the minimal width plus one */
11106 if (n < wp->w_p_nuw - 1)
11107 n = wp->w_p_nuw - 1;
11108
11109 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011110 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011111 return n;
11112}
11113#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011114
11115/*
11116 * Return the current cursor column. This is the actual position on the
11117 * screen. First column is 0.
11118 */
11119 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011120screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011121{
11122 return screen_cur_col;
11123}
11124
11125/*
11126 * Return the current cursor row. This is the actual position on the screen.
11127 * First row is 0.
11128 */
11129 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011130screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011131{
11132 return screen_cur_row;
11133}