blob: cac5a3a1f8685fa5a026c9628efec56f37702a3d [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
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200570 /* May need to update w_lines[]. */
571 if (curwin->w_lines_valid == 0 && type < NOT_VALID
572#ifdef FEAT_TERMINAL
573 && !term_do_update_window(curwin)
574#endif
575 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 type = NOT_VALID;
577
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000578 /* Postpone the redrawing when it's not needed and when being called
579 * recursively. */
580 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 {
582 redraw_later(type); /* remember type for next time */
583 must_redraw = type;
584 if (type > INVERTED_ALL)
585 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
588
589 updating_screen = TRUE;
590#ifdef FEAT_SYN_HL
591 ++display_tick; /* let syntax code know we're in a next round of
592 * display updating */
593#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200594 if (no_update)
595 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596
597 /*
598 * if the screen was scrolled up when displaying a message, scroll it down
599 */
600 if (msg_scrolled)
601 {
602 clear_cmdline = TRUE;
603 if (msg_scrolled > Rows - 5) /* clearing is faster */
604 type = CLEAR;
605 else if (type != CLEAR)
606 {
607 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200608 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
609 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 type = CLEAR;
611 FOR_ALL_WINDOWS(wp)
612 {
613 if (W_WINROW(wp) < msg_scrolled)
614 {
615 if (W_WINROW(wp) + wp->w_height > msg_scrolled
616 && wp->w_redr_type < REDRAW_TOP
617 && wp->w_lines_valid > 0
618 && wp->w_topline == wp->w_lines[0].wl_lnum)
619 {
620 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
621 wp->w_redr_type = REDRAW_TOP;
622 }
623 else
624 {
625 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200626 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
627 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 }
630 }
631 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200632 if (!no_update)
633 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000634 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
636 msg_scrolled = 0;
637 need_wait_return = FALSE;
638 }
639
640 /* reset cmdline_row now (may have been changed temporarily) */
641 compute_cmdrow();
642
643 /* Check for changed highlighting */
644 if (need_highlight_changed)
645 highlight_changed();
646
647 if (type == CLEAR) /* first clear screen */
648 {
649 screenclear(); /* will reset clear_cmdline */
650 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200651 /* must_redraw may be set indirectly, avoid another redraw later */
652 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654
655 if (clear_cmdline) /* going to clear cmdline (done below) */
656 check_for_delay(FALSE);
657
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000658#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200659 /* Force redraw when width of 'number' or 'relativenumber' column
660 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000661 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200662 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
663 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000664 curwin->w_redr_type = NOT_VALID;
665#endif
666
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 /*
668 * Only start redrawing if there is really something to do.
669 */
670 if (type == INVERTED)
671 update_curswant();
672 if (curwin->w_redr_type < type
673 && !((type == VALID
674 && curwin->w_lines[0].wl_valid
675#ifdef FEAT_DIFF
676 && curwin->w_topfill == curwin->w_old_topfill
677 && curwin->w_botfill == curwin->w_old_botfill
678#endif
679 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000681 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
683 && curwin->w_old_visual_mode == VIsual_mode
684 && (curwin->w_valid & VALID_VIRTCOL)
685 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 ))
687 curwin->w_redr_type = type;
688
Bram Moolenaar5a305422006-04-28 22:38:25 +0000689 /* Redraw the tab pages line if needed. */
690 if (redraw_tabline || type >= NOT_VALID)
691 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693#ifdef FEAT_SYN_HL
694 /*
695 * Correct stored syntax highlighting info for changes in each displayed
696 * buffer. Each buffer must only be done once.
697 */
698 FOR_ALL_WINDOWS(wp)
699 {
700 if (wp->w_buffer->b_mod_set)
701 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 win_T *wwp;
703
704 /* Check if we already did this buffer. */
705 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
706 if (wwp->w_buffer == wp->w_buffer)
707 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200708 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 syn_stack_apply_changes(wp->w_buffer);
710 }
711 }
712#endif
713
714 /*
715 * Go from top to bottom through the windows, redrawing the ones that need
716 * it.
717 */
718#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
719 did_one = FALSE;
720#endif
721#ifdef FEAT_SEARCH_EXTRA
722 search_hl.rm.regprog = NULL;
723#endif
724 FOR_ALL_WINDOWS(wp)
725 {
726 if (wp->w_redr_type != 0)
727 {
728 cursor_off();
729#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
730 if (!did_one)
731 {
732 did_one = TRUE;
733# ifdef FEAT_SEARCH_EXTRA
734 start_search_hl();
735# endif
736# ifdef FEAT_CLIPBOARD
737 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200738 if (clip_star.available && clip_isautosel_star())
739 clip_update_selection(&clip_star);
740 if (clip_plus.available && clip_isautosel_plus())
741 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742# endif
743#ifdef FEAT_GUI
744 /* Remove the cursor before starting to do anything, because
745 * scrolling may make it difficult to redraw the text under
746 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200747 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200748 {
749 gui_cursor_col = gui.cursor_col;
750 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200752 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754#endif
755 }
756#endif
757 win_update(wp);
758 }
759
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 /* redraw status line after the window to minimize cursor movement */
761 if (wp->w_redr_status)
762 {
763 cursor_off();
764 win_redr_status(wp);
765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 }
767#if defined(FEAT_SEARCH_EXTRA)
768 end_search_hl();
769#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100770#ifdef FEAT_INS_EXPAND
771 /* May need to redraw the popup menu. */
772 if (pum_visible())
773 pum_redraw();
774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 /* Reset b_mod_set flags. Going through all windows is probably faster
777 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200778 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
781 updating_screen = FALSE;
782#ifdef FEAT_GUI
783 gui_may_resize_shell();
784#endif
785
786 /* Clear or redraw the command line. Done last, because scrolling may
787 * mess up the command line. */
788 if (clear_cmdline || redraw_cmdline)
789 showmode();
790
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200791 if (no_update)
792 --no_win_do_lines_ins;
793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200795 if (!did_intro)
796 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 did_intro = TRUE;
798
799#ifdef FEAT_GUI
800 /* Redraw the cursor and update the scrollbars when all screen updating is
801 * done. */
802 if (gui.in_use)
803 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200804 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200805 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100806 mch_disable_flush();
807 out_flush(); /* required before updating the cursor */
808 mch_enable_flush();
809
Bram Moolenaar144445d2016-07-08 21:41:54 +0200810 /* Put the GUI position where the cursor was, gui_update_cursor()
811 * uses that. */
812 gui.col = gui_cursor_col;
813 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200814# ifdef FEAT_MBYTE
815 gui.col = mb_fix_col(gui.col, gui.row);
816# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100818 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200819 screen_cur_col = gui.col;
820 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200821 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100822 else
823 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 gui_update_scrollbars(FALSE);
825 }
826#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200827 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828}
829
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100830#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
831/*
832 * Prepare for updating one or more windows.
833 * Caller must check for "updating_screen" already set to avoid recursiveness.
834 */
835 static void
836update_prepare(void)
837{
838 cursor_off();
839 updating_screen = TRUE;
840#ifdef FEAT_GUI
841 /* Remove the cursor before starting to do anything, because scrolling may
842 * make it difficult to redraw the text under it. */
843 if (gui.in_use)
844 gui_undraw_cursor();
845#endif
846#ifdef FEAT_SEARCH_EXTRA
847 start_search_hl();
848#endif
849}
850
851/*
852 * Finish updating one or more windows.
853 */
854 static void
855update_finish(void)
856{
857 if (redraw_cmdline)
858 showmode();
859
860# ifdef FEAT_SEARCH_EXTRA
861 end_search_hl();
862# endif
863
864 updating_screen = FALSE;
865
866# ifdef FEAT_GUI
867 gui_may_resize_shell();
868
869 /* Redraw the cursor and update the scrollbars when all screen updating is
870 * done. */
871 if (gui.in_use)
872 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100873 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100874 gui_update_scrollbars(FALSE);
875 }
876# endif
877}
878#endif
879
Bram Moolenaar860cae12010-06-05 23:22:07 +0200880#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200881/*
882 * Return TRUE if the cursor line in window "wp" may be concealed, according
883 * to the 'concealcursor' option.
884 */
885 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100886conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200887{
888 int c;
889
890 if (*wp->w_p_cocu == NUL)
891 return FALSE;
892 if (get_real_state() & VISUAL)
893 c = 'v';
894 else if (State & INSERT)
895 c = 'i';
896 else if (State & NORMAL)
897 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200898 else if (State & CMDLINE)
899 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200900 else
901 return FALSE;
902 return vim_strchr(wp->w_p_cocu, c) != NULL;
903}
904
905/*
906 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
907 */
908 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200909conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200910{
911 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
912 {
913 need_cursor_line_redraw = TRUE;
914 /* Need to recompute cursor column, e.g., when starting Visual mode
915 * without concealing. */
916 curs_columns(TRUE);
917 }
918}
919
Bram Moolenaar860cae12010-06-05 23:22:07 +0200920 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100921update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200922{
923 int row;
924 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200925#ifdef SYN_TIME_LIMIT
926 proftime_T syntax_tm;
927#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200928
Bram Moolenaar908be432016-05-24 10:51:30 +0200929 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100930 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200931 return;
932
Bram Moolenaar860cae12010-06-05 23:22:07 +0200933 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200934 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200935 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200936#ifdef SYN_TIME_LIMIT
937 /* Set the time limit to 'redrawtime'. */
938 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200939 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200940#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100941 update_prepare();
942
Bram Moolenaar860cae12010-06-05 23:22:07 +0200943 row = 0;
944 for (j = 0; j < wp->w_lines_valid; ++j)
945 {
946 if (lnum == wp->w_lines[j].wl_lnum)
947 {
948 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200949# ifdef FEAT_SEARCH_EXTRA
950 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200951 start_search_hl();
952 prepare_search_hl(wp, lnum);
953# endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200954 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200955# if defined(FEAT_SEARCH_EXTRA)
956 end_search_hl();
957# endif
958 break;
959 }
960 row += wp->w_lines[j].wl_size;
961 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100962
963 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200964
965#ifdef SYN_TIME_LIMIT
966 syn_set_timeout(NULL);
967#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200968 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200969 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970}
971#endif
972
973#if defined(FEAT_SIGNS) || defined(PROTO)
974 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100975update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977 win_T *wp;
978 int doit = FALSE;
979
980# ifdef FEAT_FOLDING
981 win_foldinfo.fi_level = 0;
982# endif
983
984 /* update/delete a specific mark */
985 FOR_ALL_WINDOWS(wp)
986 {
987 if (buf != NULL && lnum > 0)
988 {
989 if (wp->w_buffer == buf && lnum >= wp->w_topline
990 && lnum < wp->w_botline)
991 {
992 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
993 wp->w_redraw_top = lnum;
994 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
995 wp->w_redraw_bot = lnum;
996 redraw_win_later(wp, VALID);
997 }
998 }
999 else
1000 redraw_win_later(wp, VALID);
1001 if (wp->w_redr_type != 0)
1002 doit = TRUE;
1003 }
1004
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001005 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001006 * happening (recursive call), messages on the screen or still starting up.
1007 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001008 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001009 || State == ASKMORE || State == HITRETURN
1010 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001011#ifdef FEAT_GUI
1012 || gui.starting
1013#endif
1014 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 return;
1016
1017 /* update all windows that need updating */
1018 update_prepare();
1019
Bram Moolenaar29323592016-07-24 22:04:11 +02001020 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {
1022 if (wp->w_redr_type != 0)
1023 win_update(wp);
1024 if (wp->w_redr_status)
1025 win_redr_status(wp);
1026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027
1028 update_finish();
1029}
1030#endif
1031
1032
1033#if defined(FEAT_GUI) || defined(PROTO)
1034/*
1035 * Update a single window, its status line and maybe the command line msg.
1036 * Used for the GUI scrollbar.
1037 */
1038 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001039updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001041 /* return if already busy updating */
1042 if (updating_screen)
1043 return;
1044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 update_prepare();
1046
1047#ifdef FEAT_CLIPBOARD
1048 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001049 if (clip_star.available && clip_isautosel_star())
1050 clip_update_selection(&clip_star);
1051 if (clip_plus.available && clip_isautosel_plus())
1052 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001056
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001057 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001058 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001059 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001060
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if (wp->w_redr_status
1062# ifdef FEAT_CMDL_INFO
1063 || p_ru
1064# endif
1065# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001066 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067# endif
1068 )
1069 win_redr_status(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070
1071 update_finish();
1072}
1073#endif
1074
1075/*
1076 * Update a single window.
1077 *
1078 * This may cause the windows below it also to be redrawn (when clearing the
1079 * screen or scrolling lines).
1080 *
1081 * How the window is redrawn depends on wp->w_redr_type. Each type also
1082 * implies the one below it.
1083 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001084 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1086 * INVERTED redraw the changed part of the Visual area
1087 * INVERTED_ALL redraw the whole Visual area
1088 * VALID 1. scroll up/down to adjust for a changed w_topline
1089 * 2. update lines at the top when scrolled down
1090 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001091 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 * b_mod_top and b_mod_bot.
1093 * - if wp->w_redraw_top non-zero, redraw lines between
1094 * wp->w_redraw_top and wp->w_redr_bot.
1095 * - continue redrawing when syntax status is invalid.
1096 * 4. if scrolled up, update lines at the bottom.
1097 * This results in three areas that may need updating:
1098 * top: from first row to top_end (when scrolled down)
1099 * mid: from mid_start to mid_end (update inversion or changed text)
1100 * bot: from bot_start to last row (when scrolled up)
1101 */
1102 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001103win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104{
1105 buf_T *buf = wp->w_buffer;
1106 int type;
1107 int top_end = 0; /* Below last row of the top area that needs
1108 updating. 0 when no top area updating. */
1109 int mid_start = 999;/* first row of the mid area that needs
1110 updating. 999 when no mid area updating. */
1111 int mid_end = 0; /* Below last row of the mid area that needs
1112 updating. 0 when no mid area updating. */
1113 int bot_start = 999;/* first row of the bot area that needs
1114 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 int scrolled_down = FALSE; /* TRUE when scrolled down when
1116 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001118 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 int top_to_mod = FALSE; /* redraw above mod_top */
1120#endif
1121
1122 int row; /* current window row to display */
1123 linenr_T lnum; /* current buffer lnum to display */
1124 int idx; /* current index in w_lines[] */
1125 int srow; /* starting row of the current line */
1126
1127 int eof = FALSE; /* if TRUE, we hit the end of the file */
1128 int didline = FALSE; /* if TRUE, we finished the last line */
1129 int i;
1130 long j;
1131 static int recursive = FALSE; /* being called recursively */
1132 int old_botline = wp->w_botline;
1133#ifdef FEAT_FOLDING
1134 long fold_count;
1135#endif
1136#ifdef FEAT_SYN_HL
1137 /* remember what happened to the previous line, to know if
1138 * check_visual_highlight() can be used */
1139#define DID_NONE 1 /* didn't update a line */
1140#define DID_LINE 2 /* updated a normal line */
1141#define DID_FOLD 3 /* updated a folded line */
1142 int did_update = DID_NONE;
1143 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1144#endif
1145 linenr_T mod_top = 0;
1146 linenr_T mod_bot = 0;
1147#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1148 int save_got_int;
1149#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001150#ifdef SYN_TIME_LIMIT
1151 proftime_T syntax_tm;
1152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153
1154 type = wp->w_redr_type;
1155
1156 if (type == NOT_VALID)
1157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 wp->w_lines_valid = 0;
1160 }
1161
1162 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001163 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {
1165 wp->w_redr_type = 0;
1166 return;
1167 }
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 /* Window is zero-width: Only need to draw the separator. */
1170 if (wp->w_width == 0)
1171 {
1172 /* draw the vertical separator right of this window */
1173 draw_vsep_win(wp, 0);
1174 wp->w_redr_type = 0;
1175 return;
1176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001178#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001179 // If this window contains a terminal, redraw works completely differently.
1180 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001181 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001182 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001183# ifdef FEAT_MENU
1184 /* Draw the window toolbar, if there is one. */
1185 if (winbar_height(wp) > 0)
1186 redraw_win_toolbar(wp);
1187# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001188 wp->w_redr_type = 0;
1189 return;
1190 }
1191#endif
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001194 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#endif
1196
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001197#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001198 /* Force redraw when width of 'number' or 'relativenumber' column
1199 * changes. */
1200 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001201 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001202 {
1203 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001204 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001205 }
1206 else
1207#endif
1208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1210 {
1211 /*
1212 * When there are both inserted/deleted lines and specific lines to be
1213 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1214 * everything (only happens when redrawing is off for while).
1215 */
1216 type = NOT_VALID;
1217 }
1218 else
1219 {
1220 /*
1221 * Set mod_top to the first line that needs displaying because of
1222 * changes. Set mod_bot to the first line after the changes.
1223 */
1224 mod_top = wp->w_redraw_top;
1225 if (wp->w_redraw_bot != 0)
1226 mod_bot = wp->w_redraw_bot + 1;
1227 else
1228 mod_bot = 0;
1229 wp->w_redraw_top = 0; /* reset for next time */
1230 wp->w_redraw_bot = 0;
1231 if (buf->b_mod_set)
1232 {
1233 if (mod_top == 0 || mod_top > buf->b_mod_top)
1234 {
1235 mod_top = buf->b_mod_top;
1236#ifdef FEAT_SYN_HL
1237 /* Need to redraw lines above the change that may be included
1238 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001239 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001241 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (mod_top < 1)
1243 mod_top = 1;
1244 }
1245#endif
1246 }
1247 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1248 mod_bot = buf->b_mod_bot;
1249
1250#ifdef FEAT_SEARCH_EXTRA
1251 /* When 'hlsearch' is on and using a multi-line search pattern, a
1252 * change in one line may make the Search highlighting in a
1253 * previous line invalid. Simple solution: redraw all visible
1254 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001255 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001257 if (search_hl.rm.regprog != NULL
1258 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001260 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001261 {
1262 cur = wp->w_match_head;
1263 while (cur != NULL)
1264 {
1265 if (cur->match.regprog != NULL
1266 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001267 {
1268 top_to_mod = TRUE;
1269 break;
1270 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001271 cur = cur->next;
1272 }
1273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274#endif
1275 }
1276#ifdef FEAT_FOLDING
1277 if (mod_top != 0 && hasAnyFolding(wp))
1278 {
1279 linenr_T lnumt, lnumb;
1280
1281 /*
1282 * A change in a line can cause lines above it to become folded or
1283 * unfolded. Find the top most buffer line that may be affected.
1284 * If the line was previously folded and displayed, get the first
1285 * line of that fold. If the line is folded now, get the first
1286 * folded line. Use the minimum of these two.
1287 */
1288
1289 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1290 * the line below it. If there is no valid entry, use w_topline.
1291 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1292 * to this line. If there is no valid entry, use MAXLNUM. */
1293 lnumt = wp->w_topline;
1294 lnumb = MAXLNUM;
1295 for (i = 0; i < wp->w_lines_valid; ++i)
1296 if (wp->w_lines[i].wl_valid)
1297 {
1298 if (wp->w_lines[i].wl_lastlnum < mod_top)
1299 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1300 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1301 {
1302 lnumb = wp->w_lines[i].wl_lnum;
1303 /* When there is a fold column it might need updating
1304 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001305 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 ++lnumb;
1307 }
1308 }
1309
1310 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1311 if (mod_top > lnumt)
1312 mod_top = lnumt;
1313
1314 /* Now do the same for the bottom line (one above mod_bot). */
1315 --mod_bot;
1316 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1317 ++mod_bot;
1318 if (mod_bot < lnumb)
1319 mod_bot = lnumb;
1320 }
1321#endif
1322
1323 /* When a change starts above w_topline and the end is below
1324 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001325 * If the end of the change is above w_topline: do like no change was
1326 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (mod_top != 0 && mod_top < wp->w_topline)
1328 {
1329 if (mod_bot > wp->w_topline)
1330 mod_top = wp->w_topline;
1331#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001332 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 top_end = 1;
1334#endif
1335 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001336
1337 /* When line numbers are displayed need to redraw all lines below
1338 * inserted/deleted lines. */
1339 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1340 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 }
1342
1343 /*
1344 * When only displaying the lines at the top, set top_end. Used when
1345 * window has scrolled down for msg_scrolled.
1346 */
1347 if (type == REDRAW_TOP)
1348 {
1349 j = 0;
1350 for (i = 0; i < wp->w_lines_valid; ++i)
1351 {
1352 j += wp->w_lines[i].wl_size;
1353 if (j >= wp->w_upd_rows)
1354 {
1355 top_end = j;
1356 break;
1357 }
1358 }
1359 if (top_end == 0)
1360 /* not found (cannot happen?): redraw everything */
1361 type = NOT_VALID;
1362 else
1363 /* top area defined, the rest is VALID */
1364 type = VALID;
1365 }
1366
Bram Moolenaar367329b2007-08-30 11:53:22 +00001367 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001368 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1369 * non-zero and thus not FALSE) will indicate that screenclear() was not
1370 * called. */
1371 if (screen_cleared)
1372 screen_cleared = MAYBE;
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 /*
1375 * If there are no changes on the screen that require a complete redraw,
1376 * handle three cases:
1377 * 1: we are off the top of the screen by a few lines: scroll down
1378 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1379 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1380 * w_lines[] that needs updating.
1381 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001382 if ((type == VALID || type == SOME_VALID
1383 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384#ifdef FEAT_DIFF
1385 && !wp->w_botfill && !wp->w_old_botfill
1386#endif
1387 )
1388 {
1389 if (mod_top != 0 && wp->w_topline == mod_top)
1390 {
1391 /*
1392 * w_topline is the first changed line, the scrolling will be done
1393 * further down.
1394 */
1395 }
1396 else if (wp->w_lines[0].wl_valid
1397 && (wp->w_topline < wp->w_lines[0].wl_lnum
1398#ifdef FEAT_DIFF
1399 || (wp->w_topline == wp->w_lines[0].wl_lnum
1400 && wp->w_topfill > wp->w_old_topfill)
1401#endif
1402 ))
1403 {
1404 /*
1405 * New topline is above old topline: May scroll down.
1406 */
1407#ifdef FEAT_FOLDING
1408 if (hasAnyFolding(wp))
1409 {
1410 linenr_T ln;
1411
1412 /* count the number of lines we are off, counting a sequence
1413 * of folded lines as one */
1414 j = 0;
1415 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1416 {
1417 ++j;
1418 if (j >= wp->w_height - 2)
1419 break;
1420 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1421 }
1422 }
1423 else
1424#endif
1425 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1426 if (j < wp->w_height - 2) /* not too far off */
1427 {
1428 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1429#ifdef FEAT_DIFF
1430 /* insert extra lines for previously invisible filler lines */
1431 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1432 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1433 - wp->w_old_topfill;
1434#endif
1435 if (i < wp->w_height - 2) /* less than a screen off */
1436 {
1437 /*
1438 * Try to insert the correct number of lines.
1439 * If not the last window, delete the lines at the bottom.
1440 * win_ins_lines may fail when the terminal can't do it.
1441 */
1442 if (i > 0)
1443 check_for_delay(FALSE);
1444 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1445 {
1446 if (wp->w_lines_valid != 0)
1447 {
1448 /* Need to update rows that are new, stop at the
1449 * first one that scrolled down. */
1450 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
1453 /* Move the entries that were scrolled, disable
1454 * the entries for the lines to be redrawn. */
1455 if ((wp->w_lines_valid += j) > wp->w_height)
1456 wp->w_lines_valid = wp->w_height;
1457 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1458 wp->w_lines[idx] = wp->w_lines[idx - j];
1459 while (idx >= 0)
1460 wp->w_lines[idx--].wl_valid = FALSE;
1461 }
1462 }
1463 else
1464 mid_start = 0; /* redraw all lines */
1465 }
1466 else
1467 mid_start = 0; /* redraw all lines */
1468 }
1469 else
1470 mid_start = 0; /* redraw all lines */
1471 }
1472 else
1473 {
1474 /*
1475 * New topline is at or below old topline: May scroll up.
1476 * When topline didn't change, find first entry in w_lines[] that
1477 * needs updating.
1478 */
1479
1480 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1481 j = -1;
1482 row = 0;
1483 for (i = 0; i < wp->w_lines_valid; i++)
1484 {
1485 if (wp->w_lines[i].wl_valid
1486 && wp->w_lines[i].wl_lnum == wp->w_topline)
1487 {
1488 j = i;
1489 break;
1490 }
1491 row += wp->w_lines[i].wl_size;
1492 }
1493 if (j == -1)
1494 {
1495 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1496 * lines */
1497 mid_start = 0;
1498 }
1499 else
1500 {
1501 /*
1502 * Try to delete the correct number of lines.
1503 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1504 */
1505#ifdef FEAT_DIFF
1506 /* If the topline didn't change, delete old filler lines,
1507 * otherwise delete filler lines of the new topline... */
1508 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1509 row += wp->w_old_topfill;
1510 else
1511 row += diff_check_fill(wp, wp->w_topline);
1512 /* ... but don't delete new filler lines. */
1513 row -= wp->w_topfill;
1514#endif
1515 if (row > 0)
1516 {
1517 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001518 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1519 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 bot_start = wp->w_height - row;
1521 else
1522 mid_start = 0; /* redraw all lines */
1523 }
1524 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1525 {
1526 /*
1527 * Skip the lines (below the deleted lines) that are still
1528 * valid and don't need redrawing. Copy their info
1529 * upwards, to compensate for the deleted lines. Set
1530 * bot_start to the first row that needs redrawing.
1531 */
1532 bot_start = 0;
1533 idx = 0;
1534 for (;;)
1535 {
1536 wp->w_lines[idx] = wp->w_lines[j];
1537 /* stop at line that didn't fit, unless it is still
1538 * valid (no lines deleted) */
1539 if (row > 0 && bot_start + row
1540 + (int)wp->w_lines[j].wl_size > wp->w_height)
1541 {
1542 wp->w_lines_valid = idx + 1;
1543 break;
1544 }
1545 bot_start += wp->w_lines[idx++].wl_size;
1546
1547 /* stop at the last valid entry in w_lines[].wl_size */
1548 if (++j >= wp->w_lines_valid)
1549 {
1550 wp->w_lines_valid = idx;
1551 break;
1552 }
1553 }
1554#ifdef FEAT_DIFF
1555 /* Correct the first entry for filler lines at the top
1556 * when it won't get updated below. */
1557 if (wp->w_p_diff && bot_start > 0)
1558 wp->w_lines[0].wl_size =
1559 plines_win_nofill(wp, wp->w_topline, TRUE)
1560 + wp->w_topfill;
1561#endif
1562 }
1563 }
1564 }
1565
1566 /* When starting redraw in the first line, redraw all lines. When
1567 * there is only one window it's probably faster to clear the screen
1568 * first. */
1569 if (mid_start == 0)
1570 {
1571 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001572 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001573 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001574 /* Clear the screen when it was not done by win_del_lines() or
1575 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1576 * then. */
1577 if (screen_cleared != TRUE)
1578 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001579 /* The screen was cleared, redraw the tab pages line. */
1580 if (redraw_tabline)
1581 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001584
1585 /* When win_del_lines() or win_ins_lines() caused the screen to be
1586 * cleared (only happens for the first window) or when screenclear()
1587 * was called directly above, "must_redraw" will have been set to
1588 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1589 if (screen_cleared == TRUE)
1590 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592 else
1593 {
1594 /* Not VALID or INVERTED: redraw all lines. */
1595 mid_start = 0;
1596 mid_end = wp->w_height;
1597 }
1598
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001599 if (type == SOME_VALID)
1600 {
1601 /* SOME_VALID: redraw all lines. */
1602 mid_start = 0;
1603 mid_end = wp->w_height;
1604 type = NOT_VALID;
1605 }
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 /* check if we are updating or removing the inverted part */
1608 if ((VIsual_active && buf == curwin->w_buffer)
1609 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1610 {
1611 linenr_T from, to;
1612
1613 if (VIsual_active)
1614 {
1615 if (VIsual_active
1616 && (VIsual_mode != wp->w_old_visual_mode
1617 || type == INVERTED_ALL))
1618 {
1619 /*
1620 * If the type of Visual selection changed, redraw the whole
1621 * selection. Also when the ownership of the X selection is
1622 * gained or lost.
1623 */
1624 if (curwin->w_cursor.lnum < VIsual.lnum)
1625 {
1626 from = curwin->w_cursor.lnum;
1627 to = VIsual.lnum;
1628 }
1629 else
1630 {
1631 from = VIsual.lnum;
1632 to = curwin->w_cursor.lnum;
1633 }
1634 /* redraw more when the cursor moved as well */
1635 if (wp->w_old_cursor_lnum < from)
1636 from = wp->w_old_cursor_lnum;
1637 if (wp->w_old_cursor_lnum > to)
1638 to = wp->w_old_cursor_lnum;
1639 if (wp->w_old_visual_lnum < from)
1640 from = wp->w_old_visual_lnum;
1641 if (wp->w_old_visual_lnum > to)
1642 to = wp->w_old_visual_lnum;
1643 }
1644 else
1645 {
1646 /*
1647 * Find the line numbers that need to be updated: The lines
1648 * between the old cursor position and the current cursor
1649 * position. Also check if the Visual position changed.
1650 */
1651 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1652 {
1653 from = curwin->w_cursor.lnum;
1654 to = wp->w_old_cursor_lnum;
1655 }
1656 else
1657 {
1658 from = wp->w_old_cursor_lnum;
1659 to = curwin->w_cursor.lnum;
1660 if (from == 0) /* Visual mode just started */
1661 from = to;
1662 }
1663
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001664 if (VIsual.lnum != wp->w_old_visual_lnum
1665 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 {
1667 if (wp->w_old_visual_lnum < from
1668 && wp->w_old_visual_lnum != 0)
1669 from = wp->w_old_visual_lnum;
1670 if (wp->w_old_visual_lnum > to)
1671 to = wp->w_old_visual_lnum;
1672 if (VIsual.lnum < from)
1673 from = VIsual.lnum;
1674 if (VIsual.lnum > to)
1675 to = VIsual.lnum;
1676 }
1677 }
1678
1679 /*
1680 * If in block mode and changed column or curwin->w_curswant:
1681 * update all lines.
1682 * First compute the actual start and end column.
1683 */
1684 if (VIsual_mode == Ctrl_V)
1685 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001686 colnr_T fromc, toc;
1687#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1688 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689
Bram Moolenaar404406a2014-10-09 13:24:43 +02001690 if (curwin->w_p_lbr)
1691 ve_flags = VE_ALL;
1692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001694#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1695 ve_flags = save_ve_flags;
1696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 ++toc;
1698 if (curwin->w_curswant == MAXCOL)
1699 toc = MAXCOL;
1700
1701 if (fromc != wp->w_old_cursor_fcol
1702 || toc != wp->w_old_cursor_lcol)
1703 {
1704 if (from > VIsual.lnum)
1705 from = VIsual.lnum;
1706 if (to < VIsual.lnum)
1707 to = VIsual.lnum;
1708 }
1709 wp->w_old_cursor_fcol = fromc;
1710 wp->w_old_cursor_lcol = toc;
1711 }
1712 }
1713 else
1714 {
1715 /* Use the line numbers of the old Visual area. */
1716 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1717 {
1718 from = wp->w_old_cursor_lnum;
1719 to = wp->w_old_visual_lnum;
1720 }
1721 else
1722 {
1723 from = wp->w_old_visual_lnum;
1724 to = wp->w_old_cursor_lnum;
1725 }
1726 }
1727
1728 /*
1729 * There is no need to update lines above the top of the window.
1730 */
1731 if (from < wp->w_topline)
1732 from = wp->w_topline;
1733
1734 /*
1735 * If we know the value of w_botline, use it to restrict the update to
1736 * the lines that are visible in the window.
1737 */
1738 if (wp->w_valid & VALID_BOTLINE)
1739 {
1740 if (from >= wp->w_botline)
1741 from = wp->w_botline - 1;
1742 if (to >= wp->w_botline)
1743 to = wp->w_botline - 1;
1744 }
1745
1746 /*
1747 * Find the minimal part to be updated.
1748 * Watch out for scrolling that made entries in w_lines[] invalid.
1749 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1750 * top_end; need to redraw from top_end to the "to" line.
1751 * A middle mouse click with a Visual selection may change the text
1752 * above the Visual area and reset wl_valid, do count these for
1753 * mid_end (in srow).
1754 */
1755 if (mid_start > 0)
1756 {
1757 lnum = wp->w_topline;
1758 idx = 0;
1759 srow = 0;
1760 if (scrolled_down)
1761 mid_start = top_end;
1762 else
1763 mid_start = 0;
1764 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1765 {
1766 if (wp->w_lines[idx].wl_valid)
1767 mid_start += wp->w_lines[idx].wl_size;
1768 else if (!scrolled_down)
1769 srow += wp->w_lines[idx].wl_size;
1770 ++idx;
1771# ifdef FEAT_FOLDING
1772 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1773 lnum = wp->w_lines[idx].wl_lnum;
1774 else
1775# endif
1776 ++lnum;
1777 }
1778 srow += mid_start;
1779 mid_end = wp->w_height;
1780 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1781 {
1782 if (wp->w_lines[idx].wl_valid
1783 && wp->w_lines[idx].wl_lnum >= to + 1)
1784 {
1785 /* Only update until first row of this line */
1786 mid_end = srow;
1787 break;
1788 }
1789 srow += wp->w_lines[idx].wl_size;
1790 }
1791 }
1792 }
1793
1794 if (VIsual_active && buf == curwin->w_buffer)
1795 {
1796 wp->w_old_visual_mode = VIsual_mode;
1797 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1798 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001799 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 wp->w_old_curswant = curwin->w_curswant;
1801 }
1802 else
1803 {
1804 wp->w_old_visual_mode = 0;
1805 wp->w_old_cursor_lnum = 0;
1806 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001807 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
1810#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1811 /* reset got_int, otherwise regexp won't work */
1812 save_got_int = got_int;
1813 got_int = 0;
1814#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001815#ifdef SYN_TIME_LIMIT
1816 /* Set the time limit to 'redrawtime'. */
1817 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001818 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820#ifdef FEAT_FOLDING
1821 win_foldinfo.fi_level = 0;
1822#endif
1823
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001824#ifdef FEAT_MENU
1825 /*
1826 * Draw the window toolbar, if there is one.
1827 * TODO: only when needed.
1828 */
1829 if (winbar_height(wp) > 0)
1830 redraw_win_toolbar(wp);
1831#endif
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 /*
1834 * Update all the window rows.
1835 */
1836 idx = 0; /* first entry in w_lines[].wl_size */
1837 row = 0;
1838 srow = 0;
1839 lnum = wp->w_topline; /* first line shown in window */
1840 for (;;)
1841 {
1842 /* stop updating when reached the end of the window (check for _past_
1843 * the end of the window is at the end of the loop) */
1844 if (row == wp->w_height)
1845 {
1846 didline = TRUE;
1847 break;
1848 }
1849
1850 /* stop updating when hit the end of the file */
1851 if (lnum > buf->b_ml.ml_line_count)
1852 {
1853 eof = TRUE;
1854 break;
1855 }
1856
1857 /* Remember the starting row of the line that is going to be dealt
1858 * with. It is used further down when the line doesn't fit. */
1859 srow = row;
1860
1861 /*
1862 * Update a line when it is in an area that needs updating, when it
1863 * has changes or w_lines[idx] is invalid.
1864 * bot_start may be halfway a wrapped line after using
1865 * win_del_lines(), check if the current line includes it.
1866 * When syntax folding is being used, the saved syntax states will
1867 * already have been updated, we can't see where the syntax state is
1868 * the same again, just update until the end of the window.
1869 */
1870 if (row < top_end
1871 || (row >= mid_start && row < mid_end)
1872#ifdef FEAT_SEARCH_EXTRA
1873 || top_to_mod
1874#endif
1875 || idx >= wp->w_lines_valid
1876 || (row + wp->w_lines[idx].wl_size > bot_start)
1877 || (mod_top != 0
1878 && (lnum == mod_top
1879 || (lnum >= mod_top
1880 && (lnum < mod_bot
1881#ifdef FEAT_SYN_HL
1882 || did_update == DID_FOLD
1883 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001884 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 && (
1886# ifdef FEAT_FOLDING
1887 (foldmethodIsSyntax(wp)
1888 && hasAnyFolding(wp)) ||
1889# endif
1890 syntax_check_changed(lnum)))
1891#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001892#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001893 /* match in fixed position might need redraw
1894 * if lines were inserted or deleted */
1895 || (wp->w_match_head != NULL
1896 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 )))))
1899 {
1900#ifdef FEAT_SEARCH_EXTRA
1901 if (lnum == mod_top)
1902 top_to_mod = FALSE;
1903#endif
1904
1905 /*
1906 * When at start of changed lines: May scroll following lines
1907 * up or down to minimize redrawing.
1908 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001909 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 */
1911 if (lnum == mod_top
1912 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001913 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
1915 int old_rows = 0;
1916 int new_rows = 0;
1917 int xtra_rows;
1918 linenr_T l;
1919
1920 /* Count the old number of window rows, using w_lines[], which
1921 * should still contain the sizes for the lines as they are
1922 * currently displayed. */
1923 for (i = idx; i < wp->w_lines_valid; ++i)
1924 {
1925 /* Only valid lines have a meaningful wl_lnum. Invalid
1926 * lines are part of the changed area. */
1927 if (wp->w_lines[i].wl_valid
1928 && wp->w_lines[i].wl_lnum == mod_bot)
1929 break;
1930 old_rows += wp->w_lines[i].wl_size;
1931#ifdef FEAT_FOLDING
1932 if (wp->w_lines[i].wl_valid
1933 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1934 {
1935 /* Must have found the last valid entry above mod_bot.
1936 * Add following invalid entries. */
1937 ++i;
1938 while (i < wp->w_lines_valid
1939 && !wp->w_lines[i].wl_valid)
1940 old_rows += wp->w_lines[i++].wl_size;
1941 break;
1942 }
1943#endif
1944 }
1945
1946 if (i >= wp->w_lines_valid)
1947 {
1948 /* We can't find a valid line below the changed lines,
1949 * need to redraw until the end of the window.
1950 * Inserting/deleting lines has no use. */
1951 bot_start = 0;
1952 }
1953 else
1954 {
1955 /* Able to count old number of rows: Count new window
1956 * rows, and may insert/delete lines */
1957 j = idx;
1958 for (l = lnum; l < mod_bot; ++l)
1959 {
1960#ifdef FEAT_FOLDING
1961 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1962 ++new_rows;
1963 else
1964#endif
1965#ifdef FEAT_DIFF
1966 if (l == wp->w_topline)
1967 new_rows += plines_win_nofill(wp, l, TRUE)
1968 + wp->w_topfill;
1969 else
1970#endif
1971 new_rows += plines_win(wp, l, TRUE);
1972 ++j;
1973 if (new_rows > wp->w_height - row - 2)
1974 {
1975 /* it's getting too much, must redraw the rest */
1976 new_rows = 9999;
1977 break;
1978 }
1979 }
1980 xtra_rows = new_rows - old_rows;
1981 if (xtra_rows < 0)
1982 {
1983 /* May scroll text up. If there is not enough
1984 * remaining text or scrolling fails, must redraw the
1985 * rest. If scrolling works, must redraw the text
1986 * below the scrolled text. */
1987 if (row - xtra_rows >= wp->w_height - 2)
1988 mod_bot = MAXLNUM;
1989 else
1990 {
1991 check_for_delay(FALSE);
1992 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001993 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 mod_bot = MAXLNUM;
1995 else
1996 bot_start = wp->w_height + xtra_rows;
1997 }
1998 }
1999 else if (xtra_rows > 0)
2000 {
2001 /* May scroll text down. If there is not enough
2002 * remaining text of scrolling fails, must redraw the
2003 * rest. */
2004 if (row + xtra_rows >= wp->w_height - 2)
2005 mod_bot = MAXLNUM;
2006 else
2007 {
2008 check_for_delay(FALSE);
2009 if (win_ins_lines(wp, row + old_rows,
2010 xtra_rows, FALSE, FALSE) == FAIL)
2011 mod_bot = MAXLNUM;
2012 else if (top_end > row + old_rows)
2013 /* Scrolled the part at the top that requires
2014 * updating down. */
2015 top_end += xtra_rows;
2016 }
2017 }
2018
2019 /* When not updating the rest, may need to move w_lines[]
2020 * entries. */
2021 if (mod_bot != MAXLNUM && i != j)
2022 {
2023 if (j < i)
2024 {
2025 int x = row + new_rows;
2026
2027 /* move entries in w_lines[] upwards */
2028 for (;;)
2029 {
2030 /* stop at last valid entry in w_lines[] */
2031 if (i >= wp->w_lines_valid)
2032 {
2033 wp->w_lines_valid = j;
2034 break;
2035 }
2036 wp->w_lines[j] = wp->w_lines[i];
2037 /* stop at a line that won't fit */
2038 if (x + (int)wp->w_lines[j].wl_size
2039 > wp->w_height)
2040 {
2041 wp->w_lines_valid = j + 1;
2042 break;
2043 }
2044 x += wp->w_lines[j++].wl_size;
2045 ++i;
2046 }
2047 if (bot_start > x)
2048 bot_start = x;
2049 }
2050 else /* j > i */
2051 {
2052 /* move entries in w_lines[] downwards */
2053 j -= i;
2054 wp->w_lines_valid += j;
2055 if (wp->w_lines_valid > wp->w_height)
2056 wp->w_lines_valid = wp->w_height;
2057 for (i = wp->w_lines_valid; i - j >= idx; --i)
2058 wp->w_lines[i] = wp->w_lines[i - j];
2059
2060 /* The w_lines[] entries for inserted lines are
2061 * now invalid, but wl_size may be used above.
2062 * Reset to zero. */
2063 while (i >= idx)
2064 {
2065 wp->w_lines[i].wl_size = 0;
2066 wp->w_lines[i--].wl_valid = FALSE;
2067 }
2068 }
2069 }
2070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 }
2072
2073#ifdef FEAT_FOLDING
2074 /*
2075 * When lines are folded, display one line for all of them.
2076 * Otherwise, display normally (can be several display lines when
2077 * 'wrap' is on).
2078 */
2079 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2080 if (fold_count != 0)
2081 {
2082 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2083 ++row;
2084 --fold_count;
2085 wp->w_lines[idx].wl_folded = TRUE;
2086 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2087# ifdef FEAT_SYN_HL
2088 did_update = DID_FOLD;
2089# endif
2090 }
2091 else
2092#endif
2093 if (idx < wp->w_lines_valid
2094 && wp->w_lines[idx].wl_valid
2095 && wp->w_lines[idx].wl_lnum == lnum
2096 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002097 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 && srow + wp->w_lines[idx].wl_size > wp->w_height
2099#ifdef FEAT_DIFF
2100 && diff_check_fill(wp, lnum) == 0
2101#endif
2102 )
2103 {
2104 /* This line is not going to fit. Don't draw anything here,
2105 * will draw "@ " lines below. */
2106 row = wp->w_height + 1;
2107 }
2108 else
2109 {
2110#ifdef FEAT_SEARCH_EXTRA
2111 prepare_search_hl(wp, lnum);
2112#endif
2113#ifdef FEAT_SYN_HL
2114 /* Let the syntax stuff know we skipped a few lines. */
2115 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002116 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 syntax_end_parsing(syntax_last_parsed + 1);
2118#endif
2119
2120 /*
2121 * Display one line.
2122 */
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002123 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125#ifdef FEAT_FOLDING
2126 wp->w_lines[idx].wl_folded = FALSE;
2127 wp->w_lines[idx].wl_lastlnum = lnum;
2128#endif
2129#ifdef FEAT_SYN_HL
2130 did_update = DID_LINE;
2131 syntax_last_parsed = lnum;
2132#endif
2133 }
2134
2135 wp->w_lines[idx].wl_lnum = lnum;
2136 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002137
2138 /* Past end of the window or end of the screen. Note that after
2139 * resizing wp->w_height may be end up too big. That's a problem
2140 * elsewhere, but prevent a crash here. */
2141 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
2143 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002144 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2146 ++idx;
2147 break;
2148 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002149 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 wp->w_lines[idx].wl_size = row - srow;
2151 ++idx;
2152#ifdef FEAT_FOLDING
2153 lnum += fold_count + 1;
2154#else
2155 ++lnum;
2156#endif
2157 }
2158 else
2159 {
2160 /* This line does not need updating, advance to the next one */
2161 row += wp->w_lines[idx++].wl_size;
2162 if (row > wp->w_height) /* past end of screen */
2163 break;
2164#ifdef FEAT_FOLDING
2165 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2166#else
2167 ++lnum;
2168#endif
2169#ifdef FEAT_SYN_HL
2170 did_update = DID_NONE;
2171#endif
2172 }
2173
2174 if (lnum > buf->b_ml.ml_line_count)
2175 {
2176 eof = TRUE;
2177 break;
2178 }
2179 }
2180 /*
2181 * End of loop over all window lines.
2182 */
2183
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002184#ifdef FEAT_VTP
2185 /* Rewrite the character at the end of the screen line. */
2186 if (use_vtp())
2187 {
2188 int i;
2189
2190 for (i = 0; i < Rows; ++i)
2191# ifdef FEAT_MBYTE
2192 if (enc_utf8)
2193 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2194 LineOffset[i] + screen_Columns) > 1)
2195 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2196 else
2197 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2198 else
2199# endif
2200 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2201 }
2202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203
2204 if (idx > wp->w_lines_valid)
2205 wp->w_lines_valid = idx;
2206
2207#ifdef FEAT_SYN_HL
2208 /*
2209 * Let the syntax stuff know we stop parsing here.
2210 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002211 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 syntax_end_parsing(syntax_last_parsed + 1);
2213#endif
2214
2215 /*
2216 * If we didn't hit the end of the file, and we didn't finish the last
2217 * line we were working on, then the line didn't fit.
2218 */
2219 wp->w_empty_rows = 0;
2220#ifdef FEAT_DIFF
2221 wp->w_filler_rows = 0;
2222#endif
2223 if (!eof && !didline)
2224 {
2225 if (lnum == wp->w_topline)
2226 {
2227 /*
2228 * Single line that does not fit!
2229 * Don't overwrite it, it can be edited.
2230 */
2231 wp->w_botline = lnum + 1;
2232 }
2233#ifdef FEAT_DIFF
2234 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2235 {
2236 /* Window ends in filler lines. */
2237 wp->w_botline = lnum;
2238 wp->w_filler_rows = wp->w_height - srow;
2239 }
2240#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002241 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2242 {
2243 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2244
2245 /*
2246 * Last line isn't finished: Display "@@@" in the last screen line.
2247 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002248 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002249 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002250 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002251 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002252 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002253 set_empty_rows(wp, srow);
2254 wp->w_botline = lnum;
2255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2257 {
2258 /*
2259 * Last line isn't finished: Display "@@@" at the end.
2260 */
2261 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2262 W_WINROW(wp) + wp->w_height,
2263 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002264 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 set_empty_rows(wp, srow);
2266 wp->w_botline = lnum;
2267 }
2268 else
2269 {
2270 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2271 wp->w_botline = lnum;
2272 }
2273 }
2274 else
2275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 if (eof) /* we hit the end of the file */
2278 {
2279 wp->w_botline = buf->b_ml.ml_line_count + 1;
2280#ifdef FEAT_DIFF
2281 j = diff_check_fill(wp, wp->w_botline);
2282 if (j > 0 && !wp->w_botfill)
2283 {
2284 /*
2285 * Display filler lines at the end of the file
2286 */
2287 if (char2cells(fill_diff) > 1)
2288 i = '-';
2289 else
2290 i = fill_diff;
2291 if (row + j > wp->w_height)
2292 j = wp->w_height - row;
2293 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2294 row += j;
2295 }
2296#endif
2297 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002298 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 wp->w_botline = lnum;
2300
2301 /* make sure the rest of the screen is blank */
2302 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002303 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002306#ifdef SYN_TIME_LIMIT
2307 syn_set_timeout(NULL);
2308#endif
2309
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 /* Reset the type of redrawing required, the window has been updated. */
2311 wp->w_redr_type = 0;
2312#ifdef FEAT_DIFF
2313 wp->w_old_topfill = wp->w_topfill;
2314 wp->w_old_botfill = wp->w_botfill;
2315#endif
2316
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002317 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
2319 /*
2320 * There is a trick with w_botline. If we invalidate it on each
2321 * change that might modify it, this will cause a lot of expensive
2322 * calls to plines() in update_topline() each time. Therefore the
2323 * value of w_botline is often approximated, and this value is used to
2324 * compute the value of w_topline. If the value of w_botline was
2325 * wrong, check that the value of w_topline is correct (cursor is on
2326 * the visible part of the text). If it's not, we need to redraw
2327 * again. Mostly this just means scrolling up a few lines, so it
2328 * doesn't look too bad. Only do this for the current window (where
2329 * changes are relevant).
2330 */
2331 wp->w_valid |= VALID_BOTLINE;
2332 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2333 {
2334 recursive = TRUE;
2335 curwin->w_valid &= ~VALID_TOPLINE;
2336 update_topline(); /* may invalidate w_botline again */
2337 if (must_redraw != 0)
2338 {
2339 /* Don't update for changes in buffer again. */
2340 i = curbuf->b_mod_set;
2341 curbuf->b_mod_set = FALSE;
2342 win_update(curwin);
2343 must_redraw = 0;
2344 curbuf->b_mod_set = i;
2345 }
2346 recursive = FALSE;
2347 }
2348 }
2349
2350#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2351 /* restore got_int, unless CTRL-C was hit while redrawing */
2352 if (!got_int)
2353 got_int = save_got_int;
2354#endif
2355}
2356
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357/*
2358 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2359 * as the filler character.
2360 */
2361 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002362win_draw_end(
2363 win_T *wp,
2364 int c1,
2365 int c2,
2366 int row,
2367 int endrow,
2368 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2371 int n = 0;
2372# define FDC_OFF n
2373#else
2374# define FDC_OFF 0
2375#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002376#ifdef FEAT_FOLDING
2377 int fdc = compute_foldcolumn(wp, 0);
2378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380#ifdef FEAT_RIGHTLEFT
2381 if (wp->w_p_rl)
2382 {
2383 /* No check for cmdline window: should never be right-left. */
2384# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002385 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387 if (n > 0)
2388 {
2389 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002390 if (n > wp->w_width)
2391 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2393 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002394 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
2396# endif
2397# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002398 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
2400 int nn = n + 2;
2401
2402 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002403 if (nn > wp->w_width)
2404 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2406 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002407 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 n = nn;
2409 }
2410# endif
2411 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002412 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002413 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2415 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002416 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
2418 else
2419#endif
2420 {
2421#ifdef FEAT_CMDWIN
2422 if (cmdwin_type != 0 && wp == curwin)
2423 {
2424 /* draw the cmdline character in the leftmost column */
2425 n = 1;
2426 if (n > wp->w_width)
2427 n = wp->w_width;
2428 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002429 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002430 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 }
2432#endif
2433#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002434 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002436 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002439 if (nn > wp->w_width)
2440 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002442 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002443 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 n = nn;
2445 }
2446#endif
2447#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002448 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
2450 int nn = n + 2;
2451
2452 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002453 if (nn > wp->w_width)
2454 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002456 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002457 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 n = nn;
2459 }
2460#endif
2461 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002462 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002463 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
2465 set_empty_rows(wp, row);
2466}
2467
Bram Moolenaar1a384422010-07-14 19:53:30 +02002468#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002469static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002470
2471/*
2472 * Advance **color_cols and return TRUE when there are columns to draw.
2473 */
2474 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002475advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002476{
2477 while (**color_cols >= 0 && vcol > **color_cols)
2478 ++*color_cols;
2479 return (**color_cols >= 0);
2480}
2481#endif
2482
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002483#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2484/*
2485 * Copy "text" to ScreenLines using "attr".
2486 * Returns the next screen column.
2487 */
2488 static int
2489text_to_screenline(win_T *wp, char_u *text, int col)
2490{
2491 int off = (int)(current_ScreenLine - ScreenLines);
2492
2493#ifdef FEAT_MBYTE
2494 if (has_mbyte)
2495 {
2496 int cells;
2497 int u8c, u8cc[MAX_MCO];
2498 int i;
2499 int idx;
2500 int c_len;
2501 char_u *p;
2502# ifdef FEAT_ARABIC
2503 int prev_c = 0; /* previous Arabic character */
2504 int prev_c1 = 0; /* first composing char for prev_c */
2505# endif
2506
2507# ifdef FEAT_RIGHTLEFT
2508 if (wp->w_p_rl)
2509 idx = off;
2510 else
2511# endif
2512 idx = off + col;
2513
2514 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2515 for (p = text; *p != NUL; )
2516 {
2517 cells = (*mb_ptr2cells)(p);
2518 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002519 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002520# ifdef FEAT_RIGHTLEFT
2521 - (wp->w_p_rl ? col : 0)
2522# endif
2523 )
2524 break;
2525 ScreenLines[idx] = *p;
2526 if (enc_utf8)
2527 {
2528 u8c = utfc_ptr2char(p, u8cc);
2529 if (*p < 0x80 && u8cc[0] == 0)
2530 {
2531 ScreenLinesUC[idx] = 0;
2532#ifdef FEAT_ARABIC
2533 prev_c = u8c;
2534#endif
2535 }
2536 else
2537 {
2538#ifdef FEAT_ARABIC
2539 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2540 {
2541 /* Do Arabic shaping. */
2542 int pc, pc1, nc;
2543 int pcc[MAX_MCO];
2544 int firstbyte = *p;
2545
2546 /* The idea of what is the previous and next
2547 * character depends on 'rightleft'. */
2548 if (wp->w_p_rl)
2549 {
2550 pc = prev_c;
2551 pc1 = prev_c1;
2552 nc = utf_ptr2char(p + c_len);
2553 prev_c1 = u8cc[0];
2554 }
2555 else
2556 {
2557 pc = utfc_ptr2char(p + c_len, pcc);
2558 nc = prev_c;
2559 pc1 = pcc[0];
2560 }
2561 prev_c = u8c;
2562
2563 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2564 pc, pc1, nc);
2565 ScreenLines[idx] = firstbyte;
2566 }
2567 else
2568 prev_c = u8c;
2569#endif
2570 /* Non-BMP character: display as ? or fullwidth ?. */
2571#ifdef UNICODE16
2572 if (u8c >= 0x10000)
2573 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2574 else
2575#endif
2576 ScreenLinesUC[idx] = u8c;
2577 for (i = 0; i < Screen_mco; ++i)
2578 {
2579 ScreenLinesC[i][idx] = u8cc[i];
2580 if (u8cc[i] == 0)
2581 break;
2582 }
2583 }
2584 if (cells > 1)
2585 ScreenLines[idx + 1] = 0;
2586 }
2587 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2588 /* double-byte single width character */
2589 ScreenLines2[idx] = p[1];
2590 else if (cells > 1)
2591 /* double-width character */
2592 ScreenLines[idx + 1] = p[1];
2593 col += cells;
2594 idx += cells;
2595 p += c_len;
2596 }
2597 }
2598 else
2599#endif
2600 {
2601 int len = (int)STRLEN(text);
2602
Bram Moolenaar02631462017-09-22 15:20:32 +02002603 if (len > wp->w_width - col)
2604 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002605 if (len > 0)
2606 {
2607#ifdef FEAT_RIGHTLEFT
2608 if (wp->w_p_rl)
2609 STRNCPY(current_ScreenLine, text, len);
2610 else
2611#endif
2612 STRNCPY(current_ScreenLine + col, text, len);
2613 col += len;
2614 }
2615 }
2616 return col;
2617}
2618#endif
2619
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620#ifdef FEAT_FOLDING
2621/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002622 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2623 * space is available for window "wp", minus "col".
2624 */
2625 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002626compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002627{
2628 int fdc = wp->w_p_fdc;
2629 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002630 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002631
2632 if (fdc > wwidth - (col + wmw))
2633 fdc = wwidth - (col + wmw);
2634 return fdc;
2635}
2636
2637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 * Display one folded line.
2639 */
2640 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002641fold_line(
2642 win_T *wp,
2643 long fold_count,
2644 foldinfo_T *foldinfo,
2645 linenr_T lnum,
2646 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002648 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 pos_T *top, *bot;
2650 linenr_T lnume = lnum + fold_count - 1;
2651 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002652 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 int col;
2655 int txtcol;
2656 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002657 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
2659 /* Build the fold line:
2660 * 1. Add the cmdwin_type for the command-line window
2661 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002662 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 * 4. Compose the text
2664 * 5. Add the text
2665 * 6. set highlighting for the Visual area an other text
2666 */
2667 col = 0;
2668
2669 /*
2670 * 1. Add the cmdwin_type for the command-line window
2671 * Ignores 'rightleft', this window is never right-left.
2672 */
2673#ifdef FEAT_CMDWIN
2674 if (cmdwin_type != 0 && wp == curwin)
2675 {
2676 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002677 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678#ifdef FEAT_MBYTE
2679 if (enc_utf8)
2680 ScreenLinesUC[off] = 0;
2681#endif
2682 ++col;
2683 }
2684#endif
2685
2686 /*
2687 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002688 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002690 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (fdc > 0)
2692 {
2693 fill_foldcolumn(buf, wp, TRUE, lnum);
2694#ifdef FEAT_RIGHTLEFT
2695 if (wp->w_p_rl)
2696 {
2697 int i;
2698
Bram Moolenaar02631462017-09-22 15:20:32 +02002699 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002700 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 /* reverse the fold column */
2702 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002703 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 }
2705 else
2706#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002707 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 col += fdc;
2709 }
2710
2711#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002712# define RL_MEMSET(p, v, l) \
2713 do { \
2714 if (wp->w_p_rl) \
2715 for (ri = 0; ri < l; ++ri) \
2716 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2717 else \
2718 for (ri = 0; ri < l; ++ri) \
2719 ScreenAttrs[off + (p) + ri] = v; \
2720 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002722# define RL_MEMSET(p, v, l) \
2723 do { \
2724 for (ri = 0; ri < l; ++ri) \
2725 ScreenAttrs[off + (p) + ri] = v; \
2726 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727#endif
2728
Bram Moolenaar64486672010-05-16 15:46:46 +02002729 /* Set all attributes of the 'number' or 'relativenumber' column and the
2730 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002731 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
2733#ifdef FEAT_SIGNS
2734 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002735 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002737 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 if (len > 0)
2739 {
2740 if (len > 2)
2741 len = 2;
2742# ifdef FEAT_RIGHTLEFT
2743 if (wp->w_p_rl)
2744 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002745 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002746 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 else
2748# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002749 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 col += len;
2751 }
2752 }
2753#endif
2754
2755 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002756 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002758 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002760 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 if (len > 0)
2762 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002763 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002764 long num;
2765 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002766
2767 if (len > w + 1)
2768 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002769
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002770 if (wp->w_p_nu && !wp->w_p_rnu)
2771 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002772 num = (long)lnum;
2773 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002774 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002775 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002776 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002777 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002778 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002779 /* 'number' + 'relativenumber': cursor line shows absolute
2780 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002781 num = lnum;
2782 fmt = "%-*ld ";
2783 }
2784 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002785
Bram Moolenaar700e7342013-01-30 12:31:36 +01002786 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787#ifdef FEAT_RIGHTLEFT
2788 if (wp->w_p_rl)
2789 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002790 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002791 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 else
2793#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002794 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 col += len;
2796 }
2797 }
2798
2799 /*
2800 * 4. Compose the folded-line string with 'foldtext', if set.
2801 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002802 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803
2804 txtcol = col; /* remember where text starts */
2805
2806 /*
2807 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2808 * Right-left text is put in columns 0 - number-col, normal text is put
2809 * in columns number-col - window-width.
2810 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002811 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 /* Fill the rest of the line with the fold filler */
2814#ifdef FEAT_RIGHTLEFT
2815 if (wp->w_p_rl)
2816 col -= txtcol;
2817#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002818 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819#ifdef FEAT_RIGHTLEFT
2820 - (wp->w_p_rl ? txtcol : 0)
2821#endif
2822 )
2823 {
2824#ifdef FEAT_MBYTE
2825 if (enc_utf8)
2826 {
2827 if (fill_fold >= 0x80)
2828 {
2829 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002830 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002831 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 }
2833 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002836 ScreenLines[off + col] = fill_fold;
2837 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002838 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002840 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002842 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 }
2844
2845 if (text != buf)
2846 vim_free(text);
2847
2848 /*
2849 * 6. set highlighting for the Visual area an other text.
2850 * If all folded lines are in the Visual area, highlight the line.
2851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2853 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002854 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 {
2856 /* Visual is after curwin->w_cursor */
2857 top = &curwin->w_cursor;
2858 bot = &VIsual;
2859 }
2860 else
2861 {
2862 /* Visual is before curwin->w_cursor */
2863 top = &VIsual;
2864 bot = &curwin->w_cursor;
2865 }
2866 if (lnum >= top->lnum
2867 && lnume <= bot->lnum
2868 && (VIsual_mode != 'v'
2869 || ((lnum > top->lnum
2870 || (lnum == top->lnum
2871 && top->col == 0))
2872 && (lnume < bot->lnum
2873 || (lnume == bot->lnum
2874 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002875 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 {
2877 if (VIsual_mode == Ctrl_V)
2878 {
2879 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002880 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002882 if (wp->w_old_cursor_lcol != MAXCOL
2883 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002884 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 len = wp->w_old_cursor_lcol;
2886 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002887 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002888 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002889 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
2891 }
2892 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002895 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
2898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002900#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002901 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2902 if (wp->w_p_cc_cols)
2903 {
2904 int i = 0;
2905 int j = wp->w_p_cc_cols[i];
2906 int old_txtcol = txtcol;
2907
2908 while (j > -1)
2909 {
2910 txtcol += j;
2911 if (wp->w_p_wrap)
2912 txtcol -= wp->w_skipcol;
2913 else
2914 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002915 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002916 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002917 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002918 txtcol = old_txtcol;
2919 j = wp->w_p_cc_cols[++i];
2920 }
2921 }
2922
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002923 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002924 if (wp->w_p_cuc)
2925 {
2926 txtcol += wp->w_virtcol;
2927 if (wp->w_p_wrap)
2928 txtcol -= wp->w_skipcol;
2929 else
2930 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002931 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002932 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002933 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002934 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936
Bram Moolenaar02631462017-09-22 15:20:32 +02002937 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2938 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939
2940 /*
2941 * Update w_cline_height and w_cline_folded if the cursor line was
2942 * updated (saves a call to plines() later).
2943 */
2944 if (wp == curwin
2945 && lnum <= curwin->w_cursor.lnum
2946 && lnume >= curwin->w_cursor.lnum)
2947 {
2948 curwin->w_cline_row = row;
2949 curwin->w_cline_height = 1;
2950 curwin->w_cline_folded = TRUE;
2951 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2952 }
2953}
2954
2955/*
2956 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2957 */
2958 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002959copy_text_attr(
2960 int off,
2961 char_u *buf,
2962 int len,
2963 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002965 int i;
2966
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 mch_memmove(ScreenLines + off, buf, (size_t)len);
2968# ifdef FEAT_MBYTE
2969 if (enc_utf8)
2970 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2971# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002972 for (i = 0; i < len; ++i)
2973 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974}
2975
2976/*
2977 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002978 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 */
2980 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002981fill_foldcolumn(
2982 char_u *p,
2983 win_T *wp,
2984 int closed, /* TRUE of FALSE */
2985 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
2987 int i = 0;
2988 int level;
2989 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002990 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002991 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992
2993 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002994 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995
2996 level = win_foldinfo.fi_level;
2997 if (level > 0)
2998 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002999 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003000 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 /* If the column is too narrow, we start at the lowest level that
3003 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003004 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 if (first_level < 1)
3006 first_level = 1;
3007
Bram Moolenaar1c934292015-01-27 16:39:29 +01003008 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 {
3010 if (win_foldinfo.fi_lnum == lnum
3011 && first_level + i >= win_foldinfo.fi_low_level)
3012 p[i] = '-';
3013 else if (first_level == 1)
3014 p[i] = '|';
3015 else if (first_level + i <= 9)
3016 p[i] = '0' + first_level + i;
3017 else
3018 p[i] = '>';
3019 if (first_level + i == level)
3020 break;
3021 }
3022 }
3023 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003024 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025}
3026#endif /* FEAT_FOLDING */
3027
3028/*
3029 * Display line "lnum" of window 'wp' on the screen.
3030 * Start at row "startrow", stop when "endrow" is reached.
3031 * wp->w_virtcol needs to be valid.
3032 *
3033 * Return the number of last row the line occupies.
3034 */
3035 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003036win_line(
3037 win_T *wp,
3038 linenr_T lnum,
3039 int startrow,
3040 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003041 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003043 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3045 int c = 0; /* init for GCC */
3046 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003047#ifdef FEAT_LINEBREAK
3048 long vcol_sbr = -1; /* virtual column after showbreak */
3049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 long vcol_prev = -1; /* "vcol" of previous character */
3051 char_u *line; /* current line */
3052 char_u *ptr; /* current position in "line" */
3053 int row; /* row in the window, excl w_winrow */
3054 int screen_row; /* row on the screen, incl w_winrow */
3055
3056 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3057 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003058 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003059 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 int c_extra = NUL; /* extra chars, all the same */
3061 int extra_attr = 0; /* attributes when n_extra != 0 */
3062 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3063 displaying lcs_eol at end-of-line */
3064 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3065 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3066
3067 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3068 int saved_n_extra = 0;
3069 char_u *saved_p_extra = NULL;
3070 int saved_c_extra = 0;
3071 int saved_char_attr = 0;
3072
3073 int n_attr = 0; /* chars with special attr */
3074 int saved_attr2 = 0; /* char_attr saved for n_attr */
3075 int n_attr3 = 0; /* chars with overruling special attr */
3076 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3077
3078 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3079
3080 int fromcol, tocol; /* start/end of inverting */
3081 int fromcol_prev = -2; /* start of inverting after cursor */
3082 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003084 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 pos_T pos;
3086 long v;
3087
3088 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003089 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3091 in this line */
3092 int attr = 0; /* attributes for area highlighting */
3093 int area_attr = 0; /* attributes desired by highlighting */
3094 int search_attr = 0; /* attributes desired by 'hlsearch' */
3095#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003096 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 int syntax_attr = 0; /* attributes desired by syntax */
3098 int has_syntax = FALSE; /* this buffer has syntax highl. */
3099 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003100 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003101 int draw_color_col = FALSE; /* highlight colorcolumn */
3102 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003103#endif
3104#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003105 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003106# define SPWORDLEN 150
3107 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003108 int nextlinecol = 0; /* column where nextline[] starts */
3109 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003110 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003111 int spell_attr = 0; /* attributes desired by spelling */
3112 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003113 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3114 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003115 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003116 static int cap_col = -1; /* column to check for Cap word */
3117 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003118 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119#endif
3120 int extra_check; /* has syntax or linebreak */
3121#ifdef FEAT_MBYTE
3122 int multi_attr = 0; /* attributes desired by multibyte */
3123 int mb_l = 1; /* multi-byte byte length */
3124 int mb_c = 0; /* decoded multi-byte character */
3125 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003126 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#endif
3128#ifdef FEAT_DIFF
3129 int filler_lines; /* nr of filler lines to be drawn */
3130 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003131 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 int change_start = MAXCOL; /* first col of changed area */
3133 int change_end = -1; /* last col of changed area */
3134#endif
3135 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3136#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003137 int need_showbreak = FALSE; /* overlong line, skipping first x
3138 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003140#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003141 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003143 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144#endif
3145#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003146 matchitem_T *cur; /* points to the match list */
3147 match_T *shl; /* points to search_hl or a match */
3148 int shl_flag; /* flag to indicate whether search_hl
3149 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003150 int pos_inprogress; /* marks that position match search is
3151 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003152 int prevcol_hl_flag; /* flag to indicate whether prevcol
3153 equals startcol of search_hl or one
3154 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#endif
3156#ifdef FEAT_ARABIC
3157 int prev_c = 0; /* previous Arabic character */
3158 int prev_c1 = 0; /* first composing char for prev_c */
3159#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003160#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003161 int did_line_attr = 0;
3162#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003163#ifdef FEAT_TERMINAL
3164 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003165 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
3168 /* draw_state: items that are drawn in sequence: */
3169#define WL_START 0 /* nothing done yet */
3170#ifdef FEAT_CMDWIN
3171# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3172#else
3173# define WL_CMDLINE WL_START
3174#endif
3175#ifdef FEAT_FOLDING
3176# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3177#else
3178# define WL_FOLD WL_CMDLINE
3179#endif
3180#ifdef FEAT_SIGNS
3181# define WL_SIGN WL_FOLD + 1 /* column for signs */
3182#else
3183# define WL_SIGN WL_FOLD /* column for signs */
3184#endif
3185#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003186#ifdef FEAT_LINEBREAK
3187# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003189# define WL_BRI WL_NR
3190#endif
3191#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3192# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3193#else
3194# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#endif
3196#define WL_LINE WL_SBR + 1 /* text in the line */
3197 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003198#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 int feedback_col = 0;
3200 int feedback_old_attr = -1;
3201#endif
3202
Bram Moolenaar860cae12010-06-05 23:22:07 +02003203#ifdef FEAT_CONCEAL
3204 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003205 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003206 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003207 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003208 int is_concealing = FALSE;
3209 int boguscols = 0; /* nonexistent columns added to force
3210 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003211 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003212 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003213 int match_conc = 0; /* cchar for match functions */
3214 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003215 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003216# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003217# define FIX_FOR_BOGUSCOLS \
3218 { \
3219 n_extra += vcol_off; \
3220 vcol -= vcol_off; \
3221 vcol_off = 0; \
3222 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003223 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003224 boguscols = 0; \
3225 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003226#else
3227# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
3230 if (startrow > endrow) /* past the end already! */
3231 return startrow;
3232
3233 row = startrow;
3234 screen_row = row + W_WINROW(wp);
3235
3236 /*
3237 * To speed up the loop below, set extra_check when there is linebreak,
3238 * trailing white space and/or syntax processing to be done.
3239 */
3240#ifdef FEAT_LINEBREAK
3241 extra_check = wp->w_p_lbr;
3242#else
3243 extra_check = 0;
3244#endif
3245#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003246 if (syntax_present(wp) && !wp->w_s->b_syn_error
3247# ifdef SYN_TIME_LIMIT
3248 && !wp->w_s->b_syn_slow
3249# endif
3250 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 {
3252 /* Prepare for syntax highlighting in this line. When there is an
3253 * error, stop syntax highlighting. */
3254 save_did_emsg = did_emsg;
3255 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003256 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003258 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 else
3260 {
3261 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003262#ifdef SYN_TIME_LIMIT
3263 if (!wp->w_s->b_syn_slow)
3264#endif
3265 {
3266 has_syntax = TRUE;
3267 extra_check = TRUE;
3268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 }
3270 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003271
3272 /* Check for columns to display for 'colorcolumn'. */
3273 color_cols = wp->w_p_cc_cols;
3274 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003275 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003276#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003277
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003278#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003279 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003280 {
3281 extra_check = TRUE;
3282 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003283 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003284 }
3285#endif
3286
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003287#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003288 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003289 && *wp->w_s->b_p_spl != NUL
3290 && wp->w_s->b_langp.ga_len > 0
3291 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003292 {
3293 /* Prepare for spell checking. */
3294 has_spell = TRUE;
3295 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003296
3297 /* Get the start of the next line, so that words that wrap to the next
3298 * line are found too: "et<line-break>al.".
3299 * Trick: skip a few chars for C/shell/Vim comments */
3300 nextline[SPWORDLEN] = NUL;
3301 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3302 {
3303 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3304 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3305 }
3306
3307 /* When a word wrapped from the previous line the start of the current
3308 * line is valid. */
3309 if (lnum == checked_lnum)
3310 cur_checked_col = checked_col;
3311 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003312
3313 /* When there was a sentence end in the previous line may require a
3314 * word starting with capital in this line. In line 1 always check
3315 * the first word. */
3316 if (lnum != capcol_lnum)
3317 cap_col = -1;
3318 if (lnum == 1)
3319 cap_col = 0;
3320 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#endif
3323
3324 /*
3325 * handle visual active in this window
3326 */
3327 fromcol = -10;
3328 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3330 {
3331 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003332 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
3334 top = &curwin->w_cursor;
3335 bot = &VIsual;
3336 }
3337 else /* Visual is before curwin->w_cursor */
3338 {
3339 top = &VIsual;
3340 bot = &curwin->w_cursor;
3341 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003342 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 if (VIsual_mode == Ctrl_V) /* block mode */
3344 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003345 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
3347 fromcol = wp->w_old_cursor_fcol;
3348 tocol = wp->w_old_cursor_lcol;
3349 }
3350 }
3351 else /* non-block mode */
3352 {
3353 if (lnum > top->lnum && lnum <= bot->lnum)
3354 fromcol = 0;
3355 else if (lnum == top->lnum)
3356 {
3357 if (VIsual_mode == 'V') /* linewise */
3358 fromcol = 0;
3359 else
3360 {
3361 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3362 if (gchar_pos(top) == NUL)
3363 tocol = fromcol + 1;
3364 }
3365 }
3366 if (VIsual_mode != 'V' && lnum == bot->lnum)
3367 {
3368 if (*p_sel == 'e' && bot->col == 0
3369#ifdef FEAT_VIRTUALEDIT
3370 && bot->coladd == 0
3371#endif
3372 )
3373 {
3374 fromcol = -10;
3375 tocol = MAXCOL;
3376 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003377 else if (bot->col == MAXCOL)
3378 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 else
3380 {
3381 pos = *bot;
3382 if (*p_sel == 'e')
3383 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3384 else
3385 {
3386 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3387 ++tocol;
3388 }
3389 }
3390 }
3391 }
3392
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 /* Check if the character under the cursor should not be inverted */
3394 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003395#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 )
3399 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
3401 /* if inverting in this line set area_highlighting */
3402 if (fromcol >= 0)
3403 {
3404 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003405 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003407 if ((clip_star.available && !clip_star.owned
3408 && clip_isautosel_star())
3409 || (clip_plus.available && !clip_plus.owned
3410 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003411 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412#endif
3413 }
3414 }
3415
3416 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003417 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003419 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 && wp == curwin
3421 && lnum >= curwin->w_cursor.lnum
3422 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3423 {
3424 if (lnum == curwin->w_cursor.lnum)
3425 getvcol(curwin, &(curwin->w_cursor),
3426 (colnr_T *)&fromcol, NULL, NULL);
3427 else
3428 fromcol = 0;
3429 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3430 {
3431 pos.lnum = lnum;
3432 pos.col = search_match_endcol;
3433 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3434 }
3435 else
3436 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003437 /* do at least one character; happens when past end of line */
3438 if (fromcol == tocol)
3439 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003441 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 }
3443
3444#ifdef FEAT_DIFF
3445 filler_lines = diff_check(wp, lnum);
3446 if (filler_lines < 0)
3447 {
3448 if (filler_lines == -1)
3449 {
3450 if (diff_find_change(wp, lnum, &change_start, &change_end))
3451 diff_hlf = HLF_ADD; /* added line */
3452 else if (change_start == 0)
3453 diff_hlf = HLF_TXD; /* changed text */
3454 else
3455 diff_hlf = HLF_CHD; /* changed line */
3456 }
3457 else
3458 diff_hlf = HLF_ADD; /* added line */
3459 filler_lines = 0;
3460 area_highlighting = TRUE;
3461 }
3462 if (lnum == wp->w_topline)
3463 filler_lines = wp->w_topfill;
3464 filler_todo = filler_lines;
3465#endif
3466
3467#ifdef LINE_ATTR
3468# ifdef FEAT_SIGNS
3469 /* If this line has a sign with line highlighting set line_attr. */
3470 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3471 if (v != 0)
3472 line_attr = sign_get_attr((int)v, TRUE);
3473# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003474# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003476 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003477 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478# endif
3479 if (line_attr != 0)
3480 area_highlighting = TRUE;
3481#endif
3482
3483 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3484 ptr = line;
3485
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003486#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003487 if (has_spell)
3488 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003489 /* For checking first word with a capital skip white space. */
3490 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003491 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003492
Bram Moolenaar30abd282005-06-22 22:35:10 +00003493 /* To be able to spell-check over line boundaries copy the end of the
3494 * current line into nextline[]. Above the start of the next line was
3495 * copied to nextline[SPWORDLEN]. */
3496 if (nextline[SPWORDLEN] == NUL)
3497 {
3498 /* No next line or it is empty. */
3499 nextlinecol = MAXCOL;
3500 nextline_idx = 0;
3501 }
3502 else
3503 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003504 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003505 if (v < SPWORDLEN)
3506 {
3507 /* Short line, use it completely and append the start of the
3508 * next line. */
3509 nextlinecol = 0;
3510 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003511 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003512 nextline_idx = v + 1;
3513 }
3514 else
3515 {
3516 /* Long line, use only the last SPWORDLEN bytes. */
3517 nextlinecol = v - SPWORDLEN;
3518 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3519 nextline_idx = SPWORDLEN + 1;
3520 }
3521 }
3522 }
3523#endif
3524
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003525 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003527 if (lcs_space || lcs_trail)
3528 extra_check = TRUE;
3529 /* find start of trailing whitespace */
3530 if (lcs_trail)
3531 {
3532 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003533 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003534 --trailcol;
3535 trailcol += (colnr_T) (ptr - line);
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538
3539 /*
3540 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3541 * first character to be displayed.
3542 */
3543 if (wp->w_p_wrap)
3544 v = wp->w_skipcol;
3545 else
3546 v = wp->w_leftcol;
3547 if (v > 0)
3548 {
3549#ifdef FEAT_MBYTE
3550 char_u *prev_ptr = ptr;
3551#endif
3552 while (vcol < v && *ptr != NUL)
3553 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003554 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 vcol += c;
3556#ifdef FEAT_MBYTE
3557 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003559 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
3561
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003562 /* When:
3563 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003564 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003565 * - 'virtualedit' is set, or
3566 * - the visual mode is active,
3567 * the end of the line may be before the start of the displayed part.
3568 */
3569 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003570#ifdef FEAT_SYN_HL
3571 wp->w_p_cuc || draw_color_col ||
3572#endif
3573#ifdef FEAT_VIRTUALEDIT
3574 virtual_active() ||
3575#endif
3576 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580
3581 /* Handle a character that's not completely on the screen: Put ptr at
3582 * that character but skip the first few screen characters. */
3583 if (vcol > v)
3584 {
3585 vcol -= c;
3586#ifdef FEAT_MBYTE
3587 ptr = prev_ptr;
3588#else
3589 --ptr;
3590#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003591 /* If the character fits on the screen, don't need to skip it.
3592 * Except for a TAB. */
3593 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003594#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003595 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003596#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003597 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003598 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
3600
3601 /*
3602 * Adjust for when the inverted text is before the screen,
3603 * and when the start of the inverted text is before the screen.
3604 */
3605 if (tocol <= vcol)
3606 fromcol = 0;
3607 else if (fromcol >= 0 && fromcol < vcol)
3608 fromcol = vcol;
3609
3610#ifdef FEAT_LINEBREAK
3611 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3612 if (wp->w_p_wrap)
3613 need_showbreak = TRUE;
3614#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003615#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003616 /* When spell checking a word we need to figure out the start of the
3617 * word and if it's badly spelled or not. */
3618 if (has_spell)
3619 {
3620 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003621 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003622 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003623
3624 pos = wp->w_cursor;
3625 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003626 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003627 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003628
3629 /* spell_move_to() may call ml_get() and make "line" invalid */
3630 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3631 ptr = line + linecol;
3632
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003633 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003634 {
3635 /* no bad word found at line start, don't check until end of a
3636 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003637 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003638 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003639 }
3640 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003641 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003642 /* bad word found, use attributes until end of word */
3643 word_end = wp->w_cursor.col + len + 1;
3644
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003645 /* Turn index into actual attributes. */
3646 if (spell_hlf != HLF_COUNT)
3647 spell_attr = highlight_attr[spell_hlf];
3648 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003649 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003650
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003651# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003652 /* Need to restart syntax highlighting for this line. */
3653 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003654 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003655# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003656 }
3657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659
3660 /*
3661 * Correct highlighting for cursor that can't be disabled.
3662 * Avoids having to check this for each character.
3663 */
3664 if (fromcol >= 0)
3665 {
3666 if (noinvcur)
3667 {
3668 if ((colnr_T)fromcol == wp->w_virtcol)
3669 {
3670 /* highlighting starts at cursor, let it start just after the
3671 * cursor */
3672 fromcol_prev = fromcol;
3673 fromcol = -1;
3674 }
3675 else if ((colnr_T)fromcol < wp->w_virtcol)
3676 /* restart highlighting after the cursor */
3677 fromcol_prev = wp->w_virtcol;
3678 }
3679 if (fromcol >= tocol)
3680 fromcol = -1;
3681 }
3682
3683#ifdef FEAT_SEARCH_EXTRA
3684 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003685 * Handle highlighting the last used search pattern and matches.
3686 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003688 cur = wp->w_match_head;
3689 shl_flag = FALSE;
3690 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003692 if (shl_flag == FALSE)
3693 {
3694 shl = &search_hl;
3695 shl_flag = TRUE;
3696 }
3697 else
3698 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003699 shl->startcol = MAXCOL;
3700 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003702 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003703 v = (long)(ptr - line);
3704 if (cur != NULL)
3705 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003706 next_search_hl(wp, shl, lnum, (colnr_T)v,
3707 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003708
3709 /* Need to get the line again, a multi-line regexp may have made it
3710 * invalid. */
3711 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3712 ptr = line + v;
3713
3714 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003716 if (shl->lnum == lnum)
3717 shl->startcol = shl->rm.startpos[0].col;
3718 else
3719 shl->startcol = 0;
3720 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3721 - shl->rm.startpos[0].lnum)
3722 shl->endcol = shl->rm.endpos[0].col;
3723 else
3724 shl->endcol = MAXCOL;
3725 /* Highlight one character for an empty match. */
3726 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003729 if (has_mbyte && line[shl->endcol] != NUL)
3730 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3731 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003733 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003735 if ((long)shl->startcol < v) /* match at leftcol */
3736 {
3737 shl->attr_cur = shl->attr;
3738 search_attr = shl->attr;
3739 }
3740 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003742 if (shl != &search_hl && cur != NULL)
3743 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745#endif
3746
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003747#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003748 /* Cursor line highlighting for 'cursorline' in the current window. Not
3749 * when Visual mode is active, because it's not clear what is selected
3750 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003751 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003752 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003753 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003754 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003755 area_highlighting = TRUE;
3756 }
3757#endif
3758
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003759 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 col = 0;
3761#ifdef FEAT_RIGHTLEFT
3762 if (wp->w_p_rl)
3763 {
3764 /* Rightleft window: process the text in the normal direction, but put
3765 * it in current_ScreenLine[] from right to left. Start at the
3766 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003767 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 off += col;
3769 }
3770#endif
3771
3772 /*
3773 * Repeat for the whole displayed line.
3774 */
3775 for (;;)
3776 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003777#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003778 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 /* Skip this quickly when working on the text. */
3781 if (draw_state != WL_LINE)
3782 {
3783#ifdef FEAT_CMDWIN
3784 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3785 {
3786 draw_state = WL_CMDLINE;
3787 if (cmdwin_type != 0 && wp == curwin)
3788 {
3789 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003791 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003792 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
3794 }
3795#endif
3796
3797#ifdef FEAT_FOLDING
3798 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3799 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003800 int fdc = compute_foldcolumn(wp, 0);
3801
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003803 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003805 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003806 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003807 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003808 p_extra_free = alloc(12 + 1);
3809
3810 if (p_extra_free != NULL)
3811 {
3812 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3813 n_extra = fdc;
3814 p_extra_free[n_extra] = NUL;
3815 p_extra = p_extra_free;
3816 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003817 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 }
3820 }
3821#endif
3822
3823#ifdef FEAT_SIGNS
3824 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3825 {
3826 draw_state = WL_SIGN;
3827 /* Show the sign column when there are any signs in this
3828 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003829 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003831 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003833 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834# endif
3835
3836 /* Draw two cells with the sign value or blank. */
3837 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003838 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 n_extra = 2;
3840
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003841 if (row == startrow
3842#ifdef FEAT_DIFF
3843 + filler_lines && filler_todo <= 0
3844#endif
3845 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
3847 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3848 SIGN_TEXT);
3849# ifdef FEAT_SIGN_ICONS
3850 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3851 SIGN_ICON);
3852 if (gui.in_use && icon_sign != 0)
3853 {
3854 /* Use the image in this position. */
3855 c_extra = SIGN_BYTE;
3856# ifdef FEAT_NETBEANS_INTG
3857 if (buf_signcount(wp->w_buffer, lnum) > 1)
3858 c_extra = MULTISIGN_BYTE;
3859# endif
3860 char_attr = icon_sign;
3861 }
3862 else
3863# endif
3864 if (text_sign != 0)
3865 {
3866 p_extra = sign_get_text(text_sign);
3867 if (p_extra != NULL)
3868 {
3869 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003870 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 }
3872 char_attr = sign_get_attr(text_sign, FALSE);
3873 }
3874 }
3875 }
3876 }
3877#endif
3878
3879 if (draw_state == WL_NR - 1 && n_extra == 0)
3880 {
3881 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003882 /* Display the absolute or relative line number. After the
3883 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3884 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 && (row == startrow
3886#ifdef FEAT_DIFF
3887 + filler_lines
3888#endif
3889 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3890 {
3891 /* Draw the line number (empty space after wrapping). */
3892 if (row == startrow
3893#ifdef FEAT_DIFF
3894 + filler_lines
3895#endif
3896 )
3897 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003898 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003899 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003900
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003901 if (wp->w_p_nu && !wp->w_p_rnu)
3902 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003903 num = (long)lnum;
3904 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003905 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003906 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003907 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003908 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003909 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003910 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003911 num = lnum;
3912 fmt = "%-*ld ";
3913 }
3914 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003915
Bram Moolenaar700e7342013-01-30 12:31:36 +01003916 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003917 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 if (wp->w_skipcol > 0)
3919 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3920 *p_extra = '-';
3921#ifdef FEAT_RIGHTLEFT
3922 if (wp->w_p_rl) /* reverse line numbers */
3923 rl_mirror(extra);
3924#endif
3925 p_extra = extra;
3926 c_extra = NUL;
3927 }
3928 else
3929 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003930 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003931 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003932#ifdef FEAT_SYN_HL
3933 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003934 * the current line differently.
3935 * TODO: Can we use CursorLine instead of CursorLineNr
3936 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003937 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003938 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003939 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
3942 }
3943
Bram Moolenaar597a4222014-06-25 14:39:50 +02003944#ifdef FEAT_LINEBREAK
3945 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3946 && n_extra == 0 && *p_sbr != NUL)
3947 /* draw indent after showbreak value */
3948 draw_state = WL_BRI;
3949 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3950 /* After the showbreak, draw the breakindent */
3951 draw_state = WL_BRI - 1;
3952
3953 /* draw 'breakindent': indent wrapped text accordingly */
3954 if (draw_state == WL_BRI - 1 && n_extra == 0)
3955 {
3956 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003957 /* if need_showbreak is set, breakindent also applies */
3958 if (wp->w_p_bri && n_extra == 0
3959 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003960# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003961 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003962# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003963 )
3964 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003965 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003966# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003967 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003968 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003969 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003970# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003971 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3972 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003973 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003974# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003975 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003976# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003977 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003978 c_extra = ' ';
3979 n_extra = get_breakindent_win(wp,
3980 ml_get_buf(wp->w_buffer, lnum, FALSE));
3981 /* Correct end of highlighted area for 'breakindent',
3982 * required when 'linebreak' is also set. */
3983 if (tocol == vcol)
3984 tocol += n_extra;
3985 }
3986 }
3987#endif
3988
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3990 if (draw_state == WL_SBR - 1 && n_extra == 0)
3991 {
3992 draw_state = WL_SBR;
3993# ifdef FEAT_DIFF
3994 if (filler_todo > 0)
3995 {
3996 /* Draw "deleted" diff line(s). */
3997 if (char2cells(fill_diff) > 1)
3998 c_extra = '-';
3999 else
4000 c_extra = fill_diff;
4001# ifdef FEAT_RIGHTLEFT
4002 if (wp->w_p_rl)
4003 n_extra = col + 1;
4004 else
4005# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004006 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004007 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009# endif
4010# ifdef FEAT_LINEBREAK
4011 if (*p_sbr != NUL && need_showbreak)
4012 {
4013 /* Draw 'showbreak' at the start of each broken line. */
4014 p_extra = p_sbr;
4015 c_extra = NUL;
4016 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004017 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004019 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 /* Correct end of highlighted area for 'showbreak',
4021 * required when 'linebreak' is also set. */
4022 if (tocol == vcol)
4023 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004024#ifdef FEAT_SYN_HL
4025 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004026 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004027 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004028 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031# endif
4032 }
4033#endif
4034
4035 if (draw_state == WL_LINE - 1 && n_extra == 0)
4036 {
4037 draw_state = WL_LINE;
4038 if (saved_n_extra)
4039 {
4040 /* Continue item from end of wrapped line. */
4041 n_extra = saved_n_extra;
4042 c_extra = saved_c_extra;
4043 p_extra = saved_p_extra;
4044 char_attr = saved_char_attr;
4045 }
4046 else
4047 char_attr = 0;
4048 }
4049 }
4050
4051 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004052 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004053 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054#ifdef FEAT_DIFF
4055 && filler_todo <= 0
4056#endif
4057 )
4058 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004059 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004060 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004061 /* Pretend we have finished updating the window. Except when
4062 * 'cursorcolumn' is set. */
4063#ifdef FEAT_SYN_HL
4064 if (wp->w_p_cuc)
4065 row = wp->w_cline_row + wp->w_cline_height;
4066 else
4067#endif
4068 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 break;
4070 }
4071
4072 if (draw_state == WL_LINE && area_highlighting)
4073 {
4074 /* handle Visual or match highlighting in this line */
4075 if (vcol == fromcol
4076#ifdef FEAT_MBYTE
4077 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4078 && (*mb_ptr2cells)(ptr) > 1)
4079#endif
4080 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004081 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 && vcol < tocol))
4083 area_attr = attr; /* start highlighting */
4084 else if (area_attr != 0
4085 && (vcol == tocol
4086 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089#ifdef FEAT_SEARCH_EXTRA
4090 if (!n_extra)
4091 {
4092 /*
4093 * Check for start/end of search pattern match.
4094 * After end, check for start/end of next match.
4095 * When another match, have to check for start again.
4096 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004097 * Do this for 'search_hl' and the match list (ordered by
4098 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004100 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004101 cur = wp->w_match_head;
4102 shl_flag = FALSE;
4103 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004105 if (shl_flag == FALSE
4106 && ((cur != NULL
4107 && cur->priority > SEARCH_HL_PRIORITY)
4108 || cur == NULL))
4109 {
4110 shl = &search_hl;
4111 shl_flag = TRUE;
4112 }
4113 else
4114 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004115 if (cur != NULL)
4116 cur->pos.cur = 0;
4117 pos_inprogress = TRUE;
4118 while (shl->rm.regprog != NULL
4119 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004121 if (shl->startcol != MAXCOL
4122 && v >= (long)shl->startcol
4123 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004125#ifdef FEAT_MBYTE
4126 int tmp_col = v + MB_PTR2LEN(ptr);
4127
4128 if (shl->endcol < tmp_col)
4129 shl->endcol = tmp_col;
4130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004132#ifdef FEAT_CONCEAL
4133 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4134 == cur->hlg_id)
4135 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004136 has_match_conc =
4137 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004138 match_conc = cur->conceal_char;
4139 }
4140 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004141 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004144 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
4146 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004147 next_search_hl(wp, shl, lnum, (colnr_T)v,
4148 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004149 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004150 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
4152 /* Need to get the line again, a multi-line regexp
4153 * may have made it invalid. */
4154 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4155 ptr = line + v;
4156
4157 if (shl->lnum == lnum)
4158 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004159 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004161 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004163 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004165 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 {
4167 /* highlight empty match, try again after
4168 * it */
4169#ifdef FEAT_MBYTE
4170 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004171 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004172 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 else
4174#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004175 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177
4178 /* Loop to check if the match starts at the
4179 * current position */
4180 continue;
4181 }
4182 }
4183 break;
4184 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004185 if (shl != &search_hl && cur != NULL)
4186 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004188
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004189 /* Use attributes from match with highest priority among
4190 * 'search_hl' and the match list. */
4191 search_attr = search_hl.attr_cur;
4192 cur = wp->w_match_head;
4193 shl_flag = FALSE;
4194 while (cur != NULL || shl_flag == FALSE)
4195 {
4196 if (shl_flag == FALSE
4197 && ((cur != NULL
4198 && cur->priority > SEARCH_HL_PRIORITY)
4199 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004200 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004201 shl = &search_hl;
4202 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004203 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004204 else
4205 shl = &cur->hl;
4206 if (shl->attr_cur != 0)
4207 search_attr = shl->attr_cur;
4208 if (shl != &search_hl && cur != NULL)
4209 cur = cur->next;
4210 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004211 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004212 if (*ptr == NUL && (did_line_attr >= 1
4213 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004214 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 }
4216#endif
4217
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004219 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004221 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4222 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004224 if (diff_hlf == HLF_TXD && ptr - line > change_end
4225 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004227 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004228 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004229 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004232
4233 /* Decide which of the highlight attributes to use. */
4234 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004235#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004236 if (area_attr != 0)
4237 char_attr = hl_combine_attr(line_attr, area_attr);
4238 else if (search_attr != 0)
4239 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004240 /* Use line_attr when not in the Visual or 'incsearch' area
4241 * (area_attr may be 0 when "noinvcur" is set). */
4242 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004243 || vcol < fromcol || vcol_prev < fromcol_prev
4244 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004245 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004246#else
4247 if (area_attr != 0)
4248 char_attr = area_attr;
4249 else if (search_attr != 0)
4250 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004251#endif
4252 else
4253 {
4254 attr_pri = FALSE;
4255#ifdef FEAT_SYN_HL
4256 if (has_syntax)
4257 char_attr = syntax_attr;
4258 else
4259#endif
4260 char_attr = 0;
4261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263
4264 /*
4265 * Get the next character to put on the screen.
4266 */
4267 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004268 * The "p_extra" points to the extra stuff that is inserted to
4269 * represent special characters (non-printable stuff) and other
4270 * things. When all characters are the same, c_extra is used.
4271 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4272 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4274 */
4275 if (n_extra > 0)
4276 {
4277 if (c_extra != NUL)
4278 {
4279 c = c_extra;
4280#ifdef FEAT_MBYTE
4281 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004282 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 {
4284 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004285 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004286 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 else
4289 mb_utf8 = FALSE;
4290#endif
4291 }
4292 else
4293 {
4294 c = *p_extra;
4295#ifdef FEAT_MBYTE
4296 if (has_mbyte)
4297 {
4298 mb_c = c;
4299 if (enc_utf8)
4300 {
4301 /* If the UTF-8 character is more than one byte:
4302 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004303 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 mb_utf8 = FALSE;
4305 if (mb_l > n_extra)
4306 mb_l = 1;
4307 else if (mb_l > 1)
4308 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004309 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004311 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313 }
4314 else
4315 {
4316 /* if this is a DBCS character, put it in "mb_c" */
4317 mb_l = MB_BYTE2LEN(c);
4318 if (mb_l >= n_extra)
4319 mb_l = 1;
4320 else if (mb_l > 1)
4321 mb_c = (c << 8) + p_extra[1];
4322 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004323 if (mb_l == 0) /* at the NUL at end-of-line */
4324 mb_l = 1;
4325
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 /* If a double-width char doesn't fit display a '>' in the
4327 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004328 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329# ifdef FEAT_RIGHTLEFT
4330 wp->w_p_rl ? (col <= 0) :
4331# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004332 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 && (*mb_char2cells)(mb_c) == 2)
4334 {
4335 c = '>';
4336 mb_c = c;
4337 mb_l = 1;
4338 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004339 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 /* put the pointer back to output the double-width
4341 * character at the start of the next line. */
4342 ++n_extra;
4343 --p_extra;
4344 }
4345 else
4346 {
4347 n_extra -= mb_l - 1;
4348 p_extra += mb_l - 1;
4349 }
4350 }
4351#endif
4352 ++p_extra;
4353 }
4354 --n_extra;
4355 }
4356 else
4357 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004358#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004359 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004360#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004361
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004362 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004363 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 /*
4365 * Get a character from the line itself.
4366 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004367 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004368#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004369 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371#ifdef FEAT_MBYTE
4372 if (has_mbyte)
4373 {
4374 mb_c = c;
4375 if (enc_utf8)
4376 {
4377 /* If the UTF-8 character is more than one byte: Decode it
4378 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004379 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 mb_utf8 = FALSE;
4381 if (mb_l > 1)
4382 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004383 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 /* Overlong encoded ASCII or ASCII with composing char
4385 * is displayed normally, except a NUL. */
4386 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004387 {
4388 c = mb_c;
4389# ifdef FEAT_LINEBREAK
4390 c0 = mb_c;
4391# endif
4392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004394
4395 /* At start of the line we can have a composing char.
4396 * Draw it as a space with a composing char. */
4397 if (utf_iscomposing(mb_c))
4398 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004399 int i;
4400
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004401 for (i = Screen_mco - 1; i > 0; --i)
4402 u8cc[i] = u8cc[i - 1];
4403 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004404 mb_c = ' ';
4405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 }
4407
4408 if ((mb_l == 1 && c >= 0x80)
4409 || (mb_l >= 1 && mb_c == 0)
4410 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004411# ifdef UNICODE16
4412 || mb_c >= 0x10000
4413# endif
4414 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
4416 /*
4417 * Illegal UTF-8 byte: display as <xx>.
4418 * Non-BMP character : display as ? or fullwidth ?.
4419 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004420# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
4424 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004425# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 if (wp->w_p_rl) /* reverse */
4427 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004430# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 else if (utf_char2cells(mb_c) != 2)
4432 STRCPY(extra, "?");
4433 else
4434 /* 0xff1f in UTF-8: full-width '?' */
4435 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438 p_extra = extra;
4439 c = *p_extra;
4440 mb_c = mb_ptr2char_adv(&p_extra);
4441 mb_utf8 = (c >= 0x80);
4442 n_extra = (int)STRLEN(p_extra);
4443 c_extra = NUL;
4444 if (area_attr == 0 && search_attr == 0)
4445 {
4446 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004447 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 saved_attr2 = char_attr; /* save current attr */
4449 }
4450 }
4451 else if (mb_l == 0) /* at the NUL at end-of-line */
4452 mb_l = 1;
4453#ifdef FEAT_ARABIC
4454 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4455 {
4456 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004457 int pc, pc1, nc;
4458 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459
4460 /* The idea of what is the previous and next
4461 * character depends on 'rightleft'. */
4462 if (wp->w_p_rl)
4463 {
4464 pc = prev_c;
4465 pc1 = prev_c1;
4466 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004467 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469 else
4470 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004471 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004473 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475 prev_c = mb_c;
4476
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004477 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 }
4479 else
4480 prev_c = mb_c;
4481#endif
4482 }
4483 else /* enc_dbcs */
4484 {
4485 mb_l = MB_BYTE2LEN(c);
4486 if (mb_l == 0) /* at the NUL at end-of-line */
4487 mb_l = 1;
4488 else if (mb_l > 1)
4489 {
4490 /* We assume a second byte below 32 is illegal.
4491 * Hopefully this is OK for all double-byte encodings!
4492 */
4493 if (ptr[1] >= 32)
4494 mb_c = (c << 8) + ptr[1];
4495 else
4496 {
4497 if (ptr[1] == NUL)
4498 {
4499 /* head byte at end of line */
4500 mb_l = 1;
4501 transchar_nonprint(extra, c);
4502 }
4503 else
4504 {
4505 /* illegal tail byte */
4506 mb_l = 2;
4507 STRCPY(extra, "XX");
4508 }
4509 p_extra = extra;
4510 n_extra = (int)STRLEN(extra) - 1;
4511 c_extra = NUL;
4512 c = *p_extra++;
4513 if (area_attr == 0 && search_attr == 0)
4514 {
4515 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004516 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 saved_attr2 = char_attr; /* save current attr */
4518 }
4519 mb_c = c;
4520 }
4521 }
4522 }
4523 /* If a double-width char doesn't fit display a '>' in the
4524 * last column; the character is displayed at the start of the
4525 * next line. */
4526 if ((
4527# ifdef FEAT_RIGHTLEFT
4528 wp->w_p_rl ? (col <= 0) :
4529# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004530 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 && (*mb_char2cells)(mb_c) == 2)
4532 {
4533 c = '>';
4534 mb_c = c;
4535 mb_utf8 = FALSE;
4536 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004537 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 /* Put pointer back so that the character will be
4539 * displayed at the start of the next line. */
4540 --ptr;
4541 }
4542 else if (*ptr != NUL)
4543 ptr += mb_l - 1;
4544
4545 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004546 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004547 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004548 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004551 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 c = ' ';
4553 if (area_attr == 0 && search_attr == 0)
4554 {
4555 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004556 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 saved_attr2 = char_attr; /* save current attr */
4558 }
4559 mb_c = c;
4560 mb_utf8 = FALSE;
4561 mb_l = 1;
4562 }
4563
4564 }
4565#endif
4566 ++ptr;
4567
4568 if (extra_check)
4569 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004570#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004571 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004572#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004573
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004574#ifdef FEAT_TERMINAL
4575 if (get_term_attr)
4576 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004577 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004578
4579 if (!attr_pri)
4580 char_attr = syntax_attr;
4581 else
4582 char_attr = hl_combine_attr(syntax_attr, char_attr);
4583 }
4584#endif
4585
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004586#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 /* Get syntax attribute, unless still at the start of the line
4588 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004589 v = (long)(ptr - line);
4590 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
4592 /* Get the syntax attribute for the character. If there
4593 * is an error, disable syntax highlighting. */
4594 save_did_emsg = did_emsg;
4595 did_emsg = FALSE;
4596
Bram Moolenaar217ad922005-03-20 22:37:15 +00004597 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004598# ifdef FEAT_SPELL
4599 has_spell ? &can_spell :
4600# endif
4601 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
4603 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004604 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004605 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004606 has_syntax = FALSE;
4607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 else
4609 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004610#ifdef SYN_TIME_LIMIT
4611 if (wp->w_s->b_syn_slow)
4612 has_syntax = FALSE;
4613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 /* Need to get the line again, a multi-line regexp may
4616 * have made it invalid. */
4617 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4618 ptr = line + v;
4619
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004620 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004622 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004623 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004624# ifdef FEAT_CONCEAL
4625 /* no concealing past the end of the line, it interferes
4626 * with line highlighting */
4627 if (c == NUL)
4628 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004629 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004630 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004633#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004634
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004635#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004636 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004637 * Only do this when there is no syntax highlighting, the
4638 * @Spell cluster is not used or the current syntax item
4639 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004640 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004641 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004642 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004643# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004644 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004645 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004646# endif
4647 if (c != 0 && (
4648# ifdef FEAT_SYN_HL
4649 !has_syntax ||
4650# endif
4651 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004652 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004653 char_u *prev_ptr, *p;
4654 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004655 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004656# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004657 if (has_mbyte)
4658 {
4659 prev_ptr = ptr - mb_l;
4660 v -= mb_l - 1;
4661 }
4662 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004663# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004664 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004665
4666 /* Use nextline[] if possible, it has the start of the
4667 * next line concatenated. */
4668 if ((prev_ptr - line) - nextlinecol >= 0)
4669 p = nextline + (prev_ptr - line) - nextlinecol;
4670 else
4671 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004672 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004673 len = spell_check(wp, p, &spell_hlf, &cap_col,
4674 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004675 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004676
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004677 /* In Insert mode only highlight a word that
4678 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004679 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004680 && (State & INSERT) != 0
4681 && wp->w_cursor.lnum == lnum
4682 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004683 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004684 && wp->w_cursor.col < (colnr_T)word_end)
4685 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004686 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004687 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004688 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004689
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004690 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004691 && (p - nextline) + len > nextline_idx)
4692 {
4693 /* Remember that the good word continues at the
4694 * start of the next line. */
4695 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004696 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004697 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004698
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004699 /* Turn index into actual attributes. */
4700 if (spell_hlf != HLF_COUNT)
4701 spell_attr = highlight_attr[spell_hlf];
4702
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004703 if (cap_col > 0)
4704 {
4705 if (p != prev_ptr
4706 && (p - nextline) + cap_col >= nextline_idx)
4707 {
4708 /* Remember that the word in the next line
4709 * must start with a capital. */
4710 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004711 cap_col = (int)((p - nextline) + cap_col
4712 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004713 }
4714 else
4715 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004716 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004717 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004718 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004719 }
4720 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004721 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004722 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004723 char_attr = hl_combine_attr(char_attr, spell_attr);
4724 else
4725 char_attr = hl_combine_attr(spell_attr, char_attr);
4726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727#endif
4728#ifdef FEAT_LINEBREAK
4729 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004730 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004732 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004733 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004735# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004736 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004737# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004738 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004740 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004742 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004743
Bram Moolenaar597a4222014-06-25 14:39:50 +02004744 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004745 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004746 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004747 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaara3650912014-11-19 13:21:57 +01004748 n_extra = (int)wp->w_buffer->b_p_ts
4749 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4750
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004751# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004752 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004753# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004755# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004756 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004757 {
4758#ifdef FEAT_CONCEAL
4759 if (c == TAB)
4760 /* See "Tab alignment" below. */
4761 FIX_FOR_BOGUSCOLS;
4762#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004763 if (!wp->w_p_list)
4764 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
4767#endif
4768
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004769 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4770 */
4771 if (wp->w_p_list
4772 && (((c == 160
4773#ifdef FEAT_MBYTE
4774 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4775#endif
4776 ) && lcs_nbsp)
4777 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4778 {
4779 c = (c == ' ') ? lcs_space : lcs_nbsp;
4780 if (area_attr == 0 && search_attr == 0)
4781 {
4782 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004783 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004784 saved_attr2 = char_attr; /* save current attr */
4785 }
4786#ifdef FEAT_MBYTE
4787 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004788 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004789 {
4790 mb_utf8 = TRUE;
4791 u8cc[0] = 0;
4792 c = 0xc0;
4793 }
4794 else
4795 mb_utf8 = FALSE;
4796#endif
4797 }
4798
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4800 {
4801 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004802 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
4804 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004805 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 saved_attr2 = char_attr; /* save current attr */
4807 }
4808#ifdef FEAT_MBYTE
4809 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004810 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 {
4812 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004813 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004814 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816 else
4817 mb_utf8 = FALSE;
4818#endif
4819 }
4820 }
4821
4822 /*
4823 * Handling of non-printable characters.
4824 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004825 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 {
4827 /*
4828 * when getting a character from the file, we may have to
4829 * turn it into something else on the way to putting it
4830 * into "ScreenLines".
4831 */
4832 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4833 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004834 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004835 long vcol_adjusted = vcol; /* removed showbreak length */
4836#ifdef FEAT_LINEBREAK
4837 /* only adjust the tab_len, when at the first column
4838 * after the showbreak value was drawn */
4839 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4840 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004843 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004844 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4845
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004846#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004847 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004848#endif
4849 /* tab amount depends on current column */
4850 n_extra = tab_len;
4851#ifdef FEAT_LINEBREAK
4852 else
4853 {
4854 char_u *p;
4855 int len = n_extra;
4856 int i;
4857 int saved_nextra = n_extra;
4858
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004859#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004860 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004861 /* there are characters to conceal */
4862 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004863 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4864 */
4865 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4866 && n_extra > tab_len)
4867 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004868#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004869
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004870 /* if n_extra > 0, it gives the number of chars, to
4871 * use for a tab, else we need to calculate the width
4872 * for a tab */
4873#ifdef FEAT_MBYTE
4874 len = (tab_len * mb_char2len(lcs_tab2));
4875 if (n_extra > 0)
4876 len += n_extra - tab_len;
4877#endif
4878 c = lcs_tab1;
4879 p = alloc((unsigned)(len + 1));
4880 vim_memset(p, ' ', len);
4881 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004882 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004883 p_extra_free = p;
4884 for (i = 0; i < tab_len; i++)
4885 {
4886#ifdef FEAT_MBYTE
4887 mb_char2bytes(lcs_tab2, p);
4888 p += mb_char2len(lcs_tab2);
4889 n_extra += mb_char2len(lcs_tab2)
4890 - (saved_nextra > 0 ? 1 : 0);
4891#else
4892 p[i] = lcs_tab2;
4893#endif
4894 }
4895 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004896#ifdef FEAT_CONCEAL
4897 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4898 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004899 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004900 n_extra -= vcol_off;
4901#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004902 }
4903#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004904#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004905 {
4906 int vc_saved = vcol_off;
4907
4908 /* Tab alignment should be identical regardless of
4909 * 'conceallevel' value. So tab compensates of all
4910 * previous concealed characters, and thus resets
4911 * vcol_off and boguscols accumulated so far in the
4912 * line. Note that the tab can be longer than
4913 * 'tabstop' when there are concealed characters. */
4914 FIX_FOR_BOGUSCOLS;
4915
4916 /* Make sure, the highlighting for the tab char will be
4917 * correctly set further below (effectively reverts the
4918 * FIX_FOR_BOGSUCOLS macro */
4919 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004920 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004921 tab_len += vc_saved;
4922 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924#ifdef FEAT_MBYTE
4925 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4926#endif
4927 if (wp->w_p_list)
4928 {
4929 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004930#ifdef FEAT_LINEBREAK
4931 if (wp->w_p_lbr)
4932 c_extra = NUL; /* using p_extra from above */
4933 else
4934#endif
4935 c_extra = lcs_tab2;
4936 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004937 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 saved_attr2 = char_attr; /* save current attr */
4939#ifdef FEAT_MBYTE
4940 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004941 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
4943 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004944 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004945 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
4947#endif
4948 }
4949 else
4950 {
4951 c_extra = ' ';
4952 c = ' ';
4953 }
4954 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004955 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004956 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004957 || ((fromcol >= 0 || fromcol_prev >= 0)
4958 && tocol > vcol
4959 && VIsual_mode != Ctrl_V
4960 && (
4961# ifdef FEAT_RIGHTLEFT
4962 wp->w_p_rl ? (col >= 0) :
4963# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004964 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004965 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004966 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004967 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004968 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004970 /* Display a '$' after the line or highlight an extra
4971 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4973 /* For a diff line the highlighting continues after the
4974 * "$". */
4975 if (
4976# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004977 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978# ifdef LINE_ATTR
4979 &&
4980# endif
4981# endif
4982# ifdef LINE_ATTR
4983 line_attr == 0
4984# endif
4985 )
4986#endif
4987 {
4988#ifdef FEAT_VIRTUALEDIT
4989 /* In virtualedit, visual selections may extend
4990 * beyond end of line. */
4991 if (area_highlighting && virtual_active()
4992 && tocol != MAXCOL && vcol < tocol)
4993 n_extra = 0;
4994 else
4995#endif
4996 {
4997 p_extra = at_end_str;
4998 n_extra = 1;
4999 c_extra = NUL;
5000 }
5001 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005002 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005003 c = lcs_eol;
5004 else
5005 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 lcs_eol_one = -1;
5007 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005008 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005010 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 n_attr = 1;
5012 }
5013#ifdef FEAT_MBYTE
5014 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005015 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005018 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005019 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
5021 else
5022 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5023#endif
5024 }
5025 else if (c != NUL)
5026 {
5027 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005028 if (n_extra == 0)
5029 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030#ifdef FEAT_RIGHTLEFT
5031 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5032 rl_mirror(p_extra); /* reverse "<12>" */
5033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005035#ifdef FEAT_LINEBREAK
5036 if (wp->w_p_lbr)
5037 {
5038 char_u *p;
5039
5040 c = *p_extra;
5041 p = alloc((unsigned)n_extra + 1);
5042 vim_memset(p, ' ', n_extra);
5043 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5044 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005045 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005046 p_extra_free = p_extra = p;
5047 }
5048 else
5049#endif
5050 {
5051 n_extra = byte2cells(c) - 1;
5052 c = *p_extra++;
5053 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005054 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
5056 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005057 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 saved_attr2 = char_attr; /* save current attr */
5059 }
5060#ifdef FEAT_MBYTE
5061 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5062#endif
5063 }
5064#ifdef FEAT_VIRTUALEDIT
5065 else if (VIsual_active
5066 && (VIsual_mode == Ctrl_V
5067 || VIsual_mode == 'v')
5068 && virtual_active()
5069 && tocol != MAXCOL
5070 && vcol < tocol
5071 && (
5072# ifdef FEAT_RIGHTLEFT
5073 wp->w_p_rl ? (col >= 0) :
5074# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005075 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 {
5077 c = ' ';
5078 --ptr; /* put it back at the NUL */
5079 }
5080#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005081#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 else if ((
5083# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005084 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005086# ifdef FEAT_TERMINAL
5087 term_attr != 0 ||
5088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 ) && (
5091# ifdef FEAT_RIGHTLEFT
5092 wp->w_p_rl ? (col >= 0) :
5093# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005094 (col
5095# ifdef FEAT_CONCEAL
5096 - boguscols
5097# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005098 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
5100 /* Highlight until the right side of the window */
5101 c = ' ';
5102 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005103
5104 /* Remember we do the char for line highlighting. */
5105 ++did_line_attr;
5106
5107 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005108 if (line_attr != 0 && char_attr == search_attr
5109 && (did_line_attr > 1
5110 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005111 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112# ifdef FEAT_DIFF
5113 if (diff_hlf == HLF_TXD)
5114 {
5115 diff_hlf = HLF_CHD;
5116 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005117 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005118 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005119 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5120 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005121 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005125# ifdef FEAT_TERMINAL
5126 if (term_attr != 0)
5127 {
5128 char_attr = term_attr;
5129 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5130 char_attr = hl_combine_attr(char_attr,
5131 HL_ATTR(HLF_CUL));
5132 }
5133# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
5135#endif
5136 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005137
5138#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005139 if ( wp->w_p_cole > 0
5140 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005141 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005142 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005143 && !(lnum_in_visual_area
5144 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005145 {
5146 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005147 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005148 && (syn_get_sub_char() != NUL || match_conc
5149 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005150 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005151 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005152 /* First time at this concealed item: display one
5153 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005154 if (match_conc)
5155 c = match_conc;
5156 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005157 c = syn_get_sub_char();
5158 else if (lcs_conceal != NUL)
5159 c = lcs_conceal;
5160 else
5161 c = ' ';
5162
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005163 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005164
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005165 if (n_extra > 0)
5166 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005167 vcol += n_extra;
5168 if (wp->w_p_wrap && n_extra > 0)
5169 {
5170# ifdef FEAT_RIGHTLEFT
5171 if (wp->w_p_rl)
5172 {
5173 col -= n_extra;
5174 boguscols -= n_extra;
5175 }
5176 else
5177# endif
5178 {
5179 boguscols += n_extra;
5180 col += n_extra;
5181 }
5182 }
5183 n_extra = 0;
5184 n_attr = 0;
5185 }
5186 else if (n_skip == 0)
5187 {
5188 is_concealing = TRUE;
5189 n_skip = 1;
5190 }
5191# ifdef FEAT_MBYTE
5192 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005193 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005194 {
5195 mb_utf8 = TRUE;
5196 u8cc[0] = 0;
5197 c = 0xc0;
5198 }
5199 else
5200 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5201# endif
5202 }
5203 else
5204 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005205 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005206 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005207 }
5208#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005211#ifdef FEAT_CONCEAL
5212 /* In the cursor line and we may be concealing characters: correct
5213 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005214 if (!did_wcol && draw_state == WL_LINE
5215 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005216 && conceal_cursor_line(wp)
5217 && (int)wp->w_virtcol <= vcol + n_skip)
5218 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005219# ifdef FEAT_RIGHTLEFT
5220 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005221 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005222 else
5223# endif
5224 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005225 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005226 did_wcol = TRUE;
5227 }
5228#endif
5229
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 /* Don't override visual selection highlighting. */
5231 if (n_attr > 0
5232 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005233 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 char_attr = extra_attr;
5235
Bram Moolenaar81695252004-12-29 20:58:21 +00005236#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 /* XIM don't send preedit_start and preedit_end, but they send
5238 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5239 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005240 if (p_imst == IM_ON_THE_SPOT
5241 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005242 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 && (State & INSERT)
5244 && !p_imdisable
5245 && im_is_preediting()
5246 && draw_state == WL_LINE)
5247 {
5248 colnr_T tcol;
5249
5250 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005251 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 else
5253 tcol = preedit_end_col;
5254 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5255 {
5256 if (feedback_old_attr < 0)
5257 {
5258 feedback_col = 0;
5259 feedback_old_attr = char_attr;
5260 }
5261 char_attr = im_get_feedback_attr(feedback_col);
5262 if (char_attr < 0)
5263 char_attr = feedback_old_attr;
5264 feedback_col++;
5265 }
5266 else if (feedback_old_attr >= 0)
5267 {
5268 char_attr = feedback_old_attr;
5269 feedback_old_attr = -1;
5270 feedback_col = 0;
5271 }
5272 }
5273#endif
5274 /*
5275 * Handle the case where we are in column 0 but not on the first
5276 * character of the line and the user wants us to show us a
5277 * special character (via 'listchars' option "precedes:<char>".
5278 */
5279 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005280 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5282#ifdef FEAT_DIFF
5283 && filler_todo <= 0
5284#endif
5285 && draw_state > WL_NR
5286 && c != NUL)
5287 {
5288 c = lcs_prec;
5289 lcs_prec_todo = NUL;
5290#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005291 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5292 {
5293 /* Double-width character being overwritten by the "precedes"
5294 * character, need to fill up half the character. */
5295 c_extra = MB_FILLER_CHAR;
5296 n_extra = 1;
5297 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005298 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005301 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
5303 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005304 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005305 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 }
5307 else
5308 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5309#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005310 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 {
5312 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005313 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 n_attr3 = 1;
5315 }
5316 }
5317
5318 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005319 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005321 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005322#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005323 || did_line_attr == 1
5324#endif
5325 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005327#ifdef FEAT_SEARCH_EXTRA
5328 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005329
5330 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005331 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005332 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005333#endif
5334
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005335 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 * highlight match at end of line. If it's beyond the last
5337 * char on the screen, just overwrite that one (tricky!) Not
5338 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005339#ifdef FEAT_SEARCH_EXTRA
5340 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005341 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005342 prevcol_hl_flag = TRUE;
5343 else
5344 {
5345 cur = wp->w_match_head;
5346 while (cur != NULL)
5347 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005348 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005349 {
5350 prevcol_hl_flag = TRUE;
5351 break;
5352 }
5353 cur = cur->next;
5354 }
5355 }
5356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005358 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005359 && (VIsual_mode != Ctrl_V
5360 || lnum == VIsual.lnum
5361 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005362 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363#ifdef FEAT_SEARCH_EXTRA
5364 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005365 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005366# ifdef FEAT_SYN_HL
5367 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5368 && !(wp == curwin && VIsual_active))
5369# endif
5370# ifdef FEAT_DIFF
5371 && diff_hlf == (hlf_T)0
5372# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005373# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005374 && did_line_attr <= 1
5375# endif
5376 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377#endif
5378 ))
5379 {
5380 int n = 0;
5381
5382#ifdef FEAT_RIGHTLEFT
5383 if (wp->w_p_rl)
5384 {
5385 if (col < 0)
5386 n = 1;
5387 }
5388 else
5389#endif
5390 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005391 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 n = -1;
5393 }
5394 if (n != 0)
5395 {
5396 /* At the window boundary, highlight the last character
5397 * instead (better than nothing). */
5398 off += n;
5399 col += n;
5400 }
5401 else
5402 {
5403 /* Add a blank character to highlight. */
5404 ScreenLines[off] = ' ';
5405#ifdef FEAT_MBYTE
5406 if (enc_utf8)
5407 ScreenLinesUC[off] = 0;
5408#endif
5409 }
5410#ifdef FEAT_SEARCH_EXTRA
5411 if (area_attr == 0)
5412 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005413 /* Use attributes from match with highest priority among
5414 * 'search_hl' and the match list. */
5415 char_attr = search_hl.attr;
5416 cur = wp->w_match_head;
5417 shl_flag = FALSE;
5418 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005419 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005420 if (shl_flag == FALSE
5421 && ((cur != NULL
5422 && cur->priority > SEARCH_HL_PRIORITY)
5423 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005424 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005425 shl = &search_hl;
5426 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005427 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005428 else
5429 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005430 if ((ptr - line) - 1 == (long)shl->startcol
5431 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005432 char_attr = shl->attr;
5433 if (shl != &search_hl && cur != NULL)
5434 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 }
5437#endif
5438 ScreenAttrs[off] = char_attr;
5439#ifdef FEAT_RIGHTLEFT
5440 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005443 --off;
5444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 else
5446#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005449 ++off;
5450 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005451 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005452#ifdef FEAT_SYN_HL
5453 eol_hl_off = 1;
5454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457
Bram Moolenaar91170f82006-05-05 21:15:17 +00005458 /*
5459 * At end of the text line.
5460 */
5461 if (c == NUL)
5462 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005463#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005464 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5465 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005466 {
5467 /* highlight last char after line */
5468 --col;
5469 --off;
5470 --vcol;
5471 }
5472
Bram Moolenaar1a384422010-07-14 19:53:30 +02005473 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005474 if (wp->w_p_wrap)
5475 v = wp->w_skipcol;
5476 else
5477 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005478
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005479 /* check if line ends before left margin */
5480 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005481 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005482#ifdef FEAT_CONCEAL
5483 /* Get rid of the boguscols now, we want to draw until the right
5484 * edge for 'cursorcolumn'. */
5485 col -= boguscols;
5486 boguscols = 0;
5487#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005488
5489 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005490 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005491
5492 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005493 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5494 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005495 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005496 && lnum != wp->w_cursor.lnum)
5497 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005498# ifdef FEAT_RIGHTLEFT
5499 && !wp->w_p_rl
5500# endif
5501 )
5502 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005503 int rightmost_vcol = 0;
5504 int i;
5505
5506 if (wp->w_p_cuc)
5507 rightmost_vcol = wp->w_virtcol;
5508 if (draw_color_col)
5509 /* determine rightmost colorcolumn to possibly draw */
5510 for (i = 0; color_cols[i] >= 0; ++i)
5511 if (rightmost_vcol < color_cols[i])
5512 rightmost_vcol = color_cols[i];
5513
Bram Moolenaar02631462017-09-22 15:20:32 +02005514 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005515 {
5516 ScreenLines[off] = ' ';
5517#ifdef FEAT_MBYTE
5518 if (enc_utf8)
5519 ScreenLinesUC[off] = 0;
5520#endif
5521 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005522 if (draw_color_col)
5523 draw_color_col = advance_color_col(VCOL_HLC,
5524 &color_cols);
5525
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005526 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005527 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005528 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005529 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005530 else
5531 ScreenAttrs[off++] = 0;
5532
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005533 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005534 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005535
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005536 ++vcol;
5537 }
5538 }
5539#endif
5540
Bram Moolenaar53f81742017-09-22 14:35:51 +02005541 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005542 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 row++;
5544
5545 /*
5546 * Update w_cline_height and w_cline_folded if the cursor line was
5547 * updated (saves a call to plines() later).
5548 */
5549 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5550 {
5551 curwin->w_cline_row = startrow;
5552 curwin->w_cline_height = row - startrow;
5553#ifdef FEAT_FOLDING
5554 curwin->w_cline_folded = FALSE;
5555#endif
5556 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5557 }
5558
5559 break;
5560 }
5561
5562 /* line continues beyond line end */
5563 if (lcs_ext
5564 && !wp->w_p_wrap
5565#ifdef FEAT_DIFF
5566 && filler_todo <= 0
5567#endif
5568 && (
5569#ifdef FEAT_RIGHTLEFT
5570 wp->w_p_rl ? col == 0 :
5571#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005572 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005574 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5576 {
5577 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005578 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579#ifdef FEAT_MBYTE
5580 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005581 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 {
5583 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005584 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005585 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587 else
5588 mb_utf8 = FALSE;
5589#endif
5590 }
5591
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005592#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005593 /* advance to the next 'colorcolumn' */
5594 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005595 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005596
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005597 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005598 * highlight the cursor position itself.
5599 * Also highlight the 'colorcolumn' if it is different than
5600 * 'cursorcolumn' */
5601 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005602 if (draw_state == WL_LINE && !lnum_in_visual_area
5603 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005604 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005605 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005606 && lnum != wp->w_cursor.lnum)
5607 {
5608 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005609 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005610 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005611 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005612 {
5613 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005614 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005615 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005616 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005617#endif
5618
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 /*
5620 * Store character to be displayed.
5621 * Skip characters that are left of the screen for 'nowrap'.
5622 */
5623 vcol_prev = vcol;
5624 if (draw_state < WL_LINE || n_skip <= 0)
5625 {
5626 /*
5627 * Store the character.
5628 */
5629#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5630 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5631 {
5632 /* A double-wide character is: put first halve in left cell. */
5633 --off;
5634 --col;
5635 }
5636#endif
5637 ScreenLines[off] = c;
5638#ifdef FEAT_MBYTE
5639 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005640 {
5641 if ((mb_c & 0xff00) == 0x8e00)
5642 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 else if (enc_utf8)
5646 {
5647 if (mb_utf8)
5648 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005649 int i;
5650
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005652 if ((c & 0xff) == 0)
5653 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005654 for (i = 0; i < Screen_mco; ++i)
5655 {
5656 ScreenLinesC[i][off] = u8cc[i];
5657 if (u8cc[i] == 0)
5658 break;
5659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
5661 else
5662 ScreenLinesUC[off] = 0;
5663 }
5664 if (multi_attr)
5665 {
5666 ScreenAttrs[off] = multi_attr;
5667 multi_attr = 0;
5668 }
5669 else
5670#endif
5671 ScreenAttrs[off] = char_attr;
5672
5673#ifdef FEAT_MBYTE
5674 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5675 {
5676 /* Need to fill two screen columns. */
5677 ++off;
5678 ++col;
5679 if (enc_utf8)
5680 /* UTF-8: Put a 0 in the second screen char. */
5681 ScreenLines[off] = 0;
5682 else
5683 /* DBCS: Put second byte in the second screen char. */
5684 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005685 if (draw_state > WL_NR
5686#ifdef FEAT_DIFF
5687 && filler_todo <= 0
5688#endif
5689 )
5690 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 /* When "tocol" is halfway a character, set it to the end of
5692 * the character, otherwise highlighting won't stop. */
5693 if (tocol == vcol)
5694 ++tocol;
5695#ifdef FEAT_RIGHTLEFT
5696 if (wp->w_p_rl)
5697 {
5698 /* now it's time to backup one cell */
5699 --off;
5700 --col;
5701 }
5702#endif
5703 }
5704#endif
5705#ifdef FEAT_RIGHTLEFT
5706 if (wp->w_p_rl)
5707 {
5708 --off;
5709 --col;
5710 }
5711 else
5712#endif
5713 {
5714 ++off;
5715 ++col;
5716 }
5717 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005718#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005719 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005720 {
5721 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005722 ++vcol_off;
5723 if (n_extra > 0)
5724 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005725 if (wp->w_p_wrap)
5726 {
5727 /*
5728 * Special voodoo required if 'wrap' is on.
5729 *
5730 * Advance the column indicator to force the line
5731 * drawing to wrap early. This will make the line
5732 * take up the same screen space when parts are concealed,
5733 * so that cursor line computations aren't messed up.
5734 *
5735 * To avoid the fictitious advance of 'col' causing
5736 * trailing junk to be written out of the screen line
5737 * we are building, 'boguscols' keeps track of the number
5738 * of bad columns we have advanced.
5739 */
5740 if (n_extra > 0)
5741 {
5742 vcol += n_extra;
5743# ifdef FEAT_RIGHTLEFT
5744 if (wp->w_p_rl)
5745 {
5746 col -= n_extra;
5747 boguscols -= n_extra;
5748 }
5749 else
5750# endif
5751 {
5752 col += n_extra;
5753 boguscols += n_extra;
5754 }
5755 n_extra = 0;
5756 n_attr = 0;
5757 }
5758
5759
5760# ifdef FEAT_MBYTE
5761 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5762 {
5763 /* Need to fill two screen columns. */
5764# ifdef FEAT_RIGHTLEFT
5765 if (wp->w_p_rl)
5766 {
5767 --boguscols;
5768 --col;
5769 }
5770 else
5771# endif
5772 {
5773 ++boguscols;
5774 ++col;
5775 }
5776 }
5777# endif
5778
5779# ifdef FEAT_RIGHTLEFT
5780 if (wp->w_p_rl)
5781 {
5782 --boguscols;
5783 --col;
5784 }
5785 else
5786# endif
5787 {
5788 ++boguscols;
5789 ++col;
5790 }
5791 }
5792 else
5793 {
5794 if (n_extra > 0)
5795 {
5796 vcol += n_extra;
5797 n_extra = 0;
5798 n_attr = 0;
5799 }
5800 }
5801
5802 }
5803#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 else
5805 --n_skip;
5806
Bram Moolenaar64486672010-05-16 15:46:46 +02005807 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5808 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005809 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810#ifdef FEAT_DIFF
5811 && filler_todo <= 0
5812#endif
5813 )
5814 ++vcol;
5815
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005816#ifdef FEAT_SYN_HL
5817 if (vcol_save_attr >= 0)
5818 char_attr = vcol_save_attr;
5819#endif
5820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 /* restore attributes after "predeces" in 'listchars' */
5822 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5823 char_attr = saved_attr3;
5824
5825 /* restore attributes after last 'listchars' or 'number' char */
5826 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5827 char_attr = saved_attr2;
5828
5829 /*
5830 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005831 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 */
5833 if ((
5834#ifdef FEAT_RIGHTLEFT
5835 wp->w_p_rl ? (col < 0) :
5836#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005837 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 && (*ptr != NUL
5839#ifdef FEAT_DIFF
5840 || filler_todo > 0
5841#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005842 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5844 )
5845 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005846#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005847 screen_line(screen_row, wp->w_wincol, col - boguscols,
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 boguscols = 0;
5850#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005851 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005852 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 ++row;
5855 ++screen_row;
5856
5857 /* When not wrapping and finished diff lines, or when displayed
5858 * '$' and highlighting until last column, break here. */
5859 if ((!wp->w_p_wrap
5860#ifdef FEAT_DIFF
5861 && filler_todo <= 0
5862#endif
5863 ) || lcs_eol_one == -1)
5864 break;
5865
5866 /* When the window is too narrow draw all "@" lines. */
5867 if (draw_state != WL_LINE
5868#ifdef FEAT_DIFF
5869 && filler_todo <= 0
5870#endif
5871 )
5872 {
5873 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 row = endrow;
5876 }
5877
5878 /* When line got too long for screen break here. */
5879 if (row == endrow)
5880 {
5881 ++row;
5882 break;
5883 }
5884
5885 if (screen_cur_row == screen_row - 1
5886#ifdef FEAT_DIFF
5887 && filler_todo <= 0
5888#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005889 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 {
5891 /* Remember that the line wraps, used for modeless copy. */
5892 LineWraps[screen_row - 1] = TRUE;
5893
5894 /*
5895 * Special trick to make copy/paste of wrapped lines work with
5896 * xterm/screen: write an extra character beyond the end of
5897 * the line. This will work with all terminal types
5898 * (regardless of the xn,am settings).
5899 * Only do this on a fast tty.
5900 * Only do this if the cursor is on the current line
5901 * (something has been written in it).
5902 * Don't do this for the GUI.
5903 * Don't do this for double-width characters.
5904 * Don't do this for a window not at the right screen border.
5905 */
5906 if (p_tf
5907#ifdef FEAT_GUI
5908 && !gui.in_use
5909#endif
5910#ifdef FEAT_MBYTE
5911 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005912 && ((*mb_off2cells)(LineOffset[screen_row],
5913 LineOffset[screen_row] + screen_Columns)
5914 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005916 + (int)Columns - 2,
5917 LineOffset[screen_row] + screen_Columns)
5918 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919#endif
5920 )
5921 {
5922 /* First make sure we are at the end of the screen line,
5923 * then output the same character again to let the
5924 * terminal know about the wrap. If the terminal doesn't
5925 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005926 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 screen_char(LineOffset[screen_row - 1]
5928 + (unsigned)Columns - 1,
5929 screen_row - 1, (int)(Columns - 1));
5930
5931#ifdef FEAT_MBYTE
5932 /* When there is a multi-byte character, just output a
5933 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005934 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5935 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 out_char(' ');
5937 else
5938#endif
5939 out_char(ScreenLines[LineOffset[screen_row - 1]
5940 + (Columns - 1)]);
5941 /* force a redraw of the first char on the next line */
5942 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5943 screen_start(); /* don't know where cursor is now */
5944 }
5945 }
5946
5947 col = 0;
5948 off = (unsigned)(current_ScreenLine - ScreenLines);
5949#ifdef FEAT_RIGHTLEFT
5950 if (wp->w_p_rl)
5951 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005952 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 off += col;
5954 }
5955#endif
5956
5957 /* reset the drawing state for the start of a wrapped line */
5958 draw_state = WL_START;
5959 saved_n_extra = n_extra;
5960 saved_p_extra = p_extra;
5961 saved_c_extra = c_extra;
5962 saved_char_attr = char_attr;
5963 n_extra = 0;
5964 lcs_prec_todo = lcs_prec;
5965#ifdef FEAT_LINEBREAK
5966# ifdef FEAT_DIFF
5967 if (filler_todo <= 0)
5968# endif
5969 need_showbreak = TRUE;
5970#endif
5971#ifdef FEAT_DIFF
5972 --filler_todo;
5973 /* When the filler lines are actually below the last line of the
5974 * file, don't draw the line itself, break here. */
5975 if (filler_todo == 0 && wp->w_botfill)
5976 break;
5977#endif
5978 }
5979
5980 } /* for every character in the line */
5981
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005982#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005983 /* After an empty line check first word for capital. */
5984 if (*skipwhite(line) == NUL)
5985 {
5986 capcol_lnum = lnum + 1;
5987 cap_col = 0;
5988 }
5989#endif
5990
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005991 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 return row;
5993}
5994
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005995#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005996static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005997
5998/*
5999 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006000 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006001 */
6002 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006003comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006004{
6005 int i;
6006
6007 for (i = 0; i < Screen_mco; ++i)
6008 {
6009 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6010 return TRUE;
6011 if (ScreenLinesC[i][off_from] == 0)
6012 break;
6013 }
6014 return FALSE;
6015}
6016#endif
6017
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018/*
6019 * Check whether the given character needs redrawing:
6020 * - the (first byte of the) character is different
6021 * - the attributes are different
6022 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006023 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 */
6025 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006026char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027{
6028 if (cols > 0
6029 && ((ScreenLines[off_from] != ScreenLines[off_to]
6030 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6031
6032#ifdef FEAT_MBYTE
6033 || (enc_dbcs != 0
6034 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6035 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6036 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6037 : (cols > 1 && ScreenLines[off_from + 1]
6038 != ScreenLines[off_to + 1])))
6039 || (enc_utf8
6040 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6041 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006042 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006043 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6044 && ScreenLines[off_from + 1]
6045 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046#endif
6047 ))
6048 return TRUE;
6049 return FALSE;
6050}
6051
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006052#if defined(FEAT_TERMINAL) || defined(PROTO)
6053/*
6054 * Return the index in ScreenLines[] for the current screen line.
6055 */
6056 int
6057screen_get_current_line_off()
6058{
6059 return (int)(current_ScreenLine - ScreenLines);
6060}
6061#endif
6062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063/*
6064 * Move one "cooked" screen line to the screen, but only the characters that
6065 * have actually changed. Handle insert/delete character.
6066 * "coloff" gives the first column on the screen for this line.
6067 * "endcol" gives the columns where valid characters are.
6068 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6069 * needs to be cleared, negative otherwise.
6070 * "rlflag" is TRUE in a rightleft window:
6071 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6072 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6073 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006074 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006075screen_line(
6076 int row,
6077 int coloff,
6078 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006079 int clear_width,
6080 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081{
6082 unsigned off_from;
6083 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006084#ifdef FEAT_MBYTE
6085 unsigned max_off_from;
6086 unsigned max_off_to;
6087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 int force = FALSE; /* force update rest of the line */
6091 int redraw_this /* bool: does character need redraw? */
6092#ifdef FEAT_GUI
6093 = TRUE /* For GUI when while-loop empty */
6094#endif
6095 ;
6096 int redraw_next; /* redraw_this for next character */
6097#ifdef FEAT_MBYTE
6098 int clear_next = FALSE;
6099 int char_cells; /* 1: normal char */
6100 /* 2: occupies two display cells */
6101# define CHAR_CELLS char_cells
6102#else
6103# define CHAR_CELLS 1
6104#endif
6105
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006106 /* Check for illegal row and col, just in case. */
6107 if (row >= Rows)
6108 row = Rows - 1;
6109 if (endcol > Columns)
6110 endcol = Columns;
6111
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112# ifdef FEAT_CLIPBOARD
6113 clip_may_clear_selection(row, row);
6114# endif
6115
6116 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6117 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006118#ifdef FEAT_MBYTE
6119 max_off_from = off_from + screen_Columns;
6120 max_off_to = LineOffset[row] + screen_Columns;
6121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122
6123#ifdef FEAT_RIGHTLEFT
6124 if (rlflag)
6125 {
6126 /* Clear rest first, because it's left of the text. */
6127 if (clear_width > 0)
6128 {
6129 while (col <= endcol && ScreenLines[off_to] == ' '
6130 && ScreenAttrs[off_to] == 0
6131# ifdef FEAT_MBYTE
6132 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6133# endif
6134 )
6135 {
6136 ++off_to;
6137 ++col;
6138 }
6139 if (col <= endcol)
6140 screen_fill(row, row + 1, col + coloff,
6141 endcol + coloff + 1, ' ', ' ', 0);
6142 }
6143 col = endcol + 1;
6144 off_to = LineOffset[row] + col + coloff;
6145 off_from += col;
6146 endcol = (clear_width > 0 ? clear_width : -clear_width);
6147 }
6148#endif /* FEAT_RIGHTLEFT */
6149
6150 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6151
6152 while (col < endcol)
6153 {
6154#ifdef FEAT_MBYTE
6155 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006156 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 else
6158 char_cells = 1;
6159#endif
6160
6161 redraw_this = redraw_next;
6162 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6163 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6164
6165#ifdef FEAT_GUI
6166 /* If the next character was bold, then redraw the current character to
6167 * remove any pixels that might have spilt over into us. This only
6168 * happens in the GUI.
6169 */
6170 if (redraw_next && gui.in_use)
6171 {
6172 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006173 if (hl > HL_ALL)
6174 hl = syn_attr2attr(hl);
6175 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 redraw_this = TRUE;
6177 }
6178#endif
6179
6180 if (redraw_this)
6181 {
6182 /*
6183 * Special handling when 'xs' termcap flag set (hpterm):
6184 * Attributes for characters are stored at the position where the
6185 * cursor is when writing the highlighting code. The
6186 * start-highlighting code must be written with the cursor on the
6187 * first highlighted character. The stop-highlighting code must
6188 * be written with the cursor just after the last highlighted
6189 * character.
6190 * Overwriting a character doesn't remove it's highlighting. Need
6191 * to clear the rest of the line, and force redrawing it
6192 * completely.
6193 */
6194 if ( p_wiv
6195 && !force
6196#ifdef FEAT_GUI
6197 && !gui.in_use
6198#endif
6199 && ScreenAttrs[off_to] != 0
6200 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6201 {
6202 /*
6203 * Need to remove highlighting attributes here.
6204 */
6205 windgoto(row, col + coloff);
6206 out_str(T_CE); /* clear rest of this screen line */
6207 screen_start(); /* don't know where cursor is now */
6208 force = TRUE; /* force redraw of rest of the line */
6209 redraw_next = TRUE; /* or else next char would miss out */
6210
6211 /*
6212 * If the previous character was highlighted, need to stop
6213 * highlighting at this character.
6214 */
6215 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6216 {
6217 screen_attr = ScreenAttrs[off_to - 1];
6218 term_windgoto(row, col + coloff);
6219 screen_stop_highlight();
6220 }
6221 else
6222 screen_attr = 0; /* highlighting has stopped */
6223 }
6224#ifdef FEAT_MBYTE
6225 if (enc_dbcs != 0)
6226 {
6227 /* Check if overwriting a double-byte with a single-byte or
6228 * the other way around requires another character to be
6229 * redrawn. For UTF-8 this isn't needed, because comparing
6230 * ScreenLinesUC[] is sufficient. */
6231 if (char_cells == 1
6232 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006233 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 {
6235 /* Writing a single-cell character over a double-cell
6236 * character: need to redraw the next cell. */
6237 ScreenLines[off_to + 1] = 0;
6238 redraw_next = TRUE;
6239 }
6240 else if (char_cells == 2
6241 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006242 && (*mb_off2cells)(off_to, max_off_to) == 1
6243 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 {
6245 /* Writing the second half of a double-cell character over
6246 * a double-cell character: need to redraw the second
6247 * cell. */
6248 ScreenLines[off_to + 2] = 0;
6249 redraw_next = TRUE;
6250 }
6251
6252 if (enc_dbcs == DBCS_JPNU)
6253 ScreenLines2[off_to] = ScreenLines2[off_from];
6254 }
6255 /* When writing a single-width character over a double-width
6256 * character and at the end of the redrawn text, need to clear out
6257 * the right halve of the old character.
6258 * Also required when writing the right halve of a double-width
6259 * char over the left halve of an existing one. */
6260 if (has_mbyte && col + char_cells == endcol
6261 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006262 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006264 && (*mb_off2cells)(off_to, max_off_to) == 1
6265 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 clear_next = TRUE;
6267#endif
6268
6269 ScreenLines[off_to] = ScreenLines[off_from];
6270#ifdef FEAT_MBYTE
6271 if (enc_utf8)
6272 {
6273 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6274 if (ScreenLinesUC[off_from] != 0)
6275 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006276 int i;
6277
6278 for (i = 0; i < Screen_mco; ++i)
6279 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
6281 }
6282 if (char_cells == 2)
6283 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6284#endif
6285
6286#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006287 /* The bold trick makes a single column of pixels appear in the
6288 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 * character should be redrawn too. This happens for our own GUI
6290 * and for some xterms. */
6291 if (
6292# ifdef FEAT_GUI
6293 gui.in_use
6294# endif
6295# if defined(FEAT_GUI) && defined(UNIX)
6296 ||
6297# endif
6298# ifdef UNIX
6299 term_is_xterm
6300# endif
6301 )
6302 {
6303 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006304 if (hl > HL_ALL)
6305 hl = syn_attr2attr(hl);
6306 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 redraw_next = TRUE;
6308 }
6309#endif
6310 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6311#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006312 /* For simplicity set the attributes of second half of a
6313 * double-wide character equal to the first half. */
6314 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006316
6317 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 else
6320#endif
6321 screen_char(off_to, row, col + coloff);
6322 }
6323 else if ( p_wiv
6324#ifdef FEAT_GUI
6325 && !gui.in_use
6326#endif
6327 && col + coloff > 0)
6328 {
6329 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6330 {
6331 /*
6332 * Don't output stop-highlight when moving the cursor, it will
6333 * stop the highlighting when it should continue.
6334 */
6335 screen_attr = 0;
6336 }
6337 else if (screen_attr != 0)
6338 screen_stop_highlight();
6339 }
6340
6341 off_to += CHAR_CELLS;
6342 off_from += CHAR_CELLS;
6343 col += CHAR_CELLS;
6344 }
6345
6346#ifdef FEAT_MBYTE
6347 if (clear_next)
6348 {
6349 /* Clear the second half of a double-wide character of which the left
6350 * half was overwritten with a single-wide character. */
6351 ScreenLines[off_to] = ' ';
6352 if (enc_utf8)
6353 ScreenLinesUC[off_to] = 0;
6354 screen_char(off_to, row, col + coloff);
6355 }
6356#endif
6357
6358 if (clear_width > 0
6359#ifdef FEAT_RIGHTLEFT
6360 && !rlflag
6361#endif
6362 )
6363 {
6364#ifdef FEAT_GUI
6365 int startCol = col;
6366#endif
6367
6368 /* blank out the rest of the line */
6369 while (col < clear_width && ScreenLines[off_to] == ' '
6370 && ScreenAttrs[off_to] == 0
6371#ifdef FEAT_MBYTE
6372 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6373#endif
6374 )
6375 {
6376 ++off_to;
6377 ++col;
6378 }
6379 if (col < clear_width)
6380 {
6381#ifdef FEAT_GUI
6382 /*
6383 * In the GUI, clearing the rest of the line may leave pixels
6384 * behind if the first character cleared was bold. Some bold
6385 * fonts spill over the left. In this case we redraw the previous
6386 * character too. If we didn't skip any blanks above, then we
6387 * only redraw if the character wasn't already redrawn anyway.
6388 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006389 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 {
6391 hl = ScreenAttrs[off_to];
6392 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006393 {
6394 int prev_cells = 1;
6395# ifdef FEAT_MBYTE
6396 if (enc_utf8)
6397 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6398 * that its width is 2. */
6399 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6400 else if (enc_dbcs != 0)
6401 {
6402 /* find previous character by counting from first
6403 * column and get its width. */
6404 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006405 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006406
6407 while (off < off_to)
6408 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006409 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006410 off += prev_cells;
6411 }
6412 }
6413
6414 if (enc_dbcs != 0 && prev_cells > 1)
6415 screen_char_2(off_to - prev_cells, row,
6416 col + coloff - prev_cells);
6417 else
6418# endif
6419 screen_char(off_to - prev_cells, row,
6420 col + coloff - prev_cells);
6421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 }
6423#endif
6424 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6425 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 off_to += clear_width - col;
6427 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 }
6429 }
6430
6431 if (clear_width > 0)
6432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 /* For a window that's left of another, draw the separator char. */
6434 if (col + coloff < Columns)
6435 {
6436 int c;
6437
6438 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006439 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006440#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006441 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6442 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 || ScreenAttrs[off_to] != hl)
6445 {
6446 ScreenLines[off_to] = c;
6447 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006448#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 if (enc_utf8)
6450 {
6451 if (c >= 0x80)
6452 {
6453 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006454 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 }
6456 else
6457 ScreenLinesUC[off_to] = 0;
6458 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 screen_char(off_to, row, col + coloff);
6461 }
6462 }
6463 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 LineWraps[row] = FALSE;
6465 }
6466}
6467
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006468#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006470 * Mirror text "str" for right-left displaying.
6471 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006473 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006474rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475{
6476 char_u *p1, *p2;
6477 int t;
6478
6479 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6480 {
6481 t = *p1;
6482 *p1 = *p2;
6483 *p2 = t;
6484 }
6485}
6486#endif
6487
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488/*
6489 * mark all status lines for redraw; used after first :cd
6490 */
6491 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006492status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
6494 win_T *wp;
6495
Bram Moolenaar29323592016-07-24 22:04:11 +02006496 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 if (wp->w_status_height)
6498 {
6499 wp->w_redr_status = TRUE;
6500 redraw_later(VALID);
6501 }
6502}
6503
6504/*
6505 * mark all status lines of the current buffer for redraw
6506 */
6507 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006508status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
6510 win_T *wp;
6511
Bram Moolenaar29323592016-07-24 22:04:11 +02006512 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6514 {
6515 wp->w_redr_status = TRUE;
6516 redraw_later(VALID);
6517 }
6518}
6519
6520/*
6521 * Redraw all status lines that need to be redrawn.
6522 */
6523 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006524redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 win_T *wp;
6527
Bram Moolenaar29323592016-07-24 22:04:11 +02006528 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 if (wp->w_redr_status)
6530 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006531 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006532 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534
Bram Moolenaar4033c552017-09-16 20:54:51 +02006535#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536/*
6537 * Redraw all status lines at the bottom of frame "frp".
6538 */
6539 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006540win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541{
6542 if (frp->fr_layout == FR_LEAF)
6543 frp->fr_win->w_redr_status = TRUE;
6544 else if (frp->fr_layout == FR_ROW)
6545 {
6546 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6547 win_redraw_last_status(frp);
6548 }
6549 else /* frp->fr_layout == FR_COL */
6550 {
6551 frp = frp->fr_child;
6552 while (frp->fr_next != NULL)
6553 frp = frp->fr_next;
6554 win_redraw_last_status(frp);
6555 }
6556}
6557#endif
6558
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559/*
6560 * Draw the verticap separator right of window "wp" starting with line "row".
6561 */
6562 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006563draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564{
6565 int hl;
6566 int c;
6567
6568 if (wp->w_vsep_width)
6569 {
6570 /* draw the vertical separator right of this window */
6571 c = fillchar_vsep(&hl);
6572 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6573 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6574 c, ' ', hl);
6575 }
6576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006579static int status_match_len(expand_T *xp, char_u *s);
6580static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
6582/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006583 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 */
6585 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006586status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587{
6588 int len = 0;
6589
6590#ifdef FEAT_MENU
6591 int emenu = (xp->xp_context == EXPAND_MENUS
6592 || xp->xp_context == EXPAND_MENUNAMES);
6593
6594 /* Check for menu separators - replace with '|'. */
6595 if (emenu && menu_is_separator(s))
6596 return 1;
6597#endif
6598
6599 while (*s != NUL)
6600 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006601 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006602 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006603 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 }
6605
6606 return len;
6607}
6608
6609/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006610 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006611 * These are backslashes used for escaping. Do show backslashes in help tags.
6612 */
6613 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006614skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006615{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006616 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006617#ifdef FEAT_MENU
6618 || ((xp->xp_context == EXPAND_MENUS
6619 || xp->xp_context == EXPAND_MENUNAMES)
6620 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6621#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006622 )
6623 {
6624#ifndef BACKSLASH_IN_FILENAME
6625 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6626 return 2;
6627#endif
6628 return 1;
6629 }
6630 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006631}
6632
6633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 * Show wildchar matches in the status line.
6635 * Show at least the "match" item.
6636 * We start at item 'first_match' in the list and show all matches that fit.
6637 *
6638 * If inversion is possible we use it. Else '=' characters are used.
6639 */
6640 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006641win_redr_status_matches(
6642 expand_T *xp,
6643 int num_matches,
6644 char_u **matches, /* list of matches */
6645 int match,
6646 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647{
6648#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6649 int row;
6650 char_u *buf;
6651 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006652 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 int fillchar;
6654 int attr;
6655 int i;
6656 int highlight = TRUE;
6657 char_u *selstart = NULL;
6658 int selstart_col = 0;
6659 char_u *selend = NULL;
6660 static int first_match = 0;
6661 int add_left = FALSE;
6662 char_u *s;
6663#ifdef FEAT_MENU
6664 int emenu;
6665#endif
6666#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6667 int l;
6668#endif
6669
6670 if (matches == NULL) /* interrupted completion? */
6671 return;
6672
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006673#ifdef FEAT_MBYTE
6674 if (has_mbyte)
6675 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6676 else
6677#endif
6678 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 if (buf == NULL)
6680 return;
6681
6682 if (match == -1) /* don't show match but original text */
6683 {
6684 match = 0;
6685 highlight = FALSE;
6686 }
6687 /* count 1 for the ending ">" */
6688 clen = status_match_len(xp, L_MATCH(match)) + 3;
6689 if (match == 0)
6690 first_match = 0;
6691 else if (match < first_match)
6692 {
6693 /* jumping left, as far as we can go */
6694 first_match = match;
6695 add_left = TRUE;
6696 }
6697 else
6698 {
6699 /* check if match fits on the screen */
6700 for (i = first_match; i < match; ++i)
6701 clen += status_match_len(xp, L_MATCH(i)) + 2;
6702 if (first_match > 0)
6703 clen += 2;
6704 /* jumping right, put match at the left */
6705 if ((long)clen > Columns)
6706 {
6707 first_match = match;
6708 /* if showing the last match, we can add some on the left */
6709 clen = 2;
6710 for (i = match; i < num_matches; ++i)
6711 {
6712 clen += status_match_len(xp, L_MATCH(i)) + 2;
6713 if ((long)clen >= Columns)
6714 break;
6715 }
6716 if (i == num_matches)
6717 add_left = TRUE;
6718 }
6719 }
6720 if (add_left)
6721 while (first_match > 0)
6722 {
6723 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6724 if ((long)clen >= Columns)
6725 break;
6726 --first_match;
6727 }
6728
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006729 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730
6731 if (first_match == 0)
6732 {
6733 *buf = NUL;
6734 len = 0;
6735 }
6736 else
6737 {
6738 STRCPY(buf, "< ");
6739 len = 2;
6740 }
6741 clen = len;
6742
6743 i = first_match;
6744 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6745 {
6746 if (i == match)
6747 {
6748 selstart = buf + len;
6749 selstart_col = clen;
6750 }
6751
6752 s = L_MATCH(i);
6753 /* Check for menu separators - replace with '|' */
6754#ifdef FEAT_MENU
6755 emenu = (xp->xp_context == EXPAND_MENUS
6756 || xp->xp_context == EXPAND_MENUNAMES);
6757 if (emenu && menu_is_separator(s))
6758 {
6759 STRCPY(buf + len, transchar('|'));
6760 l = (int)STRLEN(buf + len);
6761 len += l;
6762 clen += l;
6763 }
6764 else
6765#endif
6766 for ( ; *s != NUL; ++s)
6767 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006768 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 clen += ptr2cells(s);
6770#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006771 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 {
6773 STRNCPY(buf + len, s, l);
6774 s += l - 1;
6775 len += l;
6776 }
6777 else
6778#endif
6779 {
6780 STRCPY(buf + len, transchar_byte(*s));
6781 len += (int)STRLEN(buf + len);
6782 }
6783 }
6784 if (i == match)
6785 selend = buf + len;
6786
6787 *(buf + len++) = ' ';
6788 *(buf + len++) = ' ';
6789 clen += 2;
6790 if (++i == num_matches)
6791 break;
6792 }
6793
6794 if (i != num_matches)
6795 {
6796 *(buf + len++) = '>';
6797 ++clen;
6798 }
6799
6800 buf[len] = NUL;
6801
6802 row = cmdline_row - 1;
6803 if (row >= 0)
6804 {
6805 if (wild_menu_showing == 0)
6806 {
6807 if (msg_scrolled > 0)
6808 {
6809 /* Put the wildmenu just above the command line. If there is
6810 * no room, scroll the screen one line up. */
6811 if (cmdline_row == Rows - 1)
6812 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006813 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 ++msg_scrolled;
6815 }
6816 else
6817 {
6818 ++cmdline_row;
6819 ++row;
6820 }
6821 wild_menu_showing = WM_SCROLLED;
6822 }
6823 else
6824 {
6825 /* Create status line if needed by setting 'laststatus' to 2.
6826 * Set 'winminheight' to zero to avoid that the window is
6827 * resized. */
6828 if (lastwin->w_status_height == 0)
6829 {
6830 save_p_ls = p_ls;
6831 save_p_wmh = p_wmh;
6832 p_ls = 2;
6833 p_wmh = 0;
6834 last_status(FALSE);
6835 }
6836 wild_menu_showing = WM_SHOWN;
6837 }
6838 }
6839
6840 screen_puts(buf, row, 0, attr);
6841 if (selstart != NULL && highlight)
6842 {
6843 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006844 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 }
6846
6847 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6848 }
6849
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 vim_free(buf);
6852}
6853#endif
6854
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855/*
6856 * Redraw the status line of window wp.
6857 *
6858 * If inversion is possible we use it. Else '=' characters are used.
6859 */
6860 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006861win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862{
6863 int row;
6864 char_u *p;
6865 int len;
6866 int fillchar;
6867 int attr;
6868 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006869 static int busy = FALSE;
6870
6871 /* It's possible to get here recursively when 'statusline' (indirectly)
6872 * invokes ":redrawstatus". Simply ignore the call then. */
6873 if (busy)
6874 return;
6875 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876
6877 wp->w_redr_status = FALSE;
6878 if (wp->w_status_height == 0)
6879 {
6880 /* no status line, can only be last window */
6881 redraw_cmdline = TRUE;
6882 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006883 else if (!redrawing()
6884#ifdef FEAT_INS_EXPAND
6885 /* don't update status line when popup menu is visible and may be
6886 * drawn over it */
6887 || pum_visible()
6888#endif
6889 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 {
6891 /* Don't redraw right now, do it later. */
6892 wp->w_redr_status = TRUE;
6893 }
6894#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006895 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 {
6897 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006898 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
6900#endif
6901 else
6902 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006903 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006905 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 p = NameBuff;
6907 len = (int)STRLEN(p);
6908
Bram Moolenaard85f2712017-07-28 21:51:57 +02006909 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910#ifdef FEAT_QUICKFIX
6911 || wp->w_p_pvw
6912#endif
6913 || bufIsChanged(wp->w_buffer)
6914 || wp->w_buffer->b_p_ro)
6915 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006916 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006918 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 len += (int)STRLEN(p + len);
6920 }
6921#ifdef FEAT_QUICKFIX
6922 if (wp->w_p_pvw)
6923 {
6924 STRCPY(p + len, _("[Preview]"));
6925 len += (int)STRLEN(p + len);
6926 }
6927#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006928 if (bufIsChanged(wp->w_buffer)
6929#ifdef FEAT_TERMINAL
6930 && !bt_terminal(wp->w_buffer)
6931#endif
6932 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
6934 STRCPY(p + len, "[+]");
6935 len += 3;
6936 }
6937 if (wp->w_buffer->b_p_ro)
6938 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006939 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006940 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 }
6942
Bram Moolenaar02631462017-09-22 15:20:32 +02006943 this_ru_col = ru_col - (Columns - wp->w_width);
6944 if (this_ru_col < (wp->w_width + 1) / 2)
6945 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 if (this_ru_col <= 1)
6947 {
6948 p = (char_u *)"<"; /* No room for file name! */
6949 len = 1;
6950 }
6951 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952#ifdef FEAT_MBYTE
6953 if (has_mbyte)
6954 {
6955 int clen = 0, i;
6956
6957 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006958 clen = mb_string2cells(p, -1);
6959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 /* Find first character that will fit.
6961 * Going from start to end is much faster for DBCS. */
6962 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006963 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 clen -= (*mb_ptr2cells)(p + i);
6965 len = clen;
6966 if (i > 0)
6967 {
6968 p = p + i - 1;
6969 *p = '<';
6970 ++len;
6971 }
6972
6973 }
6974 else
6975#endif
6976 if (len > this_ru_col - 1)
6977 {
6978 p += len - (this_ru_col - 1);
6979 *p = '<';
6980 len = this_ru_col - 1;
6981 }
6982
6983 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006984 screen_puts(p, row, wp->w_wincol, attr);
6985 screen_fill(row, row + 1, len + wp->w_wincol,
6986 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006988 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6990 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006991 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992
6993#ifdef FEAT_CMDL_INFO
6994 win_redr_ruler(wp, TRUE);
6995#endif
6996 }
6997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 /*
6999 * May need to draw the character below the vertical separator.
7000 */
7001 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7002 {
7003 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007004 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 else
7006 fillchar = fillchar_vsep(&attr);
7007 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7008 attr);
7009 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007010 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011}
7012
Bram Moolenaar238a5642006-02-21 22:12:05 +00007013#ifdef FEAT_STL_OPT
7014/*
7015 * Redraw the status line according to 'statusline' and take care of any
7016 * errors encountered.
7017 */
7018 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007019redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007020{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007021 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007022 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007023
7024 /* When called recursively return. This can happen when the statusline
7025 * contains an expression that triggers a redraw. */
7026 if (entered)
7027 return;
7028 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007029
Bram Moolenaara742e082016-04-05 21:10:38 +02007030 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007031 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007032 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007033 {
7034 /* When there is an error disable the statusline, otherwise the
7035 * display is messed up with errors and a redraw triggers the problem
7036 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007037 set_string_option_direct((char_u *)"statusline", -1,
7038 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007039 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007040 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007041 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007042 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007043}
7044#endif
7045
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046/*
7047 * Return TRUE if the status line of window "wp" is connected to the status
7048 * line of the window right of it. If not, then it's a vertical separator.
7049 * Only call if (wp->w_vsep_width != 0).
7050 */
7051 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007052stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053{
7054 frame_T *fr;
7055
7056 fr = wp->w_frame;
7057 while (fr->fr_parent != NULL)
7058 {
7059 if (fr->fr_parent->fr_layout == FR_COL)
7060 {
7061 if (fr->fr_next != NULL)
7062 break;
7063 }
7064 else
7065 {
7066 if (fr->fr_next != NULL)
7067 return TRUE;
7068 }
7069 fr = fr->fr_parent;
7070 }
7071 return FALSE;
7072}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075/*
7076 * Get the value to show for the language mappings, active 'keymap'.
7077 */
7078 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007079get_keymap_str(
7080 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007081 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007082 char_u *buf, /* buffer for the result */
7083 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
7085 char_u *p;
7086
7087 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7088 return FALSE;
7089
7090 {
7091#ifdef FEAT_EVAL
7092 buf_T *old_curbuf = curbuf;
7093 win_T *old_curwin = curwin;
7094 char_u *s;
7095
7096 curbuf = wp->w_buffer;
7097 curwin = wp;
7098 STRCPY(buf, "b:keymap_name"); /* must be writable */
7099 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007100 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 --emsg_skip;
7102 curbuf = old_curbuf;
7103 curwin = old_curwin;
7104 if (p == NULL || *p == NUL)
7105#endif
7106 {
7107#ifdef FEAT_KEYMAP
7108 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7109 p = wp->w_buffer->b_p_keymap;
7110 else
7111#endif
7112 p = (char_u *)"lang";
7113 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007114 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 buf[0] = NUL;
7116#ifdef FEAT_EVAL
7117 vim_free(s);
7118#endif
7119 }
7120 return buf[0] != NUL;
7121}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122
7123#if defined(FEAT_STL_OPT) || defined(PROTO)
7124/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007125 * Redraw the status line or ruler of window "wp".
7126 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 */
7128 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007129win_redr_custom(
7130 win_T *wp,
7131 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007133 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 int attr;
7135 int curattr;
7136 int row;
7137 int col = 0;
7138 int maxwidth;
7139 int width;
7140 int n;
7141 int len;
7142 int fillchar;
7143 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007144 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007146 struct stl_hlrec hltab[STL_MAX_ITEM];
7147 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007148 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007149 win_T *ewp;
7150 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
Bram Moolenaar1d633412013-12-11 15:52:01 +01007152 /* There is a tiny chance that this gets called recursively: When
7153 * redrawing a status line triggers redrawing the ruler or tabline.
7154 * Avoid trouble by not allowing recursion. */
7155 if (entered)
7156 return;
7157 entered = TRUE;
7158
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007160 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007162 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007163 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007164 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007165 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007166 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007167 maxwidth = Columns;
7168# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007169 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007172 else
7173 {
7174 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007175 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007176 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007177
7178 if (draw_ruler)
7179 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007180 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007181 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007182 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007183 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007184 if (*++stl == '-')
7185 stl++;
7186 if (atoi((char *)stl))
7187 while (VIM_ISDIGIT(*stl))
7188 stl++;
7189 if (*stl++ != '(')
7190 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007191 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007192 col = ru_col - (Columns - wp->w_width);
7193 if (col < (wp->w_width + 1) / 2)
7194 col = (wp->w_width + 1) / 2;
7195 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007196 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007197 {
7198 row = Rows - 1;
7199 --maxwidth; /* writing in last column may cause scrolling */
7200 fillchar = ' ';
7201 attr = 0;
7202 }
7203
7204# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007205 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007206# endif
7207 }
7208 else
7209 {
7210 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007211 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007212 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007213 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007214# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007215 use_sandbox = was_set_insecurely((char_u *)"statusline",
7216 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007217# endif
7218 }
7219
Bram Moolenaar53f81742017-09-22 14:35:51 +02007220 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007221 }
7222
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007224 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
Bram Moolenaar61452852011-02-01 18:01:11 +01007226 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7227 * the cursor away and back. */
7228 ewp = wp == NULL ? curwin : wp;
7229 p_crb_save = ewp->w_p_crb;
7230 ewp->w_p_crb = FALSE;
7231
Bram Moolenaar362f3562009-11-03 16:20:34 +00007232 /* Make a copy, because the statusline may include a function call that
7233 * might change the option value and free the memory. */
7234 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007235 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007236 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007237 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007238 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007239 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007241 /* Make all characters printable. */
7242 p = transstr(buf);
7243 if (p != NULL)
7244 {
7245 vim_strncpy(buf, p, sizeof(buf) - 1);
7246 vim_free(p);
7247 }
7248
7249 /* fill up with "fillchar" */
7250 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007251 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {
7253#ifdef FEAT_MBYTE
7254 len += (*mb_char2bytes)(fillchar, buf + len);
7255#else
7256 buf[len++] = fillchar;
7257#endif
7258 ++width;
7259 }
7260 buf[len] = NUL;
7261
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007262 /*
7263 * Draw each snippet with the specified highlighting.
7264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 curattr = attr;
7266 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007267 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007269 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 screen_puts_len(p, len, row, col, curattr);
7271 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007272 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007274 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007276 else if (hltab[n].userhl < 0)
7277 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007278#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007279 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7280 && wp->w_status_height != 0)
7281 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007282 else if (wp != NULL && bt_terminal(wp->w_buffer)
7283 && wp->w_status_height != 0)
7284 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007285#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007286 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007287 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007289 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 }
7291 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007292
7293 if (wp == NULL)
7294 {
7295 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7296 col = 0;
7297 len = 0;
7298 p = buf;
7299 fillchar = 0;
7300 for (n = 0; tabtab[n].start != NULL; n++)
7301 {
7302 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7303 while (col < len)
7304 TabPageIdxs[col++] = fillchar;
7305 p = tabtab[n].start;
7306 fillchar = tabtab[n].userhl;
7307 }
7308 while (col < Columns)
7309 TabPageIdxs[col++] = fillchar;
7310 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007311
7312theend:
7313 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314}
7315
7316#endif /* FEAT_STL_OPT */
7317
7318/*
7319 * Output a single character directly to the screen and update ScreenLines.
7320 */
7321 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007322screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 char_u buf[MB_MAXBYTES + 1];
7325
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007326#ifdef FEAT_MBYTE
7327 if (has_mbyte)
7328 buf[(*mb_char2bytes)(c, buf)] = NUL;
7329 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007331 {
7332 buf[0] = c;
7333 buf[1] = NUL;
7334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 screen_puts(buf, row, col, attr);
7336}
7337
7338/*
7339 * Get a single character directly from ScreenLines into "bytes[]".
7340 * Also return its attribute in *attrp;
7341 */
7342 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007343screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344{
7345 unsigned off;
7346
7347 /* safety check */
7348 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7349 {
7350 off = LineOffset[row] + col;
7351 *attrp = ScreenAttrs[off];
7352 bytes[0] = ScreenLines[off];
7353 bytes[1] = NUL;
7354
7355#ifdef FEAT_MBYTE
7356 if (enc_utf8 && ScreenLinesUC[off] != 0)
7357 bytes[utfc_char2bytes(off, bytes)] = NUL;
7358 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7359 {
7360 bytes[0] = ScreenLines[off];
7361 bytes[1] = ScreenLines2[off];
7362 bytes[2] = NUL;
7363 }
7364 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7365 {
7366 bytes[1] = ScreenLines[off + 1];
7367 bytes[2] = NUL;
7368 }
7369#endif
7370 }
7371}
7372
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007373#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007374static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007375
7376/*
7377 * Return TRUE if composing characters for screen posn "off" differs from
7378 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007379 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007380 */
7381 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007382screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007383{
7384 int i;
7385
7386 for (i = 0; i < Screen_mco; ++i)
7387 {
7388 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7389 return TRUE;
7390 if (u8cc[i] == 0)
7391 break;
7392 }
7393 return FALSE;
7394}
7395#endif
7396
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397/*
7398 * Put string '*text' on the screen at position 'row' and 'col', with
7399 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7400 * Note: only outputs within one row, message is truncated at screen boundary!
7401 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7402 */
7403 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007404screen_puts(
7405 char_u *text,
7406 int row,
7407 int col,
7408 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409{
7410 screen_puts_len(text, -1, row, col, attr);
7411}
7412
7413/*
7414 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7415 * a NUL.
7416 */
7417 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007418screen_puts_len(
7419 char_u *text,
7420 int textlen,
7421 int row,
7422 int col,
7423 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424{
7425 unsigned off;
7426 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007427 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 int c;
7429#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007430 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 int mbyte_blen = 1;
7432 int mbyte_cells = 1;
7433 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007434 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 int clear_next_cell = FALSE;
7436# ifdef FEAT_ARABIC
7437 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007438 int pc, nc, nc1;
7439 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440# endif
7441#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007442#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7443 int force_redraw_this;
7444 int force_redraw_next = FALSE;
7445#endif
7446 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447
7448 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7449 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007450 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451
Bram Moolenaarc236c162008-07-13 17:41:49 +00007452#ifdef FEAT_MBYTE
7453 /* When drawing over the right halve of a double-wide char clear out the
7454 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007455 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007456# ifdef FEAT_GUI
7457 && !gui.in_use
7458# endif
7459 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007460 {
7461 ScreenLines[off - 1] = ' ';
7462 ScreenAttrs[off - 1] = 0;
7463 if (enc_utf8)
7464 {
7465 ScreenLinesUC[off - 1] = 0;
7466 ScreenLinesC[0][off - 1] = 0;
7467 }
7468 /* redraw the previous cell, make it empty */
7469 screen_char(off - 1, row, col - 1);
7470 /* force the cell at "col" to be redrawn */
7471 force_redraw_next = TRUE;
7472 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007473#endif
7474
Bram Moolenaar367329b2007-08-30 11:53:22 +00007475#ifdef FEAT_MBYTE
7476 max_off = LineOffset[row] + screen_Columns;
7477#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007478 while (col < screen_Columns
7479 && (len < 0 || (int)(ptr - text) < len)
7480 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 {
7482 c = *ptr;
7483#ifdef FEAT_MBYTE
7484 /* check if this is the first byte of a multibyte */
7485 if (has_mbyte)
7486 {
7487 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007488 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007490 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7492 mbyte_cells = 1;
7493 else if (enc_dbcs != 0)
7494 mbyte_cells = mbyte_blen;
7495 else /* enc_utf8 */
7496 {
7497 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007498 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 (int)((text + len) - ptr));
7500 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007501 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007503# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 /* Non-BMP character: display as ? or fullwidth ?. */
7505 if (u8c >= 0x10000)
7506 {
7507 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7508 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007509 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512# ifdef FEAT_ARABIC
7513 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7514 {
7515 /* Do Arabic shaping. */
7516 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7517 {
7518 /* Past end of string to be displayed. */
7519 nc = NUL;
7520 nc1 = NUL;
7521 }
7522 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007523 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007524 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7525 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007526 nc1 = pcc[0];
7527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 pc = prev_c;
7529 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007530 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 }
7532 else
7533 prev_c = u8c;
7534# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007535 if (col + mbyte_cells > screen_Columns)
7536 {
7537 /* Only 1 cell left, but character requires 2 cells:
7538 * display a '>' in the last column to avoid wrapping. */
7539 c = '>';
7540 mbyte_cells = 1;
7541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 }
7543 }
7544#endif
7545
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007546#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7547 force_redraw_this = force_redraw_next;
7548 force_redraw_next = FALSE;
7549#endif
7550
7551 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552#ifdef FEAT_MBYTE
7553 || (mbyte_cells == 2
7554 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7555 || (enc_dbcs == DBCS_JPNU
7556 && c == 0x8e
7557 && ScreenLines2[off] != ptr[1])
7558 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007559 && (ScreenLinesUC[off] !=
7560 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7561 || (ScreenLinesUC[off] != 0
7562 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563#endif
7564 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007565 || exmode_active;
7566
7567 if (need_redraw
7568#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7569 || force_redraw_this
7570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 )
7572 {
7573#if defined(FEAT_GUI) || defined(UNIX)
7574 /* The bold trick makes a single row of pixels appear in the next
7575 * character. When a bold character is removed, the next
7576 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007577 * and for some xterms. */
7578 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579# ifdef FEAT_GUI
7580 gui.in_use
7581# endif
7582# if defined(FEAT_GUI) && defined(UNIX)
7583 ||
7584# endif
7585# ifdef UNIX
7586 term_is_xterm
7587# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007588 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007590 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007592 if (n > HL_ALL)
7593 n = syn_attr2attr(n);
7594 if (n & HL_BOLD)
7595 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 }
7597#endif
7598#ifdef FEAT_MBYTE
7599 /* When at the end of the text and overwriting a two-cell
7600 * character with a one-cell character, need to clear the next
7601 * cell. Also when overwriting the left halve of a two-cell char
7602 * with the right halve of a two-cell char. Do this only once
7603 * (mb_off2cells() may return 2 on the right halve). */
7604 if (clear_next_cell)
7605 clear_next_cell = FALSE;
7606 else if (has_mbyte
7607 && (len < 0 ? ptr[mbyte_blen] == NUL
7608 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007609 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007611 && (*mb_off2cells)(off, max_off) == 1
7612 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 clear_next_cell = TRUE;
7614
7615 /* Make sure we never leave a second byte of a double-byte behind,
7616 * it confuses mb_off2cells(). */
7617 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007618 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007620 && (*mb_off2cells)(off, max_off) == 1
7621 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 ScreenLines[off + mbyte_blen] = 0;
7623#endif
7624 ScreenLines[off] = c;
7625 ScreenAttrs[off] = attr;
7626#ifdef FEAT_MBYTE
7627 if (enc_utf8)
7628 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007629 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 ScreenLinesUC[off] = 0;
7631 else
7632 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007633 int i;
7634
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007636 for (i = 0; i < Screen_mco; ++i)
7637 {
7638 ScreenLinesC[i][off] = u8cc[i];
7639 if (u8cc[i] == 0)
7640 break;
7641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 }
7643 if (mbyte_cells == 2)
7644 {
7645 ScreenLines[off + 1] = 0;
7646 ScreenAttrs[off + 1] = attr;
7647 }
7648 screen_char(off, row, col);
7649 }
7650 else if (mbyte_cells == 2)
7651 {
7652 ScreenLines[off + 1] = ptr[1];
7653 ScreenAttrs[off + 1] = attr;
7654 screen_char_2(off, row, col);
7655 }
7656 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7657 {
7658 ScreenLines2[off] = ptr[1];
7659 screen_char(off, row, col);
7660 }
7661 else
7662#endif
7663 screen_char(off, row, col);
7664 }
7665#ifdef FEAT_MBYTE
7666 if (has_mbyte)
7667 {
7668 off += mbyte_cells;
7669 col += mbyte_cells;
7670 ptr += mbyte_blen;
7671 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007672 {
7673 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007675 len = -1;
7676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
7678 else
7679#endif
7680 {
7681 ++off;
7682 ++col;
7683 ++ptr;
7684 }
7685 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007686
7687#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7688 /* If we detected the next character needs to be redrawn, but the text
7689 * doesn't extend up to there, update the character here. */
7690 if (force_redraw_next && col < screen_Columns)
7691 {
7692# ifdef FEAT_MBYTE
7693 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7694 screen_char_2(off, row, col);
7695 else
7696# endif
7697 screen_char(off, row, col);
7698 }
7699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700}
7701
7702#ifdef FEAT_SEARCH_EXTRA
7703/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007704 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 */
7706 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007707start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708{
7709 if (p_hls && !no_hlsearch)
7710 {
7711 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007712 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007713# ifdef FEAT_RELTIME
7714 /* Set the time limit to 'redrawtime'. */
7715 profile_setlimit(p_rdt, &search_hl.tm);
7716# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 }
7718}
7719
7720/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007721 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 */
7723 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007724end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
7726 if (search_hl.rm.regprog != NULL)
7727 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007728 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 search_hl.rm.regprog = NULL;
7730 }
7731}
7732
7733/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007734 * Init for calling prepare_search_hl().
7735 */
7736 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007737init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007738{
7739 matchitem_T *cur;
7740
7741 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7742 * match */
7743 cur = wp->w_match_head;
7744 while (cur != NULL)
7745 {
7746 cur->hl.rm = cur->match;
7747 if (cur->hlg_id == 0)
7748 cur->hl.attr = 0;
7749 else
7750 cur->hl.attr = syn_id2attr(cur->hlg_id);
7751 cur->hl.buf = wp->w_buffer;
7752 cur->hl.lnum = 0;
7753 cur->hl.first_lnum = 0;
7754# ifdef FEAT_RELTIME
7755 /* Set the time limit to 'redrawtime'. */
7756 profile_setlimit(p_rdt, &(cur->hl.tm));
7757# endif
7758 cur = cur->next;
7759 }
7760 search_hl.buf = wp->w_buffer;
7761 search_hl.lnum = 0;
7762 search_hl.first_lnum = 0;
7763 /* time limit is set at the toplevel, for all windows */
7764}
7765
7766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 * Advance to the match in window "wp" line "lnum" or past it.
7768 */
7769 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007770prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007772 matchitem_T *cur; /* points to the match list */
7773 match_T *shl; /* points to search_hl or a match */
7774 int shl_flag; /* flag to indicate whether search_hl
7775 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007776 int pos_inprogress; /* marks that position match search is
7777 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 int n;
7779
7780 /*
7781 * When using a multi-line pattern, start searching at the top
7782 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007783 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007785 cur = wp->w_match_head;
7786 shl_flag = FALSE;
7787 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007789 if (shl_flag == FALSE)
7790 {
7791 shl = &search_hl;
7792 shl_flag = TRUE;
7793 }
7794 else
7795 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 if (shl->rm.regprog != NULL
7797 && shl->lnum == 0
7798 && re_multiline(shl->rm.regprog))
7799 {
7800 if (shl->first_lnum == 0)
7801 {
7802# ifdef FEAT_FOLDING
7803 for (shl->first_lnum = lnum;
7804 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7805 if (hasFoldingWin(wp, shl->first_lnum - 1,
7806 NULL, NULL, TRUE, NULL))
7807 break;
7808# else
7809 shl->first_lnum = wp->w_topline;
7810# endif
7811 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007812 if (cur != NULL)
7813 cur->pos.cur = 0;
7814 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007816 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7817 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007819 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7820 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007821 pos_inprogress = cur == NULL || cur->pos.cur == 0
7822 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 if (shl->lnum != 0)
7824 {
7825 shl->first_lnum = shl->lnum
7826 + shl->rm.endpos[0].lnum
7827 - shl->rm.startpos[0].lnum;
7828 n = shl->rm.endpos[0].col;
7829 }
7830 else
7831 {
7832 ++shl->first_lnum;
7833 n = 0;
7834 }
7835 }
7836 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007837 if (shl != &search_hl && cur != NULL)
7838 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 }
7840}
7841
7842/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007843 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 * Uses shl->buf.
7845 * Sets shl->lnum and shl->rm contents.
7846 * Note: Assumes a previous match is always before "lnum", unless
7847 * shl->lnum is zero.
7848 * Careful: Any pointers for buffer lines will become invalid.
7849 */
7850 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007851next_search_hl(
7852 win_T *win,
7853 match_T *shl, /* points to search_hl or a match */
7854 linenr_T lnum,
7855 colnr_T mincol, /* minimal column for a match */
7856 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857{
7858 linenr_T l;
7859 colnr_T matchcol;
7860 long nmatched;
7861
7862 if (shl->lnum != 0)
7863 {
7864 /* Check for three situations:
7865 * 1. If the "lnum" is below a previous match, start a new search.
7866 * 2. If the previous match includes "mincol", use it.
7867 * 3. Continue after the previous match.
7868 */
7869 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7870 if (lnum > l)
7871 shl->lnum = 0;
7872 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7873 return;
7874 }
7875
7876 /*
7877 * Repeat searching for a match until one is found that includes "mincol"
7878 * or none is found in this line.
7879 */
7880 called_emsg = FALSE;
7881 for (;;)
7882 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007883#ifdef FEAT_RELTIME
7884 /* Stop searching after passing the time limit. */
7885 if (profile_passed_limit(&(shl->tm)))
7886 {
7887 shl->lnum = 0; /* no match found in time */
7888 break;
7889 }
7890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 /* Three situations:
7892 * 1. No useful previous match: search from start of line.
7893 * 2. Not Vi compatible or empty match: continue at next character.
7894 * Break the loop if this is beyond the end of the line.
7895 * 3. Vi compatible searching: continue at end of previous match.
7896 */
7897 if (shl->lnum == 0)
7898 matchcol = 0;
7899 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7900 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007901 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007903 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007904
7905 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007906 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007907 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007909 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 shl->lnum = 0;
7911 break;
7912 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007913#ifdef FEAT_MBYTE
7914 if (has_mbyte)
7915 matchcol += mb_ptr2len(ml);
7916 else
7917#endif
7918 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 }
7920 else
7921 matchcol = shl->rm.endpos[0].col;
7922
7923 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007924 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007926 /* Remember whether shl->rm is using a copy of the regprog in
7927 * cur->match. */
7928 int regprog_is_copy = (shl != &search_hl && cur != NULL
7929 && shl == &cur->hl
7930 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007931 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007932
Bram Moolenaarb3414592014-06-17 17:48:32 +02007933 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7934 matchcol,
7935#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007936 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007937#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007938 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007939#endif
7940 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007941 /* Copy the regprog, in case it got freed and recompiled. */
7942 if (regprog_is_copy)
7943 cur->match.regprog = cur->hl.rm.regprog;
7944
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007945 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007946 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007947 /* Error while handling regexp: stop using this regexp. */
7948 if (shl == &search_hl)
7949 {
7950 /* don't free regprog in the match list, it's a copy */
7951 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007952 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007953 }
7954 shl->rm.regprog = NULL;
7955 shl->lnum = 0;
7956 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7957 message */
7958 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007959 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007960 }
7961 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007962 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007963 else
7964 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 if (nmatched == 0)
7966 {
7967 shl->lnum = 0; /* no match found */
7968 break;
7969 }
7970 if (shl->rm.startpos[0].lnum > 0
7971 || shl->rm.startpos[0].col >= mincol
7972 || nmatched > 1
7973 || shl->rm.endpos[0].col > mincol)
7974 {
7975 shl->lnum += shl->rm.startpos[0].lnum;
7976 break; /* useful match found */
7977 }
7978 }
7979}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980
Bram Moolenaar85077472016-10-16 14:35:48 +02007981/*
7982 * If there is a match fill "shl" and return one.
7983 * Return zero otherwise.
7984 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007985 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007986next_search_hl_pos(
7987 match_T *shl, /* points to a match */
7988 linenr_T lnum,
7989 posmatch_T *posmatch, /* match positions */
7990 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007991{
7992 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007993 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007994
Bram Moolenaarb3414592014-06-17 17:48:32 +02007995 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7996 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007997 llpos_T *pos = &posmatch->pos[i];
7998
7999 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008000 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008001 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008002 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008003 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008004 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008005 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008006 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008007 /* if this match comes before the one at "found" then swap
8008 * them */
8009 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008010 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008011 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008012
Bram Moolenaar85077472016-10-16 14:35:48 +02008013 *pos = posmatch->pos[found];
8014 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008015 }
8016 }
8017 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008018 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008019 }
8020 }
8021 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008022 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008023 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008024 colnr_T start = posmatch->pos[found].col == 0
8025 ? 0 : posmatch->pos[found].col - 1;
8026 colnr_T end = posmatch->pos[found].col == 0
8027 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008028
Bram Moolenaar85077472016-10-16 14:35:48 +02008029 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008030 shl->rm.startpos[0].lnum = 0;
8031 shl->rm.startpos[0].col = start;
8032 shl->rm.endpos[0].lnum = 0;
8033 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008034 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008035 posmatch->cur = found + 1;
8036 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008037 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008038 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008039}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008040#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008041
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008043screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
8045 attrentry_T *aep = NULL;
8046
8047 screen_attr = attr;
8048 if (full_screen
8049#ifdef WIN3264
8050 && termcap_active
8051#endif
8052 )
8053 {
8054#ifdef FEAT_GUI
8055 if (gui.in_use)
8056 {
8057 char buf[20];
8058
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008059 /* The GUI handles this internally. */
8060 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 OUT_STR(buf);
8062 }
8063 else
8064#endif
8065 {
8066 if (attr > HL_ALL) /* special HL attr. */
8067 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008068 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 aep = syn_cterm_attr2entry(attr);
8070 else
8071 aep = syn_term_attr2entry(attr);
8072 if (aep == NULL) /* did ":syntax clear" */
8073 attr = 0;
8074 else
8075 attr = aep->ae_attr;
8076 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008077 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008079 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008080#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008081 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8082 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8083 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008084#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008085 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008086 /* If the Normal FG color has BOLD attribute and the new HL
8087 * has a FG color defined, clear BOLD. */
8088 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008089 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008091 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008092 out_str(T_UCS);
8093 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008094 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8095 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008097 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008099 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008101 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008102 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103
8104 /*
8105 * Output the color or start string after bold etc., in case the
8106 * bold etc. override the color setting.
8107 */
8108 if (aep != NULL)
8109 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008110#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008111 /* When 'termguicolors' is set but fg or bg is unset,
8112 * fall back to the cterm colors. This helps for SpellBad,
8113 * where the GUI uses a red undercurl. */
8114 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008116 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008117 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008118 }
8119 else
8120#endif
8121 if (t_colors > 1)
8122 {
8123 if (aep->ae_u.cterm.fg_color)
8124 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8125 }
8126#ifdef FEAT_TERMGUICOLORS
8127 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8128 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008129 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008130 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 }
8132 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008133#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008134 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008136 if (aep->ae_u.cterm.bg_color)
8137 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8138 }
8139
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008140 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008141 {
8142 if (aep->ae_u.term.start != NULL)
8143 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 }
8145 }
8146 }
8147 }
8148}
8149
8150 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008151screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152{
8153 int do_ME = FALSE; /* output T_ME code */
8154
8155 if (screen_attr != 0
8156#ifdef WIN3264
8157 && termcap_active
8158#endif
8159 )
8160 {
8161#ifdef FEAT_GUI
8162 if (gui.in_use)
8163 {
8164 char buf[20];
8165
8166 /* use internal GUI code */
8167 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8168 OUT_STR(buf);
8169 }
8170 else
8171#endif
8172 {
8173 if (screen_attr > HL_ALL) /* special HL attr. */
8174 {
8175 attrentry_T *aep;
8176
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008177 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {
8179 /*
8180 * Assume that t_me restores the original colors!
8181 */
8182 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008183 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008184#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008185 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8186 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8187 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008188#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008189 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008190#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008191 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8192 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8193 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008194#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008195 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 do_ME = TRUE;
8197 }
8198 else
8199 {
8200 aep = syn_term_attr2entry(screen_attr);
8201 if (aep != NULL && aep->ae_u.term.stop != NULL)
8202 {
8203 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8204 do_ME = TRUE;
8205 else
8206 out_str(aep->ae_u.term.stop);
8207 }
8208 }
8209 if (aep == NULL) /* did ":syntax clear" */
8210 screen_attr = 0;
8211 else
8212 screen_attr = aep->ae_attr;
8213 }
8214
8215 /*
8216 * Often all ending-codes are equal to T_ME. Avoid outputting the
8217 * same sequence several times.
8218 */
8219 if (screen_attr & HL_STANDOUT)
8220 {
8221 if (STRCMP(T_SE, T_ME) == 0)
8222 do_ME = TRUE;
8223 else
8224 out_str(T_SE);
8225 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008226 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008227 {
8228 if (STRCMP(T_UCE, T_ME) == 0)
8229 do_ME = TRUE;
8230 else
8231 out_str(T_UCE);
8232 }
8233 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008234 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {
8236 if (STRCMP(T_UE, T_ME) == 0)
8237 do_ME = TRUE;
8238 else
8239 out_str(T_UE);
8240 }
8241 if (screen_attr & HL_ITALIC)
8242 {
8243 if (STRCMP(T_CZR, T_ME) == 0)
8244 do_ME = TRUE;
8245 else
8246 out_str(T_CZR);
8247 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008248 if (screen_attr & HL_STRIKETHROUGH)
8249 {
8250 if (STRCMP(T_STE, T_ME) == 0)
8251 do_ME = TRUE;
8252 else
8253 out_str(T_STE);
8254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8256 out_str(T_ME);
8257
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008258#ifdef FEAT_TERMGUICOLORS
8259 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008261 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008262 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008263 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008264 term_bg_rgb_color(cterm_normal_bg_gui_color);
8265 }
8266 else
8267#endif
8268 {
8269 if (t_colors > 1)
8270 {
8271 /* set Normal cterm colors */
8272 if (cterm_normal_fg_color != 0)
8273 term_fg_color(cterm_normal_fg_color - 1);
8274 if (cterm_normal_bg_color != 0)
8275 term_bg_color(cterm_normal_bg_color - 1);
8276 if (cterm_normal_fg_bold)
8277 out_str(T_MD);
8278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 }
8280 }
8281 }
8282 screen_attr = 0;
8283}
8284
8285/*
8286 * Reset the colors for a cterm. Used when leaving Vim.
8287 * The machine specific code may override this again.
8288 */
8289 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008290reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008292 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 {
8294 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008295#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008296 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8297 || cterm_normal_bg_gui_color != INVALCOLOR)
8298 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008299#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {
8303 out_str(T_OP);
8304 screen_attr = -1;
8305 }
8306 if (cterm_normal_fg_bold)
8307 {
8308 out_str(T_ME);
8309 screen_attr = -1;
8310 }
8311 }
8312}
8313
8314/*
8315 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8316 * using the attributes from ScreenAttrs["off"].
8317 */
8318 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008319screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
8321 int attr;
8322
8323 /* Check for illegal values, just in case (could happen just after
8324 * resizing). */
8325 if (row >= screen_Rows || col >= screen_Columns)
8326 return;
8327
Bram Moolenaar494838a2015-02-10 19:20:37 +01008328 /* Outputting a character in the last cell on the screen may scroll the
8329 * screen up. Only do it when the "xn" termcap property is set, otherwise
8330 * mark the character invalid (update it when scrolled up). */
8331 if (*T_XN == NUL
8332 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333#ifdef FEAT_RIGHTLEFT
8334 /* account for first command-line character in rightleft mode */
8335 && !cmdmsg_rl
8336#endif
8337 )
8338 {
8339 ScreenAttrs[off] = (sattr_T)-1;
8340 return;
8341 }
8342
8343 /*
8344 * Stop highlighting first, so it's easier to move the cursor.
8345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 if (screen_char_attr != 0)
8347 attr = screen_char_attr;
8348 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 attr = ScreenAttrs[off];
8350 if (screen_attr != attr)
8351 screen_stop_highlight();
8352
8353 windgoto(row, col);
8354
8355 if (screen_attr != attr)
8356 screen_start_highlight(attr);
8357
8358#ifdef FEAT_MBYTE
8359 if (enc_utf8 && ScreenLinesUC[off] != 0)
8360 {
8361 char_u buf[MB_MAXBYTES + 1];
8362
Bram Moolenaarcb070082016-04-02 22:14:51 +02008363 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008364 {
8365 if (*p_ambw == 'd'
8366# ifdef FEAT_GUI
8367 && !gui.in_use
8368# endif
8369 )
8370 {
8371 /* Clear the two screen cells. If the character is actually
8372 * single width it won't change the second cell. */
8373 out_str((char_u *)" ");
8374 term_windgoto(row, col);
8375 }
8376 /* not sure where the cursor is after drawing the ambiguous width
8377 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008378 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008379 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008380 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008382
8383 /* Convert the UTF-8 character to bytes and write it. */
8384 buf[utfc_char2bytes(off, buf)] = NUL;
8385 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 }
8387 else
8388#endif
8389 {
8390#ifdef FEAT_MBYTE
8391 out_flush_check();
8392#endif
8393 out_char(ScreenLines[off]);
8394#ifdef FEAT_MBYTE
8395 /* double-byte character in single-width cell */
8396 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8397 out_char(ScreenLines2[off]);
8398#endif
8399 }
8400
8401 screen_cur_col++;
8402}
8403
8404#ifdef FEAT_MBYTE
8405
8406/*
8407 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8408 * on the screen at position 'row' and 'col'.
8409 * The attributes of the first byte is used for all. This is required to
8410 * output the two bytes of a double-byte character with nothing in between.
8411 */
8412 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008413screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
8415 /* Check for illegal values (could be wrong when screen was resized). */
8416 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8417 return;
8418
8419 /* Outputting the last character on the screen may scrollup the screen.
8420 * Don't to it! Mark the character invalid (update it when scrolled up) */
8421 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8422 {
8423 ScreenAttrs[off] = (sattr_T)-1;
8424 return;
8425 }
8426
8427 /* Output the first byte normally (positions the cursor), then write the
8428 * second byte directly. */
8429 screen_char(off, row, col);
8430 out_char(ScreenLines[off + 1]);
8431 ++screen_cur_col;
8432}
8433#endif
8434
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435/*
8436 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8437 * This uses the contents of ScreenLines[] and doesn't change it.
8438 */
8439 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008440screen_draw_rectangle(
8441 int row,
8442 int col,
8443 int height,
8444 int width,
8445 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446{
8447 int r, c;
8448 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008449#ifdef FEAT_MBYTE
8450 int max_off;
8451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008453 /* Can't use ScreenLines unless initialized */
8454 if (ScreenLines == NULL)
8455 return;
8456
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 if (invert)
8458 screen_char_attr = HL_INVERSE;
8459 for (r = row; r < row + height; ++r)
8460 {
8461 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008462#ifdef FEAT_MBYTE
8463 max_off = off + screen_Columns;
8464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 for (c = col; c < col + width; ++c)
8466 {
8467#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008468 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {
8470 screen_char_2(off + c, r, c);
8471 ++c;
8472 }
8473 else
8474#endif
8475 {
8476 screen_char(off + c, r, c);
8477#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008478 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 ++c;
8480#endif
8481 }
8482 }
8483 }
8484 screen_char_attr = 0;
8485}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487/*
8488 * Redraw the characters for a vertically split window.
8489 */
8490 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008491redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
8493 int col;
8494 int width;
8495
8496# ifdef FEAT_CLIPBOARD
8497 clip_may_clear_selection(row, end - 1);
8498# endif
8499
8500 if (wp == NULL)
8501 {
8502 col = 0;
8503 width = Columns;
8504 }
8505 else
8506 {
8507 col = wp->w_wincol;
8508 width = wp->w_width;
8509 }
8510 screen_draw_rectangle(row, col, end - row, width, FALSE);
8511}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008513 static void
8514space_to_screenline(int off, int attr)
8515{
8516 ScreenLines[off] = ' ';
8517 ScreenAttrs[off] = attr;
8518# ifdef FEAT_MBYTE
8519 if (enc_utf8)
8520 ScreenLinesUC[off] = 0;
8521# endif
8522}
8523
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524/*
8525 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8526 * with character 'c1' in first column followed by 'c2' in the other columns.
8527 * Use attributes 'attr'.
8528 */
8529 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008530screen_fill(
8531 int start_row,
8532 int end_row,
8533 int start_col,
8534 int end_col,
8535 int c1,
8536 int c2,
8537 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
8539 int row;
8540 int col;
8541 int off;
8542 int end_off;
8543 int did_delete;
8544 int c;
8545 int norm_term;
8546#if defined(FEAT_GUI) || defined(UNIX)
8547 int force_next = FALSE;
8548#endif
8549
8550 if (end_row > screen_Rows) /* safety check */
8551 end_row = screen_Rows;
8552 if (end_col > screen_Columns) /* safety check */
8553 end_col = screen_Columns;
8554 if (ScreenLines == NULL
8555 || start_row >= end_row
8556 || start_col >= end_col) /* nothing to do */
8557 return;
8558
8559 /* it's a "normal" terminal when not in a GUI or cterm */
8560 norm_term = (
8561#ifdef FEAT_GUI
8562 !gui.in_use &&
8563#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008564 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 for (row = start_row; row < end_row; ++row)
8566 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008567#ifdef FEAT_MBYTE
8568 if (has_mbyte
8569# ifdef FEAT_GUI
8570 && !gui.in_use
8571# endif
8572 )
8573 {
8574 /* When drawing over the right halve of a double-wide char clear
8575 * out the left halve. When drawing over the left halve of a
8576 * double wide-char clear out the right halve. Only needed in a
8577 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008578 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008579 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008580 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008581 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008582 }
8583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 /*
8585 * Try to use delete-line termcap code, when no attributes or in a
8586 * "normal" terminal, where a bold/italic space is just a
8587 * space.
8588 */
8589 did_delete = FALSE;
8590 if (c2 == ' '
8591 && end_col == Columns
8592 && can_clear(T_CE)
8593 && (attr == 0
8594 || (norm_term
8595 && attr <= HL_ALL
8596 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8597 {
8598 /*
8599 * check if we really need to clear something
8600 */
8601 col = start_col;
8602 if (c1 != ' ') /* don't clear first char */
8603 ++col;
8604
8605 off = LineOffset[row] + col;
8606 end_off = LineOffset[row] + end_col;
8607
8608 /* skip blanks (used often, keep it fast!) */
8609#ifdef FEAT_MBYTE
8610 if (enc_utf8)
8611 while (off < end_off && ScreenLines[off] == ' '
8612 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8613 ++off;
8614 else
8615#endif
8616 while (off < end_off && ScreenLines[off] == ' '
8617 && ScreenAttrs[off] == 0)
8618 ++off;
8619 if (off < end_off) /* something to be cleared */
8620 {
8621 col = off - LineOffset[row];
8622 screen_stop_highlight();
8623 term_windgoto(row, col);/* clear rest of this screen line */
8624 out_str(T_CE);
8625 screen_start(); /* don't know where cursor is now */
8626 col = end_col - col;
8627 while (col--) /* clear chars in ScreenLines */
8628 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008629 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 ++off;
8631 }
8632 }
8633 did_delete = TRUE; /* the chars are cleared now */
8634 }
8635
8636 off = LineOffset[row] + start_col;
8637 c = c1;
8638 for (col = start_col; col < end_col; ++col)
8639 {
8640 if (ScreenLines[off] != c
8641#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008642 || (enc_utf8 && (int)ScreenLinesUC[off]
8643 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644#endif
8645 || ScreenAttrs[off] != attr
8646#if defined(FEAT_GUI) || defined(UNIX)
8647 || force_next
8648#endif
8649 )
8650 {
8651#if defined(FEAT_GUI) || defined(UNIX)
8652 /* The bold trick may make a single row of pixels appear in
8653 * the next character. When a bold character is removed, the
8654 * next character should be redrawn too. This happens for our
8655 * own GUI and for some xterms. */
8656 if (
8657# ifdef FEAT_GUI
8658 gui.in_use
8659# endif
8660# if defined(FEAT_GUI) && defined(UNIX)
8661 ||
8662# endif
8663# ifdef UNIX
8664 term_is_xterm
8665# endif
8666 )
8667 {
8668 if (ScreenLines[off] != ' '
8669 && (ScreenAttrs[off] > HL_ALL
8670 || ScreenAttrs[off] & HL_BOLD))
8671 force_next = TRUE;
8672 else
8673 force_next = FALSE;
8674 }
8675#endif
8676 ScreenLines[off] = c;
8677#ifdef FEAT_MBYTE
8678 if (enc_utf8)
8679 {
8680 if (c >= 0x80)
8681 {
8682 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008683 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 }
8685 else
8686 ScreenLinesUC[off] = 0;
8687 }
8688#endif
8689 ScreenAttrs[off] = attr;
8690 if (!did_delete || c != ' ')
8691 screen_char(off, row, col);
8692 }
8693 ++off;
8694 if (col == start_col)
8695 {
8696 if (did_delete)
8697 break;
8698 c = c2;
8699 }
8700 }
8701 if (end_col == Columns)
8702 LineWraps[row] = FALSE;
8703 if (row == Rows - 1) /* overwritten the command line */
8704 {
8705 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008706 if (start_col == 0 && end_col == Columns
8707 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008709 if (start_col == 0)
8710 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 }
8712 }
8713}
8714
8715/*
8716 * Check if there should be a delay. Used before clearing or redrawing the
8717 * screen or the command line.
8718 */
8719 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008720check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721{
8722 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8723 && !did_wait_return
8724 && emsg_silent == 0)
8725 {
8726 out_flush();
8727 ui_delay(1000L, TRUE);
8728 emsg_on_display = FALSE;
8729 if (check_msg_scroll)
8730 msg_scroll = FALSE;
8731 }
8732}
8733
8734/*
8735 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008736 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 * Returns TRUE if there is a valid screen to write to.
8738 * Returns FALSE when starting up and screen not initialized yet.
8739 */
8740 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008741screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008743 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 return (ScreenLines != NULL);
8745}
8746
8747/*
8748 * Resize the shell to Rows and Columns.
8749 * Allocate ScreenLines[] and associated items.
8750 *
8751 * There may be some time between setting Rows and Columns and (re)allocating
8752 * ScreenLines[]. This happens when starting up and when (manually) changing
8753 * the shell size. Always use screen_Rows and screen_Columns to access items
8754 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8755 * final size of the shell is needed.
8756 */
8757 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008758screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759{
8760 int new_row, old_row;
8761#ifdef FEAT_GUI
8762 int old_Rows;
8763#endif
8764 win_T *wp;
8765 int outofmem = FALSE;
8766 int len;
8767 schar_T *new_ScreenLines;
8768#ifdef FEAT_MBYTE
8769 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008770 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008772 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773#endif
8774 sattr_T *new_ScreenAttrs;
8775 unsigned *new_LineOffset;
8776 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008777 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008778 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008780 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008781 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008783retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 /*
8785 * Allocation of the screen buffers is done only when the size changes and
8786 * when Rows and Columns have been set and we have started doing full
8787 * screen stuff.
8788 */
8789 if ((ScreenLines != NULL
8790 && Rows == screen_Rows
8791 && Columns == screen_Columns
8792#ifdef FEAT_MBYTE
8793 && enc_utf8 == (ScreenLinesUC != NULL)
8794 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008795 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796#endif
8797 )
8798 || Rows == 0
8799 || Columns == 0
8800 || (!full_screen && ScreenLines == NULL))
8801 return;
8802
8803 /*
8804 * It's possible that we produce an out-of-memory message below, which
8805 * will cause this function to be called again. To break the loop, just
8806 * return here.
8807 */
8808 if (entered)
8809 return;
8810 entered = TRUE;
8811
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008812 /*
8813 * Note that the window sizes are updated before reallocating the arrays,
8814 * thus we must not redraw here!
8815 */
8816 ++RedrawingDisabled;
8817
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 win_new_shellsize(); /* fit the windows in the new sized shell */
8819
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 comp_col(); /* recompute columns for shown command and ruler */
8821
8822 /*
8823 * We're changing the size of the screen.
8824 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8825 * - Move lines from the old arrays into the new arrays, clear extra
8826 * lines (unless the screen is going to be cleared).
8827 * - Free the old arrays.
8828 *
8829 * If anything fails, make ScreenLines NULL, so we don't do anything!
8830 * Continuing with the old ScreenLines may result in a crash, because the
8831 * size is wrong.
8832 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008833 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008835 if (aucmd_win != NULL)
8836 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837
8838 new_ScreenLines = (schar_T *)lalloc((long_u)(
8839 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8840#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008841 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 if (enc_utf8)
8843 {
8844 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8845 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008846 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008847 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8849 }
8850 if (enc_dbcs == DBCS_JPNU)
8851 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8852 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8853#endif
8854 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8855 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8856 new_LineOffset = (unsigned *)lalloc((long_u)(
8857 Rows * sizeof(unsigned)), FALSE);
8858 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008859 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008861 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 {
8863 if (win_alloc_lines(wp) == FAIL)
8864 {
8865 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008866 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 }
8868 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008869 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8870 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008871 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008872give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008874#ifdef FEAT_MBYTE
8875 for (i = 0; i < p_mco; ++i)
8876 if (new_ScreenLinesC[i] == NULL)
8877 break;
8878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 if (new_ScreenLines == NULL
8880#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008881 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8883#endif
8884 || new_ScreenAttrs == NULL
8885 || new_LineOffset == NULL
8886 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008887 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 || outofmem)
8889 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008890 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008891 {
8892 /* guess the size */
8893 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8894
8895 /* Remember we did this to avoid getting outofmem messages over
8896 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008897 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008898 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008899 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008901 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008902 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008903 VIM_CLEAR(new_ScreenLinesC[i]);
8904 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008906 VIM_CLEAR(new_ScreenAttrs);
8907 VIM_CLEAR(new_LineOffset);
8908 VIM_CLEAR(new_LineWraps);
8909 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 }
8911 else
8912 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008913 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008914
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 for (new_row = 0; new_row < Rows; ++new_row)
8916 {
8917 new_LineOffset[new_row] = new_row * Columns;
8918 new_LineWraps[new_row] = FALSE;
8919
8920 /*
8921 * If the screen is not going to be cleared, copy as much as
8922 * possible from the old screen to the new one and clear the rest
8923 * (used when resizing the window at the "--more--" prompt or when
8924 * executing an external command, for the GUI).
8925 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008926 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 {
8928 (void)vim_memset(new_ScreenLines + new_row * Columns,
8929 ' ', (size_t)Columns * sizeof(schar_T));
8930#ifdef FEAT_MBYTE
8931 if (enc_utf8)
8932 {
8933 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8934 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008935 for (i = 0; i < p_mco; ++i)
8936 (void)vim_memset(new_ScreenLinesC[i]
8937 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 0, (size_t)Columns * sizeof(u8char_T));
8939 }
8940 if (enc_dbcs == DBCS_JPNU)
8941 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8942 0, (size_t)Columns * sizeof(schar_T));
8943#endif
8944 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8945 0, (size_t)Columns * sizeof(sattr_T));
8946 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008947 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 {
8949 if (screen_Columns < Columns)
8950 len = screen_Columns;
8951 else
8952 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008953#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008954 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008955 * may be invalid now. Also when p_mco changes. */
8956 if (!(enc_utf8 && ScreenLinesUC == NULL)
8957 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008958#endif
8959 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8960 ScreenLines + LineOffset[old_row],
8961 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008963 if (enc_utf8 && ScreenLinesUC != NULL
8964 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 {
8966 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8967 ScreenLinesUC + LineOffset[old_row],
8968 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008969 for (i = 0; i < p_mco; ++i)
8970 mch_memmove(new_ScreenLinesC[i]
8971 + new_LineOffset[new_row],
8972 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 (size_t)len * sizeof(u8char_T));
8974 }
8975 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8976 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8977 ScreenLines2 + LineOffset[old_row],
8978 (size_t)len * sizeof(schar_T));
8979#endif
8980 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8981 ScreenAttrs + LineOffset[old_row],
8982 (size_t)len * sizeof(sattr_T));
8983 }
8984 }
8985 }
8986 /* Use the last line of the screen for the current line. */
8987 current_ScreenLine = new_ScreenLines + Rows * Columns;
8988 }
8989
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008990 free_screenlines();
8991
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 ScreenLines = new_ScreenLines;
8993#ifdef FEAT_MBYTE
8994 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008995 for (i = 0; i < p_mco; ++i)
8996 ScreenLinesC[i] = new_ScreenLinesC[i];
8997 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 ScreenLines2 = new_ScreenLines2;
8999#endif
9000 ScreenAttrs = new_ScreenAttrs;
9001 LineOffset = new_LineOffset;
9002 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009003 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004
9005 /* It's important that screen_Rows and screen_Columns reflect the actual
9006 * size of ScreenLines[]. Set them before calling anything. */
9007#ifdef FEAT_GUI
9008 old_Rows = screen_Rows;
9009#endif
9010 screen_Rows = Rows;
9011 screen_Columns = Columns;
9012
9013 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009014 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 screenclear2();
9016
9017#ifdef FEAT_GUI
9018 else if (gui.in_use
9019 && !gui.starting
9020 && ScreenLines != NULL
9021 && old_Rows != Rows)
9022 {
9023 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9024 /*
9025 * Adjust the position of the cursor, for when executing an external
9026 * command.
9027 */
9028 if (msg_row >= Rows) /* Rows got smaller */
9029 msg_row = Rows - 1; /* put cursor at last row */
9030 else if (Rows > old_Rows) /* Rows got bigger */
9031 msg_row += Rows - old_Rows; /* put cursor in same place */
9032 if (msg_col >= Columns) /* Columns got smaller */
9033 msg_col = Columns - 1; /* put cursor at last column */
9034 }
9035#endif
9036
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009038 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009039
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009040 /*
9041 * Do not apply autocommands more than 3 times to avoid an endless loop
9042 * in case applying autocommands always changes Rows or Columns.
9043 */
9044 if (starting == 0 && ++retry_count <= 3)
9045 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009046 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009047 /* In rare cases, autocommands may have altered Rows or Columns,
9048 * jump back to check if we need to allocate the screen again. */
9049 goto retry;
9050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051}
9052
9053 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009054free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009055{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009056#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009057 int i;
9058
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009059 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009060 for (i = 0; i < Screen_mco; ++i)
9061 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009062 vim_free(ScreenLines2);
9063#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009064 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009065 vim_free(ScreenAttrs);
9066 vim_free(LineOffset);
9067 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009068 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009069}
9070
9071 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009072screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073{
9074 check_for_delay(FALSE);
9075 screenalloc(FALSE); /* allocate screen buffers if size changed */
9076 screenclear2(); /* clear the screen */
9077}
9078
9079 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009080screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082 int i;
9083
9084 if (starting == NO_SCREEN || ScreenLines == NULL
9085#ifdef FEAT_GUI
9086 || (gui.in_use && gui.starting)
9087#endif
9088 )
9089 return;
9090
9091#ifdef FEAT_GUI
9092 if (!gui.in_use)
9093#endif
9094 screen_attr = -1; /* force setting the Normal colors */
9095 screen_stop_highlight(); /* don't want highlighting here */
9096
9097#ifdef FEAT_CLIPBOARD
9098 /* disable selection without redrawing it */
9099 clip_scroll_selection(9999);
9100#endif
9101
9102 /* blank out ScreenLines */
9103 for (i = 0; i < Rows; ++i)
9104 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009105 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 LineWraps[i] = FALSE;
9107 }
9108
9109 if (can_clear(T_CL))
9110 {
9111 out_str(T_CL); /* clear the display */
9112 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009113 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 }
9115 else
9116 {
9117 /* can't clear the screen, mark all chars with invalid attributes */
9118 for (i = 0; i < Rows; ++i)
9119 lineinvalid(LineOffset[i], (int)Columns);
9120 clear_cmdline = TRUE;
9121 }
9122
9123 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9124
9125 win_rest_invalid(firstwin);
9126 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009127 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 if (must_redraw == CLEAR) /* no need to clear again */
9129 must_redraw = NOT_VALID;
9130 compute_cmdrow();
9131 msg_row = cmdline_row; /* put cursor on last line for messages */
9132 msg_col = 0;
9133 screen_start(); /* don't know where cursor is now */
9134 msg_scrolled = 0; /* can't scroll back */
9135 msg_didany = FALSE;
9136 msg_didout = FALSE;
9137}
9138
9139/*
9140 * Clear one line in ScreenLines.
9141 */
9142 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009143lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144{
9145 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9146#ifdef FEAT_MBYTE
9147 if (enc_utf8)
9148 (void)vim_memset(ScreenLinesUC + off, 0,
9149 (size_t)width * sizeof(u8char_T));
9150#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009151 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152}
9153
9154/*
9155 * Mark one line in ScreenLines invalid by setting the attributes to an
9156 * invalid value.
9157 */
9158 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009159lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160{
9161 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9162}
9163
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164/*
9165 * Copy part of a Screenline for vertically split window "wp".
9166 */
9167 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009168linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169{
9170 unsigned off_to = LineOffset[to] + wp->w_wincol;
9171 unsigned off_from = LineOffset[from] + wp->w_wincol;
9172
9173 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9174 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009175#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 if (enc_utf8)
9177 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009178 int i;
9179
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9181 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009182 for (i = 0; i < p_mco; ++i)
9183 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9184 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 }
9186 if (enc_dbcs == DBCS_JPNU)
9187 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9188 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9191 wp->w_width * sizeof(sattr_T));
9192}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193
9194/*
9195 * Return TRUE if clearing with term string "p" would work.
9196 * It can't work when the string is empty or it won't set the right background.
9197 */
9198 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009199can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
9201 return (*p != NUL && (t_colors <= 1
9202#ifdef FEAT_GUI
9203 || gui.in_use
9204#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009205#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009206 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009207 || (!p_tgc && cterm_normal_bg_color == 0)
9208#else
9209 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009210#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009211 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212}
9213
9214/*
9215 * Reset cursor position. Use whenever cursor was moved because of outputting
9216 * something directly to the screen (shell commands) or a terminal control
9217 * code.
9218 */
9219 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009220screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221{
9222 screen_cur_row = screen_cur_col = 9999;
9223}
9224
9225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 * Move the cursor to position "row","col" in the screen.
9227 * This tries to find the most efficient way to move, minimizing the number of
9228 * characters sent to the terminal.
9229 */
9230 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009231windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009233 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 int i;
9235 int plan;
9236 int cost;
9237 int wouldbe_col;
9238 int noinvcurs;
9239 char_u *bs;
9240 int goto_cost;
9241 int attr;
9242
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009243#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9245
9246#define PLAN_LE 1
9247#define PLAN_CR 2
9248#define PLAN_NL 3
9249#define PLAN_WRITE 4
9250 /* Can't use ScreenLines unless initialized */
9251 if (ScreenLines == NULL)
9252 return;
9253
9254 if (col != screen_cur_col || row != screen_cur_row)
9255 {
9256 /* Check for valid position. */
9257 if (row < 0) /* window without text lines? */
9258 row = 0;
9259 if (row >= screen_Rows)
9260 row = screen_Rows - 1;
9261 if (col >= screen_Columns)
9262 col = screen_Columns - 1;
9263
9264 /* check if no cursor movement is allowed in highlight mode */
9265 if (screen_attr && *T_MS == NUL)
9266 noinvcurs = HIGHL_COST;
9267 else
9268 noinvcurs = 0;
9269 goto_cost = GOTO_COST + noinvcurs;
9270
9271 /*
9272 * Plan how to do the positioning:
9273 * 1. Use CR to move it to column 0, same row.
9274 * 2. Use T_LE to move it a few columns to the left.
9275 * 3. Use NL to move a few lines down, column 0.
9276 * 4. Move a few columns to the right with T_ND or by writing chars.
9277 *
9278 * Don't do this if the cursor went beyond the last column, the cursor
9279 * position is unknown then (some terminals wrap, some don't )
9280 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009281 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 * characters to move the cursor to the right.
9283 */
9284 if (row >= screen_cur_row && screen_cur_col < Columns)
9285 {
9286 /*
9287 * If the cursor is in the same row, bigger col, we can use CR
9288 * or T_LE.
9289 */
9290 bs = NULL; /* init for GCC */
9291 attr = screen_attr;
9292 if (row == screen_cur_row && col < screen_cur_col)
9293 {
9294 /* "le" is preferred over "bc", because "bc" is obsolete */
9295 if (*T_LE)
9296 bs = T_LE; /* "cursor left" */
9297 else
9298 bs = T_BC; /* "backspace character (old) */
9299 if (*bs)
9300 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9301 else
9302 cost = 999;
9303 if (col + 1 < cost) /* using CR is less characters */
9304 {
9305 plan = PLAN_CR;
9306 wouldbe_col = 0;
9307 cost = 1; /* CR is just one character */
9308 }
9309 else
9310 {
9311 plan = PLAN_LE;
9312 wouldbe_col = col;
9313 }
9314 if (noinvcurs) /* will stop highlighting */
9315 {
9316 cost += noinvcurs;
9317 attr = 0;
9318 }
9319 }
9320
9321 /*
9322 * If the cursor is above where we want to be, we can use CR LF.
9323 */
9324 else if (row > screen_cur_row)
9325 {
9326 plan = PLAN_NL;
9327 wouldbe_col = 0;
9328 cost = (row - screen_cur_row) * 2; /* CR LF */
9329 if (noinvcurs) /* will stop highlighting */
9330 {
9331 cost += noinvcurs;
9332 attr = 0;
9333 }
9334 }
9335
9336 /*
9337 * If the cursor is in the same row, smaller col, just use write.
9338 */
9339 else
9340 {
9341 plan = PLAN_WRITE;
9342 wouldbe_col = screen_cur_col;
9343 cost = 0;
9344 }
9345
9346 /*
9347 * Check if any characters that need to be written have the
9348 * correct attributes. Also avoid UTF-8 characters.
9349 */
9350 i = col - wouldbe_col;
9351 if (i > 0)
9352 cost += i;
9353 if (cost < goto_cost && i > 0)
9354 {
9355 /*
9356 * Check if the attributes are correct without additionally
9357 * stopping highlighting.
9358 */
9359 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9360 while (i && *p++ == attr)
9361 --i;
9362 if (i != 0)
9363 {
9364 /*
9365 * Try if it works when highlighting is stopped here.
9366 */
9367 if (*--p == 0)
9368 {
9369 cost += noinvcurs;
9370 while (i && *p++ == 0)
9371 --i;
9372 }
9373 if (i != 0)
9374 cost = 999; /* different attributes, don't do it */
9375 }
9376#ifdef FEAT_MBYTE
9377 if (enc_utf8)
9378 {
9379 /* Don't use an UTF-8 char for positioning, it's slow. */
9380 for (i = wouldbe_col; i < col; ++i)
9381 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9382 {
9383 cost = 999;
9384 break;
9385 }
9386 }
9387#endif
9388 }
9389
9390 /*
9391 * We can do it without term_windgoto()!
9392 */
9393 if (cost < goto_cost)
9394 {
9395 if (plan == PLAN_LE)
9396 {
9397 if (noinvcurs)
9398 screen_stop_highlight();
9399 while (screen_cur_col > col)
9400 {
9401 out_str(bs);
9402 --screen_cur_col;
9403 }
9404 }
9405 else if (plan == PLAN_CR)
9406 {
9407 if (noinvcurs)
9408 screen_stop_highlight();
9409 out_char('\r');
9410 screen_cur_col = 0;
9411 }
9412 else if (plan == PLAN_NL)
9413 {
9414 if (noinvcurs)
9415 screen_stop_highlight();
9416 while (screen_cur_row < row)
9417 {
9418 out_char('\n');
9419 ++screen_cur_row;
9420 }
9421 screen_cur_col = 0;
9422 }
9423
9424 i = col - screen_cur_col;
9425 if (i > 0)
9426 {
9427 /*
9428 * Use cursor-right if it's one character only. Avoids
9429 * removing a line of pixels from the last bold char, when
9430 * using the bold trick in the GUI.
9431 */
9432 if (T_ND[0] != NUL && T_ND[1] == NUL)
9433 {
9434 while (i-- > 0)
9435 out_char(*T_ND);
9436 }
9437 else
9438 {
9439 int off;
9440
9441 off = LineOffset[row] + screen_cur_col;
9442 while (i-- > 0)
9443 {
9444 if (ScreenAttrs[off] != screen_attr)
9445 screen_stop_highlight();
9446#ifdef FEAT_MBYTE
9447 out_flush_check();
9448#endif
9449 out_char(ScreenLines[off]);
9450#ifdef FEAT_MBYTE
9451 if (enc_dbcs == DBCS_JPNU
9452 && ScreenLines[off] == 0x8e)
9453 out_char(ScreenLines2[off]);
9454#endif
9455 ++off;
9456 }
9457 }
9458 }
9459 }
9460 }
9461 else
9462 cost = 999;
9463
9464 if (cost >= goto_cost)
9465 {
9466 if (noinvcurs)
9467 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009468 if (row == screen_cur_row && (col > screen_cur_col)
9469 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 term_cursor_right(col - screen_cur_col);
9471 else
9472 term_windgoto(row, col);
9473 }
9474 screen_cur_row = row;
9475 screen_cur_col = col;
9476 }
9477}
9478
9479/*
9480 * Set cursor to its position in the current window.
9481 */
9482 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009483setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009485 setcursor_mayforce(FALSE);
9486}
9487
9488/*
9489 * Set cursor to its position in the current window.
9490 * When "force" is TRUE also when not redrawing.
9491 */
9492 void
9493setcursor_mayforce(int force)
9494{
9495 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 {
9497 validate_cursor();
9498 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009499 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009501 /* With 'rightleft' set and the cursor on a double-wide
9502 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009503 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009505 (has_mbyte
9506 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9507 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508# endif
9509 1)) :
9510#endif
9511 curwin->w_wcol));
9512 }
9513}
9514
9515
9516/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009517 * Insert 'line_count' lines at 'row' in window 'wp'.
9518 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9519 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 * scrolling.
9521 * Returns FAIL if the lines are not inserted, OK for success.
9522 */
9523 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009524win_ins_lines(
9525 win_T *wp,
9526 int row,
9527 int line_count,
9528 int invalid,
9529 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530{
9531 int did_delete;
9532 int nextrow;
9533 int lastrow;
9534 int retval;
9535
9536 if (invalid)
9537 wp->w_lines_valid = 0;
9538
9539 if (wp->w_height < 5)
9540 return FAIL;
9541
9542 if (line_count > wp->w_height - row)
9543 line_count = wp->w_height - row;
9544
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009545 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 if (retval != MAYBE)
9547 return retval;
9548
9549 /*
9550 * If there is a next window or a status line, we first try to delete the
9551 * lines at the bottom to avoid messing what is after the window.
9552 * If this fails and there are following windows, don't do anything to avoid
9553 * messing up those windows, better just redraw.
9554 */
9555 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 if (wp->w_next != NULL || wp->w_status_height)
9557 {
9558 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009559 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 did_delete = TRUE;
9561 else if (wp->w_next)
9562 return FAIL;
9563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 /*
9565 * if no lines deleted, blank the lines that will end up below the window
9566 */
9567 if (!did_delete)
9568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009571 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 lastrow = nextrow + line_count;
9573 if (lastrow > Rows)
9574 lastrow = Rows;
9575 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009576 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 ' ', ' ', 0);
9578 }
9579
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009580 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 == FAIL)
9582 {
9583 /* deletion will have messed up other windows */
9584 if (did_delete)
9585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 win_rest_invalid(W_NEXT(wp));
9588 }
9589 return FAIL;
9590 }
9591
9592 return OK;
9593}
9594
9595/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009596 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9598 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9599 * scrolling
9600 * Return OK for success, FAIL if the lines are not deleted.
9601 */
9602 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009603win_del_lines(
9604 win_T *wp,
9605 int row,
9606 int line_count,
9607 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009608 int mayclear,
9609 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
9611 int retval;
9612
9613 if (invalid)
9614 wp->w_lines_valid = 0;
9615
9616 if (line_count > wp->w_height - row)
9617 line_count = wp->w_height - row;
9618
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009619 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 if (retval != MAYBE)
9621 return retval;
9622
9623 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009624 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 return FAIL;
9626
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 /*
9628 * If there are windows or status lines below, try to put them at the
9629 * correct place. If we can't do that, they have to be redrawn.
9630 */
9631 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9632 {
9633 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009634 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 {
9636 wp->w_redr_status = TRUE;
9637 win_rest_invalid(wp->w_next);
9638 }
9639 }
9640 /*
9641 * If this is the last window and there is no status line, redraw the
9642 * command line later.
9643 */
9644 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 redraw_cmdline = TRUE;
9646 return OK;
9647}
9648
9649/*
9650 * Common code for win_ins_lines() and win_del_lines().
9651 * Returns OK or FAIL when the work has been done.
9652 * Returns MAYBE when not finished yet.
9653 */
9654 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009655win_do_lines(
9656 win_T *wp,
9657 int row,
9658 int line_count,
9659 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009660 int del,
9661 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662{
9663 int retval;
9664
9665 if (!redrawing() || line_count <= 0)
9666 return FAIL;
9667
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009668 /* When inserting lines would result in loss of command output, just redraw
9669 * the lines. */
9670 if (no_win_do_lines_ins && !del)
9671 return FAIL;
9672
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009674 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009676 if (!no_win_do_lines_ins)
9677 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 return FAIL;
9679 }
9680
9681 /*
9682 * Delete all remaining lines
9683 */
9684 if (row + line_count >= wp->w_height)
9685 {
9686 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009687 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 ' ', ' ', 0);
9689 return OK;
9690 }
9691
9692 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009693 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009695 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009697 if (!no_win_do_lines_ins)
9698 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699
9700 /*
9701 * If the terminal can set a scroll region, use that.
9702 * Always do this in a vertically split window. This will redraw from
9703 * ScreenLines[] when t_CV isn't defined. That's faster than using
9704 * win_line().
9705 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009706 * a character in the lower right corner of the scroll region may cause a
9707 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009709 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 scroll_region_set(wp, row);
9713 if (del)
9714 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009715 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 else
9717 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009718 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 scroll_region_reset();
9721 return retval;
9722 }
9723
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9725 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726
9727 return MAYBE;
9728}
9729
9730/*
9731 * window 'wp' and everything after it is messed up, mark it for redraw
9732 */
9733 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009734win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 {
9738 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 wp->w_redr_status = TRUE;
9740 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 }
9742 redraw_cmdline = TRUE;
9743}
9744
9745/*
9746 * The rest of the routines in this file perform screen manipulations. The
9747 * given operation is performed physically on the screen. The corresponding
9748 * change is also made to the internal screen image. In this way, the editor
9749 * anticipates the effect of editing changes on the appearance of the screen.
9750 * That way, when we call screenupdate a complete redraw isn't usually
9751 * necessary. Another advantage is that we can keep adding code to anticipate
9752 * screen changes, and in the meantime, everything still works.
9753 */
9754
9755/*
9756 * types for inserting or deleting lines
9757 */
9758#define USE_T_CAL 1
9759#define USE_T_CDL 2
9760#define USE_T_AL 3
9761#define USE_T_CE 4
9762#define USE_T_DL 5
9763#define USE_T_SR 6
9764#define USE_NL 7
9765#define USE_T_CD 8
9766#define USE_REDRAW 9
9767
9768/*
9769 * insert lines on the screen and update ScreenLines[]
9770 * 'end' is the line after the scrolled part. Normally it is Rows.
9771 * When scrolling region used 'off' is the offset from the top for the region.
9772 * 'row' and 'end' are relative to the start of the region.
9773 *
9774 * return FAIL for failure, OK for success.
9775 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009776 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009777screen_ins_lines(
9778 int off,
9779 int row,
9780 int line_count,
9781 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009782 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009783 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784{
9785 int i;
9786 int j;
9787 unsigned temp;
9788 int cursor_row;
9789 int type;
9790 int result_empty;
9791 int can_ce = can_clear(T_CE);
9792
9793 /*
9794 * FAIL if
9795 * - there is no valid screen
9796 * - the screen has to be redrawn completely
9797 * - the line count is less than one
9798 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009799 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009801 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9802#ifdef FEAT_CLIPBOARD
9803 || (clip_star.state != SELECT_CLEARED
9804 && redrawing_for_callback > 0)
9805#endif
9806 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 return FAIL;
9808
9809 /*
9810 * There are seven ways to insert lines:
9811 * 0. When in a vertically split window and t_CV isn't set, redraw the
9812 * characters from ScreenLines[].
9813 * 1. Use T_CD (clear to end of display) if it exists and the result of
9814 * the insert is just empty lines
9815 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9816 * present or line_count > 1. It looks better if we do all the inserts
9817 * at once.
9818 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9819 * insert is just empty lines and T_CE is not present or line_count >
9820 * 1.
9821 * 4. Use T_AL (insert line) if it exists.
9822 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9823 * just empty lines.
9824 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9825 * just empty lines.
9826 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9827 * the 'da' flag is not set or we have clear line capability.
9828 * 8. redraw the characters from ScreenLines[].
9829 *
9830 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9831 * the scrollbar for the window. It does have insert line, use that if it
9832 * exists.
9833 */
9834 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9836 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009837 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 type = USE_T_CD;
9839 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9840 type = USE_T_CAL;
9841 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9842 type = USE_T_CDL;
9843 else if (*T_AL != NUL)
9844 type = USE_T_AL;
9845 else if (can_ce && result_empty)
9846 type = USE_T_CE;
9847 else if (*T_DL != NUL && result_empty)
9848 type = USE_T_DL;
9849 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9850 type = USE_T_SR;
9851 else
9852 return FAIL;
9853
9854 /*
9855 * For clearing the lines screen_del_lines() is used. This will also take
9856 * care of t_db if necessary.
9857 */
9858 if (type == USE_T_CD || type == USE_T_CDL ||
9859 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009860 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861
9862 /*
9863 * If text is retained below the screen, first clear or delete as many
9864 * lines at the bottom of the window as are about to be inserted so that
9865 * the deleted lines won't later surface during a screen_del_lines.
9866 */
9867 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009868 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
9870#ifdef FEAT_CLIPBOARD
9871 /* Remove a modeless selection when inserting lines halfway the screen
9872 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009873 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009874 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 else
9876 clip_scroll_selection(-line_count);
9877#endif
9878
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879#ifdef FEAT_GUI
9880 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9881 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009882 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883#endif
9884
9885 if (*T_CCS != NUL) /* cursor relative to region */
9886 cursor_row = row;
9887 else
9888 cursor_row = row + off;
9889
9890 /*
9891 * Shift LineOffset[] line_count down to reflect the inserted lines.
9892 * Clear the inserted lines in ScreenLines[].
9893 */
9894 row += off;
9895 end += off;
9896 for (i = 0; i < line_count; ++i)
9897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 if (wp != NULL && wp->w_width != Columns)
9899 {
9900 /* need to copy part of a line */
9901 j = end - 1 - i;
9902 while ((j -= line_count) >= row)
9903 linecopy(j + line_count, j, wp);
9904 j += line_count;
9905 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009906 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9907 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 else
9909 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9910 LineWraps[j] = FALSE;
9911 }
9912 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 {
9914 j = end - 1 - i;
9915 temp = LineOffset[j];
9916 while ((j -= line_count) >= row)
9917 {
9918 LineOffset[j + line_count] = LineOffset[j];
9919 LineWraps[j + line_count] = LineWraps[j];
9920 }
9921 LineOffset[j + line_count] = temp;
9922 LineWraps[j + line_count] = FALSE;
9923 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009924 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 else
9926 lineinvalid(temp, (int)Columns);
9927 }
9928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929
9930 screen_stop_highlight();
9931 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009932 if (clear_attr != 0)
9933 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 /* redraw the characters */
9936 if (type == USE_REDRAW)
9937 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009938 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 {
9940 term_append_lines(line_count);
9941 screen_start(); /* don't know where cursor is now */
9942 }
9943 else
9944 {
9945 for (i = 0; i < line_count; i++)
9946 {
9947 if (type == USE_T_AL)
9948 {
9949 if (i && cursor_row != 0)
9950 windgoto(cursor_row, 0);
9951 out_str(T_AL);
9952 }
9953 else /* type == USE_T_SR */
9954 out_str(T_SR);
9955 screen_start(); /* don't know where cursor is now */
9956 }
9957 }
9958
9959 /*
9960 * With scroll-reverse and 'da' flag set we need to clear the lines that
9961 * have been scrolled down into the region.
9962 */
9963 if (type == USE_T_SR && *T_DA)
9964 {
9965 for (i = 0; i < line_count; ++i)
9966 {
9967 windgoto(off + i, 0);
9968 out_str(T_CE);
9969 screen_start(); /* don't know where cursor is now */
9970 }
9971 }
9972
9973#ifdef FEAT_GUI
9974 gui_can_update_cursor();
9975 if (gui.in_use)
9976 out_flush(); /* always flush after a scroll */
9977#endif
9978 return OK;
9979}
9980
9981/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009982 * Delete lines on the screen and update ScreenLines[].
9983 * "end" is the line after the scrolled part. Normally it is Rows.
9984 * When scrolling region used "off" is the offset from the top for the region.
9985 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 *
9987 * Return OK for success, FAIL if the lines are not deleted.
9988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009990screen_del_lines(
9991 int off,
9992 int row,
9993 int line_count,
9994 int end,
9995 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009996 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009997 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998{
9999 int j;
10000 int i;
10001 unsigned temp;
10002 int cursor_row;
10003 int cursor_end;
10004 int result_empty; /* result is empty until end of region */
10005 int can_delete; /* deleting line codes can be used */
10006 int type;
10007
10008 /*
10009 * FAIL if
10010 * - there is no valid screen
10011 * - the screen has to be redrawn completely
10012 * - the line count is less than one
10013 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010014 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010016 if (!screen_valid(TRUE) || line_count <= 0
10017 || (!force && line_count > p_ttyscroll)
10018#ifdef FEAT_CLIPBOARD
10019 || (clip_star.state != SELECT_CLEARED
10020 && redrawing_for_callback > 0)
10021#endif
10022 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 return FAIL;
10024
10025 /*
10026 * Check if the rest of the current region will become empty.
10027 */
10028 result_empty = row + line_count >= end;
10029
10030 /*
10031 * We can delete lines only when 'db' flag not set or when 'ce' option
10032 * available.
10033 */
10034 can_delete = (*T_DB == NUL || can_clear(T_CE));
10035
10036 /*
10037 * There are six ways to delete lines:
10038 * 0. When in a vertically split window and t_CV isn't set, redraw the
10039 * characters from ScreenLines[].
10040 * 1. Use T_CD if it exists and the result is empty.
10041 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10042 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10043 * none of the other ways work.
10044 * 4. Use T_CE (erase line) if the result is empty.
10045 * 5. Use T_DL (delete line) if it exists.
10046 * 6. redraw the characters from ScreenLines[].
10047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10049 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010050 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 type = USE_T_CD;
10052#if defined(__BEOS__) && defined(BEOS_DR8)
10053 /*
10054 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10055 * its internal termcap... this works okay for tests which test *T_DB !=
10056 * NUL. It has the disadvantage that the user cannot use any :set t_*
10057 * command to get T_DB (back) to empty_option, only :set term=... will do
10058 * the trick...
10059 * Anyway, this hack will hopefully go away with the next OS release.
10060 * (Olaf Seibert)
10061 */
10062 else if (row == 0 && T_DB == empty_option
10063 && (line_count == 1 || *T_CDL == NUL))
10064#else
10065 else if (row == 0 && (
10066#ifndef AMIGA
10067 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10068 * up, so use delete-line command */
10069 line_count == 1 ||
10070#endif
10071 *T_CDL == NUL))
10072#endif
10073 type = USE_NL;
10074 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10075 type = USE_T_CDL;
10076 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010077 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 type = USE_T_CE;
10079 else if (*T_DL != NUL && can_delete)
10080 type = USE_T_DL;
10081 else if (*T_CDL != NUL && can_delete)
10082 type = USE_T_CDL;
10083 else
10084 return FAIL;
10085
10086#ifdef FEAT_CLIPBOARD
10087 /* Remove a modeless selection when deleting lines halfway the screen or
10088 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010089 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010090 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 else
10092 clip_scroll_selection(line_count);
10093#endif
10094
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095#ifdef FEAT_GUI
10096 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10097 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010098 gui_dont_update_cursor(gui.cursor_row >= row + off
10099 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100#endif
10101
10102 if (*T_CCS != NUL) /* cursor relative to region */
10103 {
10104 cursor_row = row;
10105 cursor_end = end;
10106 }
10107 else
10108 {
10109 cursor_row = row + off;
10110 cursor_end = end + off;
10111 }
10112
10113 /*
10114 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10115 * Clear the inserted lines in ScreenLines[].
10116 */
10117 row += off;
10118 end += off;
10119 for (i = 0; i < line_count; ++i)
10120 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 if (wp != NULL && wp->w_width != Columns)
10122 {
10123 /* need to copy part of a line */
10124 j = row + i;
10125 while ((j += line_count) <= end - 1)
10126 linecopy(j - line_count, j, wp);
10127 j -= line_count;
10128 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010129 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10130 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 else
10132 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10133 LineWraps[j] = FALSE;
10134 }
10135 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 {
10137 /* whole width, moving the line pointers is faster */
10138 j = row + i;
10139 temp = LineOffset[j];
10140 while ((j += line_count) <= end - 1)
10141 {
10142 LineOffset[j - line_count] = LineOffset[j];
10143 LineWraps[j - line_count] = LineWraps[j];
10144 }
10145 LineOffset[j - line_count] = temp;
10146 LineWraps[j - line_count] = FALSE;
10147 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010148 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 else
10150 lineinvalid(temp, (int)Columns);
10151 }
10152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010154 if (screen_attr != clear_attr)
10155 screen_stop_highlight();
10156 if (clear_attr != 0)
10157 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 /* redraw the characters */
10160 if (type == USE_REDRAW)
10161 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010162 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 {
10164 windgoto(cursor_row, 0);
10165 out_str(T_CD);
10166 screen_start(); /* don't know where cursor is now */
10167 }
10168 else if (type == USE_T_CDL)
10169 {
10170 windgoto(cursor_row, 0);
10171 term_delete_lines(line_count);
10172 screen_start(); /* don't know where cursor is now */
10173 }
10174 /*
10175 * Deleting lines at top of the screen or scroll region: Just scroll
10176 * the whole screen (scroll region) up by outputting newlines on the
10177 * last line.
10178 */
10179 else if (type == USE_NL)
10180 {
10181 windgoto(cursor_end - 1, 0);
10182 for (i = line_count; --i >= 0; )
10183 out_char('\n'); /* cursor will remain on same line */
10184 }
10185 else
10186 {
10187 for (i = line_count; --i >= 0; )
10188 {
10189 if (type == USE_T_DL)
10190 {
10191 windgoto(cursor_row, 0);
10192 out_str(T_DL); /* delete a line */
10193 }
10194 else /* type == USE_T_CE */
10195 {
10196 windgoto(cursor_row + i, 0);
10197 out_str(T_CE); /* erase a line */
10198 }
10199 screen_start(); /* don't know where cursor is now */
10200 }
10201 }
10202
10203 /*
10204 * If the 'db' flag is set, we need to clear the lines that have been
10205 * scrolled up at the bottom of the region.
10206 */
10207 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10208 {
10209 for (i = line_count; i > 0; --i)
10210 {
10211 windgoto(cursor_end - i, 0);
10212 out_str(T_CE); /* erase a line */
10213 screen_start(); /* don't know where cursor is now */
10214 }
10215 }
10216
10217#ifdef FEAT_GUI
10218 gui_can_update_cursor();
10219 if (gui.in_use)
10220 out_flush(); /* always flush after a scroll */
10221#endif
10222
10223 return OK;
10224}
10225
10226/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010227 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 *
10229 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10230 * If clear_cmdline is FALSE there may be a message there that needs to be
10231 * cleared only if a mode is shown.
10232 * Return the length of the message (0 if no message).
10233 */
10234 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010235showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236{
10237 int need_clear;
10238 int length = 0;
10239 int do_mode;
10240 int attr;
10241 int nwr_save;
10242#ifdef FEAT_INS_EXPAND
10243 int sub_attr;
10244#endif
10245
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010246 do_mode = ((p_smd && msg_silent == 0)
10247 && ((State & INSERT)
10248 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010249 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 if (do_mode || Recording)
10251 {
10252 /*
10253 * Don't show mode right now, when not redrawing or inside a mapping.
10254 * Call char_avail() only when we are going to show something, because
10255 * it takes a bit of time.
10256 */
10257 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10258 {
10259 redraw_cmdline = TRUE; /* show mode later */
10260 return 0;
10261 }
10262
10263 nwr_save = need_wait_return;
10264
10265 /* wait a bit before overwriting an important message */
10266 check_for_delay(FALSE);
10267
10268 /* if the cmdline is more than one line high, erase top lines */
10269 need_clear = clear_cmdline;
10270 if (clear_cmdline && cmdline_row < Rows - 1)
10271 msg_clr_cmdline(); /* will reset clear_cmdline */
10272
10273 /* Position on the last line in the window, column 0 */
10274 msg_pos_mode();
10275 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010276 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 if (do_mode)
10278 {
10279 MSG_PUTS_ATTR("--", attr);
10280#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010281 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010282# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010283 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010284# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010285 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010286# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010287 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010288# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 MSG_PUTS_ATTR(" IM", attr);
10290# else
10291 MSG_PUTS_ATTR(" XIM", attr);
10292# endif
10293#endif
10294#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10295 if (gui.in_use)
10296 {
10297 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010298 {
10299 /* HANGUL */
10300 if (enc_utf8)
10301 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10302 else
10303 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 }
10306#endif
10307#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010308 /* CTRL-X in Insert mode */
10309 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 {
10311 /* These messages can get long, avoid a wrap in a narrow
10312 * window. Prefer showing edit_submode_extra. */
10313 length = (Rows - msg_row) * Columns - 3;
10314 if (edit_submode_extra != NULL)
10315 length -= vim_strsize(edit_submode_extra);
10316 if (length > 0)
10317 {
10318 if (edit_submode_pre != NULL)
10319 length -= vim_strsize(edit_submode_pre);
10320 if (length - vim_strsize(edit_submode) > 0)
10321 {
10322 if (edit_submode_pre != NULL)
10323 msg_puts_attr(edit_submode_pre, attr);
10324 msg_puts_attr(edit_submode, attr);
10325 }
10326 if (edit_submode_extra != NULL)
10327 {
10328 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10329 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010330 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 else
10332 sub_attr = attr;
10333 msg_puts_attr(edit_submode_extra, sub_attr);
10334 }
10335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 }
10337 else
10338#endif
10339 {
10340#ifdef FEAT_VREPLACE
10341 if (State & VREPLACE_FLAG)
10342 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10343 else
10344#endif
10345 if (State & REPLACE_FLAG)
10346 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10347 else if (State & INSERT)
10348 {
10349#ifdef FEAT_RIGHTLEFT
10350 if (p_ri)
10351 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10352#endif
10353 MSG_PUTS_ATTR(_(" INSERT"), attr);
10354 }
10355 else if (restart_edit == 'I')
10356 MSG_PUTS_ATTR(_(" (insert)"), attr);
10357 else if (restart_edit == 'R')
10358 MSG_PUTS_ATTR(_(" (replace)"), attr);
10359 else if (restart_edit == 'V')
10360 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10361#ifdef FEAT_RIGHTLEFT
10362 if (p_hkmap)
10363 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10364# ifdef FEAT_FKMAP
10365 if (p_fkmap)
10366 MSG_PUTS_ATTR(farsi_text_5, attr);
10367# endif
10368#endif
10369#ifdef FEAT_KEYMAP
10370 if (State & LANGMAP)
10371 {
10372# ifdef FEAT_ARABIC
10373 if (curwin->w_p_arab)
10374 MSG_PUTS_ATTR(_(" Arabic"), attr);
10375 else
10376# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010377 if (get_keymap_str(curwin, (char_u *)" (%s)",
10378 NameBuff, MAXPATHL))
10379 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 }
10381#endif
10382 if ((State & INSERT) && p_paste)
10383 MSG_PUTS_ATTR(_(" (paste)"), attr);
10384
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 if (VIsual_active)
10386 {
10387 char *p;
10388
10389 /* Don't concatenate separate words to avoid translation
10390 * problems. */
10391 switch ((VIsual_select ? 4 : 0)
10392 + (VIsual_mode == Ctrl_V) * 2
10393 + (VIsual_mode == 'V'))
10394 {
10395 case 0: p = N_(" VISUAL"); break;
10396 case 1: p = N_(" VISUAL LINE"); break;
10397 case 2: p = N_(" VISUAL BLOCK"); break;
10398 case 4: p = N_(" SELECT"); break;
10399 case 5: p = N_(" SELECT LINE"); break;
10400 default: p = N_(" SELECT BLOCK"); break;
10401 }
10402 MSG_PUTS_ATTR(_(p), attr);
10403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 MSG_PUTS_ATTR(" --", attr);
10405 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010406
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 need_clear = TRUE;
10408 }
10409 if (Recording
10410#ifdef FEAT_INS_EXPAND
10411 && edit_submode == NULL /* otherwise it gets too long */
10412#endif
10413 )
10414 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010415 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 need_clear = TRUE;
10417 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010418
10419 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 if (need_clear || clear_cmdline)
10421 msg_clr_eos();
10422 msg_didout = FALSE; /* overwrite this message */
10423 length = msg_col;
10424 msg_col = 0;
10425 need_wait_return = nwr_save; /* never ask for hit-return for this */
10426 }
10427 else if (clear_cmdline && msg_silent == 0)
10428 /* Clear the whole command line. Will reset "clear_cmdline". */
10429 msg_clr_cmdline();
10430
10431#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 /* In Visual mode the size of the selected area must be redrawn. */
10433 if (VIsual_active)
10434 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435
10436 /* If the last window has no status line, the ruler is after the mode
10437 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010438 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 win_redr_ruler(lastwin, TRUE);
10440#endif
10441 redraw_cmdline = FALSE;
10442 clear_cmdline = FALSE;
10443
10444 return length;
10445}
10446
10447/*
10448 * Position for a mode message.
10449 */
10450 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010451msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452{
10453 msg_col = 0;
10454 msg_row = Rows - 1;
10455}
10456
10457/*
10458 * Delete mode message. Used when ESC is typed which is expected to end
10459 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010460 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 */
10462 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010463unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464{
10465 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010466 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 */
10468 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10469 redraw_cmdline = TRUE; /* delete mode later */
10470 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010471 clearmode();
10472}
10473
10474/*
10475 * Clear the mode message.
10476 */
10477 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010478clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010479{
10480 msg_pos_mode();
10481 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010482 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010483 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484}
10485
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010486 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010487recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010488{
10489 MSG_PUTS_ATTR(_("recording"), attr);
10490 if (!shortmess(SHM_RECORDING))
10491 {
10492 char_u s[4];
10493 sprintf((char *)s, " @%c", Recording);
10494 MSG_PUTS_ATTR(s, attr);
10495 }
10496}
10497
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010498/*
10499 * Draw the tab pages line at the top of the Vim window.
10500 */
10501 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010502draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010503{
10504 int tabcount = 0;
10505 tabpage_T *tp;
10506 int tabwidth;
10507 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010508 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010509 int attr;
10510 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010511 win_T *cwp;
10512 int wincount;
10513 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010514 int c;
10515 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010516 int attr_sel = HL_ATTR(HLF_TPS);
10517 int attr_nosel = HL_ATTR(HLF_TP);
10518 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010519 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010520 int room;
10521 int use_sep_chars = (t_colors < 8
10522#ifdef FEAT_GUI
10523 && !gui.in_use
10524#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010525#ifdef FEAT_TERMGUICOLORS
10526 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010527#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010528 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010529
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010530 if (ScreenLines == NULL)
10531 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010532 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010533
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010534#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010535 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010536 if (gui_use_tabline())
10537 {
10538 gui_update_tabline();
10539 return;
10540 }
10541#endif
10542
10543 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010544 return;
10545
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010546#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010547
10548 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10549 for (scol = 0; scol < Columns; ++scol)
10550 TabPageIdxs[scol] = 0;
10551
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010552 /* Use the 'tabline' option if it's set. */
10553 if (*p_tal != NUL)
10554 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010555 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010556
10557 /* Check for an error. If there is one we would loop in redrawing the
10558 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010559 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010560 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010561 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010562 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010563 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010564 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010565 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010566 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010567#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010568 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010569 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010570 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010571
Bram Moolenaar238a5642006-02-21 22:12:05 +000010572 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10573 if (tabwidth < 6)
10574 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010575
Bram Moolenaar238a5642006-02-21 22:12:05 +000010576 attr = attr_nosel;
10577 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010578 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010579 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10580 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010581 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010582 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010583
Bram Moolenaar238a5642006-02-21 22:12:05 +000010584 if (tp->tp_topframe == topframe)
10585 attr = attr_sel;
10586 if (use_sep_chars && col > 0)
10587 screen_putchar('|', 0, col++, attr);
10588
10589 if (tp->tp_topframe != topframe)
10590 attr = attr_nosel;
10591
10592 screen_putchar(' ', 0, col++, attr);
10593
10594 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010595 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010596 cwp = curwin;
10597 wp = firstwin;
10598 }
10599 else
10600 {
10601 cwp = tp->tp_curwin;
10602 wp = tp->tp_firstwin;
10603 }
10604
10605 modified = FALSE;
10606 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10607 if (bufIsChanged(wp->w_buffer))
10608 modified = TRUE;
10609 if (modified || wincount > 1)
10610 {
10611 if (wincount > 1)
10612 {
10613 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010614 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010615 if (col + len >= Columns - 3)
10616 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010617 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010618#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010619 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010620#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010621 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010622#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010623 );
10624 col += len;
10625 }
10626 if (modified)
10627 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10628 screen_putchar(' ', 0, col++, attr);
10629 }
10630
10631 room = scol - col + tabwidth - 1;
10632 if (room > 0)
10633 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010634 /* Get buffer name in NameBuff[] */
10635 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010636 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010637 len = vim_strsize(NameBuff);
10638 p = NameBuff;
10639#ifdef FEAT_MBYTE
10640 if (has_mbyte)
10641 while (len > room)
10642 {
10643 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010644 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010645 }
10646 else
10647#endif
10648 if (len > room)
10649 {
10650 p += len - room;
10651 len = room;
10652 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010653 if (len > Columns - col - 1)
10654 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010655
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010656 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010657 col += len;
10658 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010659 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010660
10661 /* Store the tab page number in TabPageIdxs[], so that
10662 * jump_to_mouse() knows where each one is. */
10663 ++tabcount;
10664 while (scol < col)
10665 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010666 }
10667
Bram Moolenaar238a5642006-02-21 22:12:05 +000010668 if (use_sep_chars)
10669 c = '_';
10670 else
10671 c = ' ';
10672 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010673
10674 /* Put an "X" for closing the current tab if there are several. */
10675 if (first_tabpage->tp_next != NULL)
10676 {
10677 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10678 TabPageIdxs[Columns - 1] = -999;
10679 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010680 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010681
10682 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10683 * set. */
10684 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010685}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010686
10687/*
10688 * Get buffer name for "buf" into NameBuff[].
10689 * Takes care of special buffer names and translates special characters.
10690 */
10691 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010692get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010693{
10694 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010695 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010696 else
10697 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10698 trans_characters(NameBuff, MAXPATHL);
10699}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010700
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701/*
10702 * Get the character to use in a status line. Get its attributes in "*attr".
10703 */
10704 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010705fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706{
10707 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010708
10709#ifdef FEAT_TERMINAL
10710 if (bt_terminal(wp->w_buffer))
10711 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010712 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010713 {
10714 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010715 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010716 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010717 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010718 {
10719 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010720 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010721 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010722 }
10723 else
10724#endif
10725 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010727 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 fill = fill_stl;
10729 }
10730 else
10731 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010732 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 fill = fill_stlnc;
10734 }
10735 /* Use fill when there is highlighting, and highlighting of current
10736 * window differs, or the fillchars differ, or this is not the
10737 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010738 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010739 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 || (fill_stl != fill_stlnc)))
10741 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010742 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 return '^';
10744 return '=';
10745}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747/*
10748 * Get the character to use in a separator between vertically split windows.
10749 * Get its attributes in "*attr".
10750 */
10751 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010752fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010754 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 if (*attr == 0 && fill_vert == ' ')
10756 return '|';
10757 else
10758 return fill_vert;
10759}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760
10761/*
10762 * Return TRUE if redrawing should currently be done.
10763 */
10764 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010765redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010767#ifdef FEAT_EVAL
10768 if (disable_redraw_for_testing)
10769 return 0;
10770 else
10771#endif
10772 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10774}
10775
10776/*
10777 * Return TRUE if printing messages should currently be done.
10778 */
10779 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010780messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781{
10782 return (!(p_lz && char_avail() && !KeyTyped));
10783}
10784
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010785#ifdef FEAT_MENU
10786/*
10787 * Draw the window toolbar.
10788 */
10789 static void
10790redraw_win_toolbar(win_T *wp)
10791{
10792 vimmenu_T *menu;
10793 int item_idx = 0;
10794 int item_count = 0;
10795 int col = 0;
10796 int next_col;
10797 int off = (int)(current_ScreenLine - ScreenLines);
10798 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10799 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10800
10801 vim_free(wp->w_winbar_items);
10802 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10803 ++item_count;
10804 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10805 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10806
10807 /* TODO: use fewer spaces if there is not enough room */
10808 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010809 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010810 {
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 if (col > 1)
10815 {
10816 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010817 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010818 break;
10819 }
10820
10821 wp->w_winbar_items[item_idx].wb_startcol = col;
10822 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010823 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010824 break;
10825
10826 next_col = text_to_screenline(wp, menu->name, col);
10827 while (col < next_col)
10828 {
10829 ScreenAttrs[off + col] = button_attr;
10830 ++col;
10831 }
10832 wp->w_winbar_items[item_idx].wb_endcol = col;
10833 wp->w_winbar_items[item_idx].wb_menu = menu;
10834 ++item_idx;
10835
Bram Moolenaar02631462017-09-22 15:20:32 +020010836 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010837 break;
10838 space_to_screenline(off + col, button_attr);
10839 ++col;
10840 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010841 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010842 {
10843 space_to_screenline(off + col, fill_attr);
10844 ++col;
10845 }
10846 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10847
Bram Moolenaar02631462017-09-22 15:20:32 +020010848 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10849 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010850}
10851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852/*
10853 * Show current status info in ruler and various other places
10854 * If always is FALSE, only show ruler if position has changed.
10855 */
10856 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010857showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858{
10859 if (!always && !redrawing())
10860 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010861#ifdef FEAT_INS_EXPAND
10862 if (pum_visible())
10863 {
10864 /* Don't redraw right now, do it later. */
10865 curwin->w_redr_status = TRUE;
10866 return;
10867 }
10868#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010869#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010870 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010871 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010872 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 else
10875#endif
10876#ifdef FEAT_CMDL_INFO
10877 win_redr_ruler(curwin, always);
10878#endif
10879
10880#ifdef FEAT_TITLE
10881 if (need_maketitle
10882# ifdef FEAT_STL_OPT
10883 || (p_icon && (stl_syntax & STL_IN_ICON))
10884 || (p_title && (stl_syntax & STL_IN_TITLE))
10885# endif
10886 )
10887 maketitle();
10888#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010889 /* Redraw the tab pages line if needed. */
10890 if (redraw_tabline)
10891 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892}
10893
10894#ifdef FEAT_CMDL_INFO
10895 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010896win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010898#define RULER_BUF_LEN 70
10899 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 int row;
10901 int fillchar;
10902 int attr;
10903 int empty_line = FALSE;
10904 colnr_T virtcol;
10905 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010906 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 int this_ru_col;
10909 int off = 0;
10910 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911
10912 /* If 'ruler' off or redrawing disabled, don't do anything */
10913 if (!p_ru)
10914 return;
10915
10916 /*
10917 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10918 * after deleting lines, before cursor.lnum is corrected.
10919 */
10920 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10921 return;
10922
10923#ifdef FEAT_INS_EXPAND
10924 /* Don't draw the ruler while doing insert-completion, it might overwrite
10925 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 if (edit_submode != NULL)
10928 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010929 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10930 if (pum_visible())
10931 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932#endif
10933
10934#ifdef FEAT_STL_OPT
10935 if (*p_ruf)
10936 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010937 int save_called_emsg = called_emsg;
10938
10939 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010941 if (called_emsg)
10942 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010943 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010944 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 return;
10946 }
10947#endif
10948
10949 /*
10950 * Check if not in Insert mode and the line is empty (will show "0-1").
10951 */
10952 if (!(State & INSERT)
10953 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10954 empty_line = TRUE;
10955
10956 /*
10957 * Only draw the ruler when something changed.
10958 */
10959 validate_virtcol_win(wp);
10960 if ( redraw_cmdline
10961 || always
10962 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10963 || wp->w_cursor.col != wp->w_ru_cursor.col
10964 || wp->w_virtcol != wp->w_ru_virtcol
10965#ifdef FEAT_VIRTUALEDIT
10966 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10967#endif
10968 || wp->w_topline != wp->w_ru_topline
10969 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10970#ifdef FEAT_DIFF
10971 || wp->w_topfill != wp->w_ru_topfill
10972#endif
10973 || empty_line != wp->w_ru_empty)
10974 {
10975 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 if (wp->w_status_height)
10977 {
10978 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010979 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010980 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010981 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 }
10983 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 {
10985 row = Rows - 1;
10986 fillchar = ' ';
10987 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 width = Columns;
10989 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 }
10991
10992 /* In list mode virtcol needs to be recomputed */
10993 virtcol = wp->w_virtcol;
10994 if (wp->w_p_list && lcs_tab1 == NUL)
10995 {
10996 wp->w_p_list = FALSE;
10997 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10998 wp->w_p_list = TRUE;
10999 }
11000
11001 /*
11002 * Some sprintfs return the length, some return a pointer.
11003 * To avoid portability problems we use strlen() here.
11004 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011005 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11007 ? 0L
11008 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011009 len = STRLEN(buffer);
11010 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11012 (int)virtcol + 1);
11013
11014 /*
11015 * Add a "50%" if there is room for it.
11016 * On the last line, don't print in the last column (scrolls the
11017 * screen up on some terminals).
11018 */
11019 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011020 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 this_ru_col = ru_col - (Columns - width);
11025 if (this_ru_col < 0)
11026 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 /* Never use more than half the window/screen width, leave the other
11028 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011029 if (this_ru_col < (width + 1) / 2)
11030 this_ru_col = (width + 1) / 2;
11031 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011033 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011034 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 {
11036#ifdef FEAT_MBYTE
11037 if (has_mbyte)
11038 i += (*mb_char2bytes)(fillchar, buffer + i);
11039 else
11040#endif
11041 buffer[i++] = fillchar;
11042 ++o;
11043 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011044 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 }
11046 /* Truncate at window boundary. */
11047#ifdef FEAT_MBYTE
11048 if (has_mbyte)
11049 {
11050 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011051 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 {
11053 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011054 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 {
11056 buffer[i] = NUL;
11057 break;
11058 }
11059 }
11060 }
11061 else
11062#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011063 if (this_ru_col + (int)STRLEN(buffer) > width)
11064 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065
Bram Moolenaar4033c552017-09-16 20:54:51 +020011066 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 i = redraw_cmdline;
11068 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011069 this_ru_col + off + (int)STRLEN(buffer),
11070 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 fillchar, fillchar, attr);
11072 /* don't redraw the cmdline because of showing the ruler */
11073 redraw_cmdline = i;
11074 wp->w_ru_cursor = wp->w_cursor;
11075 wp->w_ru_virtcol = wp->w_virtcol;
11076 wp->w_ru_empty = empty_line;
11077 wp->w_ru_topline = wp->w_topline;
11078 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11079#ifdef FEAT_DIFF
11080 wp->w_ru_topfill = wp->w_topfill;
11081#endif
11082 }
11083}
11084#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011085
11086#if defined(FEAT_LINEBREAK) || defined(PROTO)
11087/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011088 * Return the width of the 'number' and 'relativenumber' column.
11089 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011090 * Otherwise it depends on 'numberwidth' and the line count.
11091 */
11092 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011093number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011094{
11095 int n;
11096 linenr_T lnum;
11097
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011098 if (wp->w_p_rnu && !wp->w_p_nu)
11099 /* cursor line shows "0" */
11100 lnum = wp->w_height;
11101 else
11102 /* cursor line shows absolute line number */
11103 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011104
Bram Moolenaar6b314672015-03-20 15:42:10 +010011105 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011106 return wp->w_nrwidth_width;
11107 wp->w_nrwidth_line_count = lnum;
11108
11109 n = 0;
11110 do
11111 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011112 lnum /= 10;
11113 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011114 } while (lnum > 0);
11115
11116 /* 'numberwidth' gives the minimal width plus one */
11117 if (n < wp->w_p_nuw - 1)
11118 n = wp->w_p_nuw - 1;
11119
11120 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011121 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011122 return n;
11123}
11124#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011125
11126/*
11127 * Return the current cursor column. This is the actual position on the
11128 * screen. First column is 0.
11129 */
11130 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011131screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011132{
11133 return screen_cur_col;
11134}
11135
11136/*
11137 * Return the current cursor row. This is the actual position on the screen.
11138 * First row is 0.
11139 */
11140 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011141screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011142{
11143 return screen_cur_row;
11144}