blob: bcfaabed191e4a1bf6083f9c28da2b71a7dda316 [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 Moolenaar6f470022018-04-10 18:47:20 +02002708# define RL_MEMSET(p, v, l) \
2709 do { \
2710 if (wp->w_p_rl) \
2711 for (ri = 0; ri < l; ++ri) \
2712 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2713 else \
2714 for (ri = 0; ri < l; ++ri) \
2715 ScreenAttrs[off + (p) + ri] = v; \
2716 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002718# define RL_MEMSET(p, v, l) \
2719 do { \
2720 for (ri = 0; ri < l; ++ri) \
2721 ScreenAttrs[off + (p) + ri] = v; \
2722 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723#endif
2724
Bram Moolenaar64486672010-05-16 15:46:46 +02002725 /* Set all attributes of the 'number' or 'relativenumber' column and the
2726 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002727 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729#ifdef FEAT_SIGNS
2730 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002731 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002733 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 if (len > 0)
2735 {
2736 if (len > 2)
2737 len = 2;
2738# ifdef FEAT_RIGHTLEFT
2739 if (wp->w_p_rl)
2740 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002741 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002742 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 else
2744# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002745 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 col += len;
2747 }
2748 }
2749#endif
2750
2751 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002752 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002754 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002756 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 if (len > 0)
2758 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002759 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002760 long num;
2761 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002762
2763 if (len > w + 1)
2764 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002765
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002766 if (wp->w_p_nu && !wp->w_p_rnu)
2767 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002768 num = (long)lnum;
2769 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002770 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002771 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002772 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002773 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002774 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002775 /* 'number' + 'relativenumber': cursor line shows absolute
2776 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002777 num = lnum;
2778 fmt = "%-*ld ";
2779 }
2780 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002781
Bram Moolenaar700e7342013-01-30 12:31:36 +01002782 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783#ifdef FEAT_RIGHTLEFT
2784 if (wp->w_p_rl)
2785 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002786 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002787 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 else
2789#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002790 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 col += len;
2792 }
2793 }
2794
2795 /*
2796 * 4. Compose the folded-line string with 'foldtext', if set.
2797 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002798 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 txtcol = col; /* remember where text starts */
2801
2802 /*
2803 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2804 * Right-left text is put in columns 0 - number-col, normal text is put
2805 * in columns number-col - window-width.
2806 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002807 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808
2809 /* Fill the rest of the line with the fold filler */
2810#ifdef FEAT_RIGHTLEFT
2811 if (wp->w_p_rl)
2812 col -= txtcol;
2813#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002814 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815#ifdef FEAT_RIGHTLEFT
2816 - (wp->w_p_rl ? txtcol : 0)
2817#endif
2818 )
2819 {
2820#ifdef FEAT_MBYTE
2821 if (enc_utf8)
2822 {
2823 if (fill_fold >= 0x80)
2824 {
2825 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002826 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002827 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
2829 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002830 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002832 ScreenLines[off + col] = fill_fold;
2833 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002834 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002836 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002838 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840
2841 if (text != buf)
2842 vim_free(text);
2843
2844 /*
2845 * 6. set highlighting for the Visual area an other text.
2846 * If all folded lines are in the Visual area, highlight the line.
2847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2849 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002850 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
2852 /* Visual is after curwin->w_cursor */
2853 top = &curwin->w_cursor;
2854 bot = &VIsual;
2855 }
2856 else
2857 {
2858 /* Visual is before curwin->w_cursor */
2859 top = &VIsual;
2860 bot = &curwin->w_cursor;
2861 }
2862 if (lnum >= top->lnum
2863 && lnume <= bot->lnum
2864 && (VIsual_mode != 'v'
2865 || ((lnum > top->lnum
2866 || (lnum == top->lnum
2867 && top->col == 0))
2868 && (lnume < bot->lnum
2869 || (lnume == bot->lnum
2870 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002871 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 {
2873 if (VIsual_mode == Ctrl_V)
2874 {
2875 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002876 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002878 if (wp->w_old_cursor_lcol != MAXCOL
2879 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002880 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 len = wp->w_old_cursor_lcol;
2882 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002883 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002884 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002885 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 }
2887 }
2888 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002891 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
2894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002896#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002897 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2898 if (wp->w_p_cc_cols)
2899 {
2900 int i = 0;
2901 int j = wp->w_p_cc_cols[i];
2902 int old_txtcol = txtcol;
2903
2904 while (j > -1)
2905 {
2906 txtcol += j;
2907 if (wp->w_p_wrap)
2908 txtcol -= wp->w_skipcol;
2909 else
2910 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002911 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002912 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002913 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002914 txtcol = old_txtcol;
2915 j = wp->w_p_cc_cols[++i];
2916 }
2917 }
2918
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002919 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002920 if (wp->w_p_cuc)
2921 {
2922 txtcol += wp->w_virtcol;
2923 if (wp->w_p_wrap)
2924 txtcol -= wp->w_skipcol;
2925 else
2926 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002927 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002928 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002929 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002930 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
Bram Moolenaar02631462017-09-22 15:20:32 +02002933 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2934 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
2936 /*
2937 * Update w_cline_height and w_cline_folded if the cursor line was
2938 * updated (saves a call to plines() later).
2939 */
2940 if (wp == curwin
2941 && lnum <= curwin->w_cursor.lnum
2942 && lnume >= curwin->w_cursor.lnum)
2943 {
2944 curwin->w_cline_row = row;
2945 curwin->w_cline_height = 1;
2946 curwin->w_cline_folded = TRUE;
2947 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2948 }
2949}
2950
2951/*
2952 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2953 */
2954 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002955copy_text_attr(
2956 int off,
2957 char_u *buf,
2958 int len,
2959 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002961 int i;
2962
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 mch_memmove(ScreenLines + off, buf, (size_t)len);
2964# ifdef FEAT_MBYTE
2965 if (enc_utf8)
2966 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2967# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002968 for (i = 0; i < len; ++i)
2969 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970}
2971
2972/*
2973 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002974 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 */
2976 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002977fill_foldcolumn(
2978 char_u *p,
2979 win_T *wp,
2980 int closed, /* TRUE of FALSE */
2981 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982{
2983 int i = 0;
2984 int level;
2985 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002986 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002987 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988
2989 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002990 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991
2992 level = win_foldinfo.fi_level;
2993 if (level > 0)
2994 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002995 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002996 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 /* If the column is too narrow, we start at the lowest level that
2999 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003000 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 if (first_level < 1)
3002 first_level = 1;
3003
Bram Moolenaar1c934292015-01-27 16:39:29 +01003004 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 {
3006 if (win_foldinfo.fi_lnum == lnum
3007 && first_level + i >= win_foldinfo.fi_low_level)
3008 p[i] = '-';
3009 else if (first_level == 1)
3010 p[i] = '|';
3011 else if (first_level + i <= 9)
3012 p[i] = '0' + first_level + i;
3013 else
3014 p[i] = '>';
3015 if (first_level + i == level)
3016 break;
3017 }
3018 }
3019 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003020 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021}
3022#endif /* FEAT_FOLDING */
3023
3024/*
3025 * Display line "lnum" of window 'wp' on the screen.
3026 * Start at row "startrow", stop when "endrow" is reached.
3027 * wp->w_virtcol needs to be valid.
3028 *
3029 * Return the number of last row the line occupies.
3030 */
3031 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003032win_line(
3033 win_T *wp,
3034 linenr_T lnum,
3035 int startrow,
3036 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003037 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003039 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3041 int c = 0; /* init for GCC */
3042 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003043#ifdef FEAT_LINEBREAK
3044 long vcol_sbr = -1; /* virtual column after showbreak */
3045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 long vcol_prev = -1; /* "vcol" of previous character */
3047 char_u *line; /* current line */
3048 char_u *ptr; /* current position in "line" */
3049 int row; /* row in the window, excl w_winrow */
3050 int screen_row; /* row on the screen, incl w_winrow */
3051
3052 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3053 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003054 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003055 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 int c_extra = NUL; /* extra chars, all the same */
3057 int extra_attr = 0; /* attributes when n_extra != 0 */
3058 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3059 displaying lcs_eol at end-of-line */
3060 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3061 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3062
3063 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3064 int saved_n_extra = 0;
3065 char_u *saved_p_extra = NULL;
3066 int saved_c_extra = 0;
3067 int saved_char_attr = 0;
3068
3069 int n_attr = 0; /* chars with special attr */
3070 int saved_attr2 = 0; /* char_attr saved for n_attr */
3071 int n_attr3 = 0; /* chars with overruling special attr */
3072 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3073
3074 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3075
3076 int fromcol, tocol; /* start/end of inverting */
3077 int fromcol_prev = -2; /* start of inverting after cursor */
3078 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003080 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 pos_T pos;
3082 long v;
3083
3084 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003085 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3087 in this line */
3088 int attr = 0; /* attributes for area highlighting */
3089 int area_attr = 0; /* attributes desired by highlighting */
3090 int search_attr = 0; /* attributes desired by 'hlsearch' */
3091#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003092 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 int syntax_attr = 0; /* attributes desired by syntax */
3094 int has_syntax = FALSE; /* this buffer has syntax highl. */
3095 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003096 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003097 int draw_color_col = FALSE; /* highlight colorcolumn */
3098 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003099#endif
3100#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003101 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003102# define SPWORDLEN 150
3103 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003104 int nextlinecol = 0; /* column where nextline[] starts */
3105 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003106 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003107 int spell_attr = 0; /* attributes desired by spelling */
3108 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003109 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3110 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003111 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003112 static int cap_col = -1; /* column to check for Cap word */
3113 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003114 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#endif
3116 int extra_check; /* has syntax or linebreak */
3117#ifdef FEAT_MBYTE
3118 int multi_attr = 0; /* attributes desired by multibyte */
3119 int mb_l = 1; /* multi-byte byte length */
3120 int mb_c = 0; /* decoded multi-byte character */
3121 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003122 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#endif
3124#ifdef FEAT_DIFF
3125 int filler_lines; /* nr of filler lines to be drawn */
3126 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003127 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 int change_start = MAXCOL; /* first col of changed area */
3129 int change_end = -1; /* last col of changed area */
3130#endif
3131 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3132#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003133 int need_showbreak = FALSE; /* overlong line, skipping first x
3134 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003136#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003137 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003139 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140#endif
3141#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003142 matchitem_T *cur; /* points to the match list */
3143 match_T *shl; /* points to search_hl or a match */
3144 int shl_flag; /* flag to indicate whether search_hl
3145 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003146 int pos_inprogress; /* marks that position match search is
3147 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003148 int prevcol_hl_flag; /* flag to indicate whether prevcol
3149 equals startcol of search_hl or one
3150 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151#endif
3152#ifdef FEAT_ARABIC
3153 int prev_c = 0; /* previous Arabic character */
3154 int prev_c1 = 0; /* first composing char for prev_c */
3155#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003156#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003157 int did_line_attr = 0;
3158#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003159#ifdef FEAT_TERMINAL
3160 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003161 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164 /* draw_state: items that are drawn in sequence: */
3165#define WL_START 0 /* nothing done yet */
3166#ifdef FEAT_CMDWIN
3167# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3168#else
3169# define WL_CMDLINE WL_START
3170#endif
3171#ifdef FEAT_FOLDING
3172# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3173#else
3174# define WL_FOLD WL_CMDLINE
3175#endif
3176#ifdef FEAT_SIGNS
3177# define WL_SIGN WL_FOLD + 1 /* column for signs */
3178#else
3179# define WL_SIGN WL_FOLD /* column for signs */
3180#endif
3181#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003182#ifdef FEAT_LINEBREAK
3183# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003185# define WL_BRI WL_NR
3186#endif
3187#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3188# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3189#else
3190# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191#endif
3192#define WL_LINE WL_SBR + 1 /* text in the line */
3193 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003194#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 int feedback_col = 0;
3196 int feedback_old_attr = -1;
3197#endif
3198
Bram Moolenaar860cae12010-06-05 23:22:07 +02003199#ifdef FEAT_CONCEAL
3200 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003201 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003202 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003203 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003204 int is_concealing = FALSE;
3205 int boguscols = 0; /* nonexistent columns added to force
3206 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003207 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003208 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003209 int match_conc = 0; /* cchar for match functions */
3210 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003211 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003212# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003213# define FIX_FOR_BOGUSCOLS \
3214 { \
3215 n_extra += vcol_off; \
3216 vcol -= vcol_off; \
3217 vcol_off = 0; \
3218 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003219 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003220 boguscols = 0; \
3221 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003222#else
3223# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225
3226 if (startrow > endrow) /* past the end already! */
3227 return startrow;
3228
3229 row = startrow;
3230 screen_row = row + W_WINROW(wp);
3231
3232 /*
3233 * To speed up the loop below, set extra_check when there is linebreak,
3234 * trailing white space and/or syntax processing to be done.
3235 */
3236#ifdef FEAT_LINEBREAK
3237 extra_check = wp->w_p_lbr;
3238#else
3239 extra_check = 0;
3240#endif
3241#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003242 if (syntax_present(wp) && !wp->w_s->b_syn_error
3243# ifdef SYN_TIME_LIMIT
3244 && !wp->w_s->b_syn_slow
3245# endif
3246 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
3248 /* Prepare for syntax highlighting in this line. When there is an
3249 * error, stop syntax highlighting. */
3250 save_did_emsg = did_emsg;
3251 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003252 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003254 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 else
3256 {
3257 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003258#ifdef SYN_TIME_LIMIT
3259 if (!wp->w_s->b_syn_slow)
3260#endif
3261 {
3262 has_syntax = TRUE;
3263 extra_check = TRUE;
3264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 }
3266 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003267
3268 /* Check for columns to display for 'colorcolumn'. */
3269 color_cols = wp->w_p_cc_cols;
3270 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003271 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003272#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003273
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003274#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003275 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003276 {
3277 extra_check = TRUE;
3278 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003279 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003280 }
3281#endif
3282
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003283#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003284 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003285 && *wp->w_s->b_p_spl != NUL
3286 && wp->w_s->b_langp.ga_len > 0
3287 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003288 {
3289 /* Prepare for spell checking. */
3290 has_spell = TRUE;
3291 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003292
3293 /* Get the start of the next line, so that words that wrap to the next
3294 * line are found too: "et<line-break>al.".
3295 * Trick: skip a few chars for C/shell/Vim comments */
3296 nextline[SPWORDLEN] = NUL;
3297 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3298 {
3299 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3300 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3301 }
3302
3303 /* When a word wrapped from the previous line the start of the current
3304 * line is valid. */
3305 if (lnum == checked_lnum)
3306 cur_checked_col = checked_col;
3307 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003308
3309 /* When there was a sentence end in the previous line may require a
3310 * word starting with capital in this line. In line 1 always check
3311 * the first word. */
3312 if (lnum != capcol_lnum)
3313 cap_col = -1;
3314 if (lnum == 1)
3315 cap_col = 0;
3316 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318#endif
3319
3320 /*
3321 * handle visual active in this window
3322 */
3323 fromcol = -10;
3324 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3326 {
3327 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003328 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 {
3330 top = &curwin->w_cursor;
3331 bot = &VIsual;
3332 }
3333 else /* Visual is before curwin->w_cursor */
3334 {
3335 top = &VIsual;
3336 bot = &curwin->w_cursor;
3337 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003338 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 if (VIsual_mode == Ctrl_V) /* block mode */
3340 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003341 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
3343 fromcol = wp->w_old_cursor_fcol;
3344 tocol = wp->w_old_cursor_lcol;
3345 }
3346 }
3347 else /* non-block mode */
3348 {
3349 if (lnum > top->lnum && lnum <= bot->lnum)
3350 fromcol = 0;
3351 else if (lnum == top->lnum)
3352 {
3353 if (VIsual_mode == 'V') /* linewise */
3354 fromcol = 0;
3355 else
3356 {
3357 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3358 if (gchar_pos(top) == NUL)
3359 tocol = fromcol + 1;
3360 }
3361 }
3362 if (VIsual_mode != 'V' && lnum == bot->lnum)
3363 {
3364 if (*p_sel == 'e' && bot->col == 0
3365#ifdef FEAT_VIRTUALEDIT
3366 && bot->coladd == 0
3367#endif
3368 )
3369 {
3370 fromcol = -10;
3371 tocol = MAXCOL;
3372 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003373 else if (bot->col == MAXCOL)
3374 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 else
3376 {
3377 pos = *bot;
3378 if (*p_sel == 'e')
3379 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3380 else
3381 {
3382 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3383 ++tocol;
3384 }
3385 }
3386 }
3387 }
3388
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 /* Check if the character under the cursor should not be inverted */
3390 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003391#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 )
3395 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396
3397 /* if inverting in this line set area_highlighting */
3398 if (fromcol >= 0)
3399 {
3400 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003401 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003403 if ((clip_star.available && !clip_star.owned
3404 && clip_isautosel_star())
3405 || (clip_plus.available && !clip_plus.owned
3406 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003407 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408#endif
3409 }
3410 }
3411
3412 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003413 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003415 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 && wp == curwin
3417 && lnum >= curwin->w_cursor.lnum
3418 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3419 {
3420 if (lnum == curwin->w_cursor.lnum)
3421 getvcol(curwin, &(curwin->w_cursor),
3422 (colnr_T *)&fromcol, NULL, NULL);
3423 else
3424 fromcol = 0;
3425 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3426 {
3427 pos.lnum = lnum;
3428 pos.col = search_match_endcol;
3429 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3430 }
3431 else
3432 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003433 /* do at least one character; happens when past end of line */
3434 if (fromcol == tocol)
3435 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003437 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439
3440#ifdef FEAT_DIFF
3441 filler_lines = diff_check(wp, lnum);
3442 if (filler_lines < 0)
3443 {
3444 if (filler_lines == -1)
3445 {
3446 if (diff_find_change(wp, lnum, &change_start, &change_end))
3447 diff_hlf = HLF_ADD; /* added line */
3448 else if (change_start == 0)
3449 diff_hlf = HLF_TXD; /* changed text */
3450 else
3451 diff_hlf = HLF_CHD; /* changed line */
3452 }
3453 else
3454 diff_hlf = HLF_ADD; /* added line */
3455 filler_lines = 0;
3456 area_highlighting = TRUE;
3457 }
3458 if (lnum == wp->w_topline)
3459 filler_lines = wp->w_topfill;
3460 filler_todo = filler_lines;
3461#endif
3462
3463#ifdef LINE_ATTR
3464# ifdef FEAT_SIGNS
3465 /* If this line has a sign with line highlighting set line_attr. */
3466 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3467 if (v != 0)
3468 line_attr = sign_get_attr((int)v, TRUE);
3469# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003470# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003472 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003473 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474# endif
3475 if (line_attr != 0)
3476 area_highlighting = TRUE;
3477#endif
3478
3479 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3480 ptr = line;
3481
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003482#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003483 if (has_spell)
3484 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003485 /* For checking first word with a capital skip white space. */
3486 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003487 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003488
Bram Moolenaar30abd282005-06-22 22:35:10 +00003489 /* To be able to spell-check over line boundaries copy the end of the
3490 * current line into nextline[]. Above the start of the next line was
3491 * copied to nextline[SPWORDLEN]. */
3492 if (nextline[SPWORDLEN] == NUL)
3493 {
3494 /* No next line or it is empty. */
3495 nextlinecol = MAXCOL;
3496 nextline_idx = 0;
3497 }
3498 else
3499 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003500 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003501 if (v < SPWORDLEN)
3502 {
3503 /* Short line, use it completely and append the start of the
3504 * next line. */
3505 nextlinecol = 0;
3506 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003507 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003508 nextline_idx = v + 1;
3509 }
3510 else
3511 {
3512 /* Long line, use only the last SPWORDLEN bytes. */
3513 nextlinecol = v - SPWORDLEN;
3514 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3515 nextline_idx = SPWORDLEN + 1;
3516 }
3517 }
3518 }
3519#endif
3520
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003521 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003523 if (lcs_space || lcs_trail)
3524 extra_check = TRUE;
3525 /* find start of trailing whitespace */
3526 if (lcs_trail)
3527 {
3528 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003529 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003530 --trailcol;
3531 trailcol += (colnr_T) (ptr - line);
3532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534
3535 /*
3536 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3537 * first character to be displayed.
3538 */
3539 if (wp->w_p_wrap)
3540 v = wp->w_skipcol;
3541 else
3542 v = wp->w_leftcol;
3543 if (v > 0)
3544 {
3545#ifdef FEAT_MBYTE
3546 char_u *prev_ptr = ptr;
3547#endif
3548 while (vcol < v && *ptr != NUL)
3549 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003550 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 vcol += c;
3552#ifdef FEAT_MBYTE
3553 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003555 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003558 /* When:
3559 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003560 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003561 * - 'virtualedit' is set, or
3562 * - the visual mode is active,
3563 * the end of the line may be before the start of the displayed part.
3564 */
3565 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003566#ifdef FEAT_SYN_HL
3567 wp->w_p_cuc || draw_color_col ||
3568#endif
3569#ifdef FEAT_VIRTUALEDIT
3570 virtual_active() ||
3571#endif
3572 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
3577 /* Handle a character that's not completely on the screen: Put ptr at
3578 * that character but skip the first few screen characters. */
3579 if (vcol > v)
3580 {
3581 vcol -= c;
3582#ifdef FEAT_MBYTE
3583 ptr = prev_ptr;
3584#else
3585 --ptr;
3586#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003587 /* If the character fits on the screen, don't need to skip it.
3588 * Except for a TAB. */
3589 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003590#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003591 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003592#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003593 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003594 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 }
3596
3597 /*
3598 * Adjust for when the inverted text is before the screen,
3599 * and when the start of the inverted text is before the screen.
3600 */
3601 if (tocol <= vcol)
3602 fromcol = 0;
3603 else if (fromcol >= 0 && fromcol < vcol)
3604 fromcol = vcol;
3605
3606#ifdef FEAT_LINEBREAK
3607 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3608 if (wp->w_p_wrap)
3609 need_showbreak = TRUE;
3610#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003611#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003612 /* When spell checking a word we need to figure out the start of the
3613 * word and if it's badly spelled or not. */
3614 if (has_spell)
3615 {
3616 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003617 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003618 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003619
3620 pos = wp->w_cursor;
3621 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003622 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003623 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003624
3625 /* spell_move_to() may call ml_get() and make "line" invalid */
3626 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3627 ptr = line + linecol;
3628
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003629 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003630 {
3631 /* no bad word found at line start, don't check until end of a
3632 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003633 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003634 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003635 }
3636 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003637 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003638 /* bad word found, use attributes until end of word */
3639 word_end = wp->w_cursor.col + len + 1;
3640
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003641 /* Turn index into actual attributes. */
3642 if (spell_hlf != HLF_COUNT)
3643 spell_attr = highlight_attr[spell_hlf];
3644 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003645 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003646
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003647# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003648 /* Need to restart syntax highlighting for this line. */
3649 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003650 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003651# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003652 }
3653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 }
3655
3656 /*
3657 * Correct highlighting for cursor that can't be disabled.
3658 * Avoids having to check this for each character.
3659 */
3660 if (fromcol >= 0)
3661 {
3662 if (noinvcur)
3663 {
3664 if ((colnr_T)fromcol == wp->w_virtcol)
3665 {
3666 /* highlighting starts at cursor, let it start just after the
3667 * cursor */
3668 fromcol_prev = fromcol;
3669 fromcol = -1;
3670 }
3671 else if ((colnr_T)fromcol < wp->w_virtcol)
3672 /* restart highlighting after the cursor */
3673 fromcol_prev = wp->w_virtcol;
3674 }
3675 if (fromcol >= tocol)
3676 fromcol = -1;
3677 }
3678
3679#ifdef FEAT_SEARCH_EXTRA
3680 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003681 * Handle highlighting the last used search pattern and matches.
3682 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003684 cur = wp->w_match_head;
3685 shl_flag = FALSE;
3686 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003688 if (shl_flag == FALSE)
3689 {
3690 shl = &search_hl;
3691 shl_flag = TRUE;
3692 }
3693 else
3694 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003695 shl->startcol = MAXCOL;
3696 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003698 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003699 v = (long)(ptr - line);
3700 if (cur != NULL)
3701 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003702 next_search_hl(wp, shl, lnum, (colnr_T)v,
3703 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003704
3705 /* Need to get the line again, a multi-line regexp may have made it
3706 * invalid. */
3707 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3708 ptr = line + v;
3709
3710 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003712 if (shl->lnum == lnum)
3713 shl->startcol = shl->rm.startpos[0].col;
3714 else
3715 shl->startcol = 0;
3716 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3717 - shl->rm.startpos[0].lnum)
3718 shl->endcol = shl->rm.endpos[0].col;
3719 else
3720 shl->endcol = MAXCOL;
3721 /* Highlight one character for an empty match. */
3722 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003725 if (has_mbyte && line[shl->endcol] != NUL)
3726 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3727 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003729 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003731 if ((long)shl->startcol < v) /* match at leftcol */
3732 {
3733 shl->attr_cur = shl->attr;
3734 search_attr = shl->attr;
3735 }
3736 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003738 if (shl != &search_hl && cur != NULL)
3739 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
3741#endif
3742
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003743#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003744 /* Cursor line highlighting for 'cursorline' in the current window. Not
3745 * when Visual mode is active, because it's not clear what is selected
3746 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003747 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003748 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003749 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003750 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003751 area_highlighting = TRUE;
3752 }
3753#endif
3754
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003755 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 col = 0;
3757#ifdef FEAT_RIGHTLEFT
3758 if (wp->w_p_rl)
3759 {
3760 /* Rightleft window: process the text in the normal direction, but put
3761 * it in current_ScreenLine[] from right to left. Start at the
3762 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003763 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 off += col;
3765 }
3766#endif
3767
3768 /*
3769 * Repeat for the whole displayed line.
3770 */
3771 for (;;)
3772 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003773#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003774 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 /* Skip this quickly when working on the text. */
3777 if (draw_state != WL_LINE)
3778 {
3779#ifdef FEAT_CMDWIN
3780 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3781 {
3782 draw_state = WL_CMDLINE;
3783 if (cmdwin_type != 0 && wp == curwin)
3784 {
3785 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003787 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003788 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 }
3791#endif
3792
3793#ifdef FEAT_FOLDING
3794 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3795 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003796 int fdc = compute_foldcolumn(wp, 0);
3797
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003799 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003801 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003802 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003803 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003804 p_extra_free = alloc(12 + 1);
3805
3806 if (p_extra_free != NULL)
3807 {
3808 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3809 n_extra = fdc;
3810 p_extra_free[n_extra] = NUL;
3811 p_extra = p_extra_free;
3812 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003813 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 }
3817#endif
3818
3819#ifdef FEAT_SIGNS
3820 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3821 {
3822 draw_state = WL_SIGN;
3823 /* Show the sign column when there are any signs in this
3824 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003825 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003827 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003829 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830# endif
3831
3832 /* Draw two cells with the sign value or blank. */
3833 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003834 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 n_extra = 2;
3836
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003837 if (row == startrow
3838#ifdef FEAT_DIFF
3839 + filler_lines && filler_todo <= 0
3840#endif
3841 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 {
3843 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3844 SIGN_TEXT);
3845# ifdef FEAT_SIGN_ICONS
3846 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3847 SIGN_ICON);
3848 if (gui.in_use && icon_sign != 0)
3849 {
3850 /* Use the image in this position. */
3851 c_extra = SIGN_BYTE;
3852# ifdef FEAT_NETBEANS_INTG
3853 if (buf_signcount(wp->w_buffer, lnum) > 1)
3854 c_extra = MULTISIGN_BYTE;
3855# endif
3856 char_attr = icon_sign;
3857 }
3858 else
3859# endif
3860 if (text_sign != 0)
3861 {
3862 p_extra = sign_get_text(text_sign);
3863 if (p_extra != NULL)
3864 {
3865 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003866 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
3868 char_attr = sign_get_attr(text_sign, FALSE);
3869 }
3870 }
3871 }
3872 }
3873#endif
3874
3875 if (draw_state == WL_NR - 1 && n_extra == 0)
3876 {
3877 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003878 /* Display the absolute or relative line number. After the
3879 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3880 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 && (row == startrow
3882#ifdef FEAT_DIFF
3883 + filler_lines
3884#endif
3885 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3886 {
3887 /* Draw the line number (empty space after wrapping). */
3888 if (row == startrow
3889#ifdef FEAT_DIFF
3890 + filler_lines
3891#endif
3892 )
3893 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003894 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003895 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003896
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003897 if (wp->w_p_nu && !wp->w_p_rnu)
3898 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003899 num = (long)lnum;
3900 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003901 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003902 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003903 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003904 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003905 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003906 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003907 num = lnum;
3908 fmt = "%-*ld ";
3909 }
3910 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003911
Bram Moolenaar700e7342013-01-30 12:31:36 +01003912 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003913 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if (wp->w_skipcol > 0)
3915 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3916 *p_extra = '-';
3917#ifdef FEAT_RIGHTLEFT
3918 if (wp->w_p_rl) /* reverse line numbers */
3919 rl_mirror(extra);
3920#endif
3921 p_extra = extra;
3922 c_extra = NUL;
3923 }
3924 else
3925 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003926 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003927 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003928#ifdef FEAT_SYN_HL
3929 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003930 * the current line differently.
3931 * TODO: Can we use CursorLine instead of CursorLineNr
3932 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003933 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003934 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003935 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938 }
3939
Bram Moolenaar597a4222014-06-25 14:39:50 +02003940#ifdef FEAT_LINEBREAK
3941 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3942 && n_extra == 0 && *p_sbr != NUL)
3943 /* draw indent after showbreak value */
3944 draw_state = WL_BRI;
3945 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3946 /* After the showbreak, draw the breakindent */
3947 draw_state = WL_BRI - 1;
3948
3949 /* draw 'breakindent': indent wrapped text accordingly */
3950 if (draw_state == WL_BRI - 1 && n_extra == 0)
3951 {
3952 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003953 /* if need_showbreak is set, breakindent also applies */
3954 if (wp->w_p_bri && n_extra == 0
3955 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003956# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003957 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003958# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003959 )
3960 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003961 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003962# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003963 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003964 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003965 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003966# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003967 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3968 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003969 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003970# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003971 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003972# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003973 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003974 c_extra = ' ';
3975 n_extra = get_breakindent_win(wp,
3976 ml_get_buf(wp->w_buffer, lnum, FALSE));
3977 /* Correct end of highlighted area for 'breakindent',
3978 * required when 'linebreak' is also set. */
3979 if (tocol == vcol)
3980 tocol += n_extra;
3981 }
3982 }
3983#endif
3984
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3986 if (draw_state == WL_SBR - 1 && n_extra == 0)
3987 {
3988 draw_state = WL_SBR;
3989# ifdef FEAT_DIFF
3990 if (filler_todo > 0)
3991 {
3992 /* Draw "deleted" diff line(s). */
3993 if (char2cells(fill_diff) > 1)
3994 c_extra = '-';
3995 else
3996 c_extra = fill_diff;
3997# ifdef FEAT_RIGHTLEFT
3998 if (wp->w_p_rl)
3999 n_extra = col + 1;
4000 else
4001# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004002 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004003 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
4005# endif
4006# ifdef FEAT_LINEBREAK
4007 if (*p_sbr != NUL && need_showbreak)
4008 {
4009 /* Draw 'showbreak' at the start of each broken line. */
4010 p_extra = p_sbr;
4011 c_extra = NUL;
4012 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004013 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004015 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 /* Correct end of highlighted area for 'showbreak',
4017 * required when 'linebreak' is also set. */
4018 if (tocol == vcol)
4019 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004020#ifdef FEAT_SYN_HL
4021 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004022 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004023 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004024 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
4027# endif
4028 }
4029#endif
4030
4031 if (draw_state == WL_LINE - 1 && n_extra == 0)
4032 {
4033 draw_state = WL_LINE;
4034 if (saved_n_extra)
4035 {
4036 /* Continue item from end of wrapped line. */
4037 n_extra = saved_n_extra;
4038 c_extra = saved_c_extra;
4039 p_extra = saved_p_extra;
4040 char_attr = saved_char_attr;
4041 }
4042 else
4043 char_attr = 0;
4044 }
4045 }
4046
4047 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004048 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004049 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050#ifdef FEAT_DIFF
4051 && filler_todo <= 0
4052#endif
4053 )
4054 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004055 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004056 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004057 /* Pretend we have finished updating the window. Except when
4058 * 'cursorcolumn' is set. */
4059#ifdef FEAT_SYN_HL
4060 if (wp->w_p_cuc)
4061 row = wp->w_cline_row + wp->w_cline_height;
4062 else
4063#endif
4064 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 break;
4066 }
4067
4068 if (draw_state == WL_LINE && area_highlighting)
4069 {
4070 /* handle Visual or match highlighting in this line */
4071 if (vcol == fromcol
4072#ifdef FEAT_MBYTE
4073 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4074 && (*mb_ptr2cells)(ptr) > 1)
4075#endif
4076 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004077 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 && vcol < tocol))
4079 area_attr = attr; /* start highlighting */
4080 else if (area_attr != 0
4081 && (vcol == tocol
4082 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085#ifdef FEAT_SEARCH_EXTRA
4086 if (!n_extra)
4087 {
4088 /*
4089 * Check for start/end of search pattern match.
4090 * After end, check for start/end of next match.
4091 * When another match, have to check for start again.
4092 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004093 * Do this for 'search_hl' and the match list (ordered by
4094 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004096 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004097 cur = wp->w_match_head;
4098 shl_flag = FALSE;
4099 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004101 if (shl_flag == FALSE
4102 && ((cur != NULL
4103 && cur->priority > SEARCH_HL_PRIORITY)
4104 || cur == NULL))
4105 {
4106 shl = &search_hl;
4107 shl_flag = TRUE;
4108 }
4109 else
4110 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004111 if (cur != NULL)
4112 cur->pos.cur = 0;
4113 pos_inprogress = TRUE;
4114 while (shl->rm.regprog != NULL
4115 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004117 if (shl->startcol != MAXCOL
4118 && v >= (long)shl->startcol
4119 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004121#ifdef FEAT_MBYTE
4122 int tmp_col = v + MB_PTR2LEN(ptr);
4123
4124 if (shl->endcol < tmp_col)
4125 shl->endcol = tmp_col;
4126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004128#ifdef FEAT_CONCEAL
4129 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4130 == cur->hlg_id)
4131 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004132 has_match_conc =
4133 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004134 match_conc = cur->conceal_char;
4135 }
4136 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004137 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004140 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
4142 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004143 next_search_hl(wp, shl, lnum, (colnr_T)v,
4144 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004145 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004146 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
4148 /* Need to get the line again, a multi-line regexp
4149 * may have made it invalid. */
4150 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4151 ptr = line + v;
4152
4153 if (shl->lnum == lnum)
4154 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004155 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004157 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004159 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004161 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 {
4163 /* highlight empty match, try again after
4164 * it */
4165#ifdef FEAT_MBYTE
4166 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004167 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004168 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 else
4170#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004171 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173
4174 /* Loop to check if the match starts at the
4175 * current position */
4176 continue;
4177 }
4178 }
4179 break;
4180 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004181 if (shl != &search_hl && cur != NULL)
4182 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004184
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004185 /* Use attributes from match with highest priority among
4186 * 'search_hl' and the match list. */
4187 search_attr = search_hl.attr_cur;
4188 cur = wp->w_match_head;
4189 shl_flag = FALSE;
4190 while (cur != NULL || shl_flag == FALSE)
4191 {
4192 if (shl_flag == FALSE
4193 && ((cur != NULL
4194 && cur->priority > SEARCH_HL_PRIORITY)
4195 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004196 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004197 shl = &search_hl;
4198 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004199 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004200 else
4201 shl = &cur->hl;
4202 if (shl->attr_cur != 0)
4203 search_attr = shl->attr_cur;
4204 if (shl != &search_hl && cur != NULL)
4205 cur = cur->next;
4206 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004207 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004208 if (*ptr == NUL && (did_line_attr >= 1
4209 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004210 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
4212#endif
4213
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004215 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004217 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4218 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004220 if (diff_hlf == HLF_TXD && ptr - line > change_end
4221 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004223 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004224 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004225 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
4227#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004228
4229 /* Decide which of the highlight attributes to use. */
4230 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004231#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004232 if (area_attr != 0)
4233 char_attr = hl_combine_attr(line_attr, area_attr);
4234 else if (search_attr != 0)
4235 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004236 /* Use line_attr when not in the Visual or 'incsearch' area
4237 * (area_attr may be 0 when "noinvcur" is set). */
4238 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004239 || vcol < fromcol || vcol_prev < fromcol_prev
4240 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004241 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004242#else
4243 if (area_attr != 0)
4244 char_attr = area_attr;
4245 else if (search_attr != 0)
4246 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004247#endif
4248 else
4249 {
4250 attr_pri = FALSE;
4251#ifdef FEAT_SYN_HL
4252 if (has_syntax)
4253 char_attr = syntax_attr;
4254 else
4255#endif
4256 char_attr = 0;
4257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259
4260 /*
4261 * Get the next character to put on the screen.
4262 */
4263 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004264 * The "p_extra" points to the extra stuff that is inserted to
4265 * represent special characters (non-printable stuff) and other
4266 * things. When all characters are the same, c_extra is used.
4267 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4268 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4270 */
4271 if (n_extra > 0)
4272 {
4273 if (c_extra != NUL)
4274 {
4275 c = c_extra;
4276#ifdef FEAT_MBYTE
4277 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004278 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
4280 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004281 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004282 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 else
4285 mb_utf8 = FALSE;
4286#endif
4287 }
4288 else
4289 {
4290 c = *p_extra;
4291#ifdef FEAT_MBYTE
4292 if (has_mbyte)
4293 {
4294 mb_c = c;
4295 if (enc_utf8)
4296 {
4297 /* If the UTF-8 character is more than one byte:
4298 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004299 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 mb_utf8 = FALSE;
4301 if (mb_l > n_extra)
4302 mb_l = 1;
4303 else if (mb_l > 1)
4304 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004305 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004307 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 }
4309 }
4310 else
4311 {
4312 /* if this is a DBCS character, put it in "mb_c" */
4313 mb_l = MB_BYTE2LEN(c);
4314 if (mb_l >= n_extra)
4315 mb_l = 1;
4316 else if (mb_l > 1)
4317 mb_c = (c << 8) + p_extra[1];
4318 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004319 if (mb_l == 0) /* at the NUL at end-of-line */
4320 mb_l = 1;
4321
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 /* If a double-width char doesn't fit display a '>' in the
4323 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004324 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325# ifdef FEAT_RIGHTLEFT
4326 wp->w_p_rl ? (col <= 0) :
4327# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004328 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 && (*mb_char2cells)(mb_c) == 2)
4330 {
4331 c = '>';
4332 mb_c = c;
4333 mb_l = 1;
4334 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004335 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 /* put the pointer back to output the double-width
4337 * character at the start of the next line. */
4338 ++n_extra;
4339 --p_extra;
4340 }
4341 else
4342 {
4343 n_extra -= mb_l - 1;
4344 p_extra += mb_l - 1;
4345 }
4346 }
4347#endif
4348 ++p_extra;
4349 }
4350 --n_extra;
4351 }
4352 else
4353 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004354#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004355 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004356#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004357
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004358 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004359 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 /*
4361 * Get a character from the line itself.
4362 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004363 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004364#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004365 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367#ifdef FEAT_MBYTE
4368 if (has_mbyte)
4369 {
4370 mb_c = c;
4371 if (enc_utf8)
4372 {
4373 /* If the UTF-8 character is more than one byte: Decode it
4374 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004375 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 mb_utf8 = FALSE;
4377 if (mb_l > 1)
4378 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004379 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 /* Overlong encoded ASCII or ASCII with composing char
4381 * is displayed normally, except a NUL. */
4382 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004383 {
4384 c = mb_c;
4385# ifdef FEAT_LINEBREAK
4386 c0 = mb_c;
4387# endif
4388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004390
4391 /* At start of the line we can have a composing char.
4392 * Draw it as a space with a composing char. */
4393 if (utf_iscomposing(mb_c))
4394 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004395 int i;
4396
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004397 for (i = Screen_mco - 1; i > 0; --i)
4398 u8cc[i] = u8cc[i - 1];
4399 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004400 mb_c = ' ';
4401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
4403
4404 if ((mb_l == 1 && c >= 0x80)
4405 || (mb_l >= 1 && mb_c == 0)
4406 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004407# ifdef UNICODE16
4408 || mb_c >= 0x10000
4409# endif
4410 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 {
4412 /*
4413 * Illegal UTF-8 byte: display as <xx>.
4414 * Non-BMP character : display as ? or fullwidth ?.
4415 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004416# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {
4420 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004421# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 if (wp->w_p_rl) /* reverse */
4423 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004424# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004426# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 else if (utf_char2cells(mb_c) != 2)
4428 STRCPY(extra, "?");
4429 else
4430 /* 0xff1f in UTF-8: full-width '?' */
4431 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
4434 p_extra = extra;
4435 c = *p_extra;
4436 mb_c = mb_ptr2char_adv(&p_extra);
4437 mb_utf8 = (c >= 0x80);
4438 n_extra = (int)STRLEN(p_extra);
4439 c_extra = NUL;
4440 if (area_attr == 0 && search_attr == 0)
4441 {
4442 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004443 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 saved_attr2 = char_attr; /* save current attr */
4445 }
4446 }
4447 else if (mb_l == 0) /* at the NUL at end-of-line */
4448 mb_l = 1;
4449#ifdef FEAT_ARABIC
4450 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4451 {
4452 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004453 int pc, pc1, nc;
4454 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
4456 /* The idea of what is the previous and next
4457 * character depends on 'rightleft'. */
4458 if (wp->w_p_rl)
4459 {
4460 pc = prev_c;
4461 pc1 = prev_c1;
4462 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004463 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465 else
4466 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004467 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004469 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 prev_c = mb_c;
4472
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004473 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475 else
4476 prev_c = mb_c;
4477#endif
4478 }
4479 else /* enc_dbcs */
4480 {
4481 mb_l = MB_BYTE2LEN(c);
4482 if (mb_l == 0) /* at the NUL at end-of-line */
4483 mb_l = 1;
4484 else if (mb_l > 1)
4485 {
4486 /* We assume a second byte below 32 is illegal.
4487 * Hopefully this is OK for all double-byte encodings!
4488 */
4489 if (ptr[1] >= 32)
4490 mb_c = (c << 8) + ptr[1];
4491 else
4492 {
4493 if (ptr[1] == NUL)
4494 {
4495 /* head byte at end of line */
4496 mb_l = 1;
4497 transchar_nonprint(extra, c);
4498 }
4499 else
4500 {
4501 /* illegal tail byte */
4502 mb_l = 2;
4503 STRCPY(extra, "XX");
4504 }
4505 p_extra = extra;
4506 n_extra = (int)STRLEN(extra) - 1;
4507 c_extra = NUL;
4508 c = *p_extra++;
4509 if (area_attr == 0 && search_attr == 0)
4510 {
4511 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004512 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 saved_attr2 = char_attr; /* save current attr */
4514 }
4515 mb_c = c;
4516 }
4517 }
4518 }
4519 /* If a double-width char doesn't fit display a '>' in the
4520 * last column; the character is displayed at the start of the
4521 * next line. */
4522 if ((
4523# ifdef FEAT_RIGHTLEFT
4524 wp->w_p_rl ? (col <= 0) :
4525# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004526 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 && (*mb_char2cells)(mb_c) == 2)
4528 {
4529 c = '>';
4530 mb_c = c;
4531 mb_utf8 = FALSE;
4532 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004533 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 /* Put pointer back so that the character will be
4535 * displayed at the start of the next line. */
4536 --ptr;
4537 }
4538 else if (*ptr != NUL)
4539 ptr += mb_l - 1;
4540
4541 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004542 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004543 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004544 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004547 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 c = ' ';
4549 if (area_attr == 0 && search_attr == 0)
4550 {
4551 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004552 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 saved_attr2 = char_attr; /* save current attr */
4554 }
4555 mb_c = c;
4556 mb_utf8 = FALSE;
4557 mb_l = 1;
4558 }
4559
4560 }
4561#endif
4562 ++ptr;
4563
4564 if (extra_check)
4565 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004566#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004567 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004568#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004569
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004570#ifdef FEAT_TERMINAL
4571 if (get_term_attr)
4572 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004573 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004574
4575 if (!attr_pri)
4576 char_attr = syntax_attr;
4577 else
4578 char_attr = hl_combine_attr(syntax_attr, char_attr);
4579 }
4580#endif
4581
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004582#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 /* Get syntax attribute, unless still at the start of the line
4584 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004585 v = (long)(ptr - line);
4586 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 {
4588 /* Get the syntax attribute for the character. If there
4589 * is an error, disable syntax highlighting. */
4590 save_did_emsg = did_emsg;
4591 did_emsg = FALSE;
4592
Bram Moolenaar217ad922005-03-20 22:37:15 +00004593 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004594# ifdef FEAT_SPELL
4595 has_spell ? &can_spell :
4596# endif
4597 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
4599 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004600 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004601 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004602 has_syntax = FALSE;
4603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 else
4605 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004606#ifdef SYN_TIME_LIMIT
4607 if (wp->w_s->b_syn_slow)
4608 has_syntax = FALSE;
4609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
4611 /* Need to get the line again, a multi-line regexp may
4612 * have made it invalid. */
4613 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4614 ptr = line + v;
4615
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004616 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004618 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004619 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004620# ifdef FEAT_CONCEAL
4621 /* no concealing past the end of the line, it interferes
4622 * with line highlighting */
4623 if (c == NUL)
4624 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004625 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004626 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004629#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004630
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004631#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004632 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004633 * Only do this when there is no syntax highlighting, the
4634 * @Spell cluster is not used or the current syntax item
4635 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004636 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004637 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004638 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004639# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004640 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004641 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004642# endif
4643 if (c != 0 && (
4644# ifdef FEAT_SYN_HL
4645 !has_syntax ||
4646# endif
4647 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004648 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004649 char_u *prev_ptr, *p;
4650 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004651 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004652# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004653 if (has_mbyte)
4654 {
4655 prev_ptr = ptr - mb_l;
4656 v -= mb_l - 1;
4657 }
4658 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004659# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004660 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004661
4662 /* Use nextline[] if possible, it has the start of the
4663 * next line concatenated. */
4664 if ((prev_ptr - line) - nextlinecol >= 0)
4665 p = nextline + (prev_ptr - line) - nextlinecol;
4666 else
4667 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004668 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004669 len = spell_check(wp, p, &spell_hlf, &cap_col,
4670 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004671 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004672
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004673 /* In Insert mode only highlight a word that
4674 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004675 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004676 && (State & INSERT) != 0
4677 && wp->w_cursor.lnum == lnum
4678 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004679 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004680 && wp->w_cursor.col < (colnr_T)word_end)
4681 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004682 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004683 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004684 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004685
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004686 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004687 && (p - nextline) + len > nextline_idx)
4688 {
4689 /* Remember that the good word continues at the
4690 * start of the next line. */
4691 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004692 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004693 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004694
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004695 /* Turn index into actual attributes. */
4696 if (spell_hlf != HLF_COUNT)
4697 spell_attr = highlight_attr[spell_hlf];
4698
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004699 if (cap_col > 0)
4700 {
4701 if (p != prev_ptr
4702 && (p - nextline) + cap_col >= nextline_idx)
4703 {
4704 /* Remember that the word in the next line
4705 * must start with a capital. */
4706 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004707 cap_col = (int)((p - nextline) + cap_col
4708 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004709 }
4710 else
4711 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004712 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004713 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004714 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004715 }
4716 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004717 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004718 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004719 char_attr = hl_combine_attr(char_attr, spell_attr);
4720 else
4721 char_attr = hl_combine_attr(spell_attr, char_attr);
4722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723#endif
4724#ifdef FEAT_LINEBREAK
4725 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004726 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004728 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004729 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004731# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004732 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004733# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004734 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004736 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004738 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004739
Bram Moolenaar597a4222014-06-25 14:39:50 +02004740 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004741 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004742 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004743 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaara3650912014-11-19 13:21:57 +01004744 n_extra = (int)wp->w_buffer->b_p_ts
4745 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4746
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004747# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004748 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004749# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004751# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004752 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004753 {
4754#ifdef FEAT_CONCEAL
4755 if (c == TAB)
4756 /* See "Tab alignment" below. */
4757 FIX_FOR_BOGUSCOLS;
4758#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004759 if (!wp->w_p_list)
4760 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
4763#endif
4764
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004765 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4766 */
4767 if (wp->w_p_list
4768 && (((c == 160
4769#ifdef FEAT_MBYTE
4770 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4771#endif
4772 ) && lcs_nbsp)
4773 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4774 {
4775 c = (c == ' ') ? lcs_space : lcs_nbsp;
4776 if (area_attr == 0 && search_attr == 0)
4777 {
4778 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004779 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004780 saved_attr2 = char_attr; /* save current attr */
4781 }
4782#ifdef FEAT_MBYTE
4783 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004784 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004785 {
4786 mb_utf8 = TRUE;
4787 u8cc[0] = 0;
4788 c = 0xc0;
4789 }
4790 else
4791 mb_utf8 = FALSE;
4792#endif
4793 }
4794
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4796 {
4797 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004798 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
4800 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004801 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 saved_attr2 = char_attr; /* save current attr */
4803 }
4804#ifdef FEAT_MBYTE
4805 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004806 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 {
4808 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004809 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004810 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 }
4812 else
4813 mb_utf8 = FALSE;
4814#endif
4815 }
4816 }
4817
4818 /*
4819 * Handling of non-printable characters.
4820 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004821 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 {
4823 /*
4824 * when getting a character from the file, we may have to
4825 * turn it into something else on the way to putting it
4826 * into "ScreenLines".
4827 */
4828 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4829 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004830 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004831 long vcol_adjusted = vcol; /* removed showbreak length */
4832#ifdef FEAT_LINEBREAK
4833 /* only adjust the tab_len, when at the first column
4834 * after the showbreak value was drawn */
4835 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4836 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004839 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004840 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4841
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004842#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004843 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004844#endif
4845 /* tab amount depends on current column */
4846 n_extra = tab_len;
4847#ifdef FEAT_LINEBREAK
4848 else
4849 {
4850 char_u *p;
4851 int len = n_extra;
4852 int i;
4853 int saved_nextra = n_extra;
4854
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004855#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004856 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004857 /* there are characters to conceal */
4858 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004859 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4860 */
4861 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4862 && n_extra > tab_len)
4863 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004864#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004865
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004866 /* if n_extra > 0, it gives the number of chars, to
4867 * use for a tab, else we need to calculate the width
4868 * for a tab */
4869#ifdef FEAT_MBYTE
4870 len = (tab_len * mb_char2len(lcs_tab2));
4871 if (n_extra > 0)
4872 len += n_extra - tab_len;
4873#endif
4874 c = lcs_tab1;
4875 p = alloc((unsigned)(len + 1));
4876 vim_memset(p, ' ', len);
4877 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004878 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004879 p_extra_free = p;
4880 for (i = 0; i < tab_len; i++)
4881 {
4882#ifdef FEAT_MBYTE
4883 mb_char2bytes(lcs_tab2, p);
4884 p += mb_char2len(lcs_tab2);
4885 n_extra += mb_char2len(lcs_tab2)
4886 - (saved_nextra > 0 ? 1 : 0);
4887#else
4888 p[i] = lcs_tab2;
4889#endif
4890 }
4891 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004892#ifdef FEAT_CONCEAL
4893 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4894 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004895 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004896 n_extra -= vcol_off;
4897#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004898 }
4899#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004900#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004901 {
4902 int vc_saved = vcol_off;
4903
4904 /* Tab alignment should be identical regardless of
4905 * 'conceallevel' value. So tab compensates of all
4906 * previous concealed characters, and thus resets
4907 * vcol_off and boguscols accumulated so far in the
4908 * line. Note that the tab can be longer than
4909 * 'tabstop' when there are concealed characters. */
4910 FIX_FOR_BOGUSCOLS;
4911
4912 /* Make sure, the highlighting for the tab char will be
4913 * correctly set further below (effectively reverts the
4914 * FIX_FOR_BOGSUCOLS macro */
4915 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004916 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004917 tab_len += vc_saved;
4918 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920#ifdef FEAT_MBYTE
4921 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4922#endif
4923 if (wp->w_p_list)
4924 {
4925 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004926#ifdef FEAT_LINEBREAK
4927 if (wp->w_p_lbr)
4928 c_extra = NUL; /* using p_extra from above */
4929 else
4930#endif
4931 c_extra = lcs_tab2;
4932 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004933 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 saved_attr2 = char_attr; /* save current attr */
4935#ifdef FEAT_MBYTE
4936 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004937 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
4939 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004940 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004941 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943#endif
4944 }
4945 else
4946 {
4947 c_extra = ' ';
4948 c = ' ';
4949 }
4950 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004951 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004952 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004953 || ((fromcol >= 0 || fromcol_prev >= 0)
4954 && tocol > vcol
4955 && VIsual_mode != Ctrl_V
4956 && (
4957# ifdef FEAT_RIGHTLEFT
4958 wp->w_p_rl ? (col >= 0) :
4959# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004960 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004961 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004962 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004963 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004964 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004966 /* Display a '$' after the line or highlight an extra
4967 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4969 /* For a diff line the highlighting continues after the
4970 * "$". */
4971 if (
4972# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004973 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974# ifdef LINE_ATTR
4975 &&
4976# endif
4977# endif
4978# ifdef LINE_ATTR
4979 line_attr == 0
4980# endif
4981 )
4982#endif
4983 {
4984#ifdef FEAT_VIRTUALEDIT
4985 /* In virtualedit, visual selections may extend
4986 * beyond end of line. */
4987 if (area_highlighting && virtual_active()
4988 && tocol != MAXCOL && vcol < tocol)
4989 n_extra = 0;
4990 else
4991#endif
4992 {
4993 p_extra = at_end_str;
4994 n_extra = 1;
4995 c_extra = NUL;
4996 }
4997 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004998 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004999 c = lcs_eol;
5000 else
5001 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 lcs_eol_one = -1;
5003 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005004 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005006 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 n_attr = 1;
5008 }
5009#ifdef FEAT_MBYTE
5010 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005011 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
5013 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005014 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005015 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 else
5018 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5019#endif
5020 }
5021 else if (c != NUL)
5022 {
5023 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005024 if (n_extra == 0)
5025 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026#ifdef FEAT_RIGHTLEFT
5027 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5028 rl_mirror(p_extra); /* reverse "<12>" */
5029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005031#ifdef FEAT_LINEBREAK
5032 if (wp->w_p_lbr)
5033 {
5034 char_u *p;
5035
5036 c = *p_extra;
5037 p = alloc((unsigned)n_extra + 1);
5038 vim_memset(p, ' ', n_extra);
5039 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5040 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005041 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005042 p_extra_free = p_extra = p;
5043 }
5044 else
5045#endif
5046 {
5047 n_extra = byte2cells(c) - 1;
5048 c = *p_extra++;
5049 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005050 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 {
5052 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005053 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 saved_attr2 = char_attr; /* save current attr */
5055 }
5056#ifdef FEAT_MBYTE
5057 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5058#endif
5059 }
5060#ifdef FEAT_VIRTUALEDIT
5061 else if (VIsual_active
5062 && (VIsual_mode == Ctrl_V
5063 || VIsual_mode == 'v')
5064 && virtual_active()
5065 && tocol != MAXCOL
5066 && vcol < tocol
5067 && (
5068# ifdef FEAT_RIGHTLEFT
5069 wp->w_p_rl ? (col >= 0) :
5070# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005071 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
5073 c = ' ';
5074 --ptr; /* put it back at the NUL */
5075 }
5076#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005077#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 else if ((
5079# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005080 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005082# ifdef FEAT_TERMINAL
5083 term_attr != 0 ||
5084# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 ) && (
5087# ifdef FEAT_RIGHTLEFT
5088 wp->w_p_rl ? (col >= 0) :
5089# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005090 (col
5091# ifdef FEAT_CONCEAL
5092 - boguscols
5093# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005094 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
5096 /* Highlight until the right side of the window */
5097 c = ' ';
5098 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005099
5100 /* Remember we do the char for line highlighting. */
5101 ++did_line_attr;
5102
5103 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005104 if (line_attr != 0 && char_attr == search_attr
5105 && (did_line_attr > 1
5106 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005107 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108# ifdef FEAT_DIFF
5109 if (diff_hlf == HLF_TXD)
5110 {
5111 diff_hlf = HLF_CHD;
5112 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005113 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005114 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005115 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5116 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005117 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005121# ifdef FEAT_TERMINAL
5122 if (term_attr != 0)
5123 {
5124 char_attr = term_attr;
5125 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5126 char_attr = hl_combine_attr(char_attr,
5127 HL_ATTR(HLF_CUL));
5128 }
5129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131#endif
5132 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005133
5134#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005135 if ( wp->w_p_cole > 0
5136 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005137 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005138 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005139 && !(lnum_in_visual_area
5140 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005141 {
5142 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005143 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005144 && (syn_get_sub_char() != NUL || match_conc
5145 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005146 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005147 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005148 /* First time at this concealed item: display one
5149 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005150 if (match_conc)
5151 c = match_conc;
5152 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005153 c = syn_get_sub_char();
5154 else if (lcs_conceal != NUL)
5155 c = lcs_conceal;
5156 else
5157 c = ' ';
5158
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005159 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005160
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005161 if (n_extra > 0)
5162 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005163 vcol += n_extra;
5164 if (wp->w_p_wrap && n_extra > 0)
5165 {
5166# ifdef FEAT_RIGHTLEFT
5167 if (wp->w_p_rl)
5168 {
5169 col -= n_extra;
5170 boguscols -= n_extra;
5171 }
5172 else
5173# endif
5174 {
5175 boguscols += n_extra;
5176 col += n_extra;
5177 }
5178 }
5179 n_extra = 0;
5180 n_attr = 0;
5181 }
5182 else if (n_skip == 0)
5183 {
5184 is_concealing = TRUE;
5185 n_skip = 1;
5186 }
5187# ifdef FEAT_MBYTE
5188 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005189 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005190 {
5191 mb_utf8 = TRUE;
5192 u8cc[0] = 0;
5193 c = 0xc0;
5194 }
5195 else
5196 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5197# endif
5198 }
5199 else
5200 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005201 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005202 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005203 }
5204#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
5206
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005207#ifdef FEAT_CONCEAL
5208 /* In the cursor line and we may be concealing characters: correct
5209 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005210 if (!did_wcol && draw_state == WL_LINE
5211 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005212 && conceal_cursor_line(wp)
5213 && (int)wp->w_virtcol <= vcol + n_skip)
5214 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005215# ifdef FEAT_RIGHTLEFT
5216 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005217 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005218 else
5219# endif
5220 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005221 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005222 did_wcol = TRUE;
5223 }
5224#endif
5225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 /* Don't override visual selection highlighting. */
5227 if (n_attr > 0
5228 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005229 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 char_attr = extra_attr;
5231
Bram Moolenaar81695252004-12-29 20:58:21 +00005232#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 /* XIM don't send preedit_start and preedit_end, but they send
5234 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5235 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005236 if (p_imst == IM_ON_THE_SPOT
5237 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005238 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 && (State & INSERT)
5240 && !p_imdisable
5241 && im_is_preediting()
5242 && draw_state == WL_LINE)
5243 {
5244 colnr_T tcol;
5245
5246 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005247 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 else
5249 tcol = preedit_end_col;
5250 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5251 {
5252 if (feedback_old_attr < 0)
5253 {
5254 feedback_col = 0;
5255 feedback_old_attr = char_attr;
5256 }
5257 char_attr = im_get_feedback_attr(feedback_col);
5258 if (char_attr < 0)
5259 char_attr = feedback_old_attr;
5260 feedback_col++;
5261 }
5262 else if (feedback_old_attr >= 0)
5263 {
5264 char_attr = feedback_old_attr;
5265 feedback_old_attr = -1;
5266 feedback_col = 0;
5267 }
5268 }
5269#endif
5270 /*
5271 * Handle the case where we are in column 0 but not on the first
5272 * character of the line and the user wants us to show us a
5273 * special character (via 'listchars' option "precedes:<char>".
5274 */
5275 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005276 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5278#ifdef FEAT_DIFF
5279 && filler_todo <= 0
5280#endif
5281 && draw_state > WL_NR
5282 && c != NUL)
5283 {
5284 c = lcs_prec;
5285 lcs_prec_todo = NUL;
5286#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005287 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5288 {
5289 /* Double-width character being overwritten by the "precedes"
5290 * character, need to fill up half the character. */
5291 c_extra = MB_FILLER_CHAR;
5292 n_extra = 1;
5293 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005294 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005297 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 {
5299 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005300 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005301 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 }
5303 else
5304 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5305#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005306 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {
5308 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005309 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 n_attr3 = 1;
5311 }
5312 }
5313
5314 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005315 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005317 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005318#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005319 || did_line_attr == 1
5320#endif
5321 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005323#ifdef FEAT_SEARCH_EXTRA
5324 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005325
5326 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005327 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005328 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005329#endif
5330
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005331 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 * highlight match at end of line. If it's beyond the last
5333 * char on the screen, just overwrite that one (tricky!) Not
5334 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005335#ifdef FEAT_SEARCH_EXTRA
5336 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005337 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005338 prevcol_hl_flag = TRUE;
5339 else
5340 {
5341 cur = wp->w_match_head;
5342 while (cur != NULL)
5343 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005344 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005345 {
5346 prevcol_hl_flag = TRUE;
5347 break;
5348 }
5349 cur = cur->next;
5350 }
5351 }
5352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005354 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005355 && (VIsual_mode != Ctrl_V
5356 || lnum == VIsual.lnum
5357 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005358 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359#ifdef FEAT_SEARCH_EXTRA
5360 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005361 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005362# ifdef FEAT_SYN_HL
5363 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5364 && !(wp == curwin && VIsual_active))
5365# endif
5366# ifdef FEAT_DIFF
5367 && diff_hlf == (hlf_T)0
5368# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005369# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005370 && did_line_attr <= 1
5371# endif
5372 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373#endif
5374 ))
5375 {
5376 int n = 0;
5377
5378#ifdef FEAT_RIGHTLEFT
5379 if (wp->w_p_rl)
5380 {
5381 if (col < 0)
5382 n = 1;
5383 }
5384 else
5385#endif
5386 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005387 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 n = -1;
5389 }
5390 if (n != 0)
5391 {
5392 /* At the window boundary, highlight the last character
5393 * instead (better than nothing). */
5394 off += n;
5395 col += n;
5396 }
5397 else
5398 {
5399 /* Add a blank character to highlight. */
5400 ScreenLines[off] = ' ';
5401#ifdef FEAT_MBYTE
5402 if (enc_utf8)
5403 ScreenLinesUC[off] = 0;
5404#endif
5405 }
5406#ifdef FEAT_SEARCH_EXTRA
5407 if (area_attr == 0)
5408 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005409 /* Use attributes from match with highest priority among
5410 * 'search_hl' and the match list. */
5411 char_attr = search_hl.attr;
5412 cur = wp->w_match_head;
5413 shl_flag = FALSE;
5414 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005415 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005416 if (shl_flag == FALSE
5417 && ((cur != NULL
5418 && cur->priority > SEARCH_HL_PRIORITY)
5419 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005420 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005421 shl = &search_hl;
5422 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005423 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005424 else
5425 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005426 if ((ptr - line) - 1 == (long)shl->startcol
5427 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005428 char_attr = shl->attr;
5429 if (shl != &search_hl && cur != NULL)
5430 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 }
5433#endif
5434 ScreenAttrs[off] = char_attr;
5435#ifdef FEAT_RIGHTLEFT
5436 if (wp->w_p_rl)
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 Moolenaar071d4272004-06-13 20:20:40 +00005441 else
5442#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005445 ++off;
5446 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005447 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005448#ifdef FEAT_SYN_HL
5449 eol_hl_off = 1;
5450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
Bram Moolenaar91170f82006-05-05 21:15:17 +00005454 /*
5455 * At end of the text line.
5456 */
5457 if (c == NUL)
5458 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005459#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005460 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5461 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005462 {
5463 /* highlight last char after line */
5464 --col;
5465 --off;
5466 --vcol;
5467 }
5468
Bram Moolenaar1a384422010-07-14 19:53:30 +02005469 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005470 if (wp->w_p_wrap)
5471 v = wp->w_skipcol;
5472 else
5473 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005474
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005475 /* check if line ends before left margin */
5476 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005477 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005478#ifdef FEAT_CONCEAL
5479 /* Get rid of the boguscols now, we want to draw until the right
5480 * edge for 'cursorcolumn'. */
5481 col -= boguscols;
5482 boguscols = 0;
5483#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005484
5485 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005486 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005487
5488 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005489 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5490 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005491 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005492 && lnum != wp->w_cursor.lnum)
5493 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005494# ifdef FEAT_RIGHTLEFT
5495 && !wp->w_p_rl
5496# endif
5497 )
5498 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005499 int rightmost_vcol = 0;
5500 int i;
5501
5502 if (wp->w_p_cuc)
5503 rightmost_vcol = wp->w_virtcol;
5504 if (draw_color_col)
5505 /* determine rightmost colorcolumn to possibly draw */
5506 for (i = 0; color_cols[i] >= 0; ++i)
5507 if (rightmost_vcol < color_cols[i])
5508 rightmost_vcol = color_cols[i];
5509
Bram Moolenaar02631462017-09-22 15:20:32 +02005510 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005511 {
5512 ScreenLines[off] = ' ';
5513#ifdef FEAT_MBYTE
5514 if (enc_utf8)
5515 ScreenLinesUC[off] = 0;
5516#endif
5517 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005518 if (draw_color_col)
5519 draw_color_col = advance_color_col(VCOL_HLC,
5520 &color_cols);
5521
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005522 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005523 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005524 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005525 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005526 else
5527 ScreenAttrs[off++] = 0;
5528
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005529 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005530 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005531
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005532 ++vcol;
5533 }
5534 }
5535#endif
5536
Bram Moolenaar53f81742017-09-22 14:35:51 +02005537 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005538 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 row++;
5540
5541 /*
5542 * Update w_cline_height and w_cline_folded if the cursor line was
5543 * updated (saves a call to plines() later).
5544 */
5545 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5546 {
5547 curwin->w_cline_row = startrow;
5548 curwin->w_cline_height = row - startrow;
5549#ifdef FEAT_FOLDING
5550 curwin->w_cline_folded = FALSE;
5551#endif
5552 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5553 }
5554
5555 break;
5556 }
5557
5558 /* line continues beyond line end */
5559 if (lcs_ext
5560 && !wp->w_p_wrap
5561#ifdef FEAT_DIFF
5562 && filler_todo <= 0
5563#endif
5564 && (
5565#ifdef FEAT_RIGHTLEFT
5566 wp->w_p_rl ? col == 0 :
5567#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005568 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005570 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5572 {
5573 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005574 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575#ifdef FEAT_MBYTE
5576 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005577 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 {
5579 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005580 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005581 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583 else
5584 mb_utf8 = FALSE;
5585#endif
5586 }
5587
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005588#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005589 /* advance to the next 'colorcolumn' */
5590 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005591 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005592
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005593 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005594 * highlight the cursor position itself.
5595 * Also highlight the 'colorcolumn' if it is different than
5596 * 'cursorcolumn' */
5597 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005598 if (draw_state == WL_LINE && !lnum_in_visual_area
5599 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005600 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005601 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005602 && lnum != wp->w_cursor.lnum)
5603 {
5604 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005605 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005606 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005607 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005608 {
5609 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005610 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005611 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005612 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005613#endif
5614
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 /*
5616 * Store character to be displayed.
5617 * Skip characters that are left of the screen for 'nowrap'.
5618 */
5619 vcol_prev = vcol;
5620 if (draw_state < WL_LINE || n_skip <= 0)
5621 {
5622 /*
5623 * Store the character.
5624 */
5625#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5626 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5627 {
5628 /* A double-wide character is: put first halve in left cell. */
5629 --off;
5630 --col;
5631 }
5632#endif
5633 ScreenLines[off] = c;
5634#ifdef FEAT_MBYTE
5635 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005636 {
5637 if ((mb_c & 0xff00) == 0x8e00)
5638 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 else if (enc_utf8)
5642 {
5643 if (mb_utf8)
5644 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005645 int i;
5646
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005648 if ((c & 0xff) == 0)
5649 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005650 for (i = 0; i < Screen_mco; ++i)
5651 {
5652 ScreenLinesC[i][off] = u8cc[i];
5653 if (u8cc[i] == 0)
5654 break;
5655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 }
5657 else
5658 ScreenLinesUC[off] = 0;
5659 }
5660 if (multi_attr)
5661 {
5662 ScreenAttrs[off] = multi_attr;
5663 multi_attr = 0;
5664 }
5665 else
5666#endif
5667 ScreenAttrs[off] = char_attr;
5668
5669#ifdef FEAT_MBYTE
5670 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5671 {
5672 /* Need to fill two screen columns. */
5673 ++off;
5674 ++col;
5675 if (enc_utf8)
5676 /* UTF-8: Put a 0 in the second screen char. */
5677 ScreenLines[off] = 0;
5678 else
5679 /* DBCS: Put second byte in the second screen char. */
5680 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005681 if (draw_state > WL_NR
5682#ifdef FEAT_DIFF
5683 && filler_todo <= 0
5684#endif
5685 )
5686 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 /* When "tocol" is halfway a character, set it to the end of
5688 * the character, otherwise highlighting won't stop. */
5689 if (tocol == vcol)
5690 ++tocol;
5691#ifdef FEAT_RIGHTLEFT
5692 if (wp->w_p_rl)
5693 {
5694 /* now it's time to backup one cell */
5695 --off;
5696 --col;
5697 }
5698#endif
5699 }
5700#endif
5701#ifdef FEAT_RIGHTLEFT
5702 if (wp->w_p_rl)
5703 {
5704 --off;
5705 --col;
5706 }
5707 else
5708#endif
5709 {
5710 ++off;
5711 ++col;
5712 }
5713 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005714#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005715 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005716 {
5717 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005718 ++vcol_off;
5719 if (n_extra > 0)
5720 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005721 if (wp->w_p_wrap)
5722 {
5723 /*
5724 * Special voodoo required if 'wrap' is on.
5725 *
5726 * Advance the column indicator to force the line
5727 * drawing to wrap early. This will make the line
5728 * take up the same screen space when parts are concealed,
5729 * so that cursor line computations aren't messed up.
5730 *
5731 * To avoid the fictitious advance of 'col' causing
5732 * trailing junk to be written out of the screen line
5733 * we are building, 'boguscols' keeps track of the number
5734 * of bad columns we have advanced.
5735 */
5736 if (n_extra > 0)
5737 {
5738 vcol += n_extra;
5739# ifdef FEAT_RIGHTLEFT
5740 if (wp->w_p_rl)
5741 {
5742 col -= n_extra;
5743 boguscols -= n_extra;
5744 }
5745 else
5746# endif
5747 {
5748 col += n_extra;
5749 boguscols += n_extra;
5750 }
5751 n_extra = 0;
5752 n_attr = 0;
5753 }
5754
5755
5756# ifdef FEAT_MBYTE
5757 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5758 {
5759 /* Need to fill two screen columns. */
5760# ifdef FEAT_RIGHTLEFT
5761 if (wp->w_p_rl)
5762 {
5763 --boguscols;
5764 --col;
5765 }
5766 else
5767# endif
5768 {
5769 ++boguscols;
5770 ++col;
5771 }
5772 }
5773# endif
5774
5775# ifdef FEAT_RIGHTLEFT
5776 if (wp->w_p_rl)
5777 {
5778 --boguscols;
5779 --col;
5780 }
5781 else
5782# endif
5783 {
5784 ++boguscols;
5785 ++col;
5786 }
5787 }
5788 else
5789 {
5790 if (n_extra > 0)
5791 {
5792 vcol += n_extra;
5793 n_extra = 0;
5794 n_attr = 0;
5795 }
5796 }
5797
5798 }
5799#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 else
5801 --n_skip;
5802
Bram Moolenaar64486672010-05-16 15:46:46 +02005803 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5804 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005805 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806#ifdef FEAT_DIFF
5807 && filler_todo <= 0
5808#endif
5809 )
5810 ++vcol;
5811
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005812#ifdef FEAT_SYN_HL
5813 if (vcol_save_attr >= 0)
5814 char_attr = vcol_save_attr;
5815#endif
5816
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 /* restore attributes after "predeces" in 'listchars' */
5818 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5819 char_attr = saved_attr3;
5820
5821 /* restore attributes after last 'listchars' or 'number' char */
5822 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5823 char_attr = saved_attr2;
5824
5825 /*
5826 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005827 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 */
5829 if ((
5830#ifdef FEAT_RIGHTLEFT
5831 wp->w_p_rl ? (col < 0) :
5832#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005833 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 && (*ptr != NUL
5835#ifdef FEAT_DIFF
5836 || filler_todo > 0
5837#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005838 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5840 )
5841 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005842#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005843 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005844 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005845 boguscols = 0;
5846#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005847 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005848 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 ++row;
5851 ++screen_row;
5852
5853 /* When not wrapping and finished diff lines, or when displayed
5854 * '$' and highlighting until last column, break here. */
5855 if ((!wp->w_p_wrap
5856#ifdef FEAT_DIFF
5857 && filler_todo <= 0
5858#endif
5859 ) || lcs_eol_one == -1)
5860 break;
5861
5862 /* When the window is too narrow draw all "@" lines. */
5863 if (draw_state != WL_LINE
5864#ifdef FEAT_DIFF
5865 && filler_todo <= 0
5866#endif
5867 )
5868 {
5869 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 row = endrow;
5872 }
5873
5874 /* When line got too long for screen break here. */
5875 if (row == endrow)
5876 {
5877 ++row;
5878 break;
5879 }
5880
5881 if (screen_cur_row == screen_row - 1
5882#ifdef FEAT_DIFF
5883 && filler_todo <= 0
5884#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005885 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 {
5887 /* Remember that the line wraps, used for modeless copy. */
5888 LineWraps[screen_row - 1] = TRUE;
5889
5890 /*
5891 * Special trick to make copy/paste of wrapped lines work with
5892 * xterm/screen: write an extra character beyond the end of
5893 * the line. This will work with all terminal types
5894 * (regardless of the xn,am settings).
5895 * Only do this on a fast tty.
5896 * Only do this if the cursor is on the current line
5897 * (something has been written in it).
5898 * Don't do this for the GUI.
5899 * Don't do this for double-width characters.
5900 * Don't do this for a window not at the right screen border.
5901 */
5902 if (p_tf
5903#ifdef FEAT_GUI
5904 && !gui.in_use
5905#endif
5906#ifdef FEAT_MBYTE
5907 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005908 && ((*mb_off2cells)(LineOffset[screen_row],
5909 LineOffset[screen_row] + screen_Columns)
5910 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005912 + (int)Columns - 2,
5913 LineOffset[screen_row] + screen_Columns)
5914 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915#endif
5916 )
5917 {
5918 /* First make sure we are at the end of the screen line,
5919 * then output the same character again to let the
5920 * terminal know about the wrap. If the terminal doesn't
5921 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005922 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 screen_char(LineOffset[screen_row - 1]
5924 + (unsigned)Columns - 1,
5925 screen_row - 1, (int)(Columns - 1));
5926
5927#ifdef FEAT_MBYTE
5928 /* When there is a multi-byte character, just output a
5929 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005930 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5931 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 out_char(' ');
5933 else
5934#endif
5935 out_char(ScreenLines[LineOffset[screen_row - 1]
5936 + (Columns - 1)]);
5937 /* force a redraw of the first char on the next line */
5938 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5939 screen_start(); /* don't know where cursor is now */
5940 }
5941 }
5942
5943 col = 0;
5944 off = (unsigned)(current_ScreenLine - ScreenLines);
5945#ifdef FEAT_RIGHTLEFT
5946 if (wp->w_p_rl)
5947 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005948 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 off += col;
5950 }
5951#endif
5952
5953 /* reset the drawing state for the start of a wrapped line */
5954 draw_state = WL_START;
5955 saved_n_extra = n_extra;
5956 saved_p_extra = p_extra;
5957 saved_c_extra = c_extra;
5958 saved_char_attr = char_attr;
5959 n_extra = 0;
5960 lcs_prec_todo = lcs_prec;
5961#ifdef FEAT_LINEBREAK
5962# ifdef FEAT_DIFF
5963 if (filler_todo <= 0)
5964# endif
5965 need_showbreak = TRUE;
5966#endif
5967#ifdef FEAT_DIFF
5968 --filler_todo;
5969 /* When the filler lines are actually below the last line of the
5970 * file, don't draw the line itself, break here. */
5971 if (filler_todo == 0 && wp->w_botfill)
5972 break;
5973#endif
5974 }
5975
5976 } /* for every character in the line */
5977
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005978#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005979 /* After an empty line check first word for capital. */
5980 if (*skipwhite(line) == NUL)
5981 {
5982 capcol_lnum = lnum + 1;
5983 cap_col = 0;
5984 }
5985#endif
5986
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005987 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 return row;
5989}
5990
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005991#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005992static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005993
5994/*
5995 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005996 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005997 */
5998 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005999comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006000{
6001 int i;
6002
6003 for (i = 0; i < Screen_mco; ++i)
6004 {
6005 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6006 return TRUE;
6007 if (ScreenLinesC[i][off_from] == 0)
6008 break;
6009 }
6010 return FALSE;
6011}
6012#endif
6013
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014/*
6015 * Check whether the given character needs redrawing:
6016 * - the (first byte of the) character is different
6017 * - the attributes are different
6018 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006019 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 */
6021 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006022char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023{
6024 if (cols > 0
6025 && ((ScreenLines[off_from] != ScreenLines[off_to]
6026 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6027
6028#ifdef FEAT_MBYTE
6029 || (enc_dbcs != 0
6030 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6031 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6032 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6033 : (cols > 1 && ScreenLines[off_from + 1]
6034 != ScreenLines[off_to + 1])))
6035 || (enc_utf8
6036 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6037 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006038 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006039 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6040 && ScreenLines[off_from + 1]
6041 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042#endif
6043 ))
6044 return TRUE;
6045 return FALSE;
6046}
6047
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006048#if defined(FEAT_TERMINAL) || defined(PROTO)
6049/*
6050 * Return the index in ScreenLines[] for the current screen line.
6051 */
6052 int
6053screen_get_current_line_off()
6054{
6055 return (int)(current_ScreenLine - ScreenLines);
6056}
6057#endif
6058
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059/*
6060 * Move one "cooked" screen line to the screen, but only the characters that
6061 * have actually changed. Handle insert/delete character.
6062 * "coloff" gives the first column on the screen for this line.
6063 * "endcol" gives the columns where valid characters are.
6064 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6065 * needs to be cleared, negative otherwise.
6066 * "rlflag" is TRUE in a rightleft window:
6067 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6068 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6069 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006070 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006071screen_line(
6072 int row,
6073 int coloff,
6074 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006075 int clear_width,
6076 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
6078 unsigned off_from;
6079 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006080#ifdef FEAT_MBYTE
6081 unsigned max_off_from;
6082 unsigned max_off_to;
6083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 int force = FALSE; /* force update rest of the line */
6087 int redraw_this /* bool: does character need redraw? */
6088#ifdef FEAT_GUI
6089 = TRUE /* For GUI when while-loop empty */
6090#endif
6091 ;
6092 int redraw_next; /* redraw_this for next character */
6093#ifdef FEAT_MBYTE
6094 int clear_next = FALSE;
6095 int char_cells; /* 1: normal char */
6096 /* 2: occupies two display cells */
6097# define CHAR_CELLS char_cells
6098#else
6099# define CHAR_CELLS 1
6100#endif
6101
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006102 /* Check for illegal row and col, just in case. */
6103 if (row >= Rows)
6104 row = Rows - 1;
6105 if (endcol > Columns)
6106 endcol = Columns;
6107
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108# ifdef FEAT_CLIPBOARD
6109 clip_may_clear_selection(row, row);
6110# endif
6111
6112 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6113 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006114#ifdef FEAT_MBYTE
6115 max_off_from = off_from + screen_Columns;
6116 max_off_to = LineOffset[row] + screen_Columns;
6117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
6119#ifdef FEAT_RIGHTLEFT
6120 if (rlflag)
6121 {
6122 /* Clear rest first, because it's left of the text. */
6123 if (clear_width > 0)
6124 {
6125 while (col <= endcol && ScreenLines[off_to] == ' '
6126 && ScreenAttrs[off_to] == 0
6127# ifdef FEAT_MBYTE
6128 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6129# endif
6130 )
6131 {
6132 ++off_to;
6133 ++col;
6134 }
6135 if (col <= endcol)
6136 screen_fill(row, row + 1, col + coloff,
6137 endcol + coloff + 1, ' ', ' ', 0);
6138 }
6139 col = endcol + 1;
6140 off_to = LineOffset[row] + col + coloff;
6141 off_from += col;
6142 endcol = (clear_width > 0 ? clear_width : -clear_width);
6143 }
6144#endif /* FEAT_RIGHTLEFT */
6145
6146 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6147
6148 while (col < endcol)
6149 {
6150#ifdef FEAT_MBYTE
6151 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006152 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 else
6154 char_cells = 1;
6155#endif
6156
6157 redraw_this = redraw_next;
6158 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6159 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6160
6161#ifdef FEAT_GUI
6162 /* If the next character was bold, then redraw the current character to
6163 * remove any pixels that might have spilt over into us. This only
6164 * happens in the GUI.
6165 */
6166 if (redraw_next && gui.in_use)
6167 {
6168 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006169 if (hl > HL_ALL)
6170 hl = syn_attr2attr(hl);
6171 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 redraw_this = TRUE;
6173 }
6174#endif
6175
6176 if (redraw_this)
6177 {
6178 /*
6179 * Special handling when 'xs' termcap flag set (hpterm):
6180 * Attributes for characters are stored at the position where the
6181 * cursor is when writing the highlighting code. The
6182 * start-highlighting code must be written with the cursor on the
6183 * first highlighted character. The stop-highlighting code must
6184 * be written with the cursor just after the last highlighted
6185 * character.
6186 * Overwriting a character doesn't remove it's highlighting. Need
6187 * to clear the rest of the line, and force redrawing it
6188 * completely.
6189 */
6190 if ( p_wiv
6191 && !force
6192#ifdef FEAT_GUI
6193 && !gui.in_use
6194#endif
6195 && ScreenAttrs[off_to] != 0
6196 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6197 {
6198 /*
6199 * Need to remove highlighting attributes here.
6200 */
6201 windgoto(row, col + coloff);
6202 out_str(T_CE); /* clear rest of this screen line */
6203 screen_start(); /* don't know where cursor is now */
6204 force = TRUE; /* force redraw of rest of the line */
6205 redraw_next = TRUE; /* or else next char would miss out */
6206
6207 /*
6208 * If the previous character was highlighted, need to stop
6209 * highlighting at this character.
6210 */
6211 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6212 {
6213 screen_attr = ScreenAttrs[off_to - 1];
6214 term_windgoto(row, col + coloff);
6215 screen_stop_highlight();
6216 }
6217 else
6218 screen_attr = 0; /* highlighting has stopped */
6219 }
6220#ifdef FEAT_MBYTE
6221 if (enc_dbcs != 0)
6222 {
6223 /* Check if overwriting a double-byte with a single-byte or
6224 * the other way around requires another character to be
6225 * redrawn. For UTF-8 this isn't needed, because comparing
6226 * ScreenLinesUC[] is sufficient. */
6227 if (char_cells == 1
6228 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006229 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
6231 /* Writing a single-cell character over a double-cell
6232 * character: need to redraw the next cell. */
6233 ScreenLines[off_to + 1] = 0;
6234 redraw_next = TRUE;
6235 }
6236 else if (char_cells == 2
6237 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006238 && (*mb_off2cells)(off_to, max_off_to) == 1
6239 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 {
6241 /* Writing the second half of a double-cell character over
6242 * a double-cell character: need to redraw the second
6243 * cell. */
6244 ScreenLines[off_to + 2] = 0;
6245 redraw_next = TRUE;
6246 }
6247
6248 if (enc_dbcs == DBCS_JPNU)
6249 ScreenLines2[off_to] = ScreenLines2[off_from];
6250 }
6251 /* When writing a single-width character over a double-width
6252 * character and at the end of the redrawn text, need to clear out
6253 * the right halve of the old character.
6254 * Also required when writing the right halve of a double-width
6255 * char over the left halve of an existing one. */
6256 if (has_mbyte && col + char_cells == endcol
6257 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006258 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006260 && (*mb_off2cells)(off_to, max_off_to) == 1
6261 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 clear_next = TRUE;
6263#endif
6264
6265 ScreenLines[off_to] = ScreenLines[off_from];
6266#ifdef FEAT_MBYTE
6267 if (enc_utf8)
6268 {
6269 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6270 if (ScreenLinesUC[off_from] != 0)
6271 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006272 int i;
6273
6274 for (i = 0; i < Screen_mco; ++i)
6275 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 }
6277 }
6278 if (char_cells == 2)
6279 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6280#endif
6281
6282#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006283 /* The bold trick makes a single column of pixels appear in the
6284 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 * character should be redrawn too. This happens for our own GUI
6286 * and for some xterms. */
6287 if (
6288# ifdef FEAT_GUI
6289 gui.in_use
6290# endif
6291# if defined(FEAT_GUI) && defined(UNIX)
6292 ||
6293# endif
6294# ifdef UNIX
6295 term_is_xterm
6296# endif
6297 )
6298 {
6299 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006300 if (hl > HL_ALL)
6301 hl = syn_attr2attr(hl);
6302 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 redraw_next = TRUE;
6304 }
6305#endif
6306 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6307#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006308 /* For simplicity set the attributes of second half of a
6309 * double-wide character equal to the first half. */
6310 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006312
6313 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 else
6316#endif
6317 screen_char(off_to, row, col + coloff);
6318 }
6319 else if ( p_wiv
6320#ifdef FEAT_GUI
6321 && !gui.in_use
6322#endif
6323 && col + coloff > 0)
6324 {
6325 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6326 {
6327 /*
6328 * Don't output stop-highlight when moving the cursor, it will
6329 * stop the highlighting when it should continue.
6330 */
6331 screen_attr = 0;
6332 }
6333 else if (screen_attr != 0)
6334 screen_stop_highlight();
6335 }
6336
6337 off_to += CHAR_CELLS;
6338 off_from += CHAR_CELLS;
6339 col += CHAR_CELLS;
6340 }
6341
6342#ifdef FEAT_MBYTE
6343 if (clear_next)
6344 {
6345 /* Clear the second half of a double-wide character of which the left
6346 * half was overwritten with a single-wide character. */
6347 ScreenLines[off_to] = ' ';
6348 if (enc_utf8)
6349 ScreenLinesUC[off_to] = 0;
6350 screen_char(off_to, row, col + coloff);
6351 }
6352#endif
6353
6354 if (clear_width > 0
6355#ifdef FEAT_RIGHTLEFT
6356 && !rlflag
6357#endif
6358 )
6359 {
6360#ifdef FEAT_GUI
6361 int startCol = col;
6362#endif
6363
6364 /* blank out the rest of the line */
6365 while (col < clear_width && ScreenLines[off_to] == ' '
6366 && ScreenAttrs[off_to] == 0
6367#ifdef FEAT_MBYTE
6368 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6369#endif
6370 )
6371 {
6372 ++off_to;
6373 ++col;
6374 }
6375 if (col < clear_width)
6376 {
6377#ifdef FEAT_GUI
6378 /*
6379 * In the GUI, clearing the rest of the line may leave pixels
6380 * behind if the first character cleared was bold. Some bold
6381 * fonts spill over the left. In this case we redraw the previous
6382 * character too. If we didn't skip any blanks above, then we
6383 * only redraw if the character wasn't already redrawn anyway.
6384 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006385 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 {
6387 hl = ScreenAttrs[off_to];
6388 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006389 {
6390 int prev_cells = 1;
6391# ifdef FEAT_MBYTE
6392 if (enc_utf8)
6393 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6394 * that its width is 2. */
6395 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6396 else if (enc_dbcs != 0)
6397 {
6398 /* find previous character by counting from first
6399 * column and get its width. */
6400 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006401 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006402
6403 while (off < off_to)
6404 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006405 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006406 off += prev_cells;
6407 }
6408 }
6409
6410 if (enc_dbcs != 0 && prev_cells > 1)
6411 screen_char_2(off_to - prev_cells, row,
6412 col + coloff - prev_cells);
6413 else
6414# endif
6415 screen_char(off_to - prev_cells, row,
6416 col + coloff - prev_cells);
6417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419#endif
6420 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6421 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 off_to += clear_width - col;
6423 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 }
6425 }
6426
6427 if (clear_width > 0)
6428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 /* For a window that's left of another, draw the separator char. */
6430 if (col + coloff < Columns)
6431 {
6432 int c;
6433
6434 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006435 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006436#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006437 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6438 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 || ScreenAttrs[off_to] != hl)
6441 {
6442 ScreenLines[off_to] = c;
6443 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006444#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 if (enc_utf8)
6446 {
6447 if (c >= 0x80)
6448 {
6449 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006450 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 }
6452 else
6453 ScreenLinesUC[off_to] = 0;
6454 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 screen_char(off_to, row, col + coloff);
6457 }
6458 }
6459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 LineWraps[row] = FALSE;
6461 }
6462}
6463
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006464#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006466 * Mirror text "str" for right-left displaying.
6467 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006469 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006470rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 char_u *p1, *p2;
6473 int t;
6474
6475 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6476 {
6477 t = *p1;
6478 *p1 = *p2;
6479 *p2 = t;
6480 }
6481}
6482#endif
6483
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484/*
6485 * mark all status lines for redraw; used after first :cd
6486 */
6487 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006488status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
6490 win_T *wp;
6491
Bram Moolenaar29323592016-07-24 22:04:11 +02006492 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 if (wp->w_status_height)
6494 {
6495 wp->w_redr_status = TRUE;
6496 redraw_later(VALID);
6497 }
6498}
6499
6500/*
6501 * mark all status lines of the current buffer for redraw
6502 */
6503 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006504status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
6506 win_T *wp;
6507
Bram Moolenaar29323592016-07-24 22:04:11 +02006508 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6510 {
6511 wp->w_redr_status = TRUE;
6512 redraw_later(VALID);
6513 }
6514}
6515
6516/*
6517 * Redraw all status lines that need to be redrawn.
6518 */
6519 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006520redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
6522 win_T *wp;
6523
Bram Moolenaar29323592016-07-24 22:04:11 +02006524 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 if (wp->w_redr_status)
6526 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006527 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006528 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
Bram Moolenaar4033c552017-09-16 20:54:51 +02006531#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532/*
6533 * Redraw all status lines at the bottom of frame "frp".
6534 */
6535 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006536win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 if (frp->fr_layout == FR_LEAF)
6539 frp->fr_win->w_redr_status = TRUE;
6540 else if (frp->fr_layout == FR_ROW)
6541 {
6542 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6543 win_redraw_last_status(frp);
6544 }
6545 else /* frp->fr_layout == FR_COL */
6546 {
6547 frp = frp->fr_child;
6548 while (frp->fr_next != NULL)
6549 frp = frp->fr_next;
6550 win_redraw_last_status(frp);
6551 }
6552}
6553#endif
6554
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555/*
6556 * Draw the verticap separator right of window "wp" starting with line "row".
6557 */
6558 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006559draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
6561 int hl;
6562 int c;
6563
6564 if (wp->w_vsep_width)
6565 {
6566 /* draw the vertical separator right of this window */
6567 c = fillchar_vsep(&hl);
6568 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6569 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6570 c, ' ', hl);
6571 }
6572}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573
6574#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006575static int status_match_len(expand_T *xp, char_u *s);
6576static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006579 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 */
6581 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006582status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583{
6584 int len = 0;
6585
6586#ifdef FEAT_MENU
6587 int emenu = (xp->xp_context == EXPAND_MENUS
6588 || xp->xp_context == EXPAND_MENUNAMES);
6589
6590 /* Check for menu separators - replace with '|'. */
6591 if (emenu && menu_is_separator(s))
6592 return 1;
6593#endif
6594
6595 while (*s != NUL)
6596 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006597 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006598 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006599 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 }
6601
6602 return len;
6603}
6604
6605/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006606 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006607 * These are backslashes used for escaping. Do show backslashes in help tags.
6608 */
6609 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006610skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006611{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006612 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006613#ifdef FEAT_MENU
6614 || ((xp->xp_context == EXPAND_MENUS
6615 || xp->xp_context == EXPAND_MENUNAMES)
6616 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6617#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006618 )
6619 {
6620#ifndef BACKSLASH_IN_FILENAME
6621 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6622 return 2;
6623#endif
6624 return 1;
6625 }
6626 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006627}
6628
6629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 * Show wildchar matches in the status line.
6631 * Show at least the "match" item.
6632 * We start at item 'first_match' in the list and show all matches that fit.
6633 *
6634 * If inversion is possible we use it. Else '=' characters are used.
6635 */
6636 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006637win_redr_status_matches(
6638 expand_T *xp,
6639 int num_matches,
6640 char_u **matches, /* list of matches */
6641 int match,
6642 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
6644#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6645 int row;
6646 char_u *buf;
6647 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006648 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 int fillchar;
6650 int attr;
6651 int i;
6652 int highlight = TRUE;
6653 char_u *selstart = NULL;
6654 int selstart_col = 0;
6655 char_u *selend = NULL;
6656 static int first_match = 0;
6657 int add_left = FALSE;
6658 char_u *s;
6659#ifdef FEAT_MENU
6660 int emenu;
6661#endif
6662#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6663 int l;
6664#endif
6665
6666 if (matches == NULL) /* interrupted completion? */
6667 return;
6668
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006669#ifdef FEAT_MBYTE
6670 if (has_mbyte)
6671 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6672 else
6673#endif
6674 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 if (buf == NULL)
6676 return;
6677
6678 if (match == -1) /* don't show match but original text */
6679 {
6680 match = 0;
6681 highlight = FALSE;
6682 }
6683 /* count 1 for the ending ">" */
6684 clen = status_match_len(xp, L_MATCH(match)) + 3;
6685 if (match == 0)
6686 first_match = 0;
6687 else if (match < first_match)
6688 {
6689 /* jumping left, as far as we can go */
6690 first_match = match;
6691 add_left = TRUE;
6692 }
6693 else
6694 {
6695 /* check if match fits on the screen */
6696 for (i = first_match; i < match; ++i)
6697 clen += status_match_len(xp, L_MATCH(i)) + 2;
6698 if (first_match > 0)
6699 clen += 2;
6700 /* jumping right, put match at the left */
6701 if ((long)clen > Columns)
6702 {
6703 first_match = match;
6704 /* if showing the last match, we can add some on the left */
6705 clen = 2;
6706 for (i = match; i < num_matches; ++i)
6707 {
6708 clen += status_match_len(xp, L_MATCH(i)) + 2;
6709 if ((long)clen >= Columns)
6710 break;
6711 }
6712 if (i == num_matches)
6713 add_left = TRUE;
6714 }
6715 }
6716 if (add_left)
6717 while (first_match > 0)
6718 {
6719 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6720 if ((long)clen >= Columns)
6721 break;
6722 --first_match;
6723 }
6724
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006725 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726
6727 if (first_match == 0)
6728 {
6729 *buf = NUL;
6730 len = 0;
6731 }
6732 else
6733 {
6734 STRCPY(buf, "< ");
6735 len = 2;
6736 }
6737 clen = len;
6738
6739 i = first_match;
6740 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6741 {
6742 if (i == match)
6743 {
6744 selstart = buf + len;
6745 selstart_col = clen;
6746 }
6747
6748 s = L_MATCH(i);
6749 /* Check for menu separators - replace with '|' */
6750#ifdef FEAT_MENU
6751 emenu = (xp->xp_context == EXPAND_MENUS
6752 || xp->xp_context == EXPAND_MENUNAMES);
6753 if (emenu && menu_is_separator(s))
6754 {
6755 STRCPY(buf + len, transchar('|'));
6756 l = (int)STRLEN(buf + len);
6757 len += l;
6758 clen += l;
6759 }
6760 else
6761#endif
6762 for ( ; *s != NUL; ++s)
6763 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006764 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 clen += ptr2cells(s);
6766#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006767 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 {
6769 STRNCPY(buf + len, s, l);
6770 s += l - 1;
6771 len += l;
6772 }
6773 else
6774#endif
6775 {
6776 STRCPY(buf + len, transchar_byte(*s));
6777 len += (int)STRLEN(buf + len);
6778 }
6779 }
6780 if (i == match)
6781 selend = buf + len;
6782
6783 *(buf + len++) = ' ';
6784 *(buf + len++) = ' ';
6785 clen += 2;
6786 if (++i == num_matches)
6787 break;
6788 }
6789
6790 if (i != num_matches)
6791 {
6792 *(buf + len++) = '>';
6793 ++clen;
6794 }
6795
6796 buf[len] = NUL;
6797
6798 row = cmdline_row - 1;
6799 if (row >= 0)
6800 {
6801 if (wild_menu_showing == 0)
6802 {
6803 if (msg_scrolled > 0)
6804 {
6805 /* Put the wildmenu just above the command line. If there is
6806 * no room, scroll the screen one line up. */
6807 if (cmdline_row == Rows - 1)
6808 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006809 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 ++msg_scrolled;
6811 }
6812 else
6813 {
6814 ++cmdline_row;
6815 ++row;
6816 }
6817 wild_menu_showing = WM_SCROLLED;
6818 }
6819 else
6820 {
6821 /* Create status line if needed by setting 'laststatus' to 2.
6822 * Set 'winminheight' to zero to avoid that the window is
6823 * resized. */
6824 if (lastwin->w_status_height == 0)
6825 {
6826 save_p_ls = p_ls;
6827 save_p_wmh = p_wmh;
6828 p_ls = 2;
6829 p_wmh = 0;
6830 last_status(FALSE);
6831 }
6832 wild_menu_showing = WM_SHOWN;
6833 }
6834 }
6835
6836 screen_puts(buf, row, 0, attr);
6837 if (selstart != NULL && highlight)
6838 {
6839 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006840 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 }
6842
6843 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6844 }
6845
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 vim_free(buf);
6848}
6849#endif
6850
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851/*
6852 * Redraw the status line of window wp.
6853 *
6854 * If inversion is possible we use it. Else '=' characters are used.
6855 */
6856 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006857win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858{
6859 int row;
6860 char_u *p;
6861 int len;
6862 int fillchar;
6863 int attr;
6864 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006865 static int busy = FALSE;
6866
6867 /* It's possible to get here recursively when 'statusline' (indirectly)
6868 * invokes ":redrawstatus". Simply ignore the call then. */
6869 if (busy)
6870 return;
6871 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872
6873 wp->w_redr_status = FALSE;
6874 if (wp->w_status_height == 0)
6875 {
6876 /* no status line, can only be last window */
6877 redraw_cmdline = TRUE;
6878 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006879 else if (!redrawing()
6880#ifdef FEAT_INS_EXPAND
6881 /* don't update status line when popup menu is visible and may be
6882 * drawn over it */
6883 || pum_visible()
6884#endif
6885 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
6887 /* Don't redraw right now, do it later. */
6888 wp->w_redr_status = TRUE;
6889 }
6890#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006891 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 {
6893 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006894 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 }
6896#endif
6897 else
6898 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006899 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006901 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 p = NameBuff;
6903 len = (int)STRLEN(p);
6904
Bram Moolenaard85f2712017-07-28 21:51:57 +02006905 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906#ifdef FEAT_QUICKFIX
6907 || wp->w_p_pvw
6908#endif
6909 || bufIsChanged(wp->w_buffer)
6910 || wp->w_buffer->b_p_ro)
6911 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006912 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006914 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 len += (int)STRLEN(p + len);
6916 }
6917#ifdef FEAT_QUICKFIX
6918 if (wp->w_p_pvw)
6919 {
6920 STRCPY(p + len, _("[Preview]"));
6921 len += (int)STRLEN(p + len);
6922 }
6923#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006924 if (bufIsChanged(wp->w_buffer)
6925#ifdef FEAT_TERMINAL
6926 && !bt_terminal(wp->w_buffer)
6927#endif
6928 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 {
6930 STRCPY(p + len, "[+]");
6931 len += 3;
6932 }
6933 if (wp->w_buffer->b_p_ro)
6934 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006935 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006936 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 }
6938
Bram Moolenaar02631462017-09-22 15:20:32 +02006939 this_ru_col = ru_col - (Columns - wp->w_width);
6940 if (this_ru_col < (wp->w_width + 1) / 2)
6941 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 if (this_ru_col <= 1)
6943 {
6944 p = (char_u *)"<"; /* No room for file name! */
6945 len = 1;
6946 }
6947 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948#ifdef FEAT_MBYTE
6949 if (has_mbyte)
6950 {
6951 int clen = 0, i;
6952
6953 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006954 clen = mb_string2cells(p, -1);
6955
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 /* Find first character that will fit.
6957 * Going from start to end is much faster for DBCS. */
6958 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006959 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 clen -= (*mb_ptr2cells)(p + i);
6961 len = clen;
6962 if (i > 0)
6963 {
6964 p = p + i - 1;
6965 *p = '<';
6966 ++len;
6967 }
6968
6969 }
6970 else
6971#endif
6972 if (len > this_ru_col - 1)
6973 {
6974 p += len - (this_ru_col - 1);
6975 *p = '<';
6976 len = this_ru_col - 1;
6977 }
6978
6979 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006980 screen_puts(p, row, wp->w_wincol, attr);
6981 screen_fill(row, row + 1, len + wp->w_wincol,
6982 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006984 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6986 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006987 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988
6989#ifdef FEAT_CMDL_INFO
6990 win_redr_ruler(wp, TRUE);
6991#endif
6992 }
6993
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 /*
6995 * May need to draw the character below the vertical separator.
6996 */
6997 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6998 {
6999 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007000 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 else
7002 fillchar = fillchar_vsep(&attr);
7003 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7004 attr);
7005 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007006 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007}
7008
Bram Moolenaar238a5642006-02-21 22:12:05 +00007009#ifdef FEAT_STL_OPT
7010/*
7011 * Redraw the status line according to 'statusline' and take care of any
7012 * errors encountered.
7013 */
7014 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007015redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007016{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007017 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007018 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007019
7020 /* When called recursively return. This can happen when the statusline
7021 * contains an expression that triggers a redraw. */
7022 if (entered)
7023 return;
7024 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007025
Bram Moolenaara742e082016-04-05 21:10:38 +02007026 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007027 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007028 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007029 {
7030 /* When there is an error disable the statusline, otherwise the
7031 * display is messed up with errors and a redraw triggers the problem
7032 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007033 set_string_option_direct((char_u *)"statusline", -1,
7034 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007035 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007036 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007037 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007038 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007039}
7040#endif
7041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042/*
7043 * Return TRUE if the status line of window "wp" is connected to the status
7044 * line of the window right of it. If not, then it's a vertical separator.
7045 * Only call if (wp->w_vsep_width != 0).
7046 */
7047 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007048stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049{
7050 frame_T *fr;
7051
7052 fr = wp->w_frame;
7053 while (fr->fr_parent != NULL)
7054 {
7055 if (fr->fr_parent->fr_layout == FR_COL)
7056 {
7057 if (fr->fr_next != NULL)
7058 break;
7059 }
7060 else
7061 {
7062 if (fr->fr_next != NULL)
7063 return TRUE;
7064 }
7065 fr = fr->fr_parent;
7066 }
7067 return FALSE;
7068}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071/*
7072 * Get the value to show for the language mappings, active 'keymap'.
7073 */
7074 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007075get_keymap_str(
7076 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007077 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007078 char_u *buf, /* buffer for the result */
7079 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080{
7081 char_u *p;
7082
7083 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7084 return FALSE;
7085
7086 {
7087#ifdef FEAT_EVAL
7088 buf_T *old_curbuf = curbuf;
7089 win_T *old_curwin = curwin;
7090 char_u *s;
7091
7092 curbuf = wp->w_buffer;
7093 curwin = wp;
7094 STRCPY(buf, "b:keymap_name"); /* must be writable */
7095 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007096 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 --emsg_skip;
7098 curbuf = old_curbuf;
7099 curwin = old_curwin;
7100 if (p == NULL || *p == NUL)
7101#endif
7102 {
7103#ifdef FEAT_KEYMAP
7104 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7105 p = wp->w_buffer->b_p_keymap;
7106 else
7107#endif
7108 p = (char_u *)"lang";
7109 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007110 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 buf[0] = NUL;
7112#ifdef FEAT_EVAL
7113 vim_free(s);
7114#endif
7115 }
7116 return buf[0] != NUL;
7117}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
7119#if defined(FEAT_STL_OPT) || defined(PROTO)
7120/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007121 * Redraw the status line or ruler of window "wp".
7122 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 */
7124 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007125win_redr_custom(
7126 win_T *wp,
7127 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007129 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 int attr;
7131 int curattr;
7132 int row;
7133 int col = 0;
7134 int maxwidth;
7135 int width;
7136 int n;
7137 int len;
7138 int fillchar;
7139 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007140 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007142 struct stl_hlrec hltab[STL_MAX_ITEM];
7143 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007144 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007145 win_T *ewp;
7146 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
Bram Moolenaar1d633412013-12-11 15:52:01 +01007148 /* There is a tiny chance that this gets called recursively: When
7149 * redrawing a status line triggers redrawing the ruler or tabline.
7150 * Avoid trouble by not allowing recursion. */
7151 if (entered)
7152 return;
7153 entered = TRUE;
7154
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007156 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007158 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007159 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007160 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007161 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007162 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007163 maxwidth = Columns;
7164# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007165 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007166# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007168 else
7169 {
7170 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007171 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007172 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007173
7174 if (draw_ruler)
7175 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007176 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007177 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007178 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007179 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007180 if (*++stl == '-')
7181 stl++;
7182 if (atoi((char *)stl))
7183 while (VIM_ISDIGIT(*stl))
7184 stl++;
7185 if (*stl++ != '(')
7186 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007187 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007188 col = ru_col - (Columns - wp->w_width);
7189 if (col < (wp->w_width + 1) / 2)
7190 col = (wp->w_width + 1) / 2;
7191 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007192 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007193 {
7194 row = Rows - 1;
7195 --maxwidth; /* writing in last column may cause scrolling */
7196 fillchar = ' ';
7197 attr = 0;
7198 }
7199
7200# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007201 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007202# endif
7203 }
7204 else
7205 {
7206 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007207 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007208 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007209 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007210# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007211 use_sandbox = was_set_insecurely((char_u *)"statusline",
7212 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007213# endif
7214 }
7215
Bram Moolenaar53f81742017-09-22 14:35:51 +02007216 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007217 }
7218
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007220 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
Bram Moolenaar61452852011-02-01 18:01:11 +01007222 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7223 * the cursor away and back. */
7224 ewp = wp == NULL ? curwin : wp;
7225 p_crb_save = ewp->w_p_crb;
7226 ewp->w_p_crb = FALSE;
7227
Bram Moolenaar362f3562009-11-03 16:20:34 +00007228 /* Make a copy, because the statusline may include a function call that
7229 * might change the option value and free the memory. */
7230 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007231 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007232 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007233 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007234 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007235 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007237 /* Make all characters printable. */
7238 p = transstr(buf);
7239 if (p != NULL)
7240 {
7241 vim_strncpy(buf, p, sizeof(buf) - 1);
7242 vim_free(p);
7243 }
7244
7245 /* fill up with "fillchar" */
7246 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007247 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 {
7249#ifdef FEAT_MBYTE
7250 len += (*mb_char2bytes)(fillchar, buf + len);
7251#else
7252 buf[len++] = fillchar;
7253#endif
7254 ++width;
7255 }
7256 buf[len] = NUL;
7257
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007258 /*
7259 * Draw each snippet with the specified highlighting.
7260 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 curattr = attr;
7262 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007263 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007265 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 screen_puts_len(p, len, row, col, curattr);
7267 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007268 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007270 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007272 else if (hltab[n].userhl < 0)
7273 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007274#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007275 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7276 && wp->w_status_height != 0)
7277 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007278 else if (wp != NULL && bt_terminal(wp->w_buffer)
7279 && wp->w_status_height != 0)
7280 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007281#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007282 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007283 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007285 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 }
7287 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007288
7289 if (wp == NULL)
7290 {
7291 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7292 col = 0;
7293 len = 0;
7294 p = buf;
7295 fillchar = 0;
7296 for (n = 0; tabtab[n].start != NULL; n++)
7297 {
7298 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7299 while (col < len)
7300 TabPageIdxs[col++] = fillchar;
7301 p = tabtab[n].start;
7302 fillchar = tabtab[n].userhl;
7303 }
7304 while (col < Columns)
7305 TabPageIdxs[col++] = fillchar;
7306 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007307
7308theend:
7309 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310}
7311
7312#endif /* FEAT_STL_OPT */
7313
7314/*
7315 * Output a single character directly to the screen and update ScreenLines.
7316 */
7317 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007318screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 char_u buf[MB_MAXBYTES + 1];
7321
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007322#ifdef FEAT_MBYTE
7323 if (has_mbyte)
7324 buf[(*mb_char2bytes)(c, buf)] = NUL;
7325 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007327 {
7328 buf[0] = c;
7329 buf[1] = NUL;
7330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 screen_puts(buf, row, col, attr);
7332}
7333
7334/*
7335 * Get a single character directly from ScreenLines into "bytes[]".
7336 * Also return its attribute in *attrp;
7337 */
7338 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007339screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340{
7341 unsigned off;
7342
7343 /* safety check */
7344 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7345 {
7346 off = LineOffset[row] + col;
7347 *attrp = ScreenAttrs[off];
7348 bytes[0] = ScreenLines[off];
7349 bytes[1] = NUL;
7350
7351#ifdef FEAT_MBYTE
7352 if (enc_utf8 && ScreenLinesUC[off] != 0)
7353 bytes[utfc_char2bytes(off, bytes)] = NUL;
7354 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7355 {
7356 bytes[0] = ScreenLines[off];
7357 bytes[1] = ScreenLines2[off];
7358 bytes[2] = NUL;
7359 }
7360 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7361 {
7362 bytes[1] = ScreenLines[off + 1];
7363 bytes[2] = NUL;
7364 }
7365#endif
7366 }
7367}
7368
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007369#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007370static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007371
7372/*
7373 * Return TRUE if composing characters for screen posn "off" differs from
7374 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007375 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007376 */
7377 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007378screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007379{
7380 int i;
7381
7382 for (i = 0; i < Screen_mco; ++i)
7383 {
7384 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7385 return TRUE;
7386 if (u8cc[i] == 0)
7387 break;
7388 }
7389 return FALSE;
7390}
7391#endif
7392
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393/*
7394 * Put string '*text' on the screen at position 'row' and 'col', with
7395 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7396 * Note: only outputs within one row, message is truncated at screen boundary!
7397 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7398 */
7399 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007400screen_puts(
7401 char_u *text,
7402 int row,
7403 int col,
7404 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405{
7406 screen_puts_len(text, -1, row, col, attr);
7407}
7408
7409/*
7410 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7411 * a NUL.
7412 */
7413 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007414screen_puts_len(
7415 char_u *text,
7416 int textlen,
7417 int row,
7418 int col,
7419 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
7421 unsigned off;
7422 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007423 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 int c;
7425#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007426 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 int mbyte_blen = 1;
7428 int mbyte_cells = 1;
7429 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007430 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 int clear_next_cell = FALSE;
7432# ifdef FEAT_ARABIC
7433 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007434 int pc, nc, nc1;
7435 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436# endif
7437#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007438#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7439 int force_redraw_this;
7440 int force_redraw_next = FALSE;
7441#endif
7442 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443
7444 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7445 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007446 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447
Bram Moolenaarc236c162008-07-13 17:41:49 +00007448#ifdef FEAT_MBYTE
7449 /* When drawing over the right halve of a double-wide char clear out the
7450 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007451 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007452# ifdef FEAT_GUI
7453 && !gui.in_use
7454# endif
7455 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007456 {
7457 ScreenLines[off - 1] = ' ';
7458 ScreenAttrs[off - 1] = 0;
7459 if (enc_utf8)
7460 {
7461 ScreenLinesUC[off - 1] = 0;
7462 ScreenLinesC[0][off - 1] = 0;
7463 }
7464 /* redraw the previous cell, make it empty */
7465 screen_char(off - 1, row, col - 1);
7466 /* force the cell at "col" to be redrawn */
7467 force_redraw_next = TRUE;
7468 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007469#endif
7470
Bram Moolenaar367329b2007-08-30 11:53:22 +00007471#ifdef FEAT_MBYTE
7472 max_off = LineOffset[row] + screen_Columns;
7473#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007474 while (col < screen_Columns
7475 && (len < 0 || (int)(ptr - text) < len)
7476 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 {
7478 c = *ptr;
7479#ifdef FEAT_MBYTE
7480 /* check if this is the first byte of a multibyte */
7481 if (has_mbyte)
7482 {
7483 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007484 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007486 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7488 mbyte_cells = 1;
7489 else if (enc_dbcs != 0)
7490 mbyte_cells = mbyte_blen;
7491 else /* enc_utf8 */
7492 {
7493 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007494 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 (int)((text + len) - ptr));
7496 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007497 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007499# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 /* Non-BMP character: display as ? or fullwidth ?. */
7501 if (u8c >= 0x10000)
7502 {
7503 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7504 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007505 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508# ifdef FEAT_ARABIC
7509 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7510 {
7511 /* Do Arabic shaping. */
7512 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7513 {
7514 /* Past end of string to be displayed. */
7515 nc = NUL;
7516 nc1 = NUL;
7517 }
7518 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007519 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007520 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7521 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007522 nc1 = pcc[0];
7523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 pc = prev_c;
7525 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007526 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 }
7528 else
7529 prev_c = u8c;
7530# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007531 if (col + mbyte_cells > screen_Columns)
7532 {
7533 /* Only 1 cell left, but character requires 2 cells:
7534 * display a '>' in the last column to avoid wrapping. */
7535 c = '>';
7536 mbyte_cells = 1;
7537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 }
7539 }
7540#endif
7541
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007542#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7543 force_redraw_this = force_redraw_next;
7544 force_redraw_next = FALSE;
7545#endif
7546
7547 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548#ifdef FEAT_MBYTE
7549 || (mbyte_cells == 2
7550 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7551 || (enc_dbcs == DBCS_JPNU
7552 && c == 0x8e
7553 && ScreenLines2[off] != ptr[1])
7554 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007555 && (ScreenLinesUC[off] !=
7556 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7557 || (ScreenLinesUC[off] != 0
7558 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559#endif
7560 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007561 || exmode_active;
7562
7563 if (need_redraw
7564#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7565 || force_redraw_this
7566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 )
7568 {
7569#if defined(FEAT_GUI) || defined(UNIX)
7570 /* The bold trick makes a single row of pixels appear in the next
7571 * character. When a bold character is removed, the next
7572 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007573 * and for some xterms. */
7574 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575# ifdef FEAT_GUI
7576 gui.in_use
7577# endif
7578# if defined(FEAT_GUI) && defined(UNIX)
7579 ||
7580# endif
7581# ifdef UNIX
7582 term_is_xterm
7583# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007584 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007586 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007588 if (n > HL_ALL)
7589 n = syn_attr2attr(n);
7590 if (n & HL_BOLD)
7591 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 }
7593#endif
7594#ifdef FEAT_MBYTE
7595 /* When at the end of the text and overwriting a two-cell
7596 * character with a one-cell character, need to clear the next
7597 * cell. Also when overwriting the left halve of a two-cell char
7598 * with the right halve of a two-cell char. Do this only once
7599 * (mb_off2cells() may return 2 on the right halve). */
7600 if (clear_next_cell)
7601 clear_next_cell = FALSE;
7602 else if (has_mbyte
7603 && (len < 0 ? ptr[mbyte_blen] == NUL
7604 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007605 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007607 && (*mb_off2cells)(off, max_off) == 1
7608 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 clear_next_cell = TRUE;
7610
7611 /* Make sure we never leave a second byte of a double-byte behind,
7612 * it confuses mb_off2cells(). */
7613 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007614 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007616 && (*mb_off2cells)(off, max_off) == 1
7617 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 ScreenLines[off + mbyte_blen] = 0;
7619#endif
7620 ScreenLines[off] = c;
7621 ScreenAttrs[off] = attr;
7622#ifdef FEAT_MBYTE
7623 if (enc_utf8)
7624 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007625 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 ScreenLinesUC[off] = 0;
7627 else
7628 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007629 int i;
7630
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007632 for (i = 0; i < Screen_mco; ++i)
7633 {
7634 ScreenLinesC[i][off] = u8cc[i];
7635 if (u8cc[i] == 0)
7636 break;
7637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 }
7639 if (mbyte_cells == 2)
7640 {
7641 ScreenLines[off + 1] = 0;
7642 ScreenAttrs[off + 1] = attr;
7643 }
7644 screen_char(off, row, col);
7645 }
7646 else if (mbyte_cells == 2)
7647 {
7648 ScreenLines[off + 1] = ptr[1];
7649 ScreenAttrs[off + 1] = attr;
7650 screen_char_2(off, row, col);
7651 }
7652 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7653 {
7654 ScreenLines2[off] = ptr[1];
7655 screen_char(off, row, col);
7656 }
7657 else
7658#endif
7659 screen_char(off, row, col);
7660 }
7661#ifdef FEAT_MBYTE
7662 if (has_mbyte)
7663 {
7664 off += mbyte_cells;
7665 col += mbyte_cells;
7666 ptr += mbyte_blen;
7667 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007668 {
7669 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007671 len = -1;
7672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 }
7674 else
7675#endif
7676 {
7677 ++off;
7678 ++col;
7679 ++ptr;
7680 }
7681 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007682
7683#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7684 /* If we detected the next character needs to be redrawn, but the text
7685 * doesn't extend up to there, update the character here. */
7686 if (force_redraw_next && col < screen_Columns)
7687 {
7688# ifdef FEAT_MBYTE
7689 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7690 screen_char_2(off, row, col);
7691 else
7692# endif
7693 screen_char(off, row, col);
7694 }
7695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696}
7697
7698#ifdef FEAT_SEARCH_EXTRA
7699/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007700 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 */
7702 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007703start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704{
7705 if (p_hls && !no_hlsearch)
7706 {
7707 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007708 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007709# ifdef FEAT_RELTIME
7710 /* Set the time limit to 'redrawtime'. */
7711 profile_setlimit(p_rdt, &search_hl.tm);
7712# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 }
7714}
7715
7716/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007717 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 */
7719 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007720end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
7722 if (search_hl.rm.regprog != NULL)
7723 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007724 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 search_hl.rm.regprog = NULL;
7726 }
7727}
7728
7729/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007730 * Init for calling prepare_search_hl().
7731 */
7732 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007733init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007734{
7735 matchitem_T *cur;
7736
7737 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7738 * match */
7739 cur = wp->w_match_head;
7740 while (cur != NULL)
7741 {
7742 cur->hl.rm = cur->match;
7743 if (cur->hlg_id == 0)
7744 cur->hl.attr = 0;
7745 else
7746 cur->hl.attr = syn_id2attr(cur->hlg_id);
7747 cur->hl.buf = wp->w_buffer;
7748 cur->hl.lnum = 0;
7749 cur->hl.first_lnum = 0;
7750# ifdef FEAT_RELTIME
7751 /* Set the time limit to 'redrawtime'. */
7752 profile_setlimit(p_rdt, &(cur->hl.tm));
7753# endif
7754 cur = cur->next;
7755 }
7756 search_hl.buf = wp->w_buffer;
7757 search_hl.lnum = 0;
7758 search_hl.first_lnum = 0;
7759 /* time limit is set at the toplevel, for all windows */
7760}
7761
7762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 * Advance to the match in window "wp" line "lnum" or past it.
7764 */
7765 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007766prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007768 matchitem_T *cur; /* points to the match list */
7769 match_T *shl; /* points to search_hl or a match */
7770 int shl_flag; /* flag to indicate whether search_hl
7771 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007772 int pos_inprogress; /* marks that position match search is
7773 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 int n;
7775
7776 /*
7777 * When using a multi-line pattern, start searching at the top
7778 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007779 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007781 cur = wp->w_match_head;
7782 shl_flag = FALSE;
7783 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007785 if (shl_flag == FALSE)
7786 {
7787 shl = &search_hl;
7788 shl_flag = TRUE;
7789 }
7790 else
7791 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 if (shl->rm.regprog != NULL
7793 && shl->lnum == 0
7794 && re_multiline(shl->rm.regprog))
7795 {
7796 if (shl->first_lnum == 0)
7797 {
7798# ifdef FEAT_FOLDING
7799 for (shl->first_lnum = lnum;
7800 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7801 if (hasFoldingWin(wp, shl->first_lnum - 1,
7802 NULL, NULL, TRUE, NULL))
7803 break;
7804# else
7805 shl->first_lnum = wp->w_topline;
7806# endif
7807 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007808 if (cur != NULL)
7809 cur->pos.cur = 0;
7810 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007812 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7813 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007815 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7816 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007817 pos_inprogress = cur == NULL || cur->pos.cur == 0
7818 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 if (shl->lnum != 0)
7820 {
7821 shl->first_lnum = shl->lnum
7822 + shl->rm.endpos[0].lnum
7823 - shl->rm.startpos[0].lnum;
7824 n = shl->rm.endpos[0].col;
7825 }
7826 else
7827 {
7828 ++shl->first_lnum;
7829 n = 0;
7830 }
7831 }
7832 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007833 if (shl != &search_hl && cur != NULL)
7834 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 }
7836}
7837
7838/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007839 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 * Uses shl->buf.
7841 * Sets shl->lnum and shl->rm contents.
7842 * Note: Assumes a previous match is always before "lnum", unless
7843 * shl->lnum is zero.
7844 * Careful: Any pointers for buffer lines will become invalid.
7845 */
7846 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007847next_search_hl(
7848 win_T *win,
7849 match_T *shl, /* points to search_hl or a match */
7850 linenr_T lnum,
7851 colnr_T mincol, /* minimal column for a match */
7852 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853{
7854 linenr_T l;
7855 colnr_T matchcol;
7856 long nmatched;
7857
7858 if (shl->lnum != 0)
7859 {
7860 /* Check for three situations:
7861 * 1. If the "lnum" is below a previous match, start a new search.
7862 * 2. If the previous match includes "mincol", use it.
7863 * 3. Continue after the previous match.
7864 */
7865 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7866 if (lnum > l)
7867 shl->lnum = 0;
7868 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7869 return;
7870 }
7871
7872 /*
7873 * Repeat searching for a match until one is found that includes "mincol"
7874 * or none is found in this line.
7875 */
7876 called_emsg = FALSE;
7877 for (;;)
7878 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007879#ifdef FEAT_RELTIME
7880 /* Stop searching after passing the time limit. */
7881 if (profile_passed_limit(&(shl->tm)))
7882 {
7883 shl->lnum = 0; /* no match found in time */
7884 break;
7885 }
7886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 /* Three situations:
7888 * 1. No useful previous match: search from start of line.
7889 * 2. Not Vi compatible or empty match: continue at next character.
7890 * Break the loop if this is beyond the end of the line.
7891 * 3. Vi compatible searching: continue at end of previous match.
7892 */
7893 if (shl->lnum == 0)
7894 matchcol = 0;
7895 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7896 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007897 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007899 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007900
7901 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007902 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007903 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007905 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 shl->lnum = 0;
7907 break;
7908 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007909#ifdef FEAT_MBYTE
7910 if (has_mbyte)
7911 matchcol += mb_ptr2len(ml);
7912 else
7913#endif
7914 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 }
7916 else
7917 matchcol = shl->rm.endpos[0].col;
7918
7919 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007920 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007922 /* Remember whether shl->rm is using a copy of the regprog in
7923 * cur->match. */
7924 int regprog_is_copy = (shl != &search_hl && cur != NULL
7925 && shl == &cur->hl
7926 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007927 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007928
Bram Moolenaarb3414592014-06-17 17:48:32 +02007929 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7930 matchcol,
7931#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007932 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007933#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007934 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007935#endif
7936 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007937 /* Copy the regprog, in case it got freed and recompiled. */
7938 if (regprog_is_copy)
7939 cur->match.regprog = cur->hl.rm.regprog;
7940
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007941 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007942 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007943 /* Error while handling regexp: stop using this regexp. */
7944 if (shl == &search_hl)
7945 {
7946 /* don't free regprog in the match list, it's a copy */
7947 vim_regfree(shl->rm.regprog);
7948 SET_NO_HLSEARCH(TRUE);
7949 }
7950 shl->rm.regprog = NULL;
7951 shl->lnum = 0;
7952 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7953 message */
7954 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007955 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007956 }
7957 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007958 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007959 else
7960 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 if (nmatched == 0)
7962 {
7963 shl->lnum = 0; /* no match found */
7964 break;
7965 }
7966 if (shl->rm.startpos[0].lnum > 0
7967 || shl->rm.startpos[0].col >= mincol
7968 || nmatched > 1
7969 || shl->rm.endpos[0].col > mincol)
7970 {
7971 shl->lnum += shl->rm.startpos[0].lnum;
7972 break; /* useful match found */
7973 }
7974 }
7975}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976
Bram Moolenaar85077472016-10-16 14:35:48 +02007977/*
7978 * If there is a match fill "shl" and return one.
7979 * Return zero otherwise.
7980 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007981 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007982next_search_hl_pos(
7983 match_T *shl, /* points to a match */
7984 linenr_T lnum,
7985 posmatch_T *posmatch, /* match positions */
7986 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007987{
7988 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007989 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990
Bram Moolenaarb3414592014-06-17 17:48:32 +02007991 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7992 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007993 llpos_T *pos = &posmatch->pos[i];
7994
7995 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007996 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007997 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007998 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007999 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008000 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008001 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008002 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008003 /* if this match comes before the one at "found" then swap
8004 * them */
8005 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008006 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008007 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008008
Bram Moolenaar85077472016-10-16 14:35:48 +02008009 *pos = posmatch->pos[found];
8010 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008011 }
8012 }
8013 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008014 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008015 }
8016 }
8017 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008018 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008019 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008020 colnr_T start = posmatch->pos[found].col == 0
8021 ? 0 : posmatch->pos[found].col - 1;
8022 colnr_T end = posmatch->pos[found].col == 0
8023 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008024
Bram Moolenaar85077472016-10-16 14:35:48 +02008025 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008026 shl->rm.startpos[0].lnum = 0;
8027 shl->rm.startpos[0].col = start;
8028 shl->rm.endpos[0].lnum = 0;
8029 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008030 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008031 posmatch->cur = found + 1;
8032 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008033 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008034 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008035}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008036#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008037
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008039screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
8041 attrentry_T *aep = NULL;
8042
8043 screen_attr = attr;
8044 if (full_screen
8045#ifdef WIN3264
8046 && termcap_active
8047#endif
8048 )
8049 {
8050#ifdef FEAT_GUI
8051 if (gui.in_use)
8052 {
8053 char buf[20];
8054
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008055 /* The GUI handles this internally. */
8056 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 OUT_STR(buf);
8058 }
8059 else
8060#endif
8061 {
8062 if (attr > HL_ALL) /* special HL attr. */
8063 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008064 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 aep = syn_cterm_attr2entry(attr);
8066 else
8067 aep = syn_term_attr2entry(attr);
8068 if (aep == NULL) /* did ":syntax clear" */
8069 attr = 0;
8070 else
8071 attr = aep->ae_attr;
8072 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008073 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008075 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008076#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008077 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8078 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8079 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008080#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008081 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008082 /* If the Normal FG color has BOLD attribute and the new HL
8083 * has a FG color defined, clear BOLD. */
8084 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008085 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008087 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008088 out_str(T_UCS);
8089 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008090 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8091 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008093 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008095 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008097 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008098 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099
8100 /*
8101 * Output the color or start string after bold etc., in case the
8102 * bold etc. override the color setting.
8103 */
8104 if (aep != NULL)
8105 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008106#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008107 /* When 'termguicolors' is set but fg or bg is unset,
8108 * fall back to the cterm colors. This helps for SpellBad,
8109 * where the GUI uses a red undercurl. */
8110 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008112 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008113 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008114 }
8115 else
8116#endif
8117 if (t_colors > 1)
8118 {
8119 if (aep->ae_u.cterm.fg_color)
8120 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8121 }
8122#ifdef FEAT_TERMGUICOLORS
8123 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8124 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008125 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008126 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 }
8128 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008129#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008130 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008132 if (aep->ae_u.cterm.bg_color)
8133 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8134 }
8135
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008136 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008137 {
8138 if (aep->ae_u.term.start != NULL)
8139 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 }
8141 }
8142 }
8143 }
8144}
8145
8146 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008147screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148{
8149 int do_ME = FALSE; /* output T_ME code */
8150
8151 if (screen_attr != 0
8152#ifdef WIN3264
8153 && termcap_active
8154#endif
8155 )
8156 {
8157#ifdef FEAT_GUI
8158 if (gui.in_use)
8159 {
8160 char buf[20];
8161
8162 /* use internal GUI code */
8163 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8164 OUT_STR(buf);
8165 }
8166 else
8167#endif
8168 {
8169 if (screen_attr > HL_ALL) /* special HL attr. */
8170 {
8171 attrentry_T *aep;
8172
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008173 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {
8175 /*
8176 * Assume that t_me restores the original colors!
8177 */
8178 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008179 if (aep != NULL && ((
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.fg_rgb != CTERMCOLOR
8182 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8183 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008184#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008185 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008186#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008187 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8188 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8189 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008190#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008191 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 do_ME = TRUE;
8193 }
8194 else
8195 {
8196 aep = syn_term_attr2entry(screen_attr);
8197 if (aep != NULL && aep->ae_u.term.stop != NULL)
8198 {
8199 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8200 do_ME = TRUE;
8201 else
8202 out_str(aep->ae_u.term.stop);
8203 }
8204 }
8205 if (aep == NULL) /* did ":syntax clear" */
8206 screen_attr = 0;
8207 else
8208 screen_attr = aep->ae_attr;
8209 }
8210
8211 /*
8212 * Often all ending-codes are equal to T_ME. Avoid outputting the
8213 * same sequence several times.
8214 */
8215 if (screen_attr & HL_STANDOUT)
8216 {
8217 if (STRCMP(T_SE, T_ME) == 0)
8218 do_ME = TRUE;
8219 else
8220 out_str(T_SE);
8221 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008222 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008223 {
8224 if (STRCMP(T_UCE, T_ME) == 0)
8225 do_ME = TRUE;
8226 else
8227 out_str(T_UCE);
8228 }
8229 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008230 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {
8232 if (STRCMP(T_UE, T_ME) == 0)
8233 do_ME = TRUE;
8234 else
8235 out_str(T_UE);
8236 }
8237 if (screen_attr & HL_ITALIC)
8238 {
8239 if (STRCMP(T_CZR, T_ME) == 0)
8240 do_ME = TRUE;
8241 else
8242 out_str(T_CZR);
8243 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008244 if (screen_attr & HL_STRIKETHROUGH)
8245 {
8246 if (STRCMP(T_STE, T_ME) == 0)
8247 do_ME = TRUE;
8248 else
8249 out_str(T_STE);
8250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8252 out_str(T_ME);
8253
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008254#ifdef FEAT_TERMGUICOLORS
8255 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008257 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008258 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008259 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008260 term_bg_rgb_color(cterm_normal_bg_gui_color);
8261 }
8262 else
8263#endif
8264 {
8265 if (t_colors > 1)
8266 {
8267 /* set Normal cterm colors */
8268 if (cterm_normal_fg_color != 0)
8269 term_fg_color(cterm_normal_fg_color - 1);
8270 if (cterm_normal_bg_color != 0)
8271 term_bg_color(cterm_normal_bg_color - 1);
8272 if (cterm_normal_fg_bold)
8273 out_str(T_MD);
8274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 }
8276 }
8277 }
8278 screen_attr = 0;
8279}
8280
8281/*
8282 * Reset the colors for a cterm. Used when leaving Vim.
8283 * The machine specific code may override this again.
8284 */
8285 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008286reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008288 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {
8290 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008291#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008292 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8293 || cterm_normal_bg_gui_color != INVALCOLOR)
8294 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008295#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {
8299 out_str(T_OP);
8300 screen_attr = -1;
8301 }
8302 if (cterm_normal_fg_bold)
8303 {
8304 out_str(T_ME);
8305 screen_attr = -1;
8306 }
8307 }
8308}
8309
8310/*
8311 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8312 * using the attributes from ScreenAttrs["off"].
8313 */
8314 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008315screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316{
8317 int attr;
8318
8319 /* Check for illegal values, just in case (could happen just after
8320 * resizing). */
8321 if (row >= screen_Rows || col >= screen_Columns)
8322 return;
8323
Bram Moolenaar494838a2015-02-10 19:20:37 +01008324 /* Outputting a character in the last cell on the screen may scroll the
8325 * screen up. Only do it when the "xn" termcap property is set, otherwise
8326 * mark the character invalid (update it when scrolled up). */
8327 if (*T_XN == NUL
8328 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329#ifdef FEAT_RIGHTLEFT
8330 /* account for first command-line character in rightleft mode */
8331 && !cmdmsg_rl
8332#endif
8333 )
8334 {
8335 ScreenAttrs[off] = (sattr_T)-1;
8336 return;
8337 }
8338
8339 /*
8340 * Stop highlighting first, so it's easier to move the cursor.
8341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 if (screen_char_attr != 0)
8343 attr = screen_char_attr;
8344 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 attr = ScreenAttrs[off];
8346 if (screen_attr != attr)
8347 screen_stop_highlight();
8348
8349 windgoto(row, col);
8350
8351 if (screen_attr != attr)
8352 screen_start_highlight(attr);
8353
8354#ifdef FEAT_MBYTE
8355 if (enc_utf8 && ScreenLinesUC[off] != 0)
8356 {
8357 char_u buf[MB_MAXBYTES + 1];
8358
Bram Moolenaarcb070082016-04-02 22:14:51 +02008359 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008360 {
8361 if (*p_ambw == 'd'
8362# ifdef FEAT_GUI
8363 && !gui.in_use
8364# endif
8365 )
8366 {
8367 /* Clear the two screen cells. If the character is actually
8368 * single width it won't change the second cell. */
8369 out_str((char_u *)" ");
8370 term_windgoto(row, col);
8371 }
8372 /* not sure where the cursor is after drawing the ambiguous width
8373 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008374 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008375 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008376 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008378
8379 /* Convert the UTF-8 character to bytes and write it. */
8380 buf[utfc_char2bytes(off, buf)] = NUL;
8381 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383 else
8384#endif
8385 {
8386#ifdef FEAT_MBYTE
8387 out_flush_check();
8388#endif
8389 out_char(ScreenLines[off]);
8390#ifdef FEAT_MBYTE
8391 /* double-byte character in single-width cell */
8392 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8393 out_char(ScreenLines2[off]);
8394#endif
8395 }
8396
8397 screen_cur_col++;
8398}
8399
8400#ifdef FEAT_MBYTE
8401
8402/*
8403 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8404 * on the screen at position 'row' and 'col'.
8405 * The attributes of the first byte is used for all. This is required to
8406 * output the two bytes of a double-byte character with nothing in between.
8407 */
8408 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008409screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410{
8411 /* Check for illegal values (could be wrong when screen was resized). */
8412 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8413 return;
8414
8415 /* Outputting the last character on the screen may scrollup the screen.
8416 * Don't to it! Mark the character invalid (update it when scrolled up) */
8417 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8418 {
8419 ScreenAttrs[off] = (sattr_T)-1;
8420 return;
8421 }
8422
8423 /* Output the first byte normally (positions the cursor), then write the
8424 * second byte directly. */
8425 screen_char(off, row, col);
8426 out_char(ScreenLines[off + 1]);
8427 ++screen_cur_col;
8428}
8429#endif
8430
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431/*
8432 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8433 * This uses the contents of ScreenLines[] and doesn't change it.
8434 */
8435 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008436screen_draw_rectangle(
8437 int row,
8438 int col,
8439 int height,
8440 int width,
8441 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442{
8443 int r, c;
8444 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008445#ifdef FEAT_MBYTE
8446 int max_off;
8447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008449 /* Can't use ScreenLines unless initialized */
8450 if (ScreenLines == NULL)
8451 return;
8452
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 if (invert)
8454 screen_char_attr = HL_INVERSE;
8455 for (r = row; r < row + height; ++r)
8456 {
8457 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008458#ifdef FEAT_MBYTE
8459 max_off = off + screen_Columns;
8460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 for (c = col; c < col + width; ++c)
8462 {
8463#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008464 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 {
8466 screen_char_2(off + c, r, c);
8467 ++c;
8468 }
8469 else
8470#endif
8471 {
8472 screen_char(off + c, r, c);
8473#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008474 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 ++c;
8476#endif
8477 }
8478 }
8479 }
8480 screen_char_attr = 0;
8481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483/*
8484 * Redraw the characters for a vertically split window.
8485 */
8486 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008487redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488{
8489 int col;
8490 int width;
8491
8492# ifdef FEAT_CLIPBOARD
8493 clip_may_clear_selection(row, end - 1);
8494# endif
8495
8496 if (wp == NULL)
8497 {
8498 col = 0;
8499 width = Columns;
8500 }
8501 else
8502 {
8503 col = wp->w_wincol;
8504 width = wp->w_width;
8505 }
8506 screen_draw_rectangle(row, col, end - row, width, FALSE);
8507}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008509 static void
8510space_to_screenline(int off, int attr)
8511{
8512 ScreenLines[off] = ' ';
8513 ScreenAttrs[off] = attr;
8514# ifdef FEAT_MBYTE
8515 if (enc_utf8)
8516 ScreenLinesUC[off] = 0;
8517# endif
8518}
8519
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520/*
8521 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8522 * with character 'c1' in first column followed by 'c2' in the other columns.
8523 * Use attributes 'attr'.
8524 */
8525 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008526screen_fill(
8527 int start_row,
8528 int end_row,
8529 int start_col,
8530 int end_col,
8531 int c1,
8532 int c2,
8533 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534{
8535 int row;
8536 int col;
8537 int off;
8538 int end_off;
8539 int did_delete;
8540 int c;
8541 int norm_term;
8542#if defined(FEAT_GUI) || defined(UNIX)
8543 int force_next = FALSE;
8544#endif
8545
8546 if (end_row > screen_Rows) /* safety check */
8547 end_row = screen_Rows;
8548 if (end_col > screen_Columns) /* safety check */
8549 end_col = screen_Columns;
8550 if (ScreenLines == NULL
8551 || start_row >= end_row
8552 || start_col >= end_col) /* nothing to do */
8553 return;
8554
8555 /* it's a "normal" terminal when not in a GUI or cterm */
8556 norm_term = (
8557#ifdef FEAT_GUI
8558 !gui.in_use &&
8559#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008560 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 for (row = start_row; row < end_row; ++row)
8562 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008563#ifdef FEAT_MBYTE
8564 if (has_mbyte
8565# ifdef FEAT_GUI
8566 && !gui.in_use
8567# endif
8568 )
8569 {
8570 /* When drawing over the right halve of a double-wide char clear
8571 * out the left halve. When drawing over the left halve of a
8572 * double wide-char clear out the right halve. Only needed in a
8573 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008574 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008575 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008576 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008577 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008578 }
8579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 /*
8581 * Try to use delete-line termcap code, when no attributes or in a
8582 * "normal" terminal, where a bold/italic space is just a
8583 * space.
8584 */
8585 did_delete = FALSE;
8586 if (c2 == ' '
8587 && end_col == Columns
8588 && can_clear(T_CE)
8589 && (attr == 0
8590 || (norm_term
8591 && attr <= HL_ALL
8592 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8593 {
8594 /*
8595 * check if we really need to clear something
8596 */
8597 col = start_col;
8598 if (c1 != ' ') /* don't clear first char */
8599 ++col;
8600
8601 off = LineOffset[row] + col;
8602 end_off = LineOffset[row] + end_col;
8603
8604 /* skip blanks (used often, keep it fast!) */
8605#ifdef FEAT_MBYTE
8606 if (enc_utf8)
8607 while (off < end_off && ScreenLines[off] == ' '
8608 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8609 ++off;
8610 else
8611#endif
8612 while (off < end_off && ScreenLines[off] == ' '
8613 && ScreenAttrs[off] == 0)
8614 ++off;
8615 if (off < end_off) /* something to be cleared */
8616 {
8617 col = off - LineOffset[row];
8618 screen_stop_highlight();
8619 term_windgoto(row, col);/* clear rest of this screen line */
8620 out_str(T_CE);
8621 screen_start(); /* don't know where cursor is now */
8622 col = end_col - col;
8623 while (col--) /* clear chars in ScreenLines */
8624 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008625 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 ++off;
8627 }
8628 }
8629 did_delete = TRUE; /* the chars are cleared now */
8630 }
8631
8632 off = LineOffset[row] + start_col;
8633 c = c1;
8634 for (col = start_col; col < end_col; ++col)
8635 {
8636 if (ScreenLines[off] != c
8637#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008638 || (enc_utf8 && (int)ScreenLinesUC[off]
8639 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640#endif
8641 || ScreenAttrs[off] != attr
8642#if defined(FEAT_GUI) || defined(UNIX)
8643 || force_next
8644#endif
8645 )
8646 {
8647#if defined(FEAT_GUI) || defined(UNIX)
8648 /* The bold trick may make a single row of pixels appear in
8649 * the next character. When a bold character is removed, the
8650 * next character should be redrawn too. This happens for our
8651 * own GUI and for some xterms. */
8652 if (
8653# ifdef FEAT_GUI
8654 gui.in_use
8655# endif
8656# if defined(FEAT_GUI) && defined(UNIX)
8657 ||
8658# endif
8659# ifdef UNIX
8660 term_is_xterm
8661# endif
8662 )
8663 {
8664 if (ScreenLines[off] != ' '
8665 && (ScreenAttrs[off] > HL_ALL
8666 || ScreenAttrs[off] & HL_BOLD))
8667 force_next = TRUE;
8668 else
8669 force_next = FALSE;
8670 }
8671#endif
8672 ScreenLines[off] = c;
8673#ifdef FEAT_MBYTE
8674 if (enc_utf8)
8675 {
8676 if (c >= 0x80)
8677 {
8678 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008679 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 }
8681 else
8682 ScreenLinesUC[off] = 0;
8683 }
8684#endif
8685 ScreenAttrs[off] = attr;
8686 if (!did_delete || c != ' ')
8687 screen_char(off, row, col);
8688 }
8689 ++off;
8690 if (col == start_col)
8691 {
8692 if (did_delete)
8693 break;
8694 c = c2;
8695 }
8696 }
8697 if (end_col == Columns)
8698 LineWraps[row] = FALSE;
8699 if (row == Rows - 1) /* overwritten the command line */
8700 {
8701 redraw_cmdline = TRUE;
8702 if (c1 == ' ' && c2 == ' ')
8703 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008704 if (start_col == 0)
8705 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 }
8707 }
8708}
8709
8710/*
8711 * Check if there should be a delay. Used before clearing or redrawing the
8712 * screen or the command line.
8713 */
8714 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008715check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716{
8717 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8718 && !did_wait_return
8719 && emsg_silent == 0)
8720 {
8721 out_flush();
8722 ui_delay(1000L, TRUE);
8723 emsg_on_display = FALSE;
8724 if (check_msg_scroll)
8725 msg_scroll = FALSE;
8726 }
8727}
8728
8729/*
8730 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008731 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 * Returns TRUE if there is a valid screen to write to.
8733 * Returns FALSE when starting up and screen not initialized yet.
8734 */
8735 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008736screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008738 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 return (ScreenLines != NULL);
8740}
8741
8742/*
8743 * Resize the shell to Rows and Columns.
8744 * Allocate ScreenLines[] and associated items.
8745 *
8746 * There may be some time between setting Rows and Columns and (re)allocating
8747 * ScreenLines[]. This happens when starting up and when (manually) changing
8748 * the shell size. Always use screen_Rows and screen_Columns to access items
8749 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8750 * final size of the shell is needed.
8751 */
8752 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008753screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754{
8755 int new_row, old_row;
8756#ifdef FEAT_GUI
8757 int old_Rows;
8758#endif
8759 win_T *wp;
8760 int outofmem = FALSE;
8761 int len;
8762 schar_T *new_ScreenLines;
8763#ifdef FEAT_MBYTE
8764 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008765 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008767 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768#endif
8769 sattr_T *new_ScreenAttrs;
8770 unsigned *new_LineOffset;
8771 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008772 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008773 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008775 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008776 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008778retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 /*
8780 * Allocation of the screen buffers is done only when the size changes and
8781 * when Rows and Columns have been set and we have started doing full
8782 * screen stuff.
8783 */
8784 if ((ScreenLines != NULL
8785 && Rows == screen_Rows
8786 && Columns == screen_Columns
8787#ifdef FEAT_MBYTE
8788 && enc_utf8 == (ScreenLinesUC != NULL)
8789 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008790 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791#endif
8792 )
8793 || Rows == 0
8794 || Columns == 0
8795 || (!full_screen && ScreenLines == NULL))
8796 return;
8797
8798 /*
8799 * It's possible that we produce an out-of-memory message below, which
8800 * will cause this function to be called again. To break the loop, just
8801 * return here.
8802 */
8803 if (entered)
8804 return;
8805 entered = TRUE;
8806
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008807 /*
8808 * Note that the window sizes are updated before reallocating the arrays,
8809 * thus we must not redraw here!
8810 */
8811 ++RedrawingDisabled;
8812
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 win_new_shellsize(); /* fit the windows in the new sized shell */
8814
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 comp_col(); /* recompute columns for shown command and ruler */
8816
8817 /*
8818 * We're changing the size of the screen.
8819 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8820 * - Move lines from the old arrays into the new arrays, clear extra
8821 * lines (unless the screen is going to be cleared).
8822 * - Free the old arrays.
8823 *
8824 * If anything fails, make ScreenLines NULL, so we don't do anything!
8825 * Continuing with the old ScreenLines may result in a crash, because the
8826 * size is wrong.
8827 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008828 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008830 if (aucmd_win != NULL)
8831 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832
8833 new_ScreenLines = (schar_T *)lalloc((long_u)(
8834 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8835#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008836 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 if (enc_utf8)
8838 {
8839 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8840 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008841 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008842 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8844 }
8845 if (enc_dbcs == DBCS_JPNU)
8846 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8847 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8848#endif
8849 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8850 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8851 new_LineOffset = (unsigned *)lalloc((long_u)(
8852 Rows * sizeof(unsigned)), FALSE);
8853 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008854 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008856 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 {
8858 if (win_alloc_lines(wp) == FAIL)
8859 {
8860 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008861 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 }
8863 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008864 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8865 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008866 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008867give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008869#ifdef FEAT_MBYTE
8870 for (i = 0; i < p_mco; ++i)
8871 if (new_ScreenLinesC[i] == NULL)
8872 break;
8873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 if (new_ScreenLines == NULL
8875#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008876 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8878#endif
8879 || new_ScreenAttrs == NULL
8880 || new_LineOffset == NULL
8881 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008882 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 || outofmem)
8884 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008885 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008886 {
8887 /* guess the size */
8888 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8889
8890 /* Remember we did this to avoid getting outofmem messages over
8891 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008892 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008893 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008894 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008896 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008897 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008898 VIM_CLEAR(new_ScreenLinesC[i]);
8899 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008901 VIM_CLEAR(new_ScreenAttrs);
8902 VIM_CLEAR(new_LineOffset);
8903 VIM_CLEAR(new_LineWraps);
8904 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 }
8906 else
8907 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008908 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008909
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 for (new_row = 0; new_row < Rows; ++new_row)
8911 {
8912 new_LineOffset[new_row] = new_row * Columns;
8913 new_LineWraps[new_row] = FALSE;
8914
8915 /*
8916 * If the screen is not going to be cleared, copy as much as
8917 * possible from the old screen to the new one and clear the rest
8918 * (used when resizing the window at the "--more--" prompt or when
8919 * executing an external command, for the GUI).
8920 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008921 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 {
8923 (void)vim_memset(new_ScreenLines + new_row * Columns,
8924 ' ', (size_t)Columns * sizeof(schar_T));
8925#ifdef FEAT_MBYTE
8926 if (enc_utf8)
8927 {
8928 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8929 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008930 for (i = 0; i < p_mco; ++i)
8931 (void)vim_memset(new_ScreenLinesC[i]
8932 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 0, (size_t)Columns * sizeof(u8char_T));
8934 }
8935 if (enc_dbcs == DBCS_JPNU)
8936 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8937 0, (size_t)Columns * sizeof(schar_T));
8938#endif
8939 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8940 0, (size_t)Columns * sizeof(sattr_T));
8941 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008942 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 {
8944 if (screen_Columns < Columns)
8945 len = screen_Columns;
8946 else
8947 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008948#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008949 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008950 * may be invalid now. Also when p_mco changes. */
8951 if (!(enc_utf8 && ScreenLinesUC == NULL)
8952 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008953#endif
8954 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8955 ScreenLines + LineOffset[old_row],
8956 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008958 if (enc_utf8 && ScreenLinesUC != NULL
8959 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 {
8961 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8962 ScreenLinesUC + LineOffset[old_row],
8963 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008964 for (i = 0; i < p_mco; ++i)
8965 mch_memmove(new_ScreenLinesC[i]
8966 + new_LineOffset[new_row],
8967 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 (size_t)len * sizeof(u8char_T));
8969 }
8970 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8971 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8972 ScreenLines2 + LineOffset[old_row],
8973 (size_t)len * sizeof(schar_T));
8974#endif
8975 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8976 ScreenAttrs + LineOffset[old_row],
8977 (size_t)len * sizeof(sattr_T));
8978 }
8979 }
8980 }
8981 /* Use the last line of the screen for the current line. */
8982 current_ScreenLine = new_ScreenLines + Rows * Columns;
8983 }
8984
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008985 free_screenlines();
8986
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 ScreenLines = new_ScreenLines;
8988#ifdef FEAT_MBYTE
8989 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008990 for (i = 0; i < p_mco; ++i)
8991 ScreenLinesC[i] = new_ScreenLinesC[i];
8992 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 ScreenLines2 = new_ScreenLines2;
8994#endif
8995 ScreenAttrs = new_ScreenAttrs;
8996 LineOffset = new_LineOffset;
8997 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008998 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999
9000 /* It's important that screen_Rows and screen_Columns reflect the actual
9001 * size of ScreenLines[]. Set them before calling anything. */
9002#ifdef FEAT_GUI
9003 old_Rows = screen_Rows;
9004#endif
9005 screen_Rows = Rows;
9006 screen_Columns = Columns;
9007
9008 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009009 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 screenclear2();
9011
9012#ifdef FEAT_GUI
9013 else if (gui.in_use
9014 && !gui.starting
9015 && ScreenLines != NULL
9016 && old_Rows != Rows)
9017 {
9018 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9019 /*
9020 * Adjust the position of the cursor, for when executing an external
9021 * command.
9022 */
9023 if (msg_row >= Rows) /* Rows got smaller */
9024 msg_row = Rows - 1; /* put cursor at last row */
9025 else if (Rows > old_Rows) /* Rows got bigger */
9026 msg_row += Rows - old_Rows; /* put cursor in same place */
9027 if (msg_col >= Columns) /* Columns got smaller */
9028 msg_col = Columns - 1; /* put cursor at last column */
9029 }
9030#endif
9031
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009033 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009034
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009035 /*
9036 * Do not apply autocommands more than 3 times to avoid an endless loop
9037 * in case applying autocommands always changes Rows or Columns.
9038 */
9039 if (starting == 0 && ++retry_count <= 3)
9040 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009041 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009042 /* In rare cases, autocommands may have altered Rows or Columns,
9043 * jump back to check if we need to allocate the screen again. */
9044 goto retry;
9045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046}
9047
9048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009049free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009050{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009051#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009052 int i;
9053
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009054 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009055 for (i = 0; i < Screen_mco; ++i)
9056 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009057 vim_free(ScreenLines2);
9058#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009059 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009060 vim_free(ScreenAttrs);
9061 vim_free(LineOffset);
9062 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009063 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009064}
9065
9066 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009067screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068{
9069 check_for_delay(FALSE);
9070 screenalloc(FALSE); /* allocate screen buffers if size changed */
9071 screenclear2(); /* clear the screen */
9072}
9073
9074 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009075screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076{
9077 int i;
9078
9079 if (starting == NO_SCREEN || ScreenLines == NULL
9080#ifdef FEAT_GUI
9081 || (gui.in_use && gui.starting)
9082#endif
9083 )
9084 return;
9085
9086#ifdef FEAT_GUI
9087 if (!gui.in_use)
9088#endif
9089 screen_attr = -1; /* force setting the Normal colors */
9090 screen_stop_highlight(); /* don't want highlighting here */
9091
9092#ifdef FEAT_CLIPBOARD
9093 /* disable selection without redrawing it */
9094 clip_scroll_selection(9999);
9095#endif
9096
9097 /* blank out ScreenLines */
9098 for (i = 0; i < Rows; ++i)
9099 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009100 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 LineWraps[i] = FALSE;
9102 }
9103
9104 if (can_clear(T_CL))
9105 {
9106 out_str(T_CL); /* clear the display */
9107 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009108 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 }
9110 else
9111 {
9112 /* can't clear the screen, mark all chars with invalid attributes */
9113 for (i = 0; i < Rows; ++i)
9114 lineinvalid(LineOffset[i], (int)Columns);
9115 clear_cmdline = TRUE;
9116 }
9117
9118 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9119
9120 win_rest_invalid(firstwin);
9121 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009122 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 if (must_redraw == CLEAR) /* no need to clear again */
9124 must_redraw = NOT_VALID;
9125 compute_cmdrow();
9126 msg_row = cmdline_row; /* put cursor on last line for messages */
9127 msg_col = 0;
9128 screen_start(); /* don't know where cursor is now */
9129 msg_scrolled = 0; /* can't scroll back */
9130 msg_didany = FALSE;
9131 msg_didout = FALSE;
9132}
9133
9134/*
9135 * Clear one line in ScreenLines.
9136 */
9137 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009138lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139{
9140 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9141#ifdef FEAT_MBYTE
9142 if (enc_utf8)
9143 (void)vim_memset(ScreenLinesUC + off, 0,
9144 (size_t)width * sizeof(u8char_T));
9145#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009146 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147}
9148
9149/*
9150 * Mark one line in ScreenLines invalid by setting the attributes to an
9151 * invalid value.
9152 */
9153 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009154lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155{
9156 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9157}
9158
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159/*
9160 * Copy part of a Screenline for vertically split window "wp".
9161 */
9162 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009163linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
9165 unsigned off_to = LineOffset[to] + wp->w_wincol;
9166 unsigned off_from = LineOffset[from] + wp->w_wincol;
9167
9168 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9169 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009170#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 if (enc_utf8)
9172 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009173 int i;
9174
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9176 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009177 for (i = 0; i < p_mco; ++i)
9178 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9179 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 }
9181 if (enc_dbcs == DBCS_JPNU)
9182 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9183 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9186 wp->w_width * sizeof(sattr_T));
9187}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188
9189/*
9190 * Return TRUE if clearing with term string "p" would work.
9191 * It can't work when the string is empty or it won't set the right background.
9192 */
9193 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009194can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
9196 return (*p != NUL && (t_colors <= 1
9197#ifdef FEAT_GUI
9198 || gui.in_use
9199#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009200#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009201 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009202 || (!p_tgc && cterm_normal_bg_color == 0)
9203#else
9204 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009205#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009206 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207}
9208
9209/*
9210 * Reset cursor position. Use whenever cursor was moved because of outputting
9211 * something directly to the screen (shell commands) or a terminal control
9212 * code.
9213 */
9214 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009215screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216{
9217 screen_cur_row = screen_cur_col = 9999;
9218}
9219
9220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 * Move the cursor to position "row","col" in the screen.
9222 * This tries to find the most efficient way to move, minimizing the number of
9223 * characters sent to the terminal.
9224 */
9225 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009226windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009228 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 int i;
9230 int plan;
9231 int cost;
9232 int wouldbe_col;
9233 int noinvcurs;
9234 char_u *bs;
9235 int goto_cost;
9236 int attr;
9237
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009238#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9240
9241#define PLAN_LE 1
9242#define PLAN_CR 2
9243#define PLAN_NL 3
9244#define PLAN_WRITE 4
9245 /* Can't use ScreenLines unless initialized */
9246 if (ScreenLines == NULL)
9247 return;
9248
9249 if (col != screen_cur_col || row != screen_cur_row)
9250 {
9251 /* Check for valid position. */
9252 if (row < 0) /* window without text lines? */
9253 row = 0;
9254 if (row >= screen_Rows)
9255 row = screen_Rows - 1;
9256 if (col >= screen_Columns)
9257 col = screen_Columns - 1;
9258
9259 /* check if no cursor movement is allowed in highlight mode */
9260 if (screen_attr && *T_MS == NUL)
9261 noinvcurs = HIGHL_COST;
9262 else
9263 noinvcurs = 0;
9264 goto_cost = GOTO_COST + noinvcurs;
9265
9266 /*
9267 * Plan how to do the positioning:
9268 * 1. Use CR to move it to column 0, same row.
9269 * 2. Use T_LE to move it a few columns to the left.
9270 * 3. Use NL to move a few lines down, column 0.
9271 * 4. Move a few columns to the right with T_ND or by writing chars.
9272 *
9273 * Don't do this if the cursor went beyond the last column, the cursor
9274 * position is unknown then (some terminals wrap, some don't )
9275 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009276 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 * characters to move the cursor to the right.
9278 */
9279 if (row >= screen_cur_row && screen_cur_col < Columns)
9280 {
9281 /*
9282 * If the cursor is in the same row, bigger col, we can use CR
9283 * or T_LE.
9284 */
9285 bs = NULL; /* init for GCC */
9286 attr = screen_attr;
9287 if (row == screen_cur_row && col < screen_cur_col)
9288 {
9289 /* "le" is preferred over "bc", because "bc" is obsolete */
9290 if (*T_LE)
9291 bs = T_LE; /* "cursor left" */
9292 else
9293 bs = T_BC; /* "backspace character (old) */
9294 if (*bs)
9295 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9296 else
9297 cost = 999;
9298 if (col + 1 < cost) /* using CR is less characters */
9299 {
9300 plan = PLAN_CR;
9301 wouldbe_col = 0;
9302 cost = 1; /* CR is just one character */
9303 }
9304 else
9305 {
9306 plan = PLAN_LE;
9307 wouldbe_col = col;
9308 }
9309 if (noinvcurs) /* will stop highlighting */
9310 {
9311 cost += noinvcurs;
9312 attr = 0;
9313 }
9314 }
9315
9316 /*
9317 * If the cursor is above where we want to be, we can use CR LF.
9318 */
9319 else if (row > screen_cur_row)
9320 {
9321 plan = PLAN_NL;
9322 wouldbe_col = 0;
9323 cost = (row - screen_cur_row) * 2; /* CR LF */
9324 if (noinvcurs) /* will stop highlighting */
9325 {
9326 cost += noinvcurs;
9327 attr = 0;
9328 }
9329 }
9330
9331 /*
9332 * If the cursor is in the same row, smaller col, just use write.
9333 */
9334 else
9335 {
9336 plan = PLAN_WRITE;
9337 wouldbe_col = screen_cur_col;
9338 cost = 0;
9339 }
9340
9341 /*
9342 * Check if any characters that need to be written have the
9343 * correct attributes. Also avoid UTF-8 characters.
9344 */
9345 i = col - wouldbe_col;
9346 if (i > 0)
9347 cost += i;
9348 if (cost < goto_cost && i > 0)
9349 {
9350 /*
9351 * Check if the attributes are correct without additionally
9352 * stopping highlighting.
9353 */
9354 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9355 while (i && *p++ == attr)
9356 --i;
9357 if (i != 0)
9358 {
9359 /*
9360 * Try if it works when highlighting is stopped here.
9361 */
9362 if (*--p == 0)
9363 {
9364 cost += noinvcurs;
9365 while (i && *p++ == 0)
9366 --i;
9367 }
9368 if (i != 0)
9369 cost = 999; /* different attributes, don't do it */
9370 }
9371#ifdef FEAT_MBYTE
9372 if (enc_utf8)
9373 {
9374 /* Don't use an UTF-8 char for positioning, it's slow. */
9375 for (i = wouldbe_col; i < col; ++i)
9376 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9377 {
9378 cost = 999;
9379 break;
9380 }
9381 }
9382#endif
9383 }
9384
9385 /*
9386 * We can do it without term_windgoto()!
9387 */
9388 if (cost < goto_cost)
9389 {
9390 if (plan == PLAN_LE)
9391 {
9392 if (noinvcurs)
9393 screen_stop_highlight();
9394 while (screen_cur_col > col)
9395 {
9396 out_str(bs);
9397 --screen_cur_col;
9398 }
9399 }
9400 else if (plan == PLAN_CR)
9401 {
9402 if (noinvcurs)
9403 screen_stop_highlight();
9404 out_char('\r');
9405 screen_cur_col = 0;
9406 }
9407 else if (plan == PLAN_NL)
9408 {
9409 if (noinvcurs)
9410 screen_stop_highlight();
9411 while (screen_cur_row < row)
9412 {
9413 out_char('\n');
9414 ++screen_cur_row;
9415 }
9416 screen_cur_col = 0;
9417 }
9418
9419 i = col - screen_cur_col;
9420 if (i > 0)
9421 {
9422 /*
9423 * Use cursor-right if it's one character only. Avoids
9424 * removing a line of pixels from the last bold char, when
9425 * using the bold trick in the GUI.
9426 */
9427 if (T_ND[0] != NUL && T_ND[1] == NUL)
9428 {
9429 while (i-- > 0)
9430 out_char(*T_ND);
9431 }
9432 else
9433 {
9434 int off;
9435
9436 off = LineOffset[row] + screen_cur_col;
9437 while (i-- > 0)
9438 {
9439 if (ScreenAttrs[off] != screen_attr)
9440 screen_stop_highlight();
9441#ifdef FEAT_MBYTE
9442 out_flush_check();
9443#endif
9444 out_char(ScreenLines[off]);
9445#ifdef FEAT_MBYTE
9446 if (enc_dbcs == DBCS_JPNU
9447 && ScreenLines[off] == 0x8e)
9448 out_char(ScreenLines2[off]);
9449#endif
9450 ++off;
9451 }
9452 }
9453 }
9454 }
9455 }
9456 else
9457 cost = 999;
9458
9459 if (cost >= goto_cost)
9460 {
9461 if (noinvcurs)
9462 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009463 if (row == screen_cur_row && (col > screen_cur_col)
9464 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 term_cursor_right(col - screen_cur_col);
9466 else
9467 term_windgoto(row, col);
9468 }
9469 screen_cur_row = row;
9470 screen_cur_col = col;
9471 }
9472}
9473
9474/*
9475 * Set cursor to its position in the current window.
9476 */
9477 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009478setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009480 setcursor_mayforce(FALSE);
9481}
9482
9483/*
9484 * Set cursor to its position in the current window.
9485 * When "force" is TRUE also when not redrawing.
9486 */
9487 void
9488setcursor_mayforce(int force)
9489{
9490 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 {
9492 validate_cursor();
9493 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009494 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009496 /* With 'rightleft' set and the cursor on a double-wide
9497 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009498 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009500 (has_mbyte
9501 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9502 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503# endif
9504 1)) :
9505#endif
9506 curwin->w_wcol));
9507 }
9508}
9509
9510
9511/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009512 * Insert 'line_count' lines at 'row' in window 'wp'.
9513 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9514 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 * scrolling.
9516 * Returns FAIL if the lines are not inserted, OK for success.
9517 */
9518 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009519win_ins_lines(
9520 win_T *wp,
9521 int row,
9522 int line_count,
9523 int invalid,
9524 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525{
9526 int did_delete;
9527 int nextrow;
9528 int lastrow;
9529 int retval;
9530
9531 if (invalid)
9532 wp->w_lines_valid = 0;
9533
9534 if (wp->w_height < 5)
9535 return FAIL;
9536
9537 if (line_count > wp->w_height - row)
9538 line_count = wp->w_height - row;
9539
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009540 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 if (retval != MAYBE)
9542 return retval;
9543
9544 /*
9545 * If there is a next window or a status line, we first try to delete the
9546 * lines at the bottom to avoid messing what is after the window.
9547 * If this fails and there are following windows, don't do anything to avoid
9548 * messing up those windows, better just redraw.
9549 */
9550 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 if (wp->w_next != NULL || wp->w_status_height)
9552 {
9553 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009554 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 did_delete = TRUE;
9556 else if (wp->w_next)
9557 return FAIL;
9558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 /*
9560 * if no lines deleted, blank the lines that will end up below the window
9561 */
9562 if (!did_delete)
9563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009566 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 lastrow = nextrow + line_count;
9568 if (lastrow > Rows)
9569 lastrow = Rows;
9570 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009571 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 ' ', ' ', 0);
9573 }
9574
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009575 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 == FAIL)
9577 {
9578 /* deletion will have messed up other windows */
9579 if (did_delete)
9580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 win_rest_invalid(W_NEXT(wp));
9583 }
9584 return FAIL;
9585 }
9586
9587 return OK;
9588}
9589
9590/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009591 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9593 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9594 * scrolling
9595 * Return OK for success, FAIL if the lines are not deleted.
9596 */
9597 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009598win_del_lines(
9599 win_T *wp,
9600 int row,
9601 int line_count,
9602 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009603 int mayclear,
9604 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605{
9606 int retval;
9607
9608 if (invalid)
9609 wp->w_lines_valid = 0;
9610
9611 if (line_count > wp->w_height - row)
9612 line_count = wp->w_height - row;
9613
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009614 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 if (retval != MAYBE)
9616 return retval;
9617
9618 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009619 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 return FAIL;
9621
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 /*
9623 * If there are windows or status lines below, try to put them at the
9624 * correct place. If we can't do that, they have to be redrawn.
9625 */
9626 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9627 {
9628 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009629 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 {
9631 wp->w_redr_status = TRUE;
9632 win_rest_invalid(wp->w_next);
9633 }
9634 }
9635 /*
9636 * If this is the last window and there is no status line, redraw the
9637 * command line later.
9638 */
9639 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 redraw_cmdline = TRUE;
9641 return OK;
9642}
9643
9644/*
9645 * Common code for win_ins_lines() and win_del_lines().
9646 * Returns OK or FAIL when the work has been done.
9647 * Returns MAYBE when not finished yet.
9648 */
9649 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009650win_do_lines(
9651 win_T *wp,
9652 int row,
9653 int line_count,
9654 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009655 int del,
9656 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
9658 int retval;
9659
9660 if (!redrawing() || line_count <= 0)
9661 return FAIL;
9662
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009663 /* When inserting lines would result in loss of command output, just redraw
9664 * the lines. */
9665 if (no_win_do_lines_ins && !del)
9666 return FAIL;
9667
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009669 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009671 if (!no_win_do_lines_ins)
9672 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 return FAIL;
9674 }
9675
9676 /*
9677 * Delete all remaining lines
9678 */
9679 if (row + line_count >= wp->w_height)
9680 {
9681 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009682 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 ' ', ' ', 0);
9684 return OK;
9685 }
9686
9687 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009688 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009690 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009692 if (!no_win_do_lines_ins)
9693 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694
9695 /*
9696 * If the terminal can set a scroll region, use that.
9697 * Always do this in a vertically split window. This will redraw from
9698 * ScreenLines[] when t_CV isn't defined. That's faster than using
9699 * win_line().
9700 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009701 * a character in the lower right corner of the scroll region may cause a
9702 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009704 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 scroll_region_set(wp, row);
9708 if (del)
9709 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009710 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 else
9712 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009713 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 scroll_region_reset();
9716 return retval;
9717 }
9718
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9720 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721
9722 return MAYBE;
9723}
9724
9725/*
9726 * window 'wp' and everything after it is messed up, mark it for redraw
9727 */
9728 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009729win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 {
9733 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 wp->w_redr_status = TRUE;
9735 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 }
9737 redraw_cmdline = TRUE;
9738}
9739
9740/*
9741 * The rest of the routines in this file perform screen manipulations. The
9742 * given operation is performed physically on the screen. The corresponding
9743 * change is also made to the internal screen image. In this way, the editor
9744 * anticipates the effect of editing changes on the appearance of the screen.
9745 * That way, when we call screenupdate a complete redraw isn't usually
9746 * necessary. Another advantage is that we can keep adding code to anticipate
9747 * screen changes, and in the meantime, everything still works.
9748 */
9749
9750/*
9751 * types for inserting or deleting lines
9752 */
9753#define USE_T_CAL 1
9754#define USE_T_CDL 2
9755#define USE_T_AL 3
9756#define USE_T_CE 4
9757#define USE_T_DL 5
9758#define USE_T_SR 6
9759#define USE_NL 7
9760#define USE_T_CD 8
9761#define USE_REDRAW 9
9762
9763/*
9764 * insert lines on the screen and update ScreenLines[]
9765 * 'end' is the line after the scrolled part. Normally it is Rows.
9766 * When scrolling region used 'off' is the offset from the top for the region.
9767 * 'row' and 'end' are relative to the start of the region.
9768 *
9769 * return FAIL for failure, OK for success.
9770 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009771 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009772screen_ins_lines(
9773 int off,
9774 int row,
9775 int line_count,
9776 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009777 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009778 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779{
9780 int i;
9781 int j;
9782 unsigned temp;
9783 int cursor_row;
9784 int type;
9785 int result_empty;
9786 int can_ce = can_clear(T_CE);
9787
9788 /*
9789 * FAIL if
9790 * - there is no valid screen
9791 * - the screen has to be redrawn completely
9792 * - the line count is less than one
9793 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009794 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009796 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9797#ifdef FEAT_CLIPBOARD
9798 || (clip_star.state != SELECT_CLEARED
9799 && redrawing_for_callback > 0)
9800#endif
9801 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 return FAIL;
9803
9804 /*
9805 * There are seven ways to insert lines:
9806 * 0. When in a vertically split window and t_CV isn't set, redraw the
9807 * characters from ScreenLines[].
9808 * 1. Use T_CD (clear to end of display) if it exists and the result of
9809 * the insert is just empty lines
9810 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9811 * present or line_count > 1. It looks better if we do all the inserts
9812 * at once.
9813 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9814 * insert is just empty lines and T_CE is not present or line_count >
9815 * 1.
9816 * 4. Use T_AL (insert line) if it exists.
9817 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9818 * just empty lines.
9819 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9820 * just empty lines.
9821 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9822 * the 'da' flag is not set or we have clear line capability.
9823 * 8. redraw the characters from ScreenLines[].
9824 *
9825 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9826 * the scrollbar for the window. It does have insert line, use that if it
9827 * exists.
9828 */
9829 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9831 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009832 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 type = USE_T_CD;
9834 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9835 type = USE_T_CAL;
9836 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9837 type = USE_T_CDL;
9838 else if (*T_AL != NUL)
9839 type = USE_T_AL;
9840 else if (can_ce && result_empty)
9841 type = USE_T_CE;
9842 else if (*T_DL != NUL && result_empty)
9843 type = USE_T_DL;
9844 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9845 type = USE_T_SR;
9846 else
9847 return FAIL;
9848
9849 /*
9850 * For clearing the lines screen_del_lines() is used. This will also take
9851 * care of t_db if necessary.
9852 */
9853 if (type == USE_T_CD || type == USE_T_CDL ||
9854 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009855 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856
9857 /*
9858 * If text is retained below the screen, first clear or delete as many
9859 * lines at the bottom of the window as are about to be inserted so that
9860 * the deleted lines won't later surface during a screen_del_lines.
9861 */
9862 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009863 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864
9865#ifdef FEAT_CLIPBOARD
9866 /* Remove a modeless selection when inserting lines halfway the screen
9867 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009868 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009869 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 else
9871 clip_scroll_selection(-line_count);
9872#endif
9873
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874#ifdef FEAT_GUI
9875 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9876 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009877 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878#endif
9879
9880 if (*T_CCS != NUL) /* cursor relative to region */
9881 cursor_row = row;
9882 else
9883 cursor_row = row + off;
9884
9885 /*
9886 * Shift LineOffset[] line_count down to reflect the inserted lines.
9887 * Clear the inserted lines in ScreenLines[].
9888 */
9889 row += off;
9890 end += off;
9891 for (i = 0; i < line_count; ++i)
9892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 if (wp != NULL && wp->w_width != Columns)
9894 {
9895 /* need to copy part of a line */
9896 j = end - 1 - i;
9897 while ((j -= line_count) >= row)
9898 linecopy(j + line_count, j, wp);
9899 j += line_count;
9900 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009901 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9902 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 else
9904 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9905 LineWraps[j] = FALSE;
9906 }
9907 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 {
9909 j = end - 1 - i;
9910 temp = LineOffset[j];
9911 while ((j -= line_count) >= row)
9912 {
9913 LineOffset[j + line_count] = LineOffset[j];
9914 LineWraps[j + line_count] = LineWraps[j];
9915 }
9916 LineOffset[j + line_count] = temp;
9917 LineWraps[j + line_count] = FALSE;
9918 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009919 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 else
9921 lineinvalid(temp, (int)Columns);
9922 }
9923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924
9925 screen_stop_highlight();
9926 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009927 if (clear_attr != 0)
9928 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 /* redraw the characters */
9931 if (type == USE_REDRAW)
9932 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009933 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 {
9935 term_append_lines(line_count);
9936 screen_start(); /* don't know where cursor is now */
9937 }
9938 else
9939 {
9940 for (i = 0; i < line_count; i++)
9941 {
9942 if (type == USE_T_AL)
9943 {
9944 if (i && cursor_row != 0)
9945 windgoto(cursor_row, 0);
9946 out_str(T_AL);
9947 }
9948 else /* type == USE_T_SR */
9949 out_str(T_SR);
9950 screen_start(); /* don't know where cursor is now */
9951 }
9952 }
9953
9954 /*
9955 * With scroll-reverse and 'da' flag set we need to clear the lines that
9956 * have been scrolled down into the region.
9957 */
9958 if (type == USE_T_SR && *T_DA)
9959 {
9960 for (i = 0; i < line_count; ++i)
9961 {
9962 windgoto(off + i, 0);
9963 out_str(T_CE);
9964 screen_start(); /* don't know where cursor is now */
9965 }
9966 }
9967
9968#ifdef FEAT_GUI
9969 gui_can_update_cursor();
9970 if (gui.in_use)
9971 out_flush(); /* always flush after a scroll */
9972#endif
9973 return OK;
9974}
9975
9976/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009977 * Delete lines on the screen and update ScreenLines[].
9978 * "end" is the line after the scrolled part. Normally it is Rows.
9979 * When scrolling region used "off" is the offset from the top for the region.
9980 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 *
9982 * Return OK for success, FAIL if the lines are not deleted.
9983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009985screen_del_lines(
9986 int off,
9987 int row,
9988 int line_count,
9989 int end,
9990 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009991 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009992 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993{
9994 int j;
9995 int i;
9996 unsigned temp;
9997 int cursor_row;
9998 int cursor_end;
9999 int result_empty; /* result is empty until end of region */
10000 int can_delete; /* deleting line codes can be used */
10001 int type;
10002
10003 /*
10004 * FAIL if
10005 * - there is no valid screen
10006 * - the screen has to be redrawn completely
10007 * - the line count is less than one
10008 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010009 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010011 if (!screen_valid(TRUE) || line_count <= 0
10012 || (!force && line_count > p_ttyscroll)
10013#ifdef FEAT_CLIPBOARD
10014 || (clip_star.state != SELECT_CLEARED
10015 && redrawing_for_callback > 0)
10016#endif
10017 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 return FAIL;
10019
10020 /*
10021 * Check if the rest of the current region will become empty.
10022 */
10023 result_empty = row + line_count >= end;
10024
10025 /*
10026 * We can delete lines only when 'db' flag not set or when 'ce' option
10027 * available.
10028 */
10029 can_delete = (*T_DB == NUL || can_clear(T_CE));
10030
10031 /*
10032 * There are six ways to delete lines:
10033 * 0. When in a vertically split window and t_CV isn't set, redraw the
10034 * characters from ScreenLines[].
10035 * 1. Use T_CD if it exists and the result is empty.
10036 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10037 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10038 * none of the other ways work.
10039 * 4. Use T_CE (erase line) if the result is empty.
10040 * 5. Use T_DL (delete line) if it exists.
10041 * 6. redraw the characters from ScreenLines[].
10042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10044 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010045 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 type = USE_T_CD;
10047#if defined(__BEOS__) && defined(BEOS_DR8)
10048 /*
10049 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10050 * its internal termcap... this works okay for tests which test *T_DB !=
10051 * NUL. It has the disadvantage that the user cannot use any :set t_*
10052 * command to get T_DB (back) to empty_option, only :set term=... will do
10053 * the trick...
10054 * Anyway, this hack will hopefully go away with the next OS release.
10055 * (Olaf Seibert)
10056 */
10057 else if (row == 0 && T_DB == empty_option
10058 && (line_count == 1 || *T_CDL == NUL))
10059#else
10060 else if (row == 0 && (
10061#ifndef AMIGA
10062 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10063 * up, so use delete-line command */
10064 line_count == 1 ||
10065#endif
10066 *T_CDL == NUL))
10067#endif
10068 type = USE_NL;
10069 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10070 type = USE_T_CDL;
10071 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010072 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 type = USE_T_CE;
10074 else if (*T_DL != NUL && can_delete)
10075 type = USE_T_DL;
10076 else if (*T_CDL != NUL && can_delete)
10077 type = USE_T_CDL;
10078 else
10079 return FAIL;
10080
10081#ifdef FEAT_CLIPBOARD
10082 /* Remove a modeless selection when deleting lines halfway the screen or
10083 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010084 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010085 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 else
10087 clip_scroll_selection(line_count);
10088#endif
10089
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090#ifdef FEAT_GUI
10091 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10092 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010093 gui_dont_update_cursor(gui.cursor_row >= row + off
10094 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095#endif
10096
10097 if (*T_CCS != NUL) /* cursor relative to region */
10098 {
10099 cursor_row = row;
10100 cursor_end = end;
10101 }
10102 else
10103 {
10104 cursor_row = row + off;
10105 cursor_end = end + off;
10106 }
10107
10108 /*
10109 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10110 * Clear the inserted lines in ScreenLines[].
10111 */
10112 row += off;
10113 end += off;
10114 for (i = 0; i < line_count; ++i)
10115 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 if (wp != NULL && wp->w_width != Columns)
10117 {
10118 /* need to copy part of a line */
10119 j = row + i;
10120 while ((j += line_count) <= end - 1)
10121 linecopy(j - line_count, j, wp);
10122 j -= line_count;
10123 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010124 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10125 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 else
10127 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10128 LineWraps[j] = FALSE;
10129 }
10130 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 {
10132 /* whole width, moving the line pointers is faster */
10133 j = row + i;
10134 temp = LineOffset[j];
10135 while ((j += line_count) <= end - 1)
10136 {
10137 LineOffset[j - line_count] = LineOffset[j];
10138 LineWraps[j - line_count] = LineWraps[j];
10139 }
10140 LineOffset[j - line_count] = temp;
10141 LineWraps[j - line_count] = FALSE;
10142 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010143 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 else
10145 lineinvalid(temp, (int)Columns);
10146 }
10147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010149 if (screen_attr != clear_attr)
10150 screen_stop_highlight();
10151 if (clear_attr != 0)
10152 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 /* redraw the characters */
10155 if (type == USE_REDRAW)
10156 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010157 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 {
10159 windgoto(cursor_row, 0);
10160 out_str(T_CD);
10161 screen_start(); /* don't know where cursor is now */
10162 }
10163 else if (type == USE_T_CDL)
10164 {
10165 windgoto(cursor_row, 0);
10166 term_delete_lines(line_count);
10167 screen_start(); /* don't know where cursor is now */
10168 }
10169 /*
10170 * Deleting lines at top of the screen or scroll region: Just scroll
10171 * the whole screen (scroll region) up by outputting newlines on the
10172 * last line.
10173 */
10174 else if (type == USE_NL)
10175 {
10176 windgoto(cursor_end - 1, 0);
10177 for (i = line_count; --i >= 0; )
10178 out_char('\n'); /* cursor will remain on same line */
10179 }
10180 else
10181 {
10182 for (i = line_count; --i >= 0; )
10183 {
10184 if (type == USE_T_DL)
10185 {
10186 windgoto(cursor_row, 0);
10187 out_str(T_DL); /* delete a line */
10188 }
10189 else /* type == USE_T_CE */
10190 {
10191 windgoto(cursor_row + i, 0);
10192 out_str(T_CE); /* erase a line */
10193 }
10194 screen_start(); /* don't know where cursor is now */
10195 }
10196 }
10197
10198 /*
10199 * If the 'db' flag is set, we need to clear the lines that have been
10200 * scrolled up at the bottom of the region.
10201 */
10202 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10203 {
10204 for (i = line_count; i > 0; --i)
10205 {
10206 windgoto(cursor_end - i, 0);
10207 out_str(T_CE); /* erase a line */
10208 screen_start(); /* don't know where cursor is now */
10209 }
10210 }
10211
10212#ifdef FEAT_GUI
10213 gui_can_update_cursor();
10214 if (gui.in_use)
10215 out_flush(); /* always flush after a scroll */
10216#endif
10217
10218 return OK;
10219}
10220
10221/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010222 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 *
10224 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10225 * If clear_cmdline is FALSE there may be a message there that needs to be
10226 * cleared only if a mode is shown.
10227 * Return the length of the message (0 if no message).
10228 */
10229 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010230showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231{
10232 int need_clear;
10233 int length = 0;
10234 int do_mode;
10235 int attr;
10236 int nwr_save;
10237#ifdef FEAT_INS_EXPAND
10238 int sub_attr;
10239#endif
10240
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010241 do_mode = ((p_smd && msg_silent == 0)
10242 && ((State & INSERT)
10243 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010244 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 if (do_mode || Recording)
10246 {
10247 /*
10248 * Don't show mode right now, when not redrawing or inside a mapping.
10249 * Call char_avail() only when we are going to show something, because
10250 * it takes a bit of time.
10251 */
10252 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10253 {
10254 redraw_cmdline = TRUE; /* show mode later */
10255 return 0;
10256 }
10257
10258 nwr_save = need_wait_return;
10259
10260 /* wait a bit before overwriting an important message */
10261 check_for_delay(FALSE);
10262
10263 /* if the cmdline is more than one line high, erase top lines */
10264 need_clear = clear_cmdline;
10265 if (clear_cmdline && cmdline_row < Rows - 1)
10266 msg_clr_cmdline(); /* will reset clear_cmdline */
10267
10268 /* Position on the last line in the window, column 0 */
10269 msg_pos_mode();
10270 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010271 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 if (do_mode)
10273 {
10274 MSG_PUTS_ATTR("--", attr);
10275#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010276 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010277# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010278 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010279# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010280 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010281# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010282 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010283# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 MSG_PUTS_ATTR(" IM", attr);
10285# else
10286 MSG_PUTS_ATTR(" XIM", attr);
10287# endif
10288#endif
10289#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10290 if (gui.in_use)
10291 {
10292 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010293 {
10294 /* HANGUL */
10295 if (enc_utf8)
10296 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10297 else
10298 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 }
10301#endif
10302#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010303 /* CTRL-X in Insert mode */
10304 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 {
10306 /* These messages can get long, avoid a wrap in a narrow
10307 * window. Prefer showing edit_submode_extra. */
10308 length = (Rows - msg_row) * Columns - 3;
10309 if (edit_submode_extra != NULL)
10310 length -= vim_strsize(edit_submode_extra);
10311 if (length > 0)
10312 {
10313 if (edit_submode_pre != NULL)
10314 length -= vim_strsize(edit_submode_pre);
10315 if (length - vim_strsize(edit_submode) > 0)
10316 {
10317 if (edit_submode_pre != NULL)
10318 msg_puts_attr(edit_submode_pre, attr);
10319 msg_puts_attr(edit_submode, attr);
10320 }
10321 if (edit_submode_extra != NULL)
10322 {
10323 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10324 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010325 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 else
10327 sub_attr = attr;
10328 msg_puts_attr(edit_submode_extra, sub_attr);
10329 }
10330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 }
10332 else
10333#endif
10334 {
10335#ifdef FEAT_VREPLACE
10336 if (State & VREPLACE_FLAG)
10337 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10338 else
10339#endif
10340 if (State & REPLACE_FLAG)
10341 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10342 else if (State & INSERT)
10343 {
10344#ifdef FEAT_RIGHTLEFT
10345 if (p_ri)
10346 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10347#endif
10348 MSG_PUTS_ATTR(_(" INSERT"), attr);
10349 }
10350 else if (restart_edit == 'I')
10351 MSG_PUTS_ATTR(_(" (insert)"), attr);
10352 else if (restart_edit == 'R')
10353 MSG_PUTS_ATTR(_(" (replace)"), attr);
10354 else if (restart_edit == 'V')
10355 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10356#ifdef FEAT_RIGHTLEFT
10357 if (p_hkmap)
10358 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10359# ifdef FEAT_FKMAP
10360 if (p_fkmap)
10361 MSG_PUTS_ATTR(farsi_text_5, attr);
10362# endif
10363#endif
10364#ifdef FEAT_KEYMAP
10365 if (State & LANGMAP)
10366 {
10367# ifdef FEAT_ARABIC
10368 if (curwin->w_p_arab)
10369 MSG_PUTS_ATTR(_(" Arabic"), attr);
10370 else
10371# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010372 if (get_keymap_str(curwin, (char_u *)" (%s)",
10373 NameBuff, MAXPATHL))
10374 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 }
10376#endif
10377 if ((State & INSERT) && p_paste)
10378 MSG_PUTS_ATTR(_(" (paste)"), attr);
10379
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 if (VIsual_active)
10381 {
10382 char *p;
10383
10384 /* Don't concatenate separate words to avoid translation
10385 * problems. */
10386 switch ((VIsual_select ? 4 : 0)
10387 + (VIsual_mode == Ctrl_V) * 2
10388 + (VIsual_mode == 'V'))
10389 {
10390 case 0: p = N_(" VISUAL"); break;
10391 case 1: p = N_(" VISUAL LINE"); break;
10392 case 2: p = N_(" VISUAL BLOCK"); break;
10393 case 4: p = N_(" SELECT"); break;
10394 case 5: p = N_(" SELECT LINE"); break;
10395 default: p = N_(" SELECT BLOCK"); break;
10396 }
10397 MSG_PUTS_ATTR(_(p), attr);
10398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 MSG_PUTS_ATTR(" --", attr);
10400 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010401
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 need_clear = TRUE;
10403 }
10404 if (Recording
10405#ifdef FEAT_INS_EXPAND
10406 && edit_submode == NULL /* otherwise it gets too long */
10407#endif
10408 )
10409 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010410 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 need_clear = TRUE;
10412 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010413
10414 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 if (need_clear || clear_cmdline)
10416 msg_clr_eos();
10417 msg_didout = FALSE; /* overwrite this message */
10418 length = msg_col;
10419 msg_col = 0;
10420 need_wait_return = nwr_save; /* never ask for hit-return for this */
10421 }
10422 else if (clear_cmdline && msg_silent == 0)
10423 /* Clear the whole command line. Will reset "clear_cmdline". */
10424 msg_clr_cmdline();
10425
10426#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 /* In Visual mode the size of the selected area must be redrawn. */
10428 if (VIsual_active)
10429 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430
10431 /* If the last window has no status line, the ruler is after the mode
10432 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010433 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 win_redr_ruler(lastwin, TRUE);
10435#endif
10436 redraw_cmdline = FALSE;
10437 clear_cmdline = FALSE;
10438
10439 return length;
10440}
10441
10442/*
10443 * Position for a mode message.
10444 */
10445 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010446msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447{
10448 msg_col = 0;
10449 msg_row = Rows - 1;
10450}
10451
10452/*
10453 * Delete mode message. Used when ESC is typed which is expected to end
10454 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010455 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 */
10457 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010458unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459{
10460 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010461 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 */
10463 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10464 redraw_cmdline = TRUE; /* delete mode later */
10465 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010466 clearmode();
10467}
10468
10469/*
10470 * Clear the mode message.
10471 */
10472 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010473clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010474{
10475 msg_pos_mode();
10476 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010477 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010478 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479}
10480
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010481 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010482recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010483{
10484 MSG_PUTS_ATTR(_("recording"), attr);
10485 if (!shortmess(SHM_RECORDING))
10486 {
10487 char_u s[4];
10488 sprintf((char *)s, " @%c", Recording);
10489 MSG_PUTS_ATTR(s, attr);
10490 }
10491}
10492
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010493/*
10494 * Draw the tab pages line at the top of the Vim window.
10495 */
10496 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010497draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010498{
10499 int tabcount = 0;
10500 tabpage_T *tp;
10501 int tabwidth;
10502 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010503 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010504 int attr;
10505 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010506 win_T *cwp;
10507 int wincount;
10508 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010509 int c;
10510 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010511 int attr_sel = HL_ATTR(HLF_TPS);
10512 int attr_nosel = HL_ATTR(HLF_TP);
10513 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010514 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010515 int room;
10516 int use_sep_chars = (t_colors < 8
10517#ifdef FEAT_GUI
10518 && !gui.in_use
10519#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010520#ifdef FEAT_TERMGUICOLORS
10521 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010522#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010523 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010524
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010525 if (ScreenLines == NULL)
10526 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010527 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010528
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010529#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010530 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010531 if (gui_use_tabline())
10532 {
10533 gui_update_tabline();
10534 return;
10535 }
10536#endif
10537
10538 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010539 return;
10540
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010541#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010542
10543 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10544 for (scol = 0; scol < Columns; ++scol)
10545 TabPageIdxs[scol] = 0;
10546
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010547 /* Use the 'tabline' option if it's set. */
10548 if (*p_tal != NUL)
10549 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010550 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010551
10552 /* Check for an error. If there is one we would loop in redrawing the
10553 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010554 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010555 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010556 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010557 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010558 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010559 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010560 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010561 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010562#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010563 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010564 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010565 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010566
Bram Moolenaar238a5642006-02-21 22:12:05 +000010567 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10568 if (tabwidth < 6)
10569 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010570
Bram Moolenaar238a5642006-02-21 22:12:05 +000010571 attr = attr_nosel;
10572 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010573 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010574 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10575 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010576 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010577 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010578
Bram Moolenaar238a5642006-02-21 22:12:05 +000010579 if (tp->tp_topframe == topframe)
10580 attr = attr_sel;
10581 if (use_sep_chars && col > 0)
10582 screen_putchar('|', 0, col++, attr);
10583
10584 if (tp->tp_topframe != topframe)
10585 attr = attr_nosel;
10586
10587 screen_putchar(' ', 0, col++, attr);
10588
10589 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010590 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010591 cwp = curwin;
10592 wp = firstwin;
10593 }
10594 else
10595 {
10596 cwp = tp->tp_curwin;
10597 wp = tp->tp_firstwin;
10598 }
10599
10600 modified = FALSE;
10601 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10602 if (bufIsChanged(wp->w_buffer))
10603 modified = TRUE;
10604 if (modified || wincount > 1)
10605 {
10606 if (wincount > 1)
10607 {
10608 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010609 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010610 if (col + len >= Columns - 3)
10611 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010612 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010613#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010614 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010615#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010616 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010617#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010618 );
10619 col += len;
10620 }
10621 if (modified)
10622 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10623 screen_putchar(' ', 0, col++, attr);
10624 }
10625
10626 room = scol - col + tabwidth - 1;
10627 if (room > 0)
10628 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010629 /* Get buffer name in NameBuff[] */
10630 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010631 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010632 len = vim_strsize(NameBuff);
10633 p = NameBuff;
10634#ifdef FEAT_MBYTE
10635 if (has_mbyte)
10636 while (len > room)
10637 {
10638 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010639 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010640 }
10641 else
10642#endif
10643 if (len > room)
10644 {
10645 p += len - room;
10646 len = room;
10647 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010648 if (len > Columns - col - 1)
10649 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010650
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010651 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010652 col += len;
10653 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010654 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010655
10656 /* Store the tab page number in TabPageIdxs[], so that
10657 * jump_to_mouse() knows where each one is. */
10658 ++tabcount;
10659 while (scol < col)
10660 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010661 }
10662
Bram Moolenaar238a5642006-02-21 22:12:05 +000010663 if (use_sep_chars)
10664 c = '_';
10665 else
10666 c = ' ';
10667 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010668
10669 /* Put an "X" for closing the current tab if there are several. */
10670 if (first_tabpage->tp_next != NULL)
10671 {
10672 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10673 TabPageIdxs[Columns - 1] = -999;
10674 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010675 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010676
10677 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10678 * set. */
10679 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010680}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010681
10682/*
10683 * Get buffer name for "buf" into NameBuff[].
10684 * Takes care of special buffer names and translates special characters.
10685 */
10686 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010687get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010688{
10689 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010690 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010691 else
10692 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10693 trans_characters(NameBuff, MAXPATHL);
10694}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010695
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696/*
10697 * Get the character to use in a status line. Get its attributes in "*attr".
10698 */
10699 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010700fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701{
10702 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010703
10704#ifdef FEAT_TERMINAL
10705 if (bt_terminal(wp->w_buffer))
10706 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010707 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010708 {
10709 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010710 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010711 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010712 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010713 {
10714 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010715 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010716 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010717 }
10718 else
10719#endif
10720 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010722 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 fill = fill_stl;
10724 }
10725 else
10726 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010727 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 fill = fill_stlnc;
10729 }
10730 /* Use fill when there is highlighting, and highlighting of current
10731 * window differs, or the fillchars differ, or this is not the
10732 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010733 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010734 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 || (fill_stl != fill_stlnc)))
10736 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010737 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 return '^';
10739 return '=';
10740}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742/*
10743 * Get the character to use in a separator between vertically split windows.
10744 * Get its attributes in "*attr".
10745 */
10746 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010747fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010749 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 if (*attr == 0 && fill_vert == ' ')
10751 return '|';
10752 else
10753 return fill_vert;
10754}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755
10756/*
10757 * Return TRUE if redrawing should currently be done.
10758 */
10759 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010760redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010762#ifdef FEAT_EVAL
10763 if (disable_redraw_for_testing)
10764 return 0;
10765 else
10766#endif
10767 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10769}
10770
10771/*
10772 * Return TRUE if printing messages should currently be done.
10773 */
10774 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010775messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776{
10777 return (!(p_lz && char_avail() && !KeyTyped));
10778}
10779
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010780#ifdef FEAT_MENU
10781/*
10782 * Draw the window toolbar.
10783 */
10784 static void
10785redraw_win_toolbar(win_T *wp)
10786{
10787 vimmenu_T *menu;
10788 int item_idx = 0;
10789 int item_count = 0;
10790 int col = 0;
10791 int next_col;
10792 int off = (int)(current_ScreenLine - ScreenLines);
10793 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10794 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10795
10796 vim_free(wp->w_winbar_items);
10797 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10798 ++item_count;
10799 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10800 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10801
10802 /* TODO: use fewer spaces if there is not enough room */
10803 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010804 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010805 {
10806 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010807 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010808 break;
10809 if (col > 1)
10810 {
10811 space_to_screenline(off + col, fill_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
10816 wp->w_winbar_items[item_idx].wb_startcol = col;
10817 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010818 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010819 break;
10820
10821 next_col = text_to_screenline(wp, menu->name, col);
10822 while (col < next_col)
10823 {
10824 ScreenAttrs[off + col] = button_attr;
10825 ++col;
10826 }
10827 wp->w_winbar_items[item_idx].wb_endcol = col;
10828 wp->w_winbar_items[item_idx].wb_menu = menu;
10829 ++item_idx;
10830
Bram Moolenaar02631462017-09-22 15:20:32 +020010831 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010832 break;
10833 space_to_screenline(off + col, button_attr);
10834 ++col;
10835 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010836 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010837 {
10838 space_to_screenline(off + col, fill_attr);
10839 ++col;
10840 }
10841 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10842
Bram Moolenaar02631462017-09-22 15:20:32 +020010843 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10844 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010845}
10846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847/*
10848 * Show current status info in ruler and various other places
10849 * If always is FALSE, only show ruler if position has changed.
10850 */
10851 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010852showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853{
10854 if (!always && !redrawing())
10855 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010856#ifdef FEAT_INS_EXPAND
10857 if (pum_visible())
10858 {
10859 /* Don't redraw right now, do it later. */
10860 curwin->w_redr_status = TRUE;
10861 return;
10862 }
10863#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010864#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010865 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010866 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010867 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 else
10870#endif
10871#ifdef FEAT_CMDL_INFO
10872 win_redr_ruler(curwin, always);
10873#endif
10874
10875#ifdef FEAT_TITLE
10876 if (need_maketitle
10877# ifdef FEAT_STL_OPT
10878 || (p_icon && (stl_syntax & STL_IN_ICON))
10879 || (p_title && (stl_syntax & STL_IN_TITLE))
10880# endif
10881 )
10882 maketitle();
10883#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010884 /* Redraw the tab pages line if needed. */
10885 if (redraw_tabline)
10886 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887}
10888
10889#ifdef FEAT_CMDL_INFO
10890 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010891win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010893#define RULER_BUF_LEN 70
10894 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 int row;
10896 int fillchar;
10897 int attr;
10898 int empty_line = FALSE;
10899 colnr_T virtcol;
10900 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010901 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 int this_ru_col;
10904 int off = 0;
10905 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906
10907 /* If 'ruler' off or redrawing disabled, don't do anything */
10908 if (!p_ru)
10909 return;
10910
10911 /*
10912 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10913 * after deleting lines, before cursor.lnum is corrected.
10914 */
10915 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10916 return;
10917
10918#ifdef FEAT_INS_EXPAND
10919 /* Don't draw the ruler while doing insert-completion, it might overwrite
10920 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 if (edit_submode != NULL)
10923 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010924 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10925 if (pum_visible())
10926 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927#endif
10928
10929#ifdef FEAT_STL_OPT
10930 if (*p_ruf)
10931 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010932 int save_called_emsg = called_emsg;
10933
10934 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010936 if (called_emsg)
10937 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010938 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010939 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 return;
10941 }
10942#endif
10943
10944 /*
10945 * Check if not in Insert mode and the line is empty (will show "0-1").
10946 */
10947 if (!(State & INSERT)
10948 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10949 empty_line = TRUE;
10950
10951 /*
10952 * Only draw the ruler when something changed.
10953 */
10954 validate_virtcol_win(wp);
10955 if ( redraw_cmdline
10956 || always
10957 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10958 || wp->w_cursor.col != wp->w_ru_cursor.col
10959 || wp->w_virtcol != wp->w_ru_virtcol
10960#ifdef FEAT_VIRTUALEDIT
10961 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10962#endif
10963 || wp->w_topline != wp->w_ru_topline
10964 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10965#ifdef FEAT_DIFF
10966 || wp->w_topfill != wp->w_ru_topfill
10967#endif
10968 || empty_line != wp->w_ru_empty)
10969 {
10970 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 if (wp->w_status_height)
10972 {
10973 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010974 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010975 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010976 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 }
10978 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 {
10980 row = Rows - 1;
10981 fillchar = ' ';
10982 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 width = Columns;
10984 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 }
10986
10987 /* In list mode virtcol needs to be recomputed */
10988 virtcol = wp->w_virtcol;
10989 if (wp->w_p_list && lcs_tab1 == NUL)
10990 {
10991 wp->w_p_list = FALSE;
10992 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10993 wp->w_p_list = TRUE;
10994 }
10995
10996 /*
10997 * Some sprintfs return the length, some return a pointer.
10998 * To avoid portability problems we use strlen() here.
10999 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011000 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11002 ? 0L
11003 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011004 len = STRLEN(buffer);
11005 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11007 (int)virtcol + 1);
11008
11009 /*
11010 * Add a "50%" if there is room for it.
11011 * On the last line, don't print in the last column (scrolls the
11012 * screen up on some terminals).
11013 */
11014 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011015 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 this_ru_col = ru_col - (Columns - width);
11020 if (this_ru_col < 0)
11021 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 /* Never use more than half the window/screen width, leave the other
11023 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011024 if (this_ru_col < (width + 1) / 2)
11025 this_ru_col = (width + 1) / 2;
11026 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011028 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011029 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 {
11031#ifdef FEAT_MBYTE
11032 if (has_mbyte)
11033 i += (*mb_char2bytes)(fillchar, buffer + i);
11034 else
11035#endif
11036 buffer[i++] = fillchar;
11037 ++o;
11038 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011039 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 }
11041 /* Truncate at window boundary. */
11042#ifdef FEAT_MBYTE
11043 if (has_mbyte)
11044 {
11045 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011046 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 {
11048 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011049 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 {
11051 buffer[i] = NUL;
11052 break;
11053 }
11054 }
11055 }
11056 else
11057#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011058 if (this_ru_col + (int)STRLEN(buffer) > width)
11059 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060
Bram Moolenaar4033c552017-09-16 20:54:51 +020011061 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 i = redraw_cmdline;
11063 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011064 this_ru_col + off + (int)STRLEN(buffer),
11065 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 fillchar, fillchar, attr);
11067 /* don't redraw the cmdline because of showing the ruler */
11068 redraw_cmdline = i;
11069 wp->w_ru_cursor = wp->w_cursor;
11070 wp->w_ru_virtcol = wp->w_virtcol;
11071 wp->w_ru_empty = empty_line;
11072 wp->w_ru_topline = wp->w_topline;
11073 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11074#ifdef FEAT_DIFF
11075 wp->w_ru_topfill = wp->w_topfill;
11076#endif
11077 }
11078}
11079#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011080
11081#if defined(FEAT_LINEBREAK) || defined(PROTO)
11082/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011083 * Return the width of the 'number' and 'relativenumber' column.
11084 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011085 * Otherwise it depends on 'numberwidth' and the line count.
11086 */
11087 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011088number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011089{
11090 int n;
11091 linenr_T lnum;
11092
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011093 if (wp->w_p_rnu && !wp->w_p_nu)
11094 /* cursor line shows "0" */
11095 lnum = wp->w_height;
11096 else
11097 /* cursor line shows absolute line number */
11098 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011099
Bram Moolenaar6b314672015-03-20 15:42:10 +010011100 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011101 return wp->w_nrwidth_width;
11102 wp->w_nrwidth_line_count = lnum;
11103
11104 n = 0;
11105 do
11106 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011107 lnum /= 10;
11108 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011109 } while (lnum > 0);
11110
11111 /* 'numberwidth' gives the minimal width plus one */
11112 if (n < wp->w_p_nuw - 1)
11113 n = wp->w_p_nuw - 1;
11114
11115 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011116 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011117 return n;
11118}
11119#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011120
11121/*
11122 * Return the current cursor column. This is the actual position on the
11123 * screen. First column is 0.
11124 */
11125 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011126screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011127{
11128 return screen_cur_col;
11129}
11130
11131/*
11132 * Return the current cursor row. This is the actual position on the screen.
11133 * First row is 0.
11134 */
11135 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011136screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011137{
11138 return screen_cur_row;
11139}