blob: 841dc18669f4a36857f32240aed603501d2d2c3a [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
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200517 void
518reset_updating_screen(int may_resize_shell UNUSED)
519{
520 updating_screen = FALSE;
521#ifdef FEAT_GUI
522 if (may_resize_shell)
523 gui_may_resize_shell();
524#endif
525#ifdef FEAT_TERMINAL
526 term_check_channel_closed_recently();
527#endif
528}
529
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200531 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 */
533 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100534update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
536 redraw_curbuf_later(type);
537 update_screen(type);
538}
539
540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * Based on the current value of curwin->w_topline, transfer a screenfull
542 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200543 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200545 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200548 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 win_T *wp;
550 static int did_intro = FALSE;
551#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
552 int did_one;
553#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200554#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200555 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200556 int gui_cursor_col;
557 int gui_cursor_row;
558#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200559 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000561 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200563 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200565 if (type == VALID_NO_UPDATE)
566 {
567 no_update = TRUE;
568 type = 0;
569 }
570
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (must_redraw)
572 {
573 if (type < must_redraw) /* use maximal type */
574 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000575
576 /* must_redraw is reset here, so that when we run into some weird
577 * reason to redraw while busy redrawing (e.g., asynchronous
578 * scrolling), or update_topline() in win_update() will cause a
579 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 must_redraw = 0;
581 }
582
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200583 /* May need to update w_lines[]. */
584 if (curwin->w_lines_valid == 0 && type < NOT_VALID
585#ifdef FEAT_TERMINAL
586 && !term_do_update_window(curwin)
587#endif
588 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 type = NOT_VALID;
590
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000591 /* Postpone the redrawing when it's not needed and when being called
592 * recursively. */
593 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {
595 redraw_later(type); /* remember type for next time */
596 must_redraw = type;
597 if (type > INVERTED_ALL)
598 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200599 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 }
601
602 updating_screen = TRUE;
603#ifdef FEAT_SYN_HL
604 ++display_tick; /* let syntax code know we're in a next round of
605 * display updating */
606#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200607 if (no_update)
608 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
610 /*
611 * if the screen was scrolled up when displaying a message, scroll it down
612 */
613 if (msg_scrolled)
614 {
615 clear_cmdline = TRUE;
616 if (msg_scrolled > Rows - 5) /* clearing is faster */
617 type = CLEAR;
618 else if (type != CLEAR)
619 {
620 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200621 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
622 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 type = CLEAR;
624 FOR_ALL_WINDOWS(wp)
625 {
626 if (W_WINROW(wp) < msg_scrolled)
627 {
628 if (W_WINROW(wp) + wp->w_height > msg_scrolled
629 && wp->w_redr_type < REDRAW_TOP
630 && wp->w_lines_valid > 0
631 && wp->w_topline == wp->w_lines[0].wl_lnum)
632 {
633 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
634 wp->w_redr_type = REDRAW_TOP;
635 }
636 else
637 {
638 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200639 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
640 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
643 }
644 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200645 if (!no_update)
646 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000647 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 }
649 msg_scrolled = 0;
650 need_wait_return = FALSE;
651 }
652
653 /* reset cmdline_row now (may have been changed temporarily) */
654 compute_cmdrow();
655
656 /* Check for changed highlighting */
657 if (need_highlight_changed)
658 highlight_changed();
659
660 if (type == CLEAR) /* first clear screen */
661 {
662 screenclear(); /* will reset clear_cmdline */
663 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200664 /* must_redraw may be set indirectly, avoid another redraw later */
665 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 }
667
668 if (clear_cmdline) /* going to clear cmdline (done below) */
669 check_for_delay(FALSE);
670
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000671#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200672 /* Force redraw when width of 'number' or 'relativenumber' column
673 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000674 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200675 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
676 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000677 curwin->w_redr_type = NOT_VALID;
678#endif
679
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 /*
681 * Only start redrawing if there is really something to do.
682 */
683 if (type == INVERTED)
684 update_curswant();
685 if (curwin->w_redr_type < type
686 && !((type == VALID
687 && curwin->w_lines[0].wl_valid
688#ifdef FEAT_DIFF
689 && curwin->w_topfill == curwin->w_old_topfill
690 && curwin->w_botfill == curwin->w_old_botfill
691#endif
692 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000694 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
696 && curwin->w_old_visual_mode == VIsual_mode
697 && (curwin->w_valid & VALID_VIRTCOL)
698 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 ))
700 curwin->w_redr_type = type;
701
Bram Moolenaar5a305422006-04-28 22:38:25 +0000702 /* Redraw the tab pages line if needed. */
703 if (redraw_tabline || type >= NOT_VALID)
704 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000705
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#ifdef FEAT_SYN_HL
707 /*
708 * Correct stored syntax highlighting info for changes in each displayed
709 * buffer. Each buffer must only be done once.
710 */
711 FOR_ALL_WINDOWS(wp)
712 {
713 if (wp->w_buffer->b_mod_set)
714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 win_T *wwp;
716
717 /* Check if we already did this buffer. */
718 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
719 if (wwp->w_buffer == wp->w_buffer)
720 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200721 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 syn_stack_apply_changes(wp->w_buffer);
723 }
724 }
725#endif
726
727 /*
728 * Go from top to bottom through the windows, redrawing the ones that need
729 * it.
730 */
731#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
732 did_one = FALSE;
733#endif
734#ifdef FEAT_SEARCH_EXTRA
735 search_hl.rm.regprog = NULL;
736#endif
737 FOR_ALL_WINDOWS(wp)
738 {
739 if (wp->w_redr_type != 0)
740 {
741 cursor_off();
742#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
743 if (!did_one)
744 {
745 did_one = TRUE;
746# ifdef FEAT_SEARCH_EXTRA
747 start_search_hl();
748# endif
749# ifdef FEAT_CLIPBOARD
750 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200751 if (clip_star.available && clip_isautosel_star())
752 clip_update_selection(&clip_star);
753 if (clip_plus.available && clip_isautosel_plus())
754 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755# endif
756#ifdef FEAT_GUI
757 /* Remove the cursor before starting to do anything, because
758 * scrolling may make it difficult to redraw the text under
759 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200760 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200761 {
762 gui_cursor_col = gui.cursor_col;
763 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200765 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767#endif
768 }
769#endif
770 win_update(wp);
771 }
772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 /* redraw status line after the window to minimize cursor movement */
774 if (wp->w_redr_status)
775 {
776 cursor_off();
777 win_redr_status(wp);
778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
780#if defined(FEAT_SEARCH_EXTRA)
781 end_search_hl();
782#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100783#ifdef FEAT_INS_EXPAND
784 /* May need to redraw the popup menu. */
785 if (pum_visible())
786 pum_redraw();
787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 /* Reset b_mod_set flags. Going through all windows is probably faster
790 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200791 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200794 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 /* Clear or redraw the command line. Done last, because scrolling may
797 * mess up the command line. */
798 if (clear_cmdline || redraw_cmdline)
799 showmode();
800
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200801 if (no_update)
802 --no_win_do_lines_ins;
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200805 if (!did_intro)
806 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 did_intro = TRUE;
808
809#ifdef FEAT_GUI
810 /* Redraw the cursor and update the scrollbars when all screen updating is
811 * done. */
812 if (gui.in_use)
813 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200814 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200815 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100816 mch_disable_flush();
817 out_flush(); /* required before updating the cursor */
818 mch_enable_flush();
819
Bram Moolenaar144445d2016-07-08 21:41:54 +0200820 /* Put the GUI position where the cursor was, gui_update_cursor()
821 * uses that. */
822 gui.col = gui_cursor_col;
823 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200824# ifdef FEAT_MBYTE
825 gui.col = mb_fix_col(gui.col, gui.row);
826# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100828 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200829 screen_cur_col = gui.col;
830 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200831 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100832 else
833 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 gui_update_scrollbars(FALSE);
835 }
836#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200837 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838}
839
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100840#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
841/*
842 * Prepare for updating one or more windows.
843 * Caller must check for "updating_screen" already set to avoid recursiveness.
844 */
845 static void
846update_prepare(void)
847{
848 cursor_off();
849 updating_screen = TRUE;
850#ifdef FEAT_GUI
851 /* Remove the cursor before starting to do anything, because scrolling may
852 * make it difficult to redraw the text under it. */
853 if (gui.in_use)
854 gui_undraw_cursor();
855#endif
856#ifdef FEAT_SEARCH_EXTRA
857 start_search_hl();
858#endif
859}
860
861/*
862 * Finish updating one or more windows.
863 */
864 static void
865update_finish(void)
866{
867 if (redraw_cmdline)
868 showmode();
869
870# ifdef FEAT_SEARCH_EXTRA
871 end_search_hl();
872# endif
873
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200874 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100875
876# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100877 /* Redraw the cursor and update the scrollbars when all screen updating is
878 * done. */
879 if (gui.in_use)
880 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100881 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100882 gui_update_scrollbars(FALSE);
883 }
884# endif
885}
886#endif
887
Bram Moolenaar860cae12010-06-05 23:22:07 +0200888#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200889/*
890 * Return TRUE if the cursor line in window "wp" may be concealed, according
891 * to the 'concealcursor' option.
892 */
893 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100894conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200895{
896 int c;
897
898 if (*wp->w_p_cocu == NUL)
899 return FALSE;
900 if (get_real_state() & VISUAL)
901 c = 'v';
902 else if (State & INSERT)
903 c = 'i';
904 else if (State & NORMAL)
905 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200906 else if (State & CMDLINE)
907 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200908 else
909 return FALSE;
910 return vim_strchr(wp->w_p_cocu, c) != NULL;
911}
912
913/*
914 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
915 */
916 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200917conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200918{
919 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
920 {
921 need_cursor_line_redraw = TRUE;
922 /* Need to recompute cursor column, e.g., when starting Visual mode
923 * without concealing. */
924 curs_columns(TRUE);
925 }
926}
927
Bram Moolenaar860cae12010-06-05 23:22:07 +0200928 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100929update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200930{
931 int row;
932 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200933#ifdef SYN_TIME_LIMIT
934 proftime_T syntax_tm;
935#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200936
Bram Moolenaar908be432016-05-24 10:51:30 +0200937 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100938 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200939 return;
940
Bram Moolenaar860cae12010-06-05 23:22:07 +0200941 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200942 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200943 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200944#ifdef SYN_TIME_LIMIT
945 /* Set the time limit to 'redrawtime'. */
946 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200947 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200948#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100949 update_prepare();
950
Bram Moolenaar860cae12010-06-05 23:22:07 +0200951 row = 0;
952 for (j = 0; j < wp->w_lines_valid; ++j)
953 {
954 if (lnum == wp->w_lines[j].wl_lnum)
955 {
956 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200957# ifdef FEAT_SEARCH_EXTRA
958 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200959 start_search_hl();
960 prepare_search_hl(wp, lnum);
961# endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200962 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200963# if defined(FEAT_SEARCH_EXTRA)
964 end_search_hl();
965# endif
966 break;
967 }
968 row += wp->w_lines[j].wl_size;
969 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100970
971 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200972
973#ifdef SYN_TIME_LIMIT
974 syn_set_timeout(NULL);
975#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200976 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200977 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978}
979#endif
980
981#if defined(FEAT_SIGNS) || defined(PROTO)
982 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100983update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984{
985 win_T *wp;
986 int doit = FALSE;
987
988# ifdef FEAT_FOLDING
989 win_foldinfo.fi_level = 0;
990# endif
991
992 /* update/delete a specific mark */
993 FOR_ALL_WINDOWS(wp)
994 {
995 if (buf != NULL && lnum > 0)
996 {
997 if (wp->w_buffer == buf && lnum >= wp->w_topline
998 && lnum < wp->w_botline)
999 {
1000 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1001 wp->w_redraw_top = lnum;
1002 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1003 wp->w_redraw_bot = lnum;
1004 redraw_win_later(wp, VALID);
1005 }
1006 }
1007 else
1008 redraw_win_later(wp, VALID);
1009 if (wp->w_redr_type != 0)
1010 doit = TRUE;
1011 }
1012
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001013 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001014 * happening (recursive call), messages on the screen or still starting up.
1015 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001016 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001017 || State == ASKMORE || State == HITRETURN
1018 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001019#ifdef FEAT_GUI
1020 || gui.starting
1021#endif
1022 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 return;
1024
1025 /* update all windows that need updating */
1026 update_prepare();
1027
Bram Moolenaar29323592016-07-24 22:04:11 +02001028 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
1030 if (wp->w_redr_type != 0)
1031 win_update(wp);
1032 if (wp->w_redr_status)
1033 win_redr_status(wp);
1034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
1036 update_finish();
1037}
1038#endif
1039
1040
1041#if defined(FEAT_GUI) || defined(PROTO)
1042/*
1043 * Update a single window, its status line and maybe the command line msg.
1044 * Used for the GUI scrollbar.
1045 */
1046 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001047updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001049 /* return if already busy updating */
1050 if (updating_screen)
1051 return;
1052
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 update_prepare();
1054
1055#ifdef FEAT_CLIPBOARD
1056 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001057 if (clip_star.available && clip_isautosel_star())
1058 clip_update_selection(&clip_star);
1059 if (clip_plus.available && clip_isautosel_plus())
1060 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001064
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001065 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001066 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001067 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (wp->w_redr_status
1070# ifdef FEAT_CMDL_INFO
1071 || p_ru
1072# endif
1073# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001074 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075# endif
1076 )
1077 win_redr_status(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
1079 update_finish();
1080}
1081#endif
1082
1083/*
1084 * Update a single window.
1085 *
1086 * This may cause the windows below it also to be redrawn (when clearing the
1087 * screen or scrolling lines).
1088 *
1089 * How the window is redrawn depends on wp->w_redr_type. Each type also
1090 * implies the one below it.
1091 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001092 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1094 * INVERTED redraw the changed part of the Visual area
1095 * INVERTED_ALL redraw the whole Visual area
1096 * VALID 1. scroll up/down to adjust for a changed w_topline
1097 * 2. update lines at the top when scrolled down
1098 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001099 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 * b_mod_top and b_mod_bot.
1101 * - if wp->w_redraw_top non-zero, redraw lines between
1102 * wp->w_redraw_top and wp->w_redr_bot.
1103 * - continue redrawing when syntax status is invalid.
1104 * 4. if scrolled up, update lines at the bottom.
1105 * This results in three areas that may need updating:
1106 * top: from first row to top_end (when scrolled down)
1107 * mid: from mid_start to mid_end (update inversion or changed text)
1108 * bot: from bot_start to last row (when scrolled up)
1109 */
1110 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001111win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112{
1113 buf_T *buf = wp->w_buffer;
1114 int type;
1115 int top_end = 0; /* Below last row of the top area that needs
1116 updating. 0 when no top area updating. */
1117 int mid_start = 999;/* first row of the mid area that needs
1118 updating. 999 when no mid area updating. */
1119 int mid_end = 0; /* Below last row of the mid area that needs
1120 updating. 0 when no mid area updating. */
1121 int bot_start = 999;/* first row of the bot area that needs
1122 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 int scrolled_down = FALSE; /* TRUE when scrolled down when
1124 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001126 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 int top_to_mod = FALSE; /* redraw above mod_top */
1128#endif
1129
1130 int row; /* current window row to display */
1131 linenr_T lnum; /* current buffer lnum to display */
1132 int idx; /* current index in w_lines[] */
1133 int srow; /* starting row of the current line */
1134
1135 int eof = FALSE; /* if TRUE, we hit the end of the file */
1136 int didline = FALSE; /* if TRUE, we finished the last line */
1137 int i;
1138 long j;
1139 static int recursive = FALSE; /* being called recursively */
1140 int old_botline = wp->w_botline;
1141#ifdef FEAT_FOLDING
1142 long fold_count;
1143#endif
1144#ifdef FEAT_SYN_HL
1145 /* remember what happened to the previous line, to know if
1146 * check_visual_highlight() can be used */
1147#define DID_NONE 1 /* didn't update a line */
1148#define DID_LINE 2 /* updated a normal line */
1149#define DID_FOLD 3 /* updated a folded line */
1150 int did_update = DID_NONE;
1151 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1152#endif
1153 linenr_T mod_top = 0;
1154 linenr_T mod_bot = 0;
1155#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1156 int save_got_int;
1157#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001158#ifdef SYN_TIME_LIMIT
1159 proftime_T syntax_tm;
1160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
1162 type = wp->w_redr_type;
1163
1164 if (type == NOT_VALID)
1165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 wp->w_lines_valid = 0;
1168 }
1169
1170 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001171 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 wp->w_redr_type = 0;
1174 return;
1175 }
1176
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 /* Window is zero-width: Only need to draw the separator. */
1178 if (wp->w_width == 0)
1179 {
1180 /* draw the vertical separator right of this window */
1181 draw_vsep_win(wp, 0);
1182 wp->w_redr_type = 0;
1183 return;
1184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001186#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001187 // If this window contains a terminal, redraw works completely differently.
1188 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001189 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001190 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001191# ifdef FEAT_MENU
1192 /* Draw the window toolbar, if there is one. */
1193 if (winbar_height(wp) > 0)
1194 redraw_win_toolbar(wp);
1195# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001196 wp->w_redr_type = 0;
1197 return;
1198 }
1199#endif
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001202 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203#endif
1204
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001205#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001206 /* Force redraw when width of 'number' or 'relativenumber' column
1207 * changes. */
1208 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001209 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001210 {
1211 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001212 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001213 }
1214 else
1215#endif
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1218 {
1219 /*
1220 * When there are both inserted/deleted lines and specific lines to be
1221 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1222 * everything (only happens when redrawing is off for while).
1223 */
1224 type = NOT_VALID;
1225 }
1226 else
1227 {
1228 /*
1229 * Set mod_top to the first line that needs displaying because of
1230 * changes. Set mod_bot to the first line after the changes.
1231 */
1232 mod_top = wp->w_redraw_top;
1233 if (wp->w_redraw_bot != 0)
1234 mod_bot = wp->w_redraw_bot + 1;
1235 else
1236 mod_bot = 0;
1237 wp->w_redraw_top = 0; /* reset for next time */
1238 wp->w_redraw_bot = 0;
1239 if (buf->b_mod_set)
1240 {
1241 if (mod_top == 0 || mod_top > buf->b_mod_top)
1242 {
1243 mod_top = buf->b_mod_top;
1244#ifdef FEAT_SYN_HL
1245 /* Need to redraw lines above the change that may be included
1246 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001247 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001249 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 if (mod_top < 1)
1251 mod_top = 1;
1252 }
1253#endif
1254 }
1255 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1256 mod_bot = buf->b_mod_bot;
1257
1258#ifdef FEAT_SEARCH_EXTRA
1259 /* When 'hlsearch' is on and using a multi-line search pattern, a
1260 * change in one line may make the Search highlighting in a
1261 * previous line invalid. Simple solution: redraw all visible
1262 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001263 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001265 if (search_hl.rm.regprog != NULL
1266 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001268 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001269 {
1270 cur = wp->w_match_head;
1271 while (cur != NULL)
1272 {
1273 if (cur->match.regprog != NULL
1274 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001275 {
1276 top_to_mod = TRUE;
1277 break;
1278 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001279 cur = cur->next;
1280 }
1281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282#endif
1283 }
1284#ifdef FEAT_FOLDING
1285 if (mod_top != 0 && hasAnyFolding(wp))
1286 {
1287 linenr_T lnumt, lnumb;
1288
1289 /*
1290 * A change in a line can cause lines above it to become folded or
1291 * unfolded. Find the top most buffer line that may be affected.
1292 * If the line was previously folded and displayed, get the first
1293 * line of that fold. If the line is folded now, get the first
1294 * folded line. Use the minimum of these two.
1295 */
1296
1297 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1298 * the line below it. If there is no valid entry, use w_topline.
1299 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1300 * to this line. If there is no valid entry, use MAXLNUM. */
1301 lnumt = wp->w_topline;
1302 lnumb = MAXLNUM;
1303 for (i = 0; i < wp->w_lines_valid; ++i)
1304 if (wp->w_lines[i].wl_valid)
1305 {
1306 if (wp->w_lines[i].wl_lastlnum < mod_top)
1307 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1308 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1309 {
1310 lnumb = wp->w_lines[i].wl_lnum;
1311 /* When there is a fold column it might need updating
1312 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001313 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 ++lnumb;
1315 }
1316 }
1317
1318 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1319 if (mod_top > lnumt)
1320 mod_top = lnumt;
1321
1322 /* Now do the same for the bottom line (one above mod_bot). */
1323 --mod_bot;
1324 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1325 ++mod_bot;
1326 if (mod_bot < lnumb)
1327 mod_bot = lnumb;
1328 }
1329#endif
1330
1331 /* When a change starts above w_topline and the end is below
1332 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001333 * If the end of the change is above w_topline: do like no change was
1334 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (mod_top != 0 && mod_top < wp->w_topline)
1336 {
1337 if (mod_bot > wp->w_topline)
1338 mod_top = wp->w_topline;
1339#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001340 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 top_end = 1;
1342#endif
1343 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001344
1345 /* When line numbers are displayed need to redraw all lines below
1346 * inserted/deleted lines. */
1347 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1348 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350
1351 /*
1352 * When only displaying the lines at the top, set top_end. Used when
1353 * window has scrolled down for msg_scrolled.
1354 */
1355 if (type == REDRAW_TOP)
1356 {
1357 j = 0;
1358 for (i = 0; i < wp->w_lines_valid; ++i)
1359 {
1360 j += wp->w_lines[i].wl_size;
1361 if (j >= wp->w_upd_rows)
1362 {
1363 top_end = j;
1364 break;
1365 }
1366 }
1367 if (top_end == 0)
1368 /* not found (cannot happen?): redraw everything */
1369 type = NOT_VALID;
1370 else
1371 /* top area defined, the rest is VALID */
1372 type = VALID;
1373 }
1374
Bram Moolenaar367329b2007-08-30 11:53:22 +00001375 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001376 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1377 * non-zero and thus not FALSE) will indicate that screenclear() was not
1378 * called. */
1379 if (screen_cleared)
1380 screen_cleared = MAYBE;
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 /*
1383 * If there are no changes on the screen that require a complete redraw,
1384 * handle three cases:
1385 * 1: we are off the top of the screen by a few lines: scroll down
1386 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1387 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1388 * w_lines[] that needs updating.
1389 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001390 if ((type == VALID || type == SOME_VALID
1391 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#ifdef FEAT_DIFF
1393 && !wp->w_botfill && !wp->w_old_botfill
1394#endif
1395 )
1396 {
1397 if (mod_top != 0 && wp->w_topline == mod_top)
1398 {
1399 /*
1400 * w_topline is the first changed line, the scrolling will be done
1401 * further down.
1402 */
1403 }
1404 else if (wp->w_lines[0].wl_valid
1405 && (wp->w_topline < wp->w_lines[0].wl_lnum
1406#ifdef FEAT_DIFF
1407 || (wp->w_topline == wp->w_lines[0].wl_lnum
1408 && wp->w_topfill > wp->w_old_topfill)
1409#endif
1410 ))
1411 {
1412 /*
1413 * New topline is above old topline: May scroll down.
1414 */
1415#ifdef FEAT_FOLDING
1416 if (hasAnyFolding(wp))
1417 {
1418 linenr_T ln;
1419
1420 /* count the number of lines we are off, counting a sequence
1421 * of folded lines as one */
1422 j = 0;
1423 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1424 {
1425 ++j;
1426 if (j >= wp->w_height - 2)
1427 break;
1428 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1429 }
1430 }
1431 else
1432#endif
1433 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1434 if (j < wp->w_height - 2) /* not too far off */
1435 {
1436 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1437#ifdef FEAT_DIFF
1438 /* insert extra lines for previously invisible filler lines */
1439 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1440 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1441 - wp->w_old_topfill;
1442#endif
1443 if (i < wp->w_height - 2) /* less than a screen off */
1444 {
1445 /*
1446 * Try to insert the correct number of lines.
1447 * If not the last window, delete the lines at the bottom.
1448 * win_ins_lines may fail when the terminal can't do it.
1449 */
1450 if (i > 0)
1451 check_for_delay(FALSE);
1452 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1453 {
1454 if (wp->w_lines_valid != 0)
1455 {
1456 /* Need to update rows that are new, stop at the
1457 * first one that scrolled down. */
1458 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
1461 /* Move the entries that were scrolled, disable
1462 * the entries for the lines to be redrawn. */
1463 if ((wp->w_lines_valid += j) > wp->w_height)
1464 wp->w_lines_valid = wp->w_height;
1465 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1466 wp->w_lines[idx] = wp->w_lines[idx - j];
1467 while (idx >= 0)
1468 wp->w_lines[idx--].wl_valid = FALSE;
1469 }
1470 }
1471 else
1472 mid_start = 0; /* redraw all lines */
1473 }
1474 else
1475 mid_start = 0; /* redraw all lines */
1476 }
1477 else
1478 mid_start = 0; /* redraw all lines */
1479 }
1480 else
1481 {
1482 /*
1483 * New topline is at or below old topline: May scroll up.
1484 * When topline didn't change, find first entry in w_lines[] that
1485 * needs updating.
1486 */
1487
1488 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1489 j = -1;
1490 row = 0;
1491 for (i = 0; i < wp->w_lines_valid; i++)
1492 {
1493 if (wp->w_lines[i].wl_valid
1494 && wp->w_lines[i].wl_lnum == wp->w_topline)
1495 {
1496 j = i;
1497 break;
1498 }
1499 row += wp->w_lines[i].wl_size;
1500 }
1501 if (j == -1)
1502 {
1503 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1504 * lines */
1505 mid_start = 0;
1506 }
1507 else
1508 {
1509 /*
1510 * Try to delete the correct number of lines.
1511 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1512 */
1513#ifdef FEAT_DIFF
1514 /* If the topline didn't change, delete old filler lines,
1515 * otherwise delete filler lines of the new topline... */
1516 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1517 row += wp->w_old_topfill;
1518 else
1519 row += diff_check_fill(wp, wp->w_topline);
1520 /* ... but don't delete new filler lines. */
1521 row -= wp->w_topfill;
1522#endif
1523 if (row > 0)
1524 {
1525 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001526 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1527 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 bot_start = wp->w_height - row;
1529 else
1530 mid_start = 0; /* redraw all lines */
1531 }
1532 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1533 {
1534 /*
1535 * Skip the lines (below the deleted lines) that are still
1536 * valid and don't need redrawing. Copy their info
1537 * upwards, to compensate for the deleted lines. Set
1538 * bot_start to the first row that needs redrawing.
1539 */
1540 bot_start = 0;
1541 idx = 0;
1542 for (;;)
1543 {
1544 wp->w_lines[idx] = wp->w_lines[j];
1545 /* stop at line that didn't fit, unless it is still
1546 * valid (no lines deleted) */
1547 if (row > 0 && bot_start + row
1548 + (int)wp->w_lines[j].wl_size > wp->w_height)
1549 {
1550 wp->w_lines_valid = idx + 1;
1551 break;
1552 }
1553 bot_start += wp->w_lines[idx++].wl_size;
1554
1555 /* stop at the last valid entry in w_lines[].wl_size */
1556 if (++j >= wp->w_lines_valid)
1557 {
1558 wp->w_lines_valid = idx;
1559 break;
1560 }
1561 }
1562#ifdef FEAT_DIFF
1563 /* Correct the first entry for filler lines at the top
1564 * when it won't get updated below. */
1565 if (wp->w_p_diff && bot_start > 0)
1566 wp->w_lines[0].wl_size =
1567 plines_win_nofill(wp, wp->w_topline, TRUE)
1568 + wp->w_topfill;
1569#endif
1570 }
1571 }
1572 }
1573
1574 /* When starting redraw in the first line, redraw all lines. When
1575 * there is only one window it's probably faster to clear the screen
1576 * first. */
1577 if (mid_start == 0)
1578 {
1579 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001580 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001581 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001582 /* Clear the screen when it was not done by win_del_lines() or
1583 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1584 * then. */
1585 if (screen_cleared != TRUE)
1586 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001587 /* The screen was cleared, redraw the tab pages line. */
1588 if (redraw_tabline)
1589 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001592
1593 /* When win_del_lines() or win_ins_lines() caused the screen to be
1594 * cleared (only happens for the first window) or when screenclear()
1595 * was called directly above, "must_redraw" will have been set to
1596 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1597 if (screen_cleared == TRUE)
1598 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600 else
1601 {
1602 /* Not VALID or INVERTED: redraw all lines. */
1603 mid_start = 0;
1604 mid_end = wp->w_height;
1605 }
1606
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001607 if (type == SOME_VALID)
1608 {
1609 /* SOME_VALID: redraw all lines. */
1610 mid_start = 0;
1611 mid_end = wp->w_height;
1612 type = NOT_VALID;
1613 }
1614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 /* check if we are updating or removing the inverted part */
1616 if ((VIsual_active && buf == curwin->w_buffer)
1617 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1618 {
1619 linenr_T from, to;
1620
1621 if (VIsual_active)
1622 {
1623 if (VIsual_active
1624 && (VIsual_mode != wp->w_old_visual_mode
1625 || type == INVERTED_ALL))
1626 {
1627 /*
1628 * If the type of Visual selection changed, redraw the whole
1629 * selection. Also when the ownership of the X selection is
1630 * gained or lost.
1631 */
1632 if (curwin->w_cursor.lnum < VIsual.lnum)
1633 {
1634 from = curwin->w_cursor.lnum;
1635 to = VIsual.lnum;
1636 }
1637 else
1638 {
1639 from = VIsual.lnum;
1640 to = curwin->w_cursor.lnum;
1641 }
1642 /* redraw more when the cursor moved as well */
1643 if (wp->w_old_cursor_lnum < from)
1644 from = wp->w_old_cursor_lnum;
1645 if (wp->w_old_cursor_lnum > to)
1646 to = wp->w_old_cursor_lnum;
1647 if (wp->w_old_visual_lnum < from)
1648 from = wp->w_old_visual_lnum;
1649 if (wp->w_old_visual_lnum > to)
1650 to = wp->w_old_visual_lnum;
1651 }
1652 else
1653 {
1654 /*
1655 * Find the line numbers that need to be updated: The lines
1656 * between the old cursor position and the current cursor
1657 * position. Also check if the Visual position changed.
1658 */
1659 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1660 {
1661 from = curwin->w_cursor.lnum;
1662 to = wp->w_old_cursor_lnum;
1663 }
1664 else
1665 {
1666 from = wp->w_old_cursor_lnum;
1667 to = curwin->w_cursor.lnum;
1668 if (from == 0) /* Visual mode just started */
1669 from = to;
1670 }
1671
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001672 if (VIsual.lnum != wp->w_old_visual_lnum
1673 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 {
1675 if (wp->w_old_visual_lnum < from
1676 && wp->w_old_visual_lnum != 0)
1677 from = wp->w_old_visual_lnum;
1678 if (wp->w_old_visual_lnum > to)
1679 to = wp->w_old_visual_lnum;
1680 if (VIsual.lnum < from)
1681 from = VIsual.lnum;
1682 if (VIsual.lnum > to)
1683 to = VIsual.lnum;
1684 }
1685 }
1686
1687 /*
1688 * If in block mode and changed column or curwin->w_curswant:
1689 * update all lines.
1690 * First compute the actual start and end column.
1691 */
1692 if (VIsual_mode == Ctrl_V)
1693 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001694 colnr_T fromc, toc;
1695#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1696 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
Bram Moolenaar404406a2014-10-09 13:24:43 +02001698 if (curwin->w_p_lbr)
1699 ve_flags = VE_ALL;
1700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001702#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1703 ve_flags = save_ve_flags;
1704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 ++toc;
1706 if (curwin->w_curswant == MAXCOL)
1707 toc = MAXCOL;
1708
1709 if (fromc != wp->w_old_cursor_fcol
1710 || toc != wp->w_old_cursor_lcol)
1711 {
1712 if (from > VIsual.lnum)
1713 from = VIsual.lnum;
1714 if (to < VIsual.lnum)
1715 to = VIsual.lnum;
1716 }
1717 wp->w_old_cursor_fcol = fromc;
1718 wp->w_old_cursor_lcol = toc;
1719 }
1720 }
1721 else
1722 {
1723 /* Use the line numbers of the old Visual area. */
1724 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1725 {
1726 from = wp->w_old_cursor_lnum;
1727 to = wp->w_old_visual_lnum;
1728 }
1729 else
1730 {
1731 from = wp->w_old_visual_lnum;
1732 to = wp->w_old_cursor_lnum;
1733 }
1734 }
1735
1736 /*
1737 * There is no need to update lines above the top of the window.
1738 */
1739 if (from < wp->w_topline)
1740 from = wp->w_topline;
1741
1742 /*
1743 * If we know the value of w_botline, use it to restrict the update to
1744 * the lines that are visible in the window.
1745 */
1746 if (wp->w_valid & VALID_BOTLINE)
1747 {
1748 if (from >= wp->w_botline)
1749 from = wp->w_botline - 1;
1750 if (to >= wp->w_botline)
1751 to = wp->w_botline - 1;
1752 }
1753
1754 /*
1755 * Find the minimal part to be updated.
1756 * Watch out for scrolling that made entries in w_lines[] invalid.
1757 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1758 * top_end; need to redraw from top_end to the "to" line.
1759 * A middle mouse click with a Visual selection may change the text
1760 * above the Visual area and reset wl_valid, do count these for
1761 * mid_end (in srow).
1762 */
1763 if (mid_start > 0)
1764 {
1765 lnum = wp->w_topline;
1766 idx = 0;
1767 srow = 0;
1768 if (scrolled_down)
1769 mid_start = top_end;
1770 else
1771 mid_start = 0;
1772 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1773 {
1774 if (wp->w_lines[idx].wl_valid)
1775 mid_start += wp->w_lines[idx].wl_size;
1776 else if (!scrolled_down)
1777 srow += wp->w_lines[idx].wl_size;
1778 ++idx;
1779# ifdef FEAT_FOLDING
1780 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1781 lnum = wp->w_lines[idx].wl_lnum;
1782 else
1783# endif
1784 ++lnum;
1785 }
1786 srow += mid_start;
1787 mid_end = wp->w_height;
1788 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1789 {
1790 if (wp->w_lines[idx].wl_valid
1791 && wp->w_lines[idx].wl_lnum >= to + 1)
1792 {
1793 /* Only update until first row of this line */
1794 mid_end = srow;
1795 break;
1796 }
1797 srow += wp->w_lines[idx].wl_size;
1798 }
1799 }
1800 }
1801
1802 if (VIsual_active && buf == curwin->w_buffer)
1803 {
1804 wp->w_old_visual_mode = VIsual_mode;
1805 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1806 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001807 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 wp->w_old_curswant = curwin->w_curswant;
1809 }
1810 else
1811 {
1812 wp->w_old_visual_mode = 0;
1813 wp->w_old_cursor_lnum = 0;
1814 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001815 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1819 /* reset got_int, otherwise regexp won't work */
1820 save_got_int = got_int;
1821 got_int = 0;
1822#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001823#ifdef SYN_TIME_LIMIT
1824 /* Set the time limit to 'redrawtime'. */
1825 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001826 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828#ifdef FEAT_FOLDING
1829 win_foldinfo.fi_level = 0;
1830#endif
1831
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001832#ifdef FEAT_MENU
1833 /*
1834 * Draw the window toolbar, if there is one.
1835 * TODO: only when needed.
1836 */
1837 if (winbar_height(wp) > 0)
1838 redraw_win_toolbar(wp);
1839#endif
1840
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 /*
1842 * Update all the window rows.
1843 */
1844 idx = 0; /* first entry in w_lines[].wl_size */
1845 row = 0;
1846 srow = 0;
1847 lnum = wp->w_topline; /* first line shown in window */
1848 for (;;)
1849 {
1850 /* stop updating when reached the end of the window (check for _past_
1851 * the end of the window is at the end of the loop) */
1852 if (row == wp->w_height)
1853 {
1854 didline = TRUE;
1855 break;
1856 }
1857
1858 /* stop updating when hit the end of the file */
1859 if (lnum > buf->b_ml.ml_line_count)
1860 {
1861 eof = TRUE;
1862 break;
1863 }
1864
1865 /* Remember the starting row of the line that is going to be dealt
1866 * with. It is used further down when the line doesn't fit. */
1867 srow = row;
1868
1869 /*
1870 * Update a line when it is in an area that needs updating, when it
1871 * has changes or w_lines[idx] is invalid.
1872 * bot_start may be halfway a wrapped line after using
1873 * win_del_lines(), check if the current line includes it.
1874 * When syntax folding is being used, the saved syntax states will
1875 * already have been updated, we can't see where the syntax state is
1876 * the same again, just update until the end of the window.
1877 */
1878 if (row < top_end
1879 || (row >= mid_start && row < mid_end)
1880#ifdef FEAT_SEARCH_EXTRA
1881 || top_to_mod
1882#endif
1883 || idx >= wp->w_lines_valid
1884 || (row + wp->w_lines[idx].wl_size > bot_start)
1885 || (mod_top != 0
1886 && (lnum == mod_top
1887 || (lnum >= mod_top
1888 && (lnum < mod_bot
1889#ifdef FEAT_SYN_HL
1890 || did_update == DID_FOLD
1891 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001892 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 && (
1894# ifdef FEAT_FOLDING
1895 (foldmethodIsSyntax(wp)
1896 && hasAnyFolding(wp)) ||
1897# endif
1898 syntax_check_changed(lnum)))
1899#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001900#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001901 /* match in fixed position might need redraw
1902 * if lines were inserted or deleted */
1903 || (wp->w_match_head != NULL
1904 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 )))))
1907 {
1908#ifdef FEAT_SEARCH_EXTRA
1909 if (lnum == mod_top)
1910 top_to_mod = FALSE;
1911#endif
1912
1913 /*
1914 * When at start of changed lines: May scroll following lines
1915 * up or down to minimize redrawing.
1916 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001917 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 */
1919 if (lnum == mod_top
1920 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001921 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
1923 int old_rows = 0;
1924 int new_rows = 0;
1925 int xtra_rows;
1926 linenr_T l;
1927
1928 /* Count the old number of window rows, using w_lines[], which
1929 * should still contain the sizes for the lines as they are
1930 * currently displayed. */
1931 for (i = idx; i < wp->w_lines_valid; ++i)
1932 {
1933 /* Only valid lines have a meaningful wl_lnum. Invalid
1934 * lines are part of the changed area. */
1935 if (wp->w_lines[i].wl_valid
1936 && wp->w_lines[i].wl_lnum == mod_bot)
1937 break;
1938 old_rows += wp->w_lines[i].wl_size;
1939#ifdef FEAT_FOLDING
1940 if (wp->w_lines[i].wl_valid
1941 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1942 {
1943 /* Must have found the last valid entry above mod_bot.
1944 * Add following invalid entries. */
1945 ++i;
1946 while (i < wp->w_lines_valid
1947 && !wp->w_lines[i].wl_valid)
1948 old_rows += wp->w_lines[i++].wl_size;
1949 break;
1950 }
1951#endif
1952 }
1953
1954 if (i >= wp->w_lines_valid)
1955 {
1956 /* We can't find a valid line below the changed lines,
1957 * need to redraw until the end of the window.
1958 * Inserting/deleting lines has no use. */
1959 bot_start = 0;
1960 }
1961 else
1962 {
1963 /* Able to count old number of rows: Count new window
1964 * rows, and may insert/delete lines */
1965 j = idx;
1966 for (l = lnum; l < mod_bot; ++l)
1967 {
1968#ifdef FEAT_FOLDING
1969 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1970 ++new_rows;
1971 else
1972#endif
1973#ifdef FEAT_DIFF
1974 if (l == wp->w_topline)
1975 new_rows += plines_win_nofill(wp, l, TRUE)
1976 + wp->w_topfill;
1977 else
1978#endif
1979 new_rows += plines_win(wp, l, TRUE);
1980 ++j;
1981 if (new_rows > wp->w_height - row - 2)
1982 {
1983 /* it's getting too much, must redraw the rest */
1984 new_rows = 9999;
1985 break;
1986 }
1987 }
1988 xtra_rows = new_rows - old_rows;
1989 if (xtra_rows < 0)
1990 {
1991 /* May scroll text up. If there is not enough
1992 * remaining text or scrolling fails, must redraw the
1993 * rest. If scrolling works, must redraw the text
1994 * below the scrolled text. */
1995 if (row - xtra_rows >= wp->w_height - 2)
1996 mod_bot = MAXLNUM;
1997 else
1998 {
1999 check_for_delay(FALSE);
2000 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002001 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 mod_bot = MAXLNUM;
2003 else
2004 bot_start = wp->w_height + xtra_rows;
2005 }
2006 }
2007 else if (xtra_rows > 0)
2008 {
2009 /* May scroll text down. If there is not enough
2010 * remaining text of scrolling fails, must redraw the
2011 * rest. */
2012 if (row + xtra_rows >= wp->w_height - 2)
2013 mod_bot = MAXLNUM;
2014 else
2015 {
2016 check_for_delay(FALSE);
2017 if (win_ins_lines(wp, row + old_rows,
2018 xtra_rows, FALSE, FALSE) == FAIL)
2019 mod_bot = MAXLNUM;
2020 else if (top_end > row + old_rows)
2021 /* Scrolled the part at the top that requires
2022 * updating down. */
2023 top_end += xtra_rows;
2024 }
2025 }
2026
2027 /* When not updating the rest, may need to move w_lines[]
2028 * entries. */
2029 if (mod_bot != MAXLNUM && i != j)
2030 {
2031 if (j < i)
2032 {
2033 int x = row + new_rows;
2034
2035 /* move entries in w_lines[] upwards */
2036 for (;;)
2037 {
2038 /* stop at last valid entry in w_lines[] */
2039 if (i >= wp->w_lines_valid)
2040 {
2041 wp->w_lines_valid = j;
2042 break;
2043 }
2044 wp->w_lines[j] = wp->w_lines[i];
2045 /* stop at a line that won't fit */
2046 if (x + (int)wp->w_lines[j].wl_size
2047 > wp->w_height)
2048 {
2049 wp->w_lines_valid = j + 1;
2050 break;
2051 }
2052 x += wp->w_lines[j++].wl_size;
2053 ++i;
2054 }
2055 if (bot_start > x)
2056 bot_start = x;
2057 }
2058 else /* j > i */
2059 {
2060 /* move entries in w_lines[] downwards */
2061 j -= i;
2062 wp->w_lines_valid += j;
2063 if (wp->w_lines_valid > wp->w_height)
2064 wp->w_lines_valid = wp->w_height;
2065 for (i = wp->w_lines_valid; i - j >= idx; --i)
2066 wp->w_lines[i] = wp->w_lines[i - j];
2067
2068 /* The w_lines[] entries for inserted lines are
2069 * now invalid, but wl_size may be used above.
2070 * Reset to zero. */
2071 while (i >= idx)
2072 {
2073 wp->w_lines[i].wl_size = 0;
2074 wp->w_lines[i--].wl_valid = FALSE;
2075 }
2076 }
2077 }
2078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080
2081#ifdef FEAT_FOLDING
2082 /*
2083 * When lines are folded, display one line for all of them.
2084 * Otherwise, display normally (can be several display lines when
2085 * 'wrap' is on).
2086 */
2087 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2088 if (fold_count != 0)
2089 {
2090 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2091 ++row;
2092 --fold_count;
2093 wp->w_lines[idx].wl_folded = TRUE;
2094 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2095# ifdef FEAT_SYN_HL
2096 did_update = DID_FOLD;
2097# endif
2098 }
2099 else
2100#endif
2101 if (idx < wp->w_lines_valid
2102 && wp->w_lines[idx].wl_valid
2103 && wp->w_lines[idx].wl_lnum == lnum
2104 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002105 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 && srow + wp->w_lines[idx].wl_size > wp->w_height
2107#ifdef FEAT_DIFF
2108 && diff_check_fill(wp, lnum) == 0
2109#endif
2110 )
2111 {
2112 /* This line is not going to fit. Don't draw anything here,
2113 * will draw "@ " lines below. */
2114 row = wp->w_height + 1;
2115 }
2116 else
2117 {
2118#ifdef FEAT_SEARCH_EXTRA
2119 prepare_search_hl(wp, lnum);
2120#endif
2121#ifdef FEAT_SYN_HL
2122 /* Let the syntax stuff know we skipped a few lines. */
2123 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002124 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 syntax_end_parsing(syntax_last_parsed + 1);
2126#endif
2127
2128 /*
2129 * Display one line.
2130 */
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002131 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
2133#ifdef FEAT_FOLDING
2134 wp->w_lines[idx].wl_folded = FALSE;
2135 wp->w_lines[idx].wl_lastlnum = lnum;
2136#endif
2137#ifdef FEAT_SYN_HL
2138 did_update = DID_LINE;
2139 syntax_last_parsed = lnum;
2140#endif
2141 }
2142
2143 wp->w_lines[idx].wl_lnum = lnum;
2144 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002145
2146 /* Past end of the window or end of the screen. Note that after
2147 * resizing wp->w_height may be end up too big. That's a problem
2148 * elsewhere, but prevent a crash here. */
2149 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002152 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2154 ++idx;
2155 break;
2156 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002157 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 wp->w_lines[idx].wl_size = row - srow;
2159 ++idx;
2160#ifdef FEAT_FOLDING
2161 lnum += fold_count + 1;
2162#else
2163 ++lnum;
2164#endif
2165 }
2166 else
2167 {
2168 /* This line does not need updating, advance to the next one */
2169 row += wp->w_lines[idx++].wl_size;
2170 if (row > wp->w_height) /* past end of screen */
2171 break;
2172#ifdef FEAT_FOLDING
2173 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2174#else
2175 ++lnum;
2176#endif
2177#ifdef FEAT_SYN_HL
2178 did_update = DID_NONE;
2179#endif
2180 }
2181
2182 if (lnum > buf->b_ml.ml_line_count)
2183 {
2184 eof = TRUE;
2185 break;
2186 }
2187 }
2188 /*
2189 * End of loop over all window lines.
2190 */
2191
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002192#ifdef FEAT_VTP
2193 /* Rewrite the character at the end of the screen line. */
2194 if (use_vtp())
2195 {
2196 int i;
2197
2198 for (i = 0; i < Rows; ++i)
2199# ifdef FEAT_MBYTE
2200 if (enc_utf8)
2201 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2202 LineOffset[i] + screen_Columns) > 1)
2203 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2204 else
2205 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2206 else
2207# endif
2208 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2209 }
2210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212 if (idx > wp->w_lines_valid)
2213 wp->w_lines_valid = idx;
2214
2215#ifdef FEAT_SYN_HL
2216 /*
2217 * Let the syntax stuff know we stop parsing here.
2218 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002219 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 syntax_end_parsing(syntax_last_parsed + 1);
2221#endif
2222
2223 /*
2224 * If we didn't hit the end of the file, and we didn't finish the last
2225 * line we were working on, then the line didn't fit.
2226 */
2227 wp->w_empty_rows = 0;
2228#ifdef FEAT_DIFF
2229 wp->w_filler_rows = 0;
2230#endif
2231 if (!eof && !didline)
2232 {
2233 if (lnum == wp->w_topline)
2234 {
2235 /*
2236 * Single line that does not fit!
2237 * Don't overwrite it, it can be edited.
2238 */
2239 wp->w_botline = lnum + 1;
2240 }
2241#ifdef FEAT_DIFF
2242 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2243 {
2244 /* Window ends in filler lines. */
2245 wp->w_botline = lnum;
2246 wp->w_filler_rows = wp->w_height - srow;
2247 }
2248#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002249 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2250 {
2251 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2252
2253 /*
2254 * Last line isn't finished: Display "@@@" in the last screen line.
2255 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002256 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002257 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002258 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002259 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002260 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002261 set_empty_rows(wp, srow);
2262 wp->w_botline = lnum;
2263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2265 {
2266 /*
2267 * Last line isn't finished: Display "@@@" at the end.
2268 */
2269 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2270 W_WINROW(wp) + wp->w_height,
2271 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002272 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 set_empty_rows(wp, srow);
2274 wp->w_botline = lnum;
2275 }
2276 else
2277 {
2278 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2279 wp->w_botline = lnum;
2280 }
2281 }
2282 else
2283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (eof) /* we hit the end of the file */
2286 {
2287 wp->w_botline = buf->b_ml.ml_line_count + 1;
2288#ifdef FEAT_DIFF
2289 j = diff_check_fill(wp, wp->w_botline);
2290 if (j > 0 && !wp->w_botfill)
2291 {
2292 /*
2293 * Display filler lines at the end of the file
2294 */
2295 if (char2cells(fill_diff) > 1)
2296 i = '-';
2297 else
2298 i = fill_diff;
2299 if (row + j > wp->w_height)
2300 j = wp->w_height - row;
2301 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2302 row += j;
2303 }
2304#endif
2305 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002306 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 wp->w_botline = lnum;
2308
2309 /* make sure the rest of the screen is blank */
2310 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002311 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
2313
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002314#ifdef SYN_TIME_LIMIT
2315 syn_set_timeout(NULL);
2316#endif
2317
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 /* Reset the type of redrawing required, the window has been updated. */
2319 wp->w_redr_type = 0;
2320#ifdef FEAT_DIFF
2321 wp->w_old_topfill = wp->w_topfill;
2322 wp->w_old_botfill = wp->w_botfill;
2323#endif
2324
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002325 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
2327 /*
2328 * There is a trick with w_botline. If we invalidate it on each
2329 * change that might modify it, this will cause a lot of expensive
2330 * calls to plines() in update_topline() each time. Therefore the
2331 * value of w_botline is often approximated, and this value is used to
2332 * compute the value of w_topline. If the value of w_botline was
2333 * wrong, check that the value of w_topline is correct (cursor is on
2334 * the visible part of the text). If it's not, we need to redraw
2335 * again. Mostly this just means scrolling up a few lines, so it
2336 * doesn't look too bad. Only do this for the current window (where
2337 * changes are relevant).
2338 */
2339 wp->w_valid |= VALID_BOTLINE;
2340 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2341 {
2342 recursive = TRUE;
2343 curwin->w_valid &= ~VALID_TOPLINE;
2344 update_topline(); /* may invalidate w_botline again */
2345 if (must_redraw != 0)
2346 {
2347 /* Don't update for changes in buffer again. */
2348 i = curbuf->b_mod_set;
2349 curbuf->b_mod_set = FALSE;
2350 win_update(curwin);
2351 must_redraw = 0;
2352 curbuf->b_mod_set = i;
2353 }
2354 recursive = FALSE;
2355 }
2356 }
2357
2358#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2359 /* restore got_int, unless CTRL-C was hit while redrawing */
2360 if (!got_int)
2361 got_int = save_got_int;
2362#endif
2363}
2364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365/*
2366 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2367 * as the filler character.
2368 */
2369 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002370win_draw_end(
2371 win_T *wp,
2372 int c1,
2373 int c2,
2374 int row,
2375 int endrow,
2376 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
2378#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2379 int n = 0;
2380# define FDC_OFF n
2381#else
2382# define FDC_OFF 0
2383#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002384#ifdef FEAT_FOLDING
2385 int fdc = compute_foldcolumn(wp, 0);
2386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388#ifdef FEAT_RIGHTLEFT
2389 if (wp->w_p_rl)
2390 {
2391 /* No check for cmdline window: should never be right-left. */
2392# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002393 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395 if (n > 0)
2396 {
2397 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002398 if (n > wp->w_width)
2399 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2401 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002402 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404# endif
2405# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002406 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
2408 int nn = n + 2;
2409
2410 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002411 if (nn > wp->w_width)
2412 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2414 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002415 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 n = nn;
2417 }
2418# endif
2419 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002420 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002421 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2423 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002424 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426 else
2427#endif
2428 {
2429#ifdef FEAT_CMDWIN
2430 if (cmdwin_type != 0 && wp == curwin)
2431 {
2432 /* draw the cmdline character in the leftmost column */
2433 n = 1;
2434 if (n > wp->w_width)
2435 n = wp->w_width;
2436 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002437 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002438 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
2440#endif
2441#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002442 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002444 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
2446 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002447 if (nn > wp->w_width)
2448 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002450 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002451 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 n = nn;
2453 }
2454#endif
2455#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002456 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
2458 int nn = n + 2;
2459
2460 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002461 if (nn > wp->w_width)
2462 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002464 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002465 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 n = nn;
2467 }
2468#endif
2469 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002470 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002471 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 }
2473 set_empty_rows(wp, row);
2474}
2475
Bram Moolenaar1a384422010-07-14 19:53:30 +02002476#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002477static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002478
2479/*
2480 * Advance **color_cols and return TRUE when there are columns to draw.
2481 */
2482 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002483advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002484{
2485 while (**color_cols >= 0 && vcol > **color_cols)
2486 ++*color_cols;
2487 return (**color_cols >= 0);
2488}
2489#endif
2490
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002491#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2492/*
2493 * Copy "text" to ScreenLines using "attr".
2494 * Returns the next screen column.
2495 */
2496 static int
2497text_to_screenline(win_T *wp, char_u *text, int col)
2498{
2499 int off = (int)(current_ScreenLine - ScreenLines);
2500
2501#ifdef FEAT_MBYTE
2502 if (has_mbyte)
2503 {
2504 int cells;
2505 int u8c, u8cc[MAX_MCO];
2506 int i;
2507 int idx;
2508 int c_len;
2509 char_u *p;
2510# ifdef FEAT_ARABIC
2511 int prev_c = 0; /* previous Arabic character */
2512 int prev_c1 = 0; /* first composing char for prev_c */
2513# endif
2514
2515# ifdef FEAT_RIGHTLEFT
2516 if (wp->w_p_rl)
2517 idx = off;
2518 else
2519# endif
2520 idx = off + col;
2521
2522 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2523 for (p = text; *p != NUL; )
2524 {
2525 cells = (*mb_ptr2cells)(p);
2526 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002527 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002528# ifdef FEAT_RIGHTLEFT
2529 - (wp->w_p_rl ? col : 0)
2530# endif
2531 )
2532 break;
2533 ScreenLines[idx] = *p;
2534 if (enc_utf8)
2535 {
2536 u8c = utfc_ptr2char(p, u8cc);
2537 if (*p < 0x80 && u8cc[0] == 0)
2538 {
2539 ScreenLinesUC[idx] = 0;
2540#ifdef FEAT_ARABIC
2541 prev_c = u8c;
2542#endif
2543 }
2544 else
2545 {
2546#ifdef FEAT_ARABIC
2547 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2548 {
2549 /* Do Arabic shaping. */
2550 int pc, pc1, nc;
2551 int pcc[MAX_MCO];
2552 int firstbyte = *p;
2553
2554 /* The idea of what is the previous and next
2555 * character depends on 'rightleft'. */
2556 if (wp->w_p_rl)
2557 {
2558 pc = prev_c;
2559 pc1 = prev_c1;
2560 nc = utf_ptr2char(p + c_len);
2561 prev_c1 = u8cc[0];
2562 }
2563 else
2564 {
2565 pc = utfc_ptr2char(p + c_len, pcc);
2566 nc = prev_c;
2567 pc1 = pcc[0];
2568 }
2569 prev_c = u8c;
2570
2571 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2572 pc, pc1, nc);
2573 ScreenLines[idx] = firstbyte;
2574 }
2575 else
2576 prev_c = u8c;
2577#endif
2578 /* Non-BMP character: display as ? or fullwidth ?. */
2579#ifdef UNICODE16
2580 if (u8c >= 0x10000)
2581 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2582 else
2583#endif
2584 ScreenLinesUC[idx] = u8c;
2585 for (i = 0; i < Screen_mco; ++i)
2586 {
2587 ScreenLinesC[i][idx] = u8cc[i];
2588 if (u8cc[i] == 0)
2589 break;
2590 }
2591 }
2592 if (cells > 1)
2593 ScreenLines[idx + 1] = 0;
2594 }
2595 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2596 /* double-byte single width character */
2597 ScreenLines2[idx] = p[1];
2598 else if (cells > 1)
2599 /* double-width character */
2600 ScreenLines[idx + 1] = p[1];
2601 col += cells;
2602 idx += cells;
2603 p += c_len;
2604 }
2605 }
2606 else
2607#endif
2608 {
2609 int len = (int)STRLEN(text);
2610
Bram Moolenaar02631462017-09-22 15:20:32 +02002611 if (len > wp->w_width - col)
2612 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002613 if (len > 0)
2614 {
2615#ifdef FEAT_RIGHTLEFT
2616 if (wp->w_p_rl)
2617 STRNCPY(current_ScreenLine, text, len);
2618 else
2619#endif
2620 STRNCPY(current_ScreenLine + col, text, len);
2621 col += len;
2622 }
2623 }
2624 return col;
2625}
2626#endif
2627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628#ifdef FEAT_FOLDING
2629/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002630 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2631 * space is available for window "wp", minus "col".
2632 */
2633 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002634compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002635{
2636 int fdc = wp->w_p_fdc;
2637 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002638 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002639
2640 if (fdc > wwidth - (col + wmw))
2641 fdc = wwidth - (col + wmw);
2642 return fdc;
2643}
2644
2645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 * Display one folded line.
2647 */
2648 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002649fold_line(
2650 win_T *wp,
2651 long fold_count,
2652 foldinfo_T *foldinfo,
2653 linenr_T lnum,
2654 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002656 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 pos_T *top, *bot;
2658 linenr_T lnume = lnum + fold_count - 1;
2659 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002660 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 int col;
2663 int txtcol;
2664 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002665 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
2667 /* Build the fold line:
2668 * 1. Add the cmdwin_type for the command-line window
2669 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002670 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 * 4. Compose the text
2672 * 5. Add the text
2673 * 6. set highlighting for the Visual area an other text
2674 */
2675 col = 0;
2676
2677 /*
2678 * 1. Add the cmdwin_type for the command-line window
2679 * Ignores 'rightleft', this window is never right-left.
2680 */
2681#ifdef FEAT_CMDWIN
2682 if (cmdwin_type != 0 && wp == curwin)
2683 {
2684 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002685 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686#ifdef FEAT_MBYTE
2687 if (enc_utf8)
2688 ScreenLinesUC[off] = 0;
2689#endif
2690 ++col;
2691 }
2692#endif
2693
2694 /*
2695 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002696 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002698 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (fdc > 0)
2700 {
2701 fill_foldcolumn(buf, wp, TRUE, lnum);
2702#ifdef FEAT_RIGHTLEFT
2703 if (wp->w_p_rl)
2704 {
2705 int i;
2706
Bram Moolenaar02631462017-09-22 15:20:32 +02002707 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002708 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 /* reverse the fold column */
2710 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002711 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713 else
2714#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002715 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 col += fdc;
2717 }
2718
2719#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002720# define RL_MEMSET(p, v, l) \
2721 do { \
2722 if (wp->w_p_rl) \
2723 for (ri = 0; ri < l; ++ri) \
2724 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2725 else \
2726 for (ri = 0; ri < l; ++ri) \
2727 ScreenAttrs[off + (p) + ri] = v; \
2728 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002730# define RL_MEMSET(p, v, l) \
2731 do { \
2732 for (ri = 0; ri < l; ++ri) \
2733 ScreenAttrs[off + (p) + ri] = v; \
2734 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735#endif
2736
Bram Moolenaar64486672010-05-16 15:46:46 +02002737 /* Set all attributes of the 'number' or 'relativenumber' column and the
2738 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002739 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
2741#ifdef FEAT_SIGNS
2742 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002743 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002745 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if (len > 0)
2747 {
2748 if (len > 2)
2749 len = 2;
2750# ifdef FEAT_RIGHTLEFT
2751 if (wp->w_p_rl)
2752 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002753 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002754 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 else
2756# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002757 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 col += len;
2759 }
2760 }
2761#endif
2762
2763 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002764 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002766 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002768 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (len > 0)
2770 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002771 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002772 long num;
2773 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002774
2775 if (len > w + 1)
2776 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002777
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002778 if (wp->w_p_nu && !wp->w_p_rnu)
2779 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002780 num = (long)lnum;
2781 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002782 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002783 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002784 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002785 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002786 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002787 /* 'number' + 'relativenumber': cursor line shows absolute
2788 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002789 num = lnum;
2790 fmt = "%-*ld ";
2791 }
2792 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002793
Bram Moolenaar700e7342013-01-30 12:31:36 +01002794 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795#ifdef FEAT_RIGHTLEFT
2796 if (wp->w_p_rl)
2797 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002798 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002799 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 else
2801#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002802 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 col += len;
2804 }
2805 }
2806
2807 /*
2808 * 4. Compose the folded-line string with 'foldtext', if set.
2809 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002810 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
2812 txtcol = col; /* remember where text starts */
2813
2814 /*
2815 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2816 * Right-left text is put in columns 0 - number-col, normal text is put
2817 * in columns number-col - window-width.
2818 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002819 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820
2821 /* Fill the rest of the line with the fold filler */
2822#ifdef FEAT_RIGHTLEFT
2823 if (wp->w_p_rl)
2824 col -= txtcol;
2825#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002826 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827#ifdef FEAT_RIGHTLEFT
2828 - (wp->w_p_rl ? txtcol : 0)
2829#endif
2830 )
2831 {
2832#ifdef FEAT_MBYTE
2833 if (enc_utf8)
2834 {
2835 if (fill_fold >= 0x80)
2836 {
2837 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002838 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002839 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 }
2841 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002844 ScreenLines[off + col] = fill_fold;
2845 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002846 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002848 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002850 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
2852
2853 if (text != buf)
2854 vim_free(text);
2855
2856 /*
2857 * 6. set highlighting for the Visual area an other text.
2858 * If all folded lines are in the Visual area, highlight the line.
2859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2861 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002862 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 {
2864 /* Visual is after curwin->w_cursor */
2865 top = &curwin->w_cursor;
2866 bot = &VIsual;
2867 }
2868 else
2869 {
2870 /* Visual is before curwin->w_cursor */
2871 top = &VIsual;
2872 bot = &curwin->w_cursor;
2873 }
2874 if (lnum >= top->lnum
2875 && lnume <= bot->lnum
2876 && (VIsual_mode != 'v'
2877 || ((lnum > top->lnum
2878 || (lnum == top->lnum
2879 && top->col == 0))
2880 && (lnume < bot->lnum
2881 || (lnume == bot->lnum
2882 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002883 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
2885 if (VIsual_mode == Ctrl_V)
2886 {
2887 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002888 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002890 if (wp->w_old_cursor_lcol != MAXCOL
2891 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002892 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 len = wp->w_old_cursor_lcol;
2894 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002895 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002896 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002897 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
2899 }
2900 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002903 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002908#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002909 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2910 if (wp->w_p_cc_cols)
2911 {
2912 int i = 0;
2913 int j = wp->w_p_cc_cols[i];
2914 int old_txtcol = txtcol;
2915
2916 while (j > -1)
2917 {
2918 txtcol += j;
2919 if (wp->w_p_wrap)
2920 txtcol -= wp->w_skipcol;
2921 else
2922 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002923 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002924 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002925 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002926 txtcol = old_txtcol;
2927 j = wp->w_p_cc_cols[++i];
2928 }
2929 }
2930
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002931 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002932 if (wp->w_p_cuc)
2933 {
2934 txtcol += wp->w_virtcol;
2935 if (wp->w_p_wrap)
2936 txtcol -= wp->w_skipcol;
2937 else
2938 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002939 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002940 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002941 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002942 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
Bram Moolenaar02631462017-09-22 15:20:32 +02002945 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2946 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947
2948 /*
2949 * Update w_cline_height and w_cline_folded if the cursor line was
2950 * updated (saves a call to plines() later).
2951 */
2952 if (wp == curwin
2953 && lnum <= curwin->w_cursor.lnum
2954 && lnume >= curwin->w_cursor.lnum)
2955 {
2956 curwin->w_cline_row = row;
2957 curwin->w_cline_height = 1;
2958 curwin->w_cline_folded = TRUE;
2959 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2960 }
2961}
2962
2963/*
2964 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2965 */
2966 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002967copy_text_attr(
2968 int off,
2969 char_u *buf,
2970 int len,
2971 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002973 int i;
2974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 mch_memmove(ScreenLines + off, buf, (size_t)len);
2976# ifdef FEAT_MBYTE
2977 if (enc_utf8)
2978 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2979# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002980 for (i = 0; i < len; ++i)
2981 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982}
2983
2984/*
2985 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002986 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 */
2988 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002989fill_foldcolumn(
2990 char_u *p,
2991 win_T *wp,
2992 int closed, /* TRUE of FALSE */
2993 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995 int i = 0;
2996 int level;
2997 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002998 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002999 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003002 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
3004 level = win_foldinfo.fi_level;
3005 if (level > 0)
3006 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003007 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003008 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003009
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 /* If the column is too narrow, we start at the lowest level that
3011 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003012 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 if (first_level < 1)
3014 first_level = 1;
3015
Bram Moolenaar1c934292015-01-27 16:39:29 +01003016 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 {
3018 if (win_foldinfo.fi_lnum == lnum
3019 && first_level + i >= win_foldinfo.fi_low_level)
3020 p[i] = '-';
3021 else if (first_level == 1)
3022 p[i] = '|';
3023 else if (first_level + i <= 9)
3024 p[i] = '0' + first_level + i;
3025 else
3026 p[i] = '>';
3027 if (first_level + i == level)
3028 break;
3029 }
3030 }
3031 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003032 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033}
3034#endif /* FEAT_FOLDING */
3035
3036/*
3037 * Display line "lnum" of window 'wp' on the screen.
3038 * Start at row "startrow", stop when "endrow" is reached.
3039 * wp->w_virtcol needs to be valid.
3040 *
3041 * Return the number of last row the line occupies.
3042 */
3043 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003044win_line(
3045 win_T *wp,
3046 linenr_T lnum,
3047 int startrow,
3048 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003049 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003051 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3053 int c = 0; /* init for GCC */
3054 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003055#ifdef FEAT_LINEBREAK
3056 long vcol_sbr = -1; /* virtual column after showbreak */
3057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 long vcol_prev = -1; /* "vcol" of previous character */
3059 char_u *line; /* current line */
3060 char_u *ptr; /* current position in "line" */
3061 int row; /* row in the window, excl w_winrow */
3062 int screen_row; /* row on the screen, incl w_winrow */
3063
3064 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3065 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003066 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003067 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 int c_extra = NUL; /* extra chars, all the same */
3069 int extra_attr = 0; /* attributes when n_extra != 0 */
3070 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3071 displaying lcs_eol at end-of-line */
3072 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3073 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3074
3075 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3076 int saved_n_extra = 0;
3077 char_u *saved_p_extra = NULL;
3078 int saved_c_extra = 0;
3079 int saved_char_attr = 0;
3080
3081 int n_attr = 0; /* chars with special attr */
3082 int saved_attr2 = 0; /* char_attr saved for n_attr */
3083 int n_attr3 = 0; /* chars with overruling special attr */
3084 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3085
3086 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3087
3088 int fromcol, tocol; /* start/end of inverting */
3089 int fromcol_prev = -2; /* start of inverting after cursor */
3090 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003092 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 pos_T pos;
3094 long v;
3095
3096 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003097 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3099 in this line */
3100 int attr = 0; /* attributes for area highlighting */
3101 int area_attr = 0; /* attributes desired by highlighting */
3102 int search_attr = 0; /* attributes desired by 'hlsearch' */
3103#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003104 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 int syntax_attr = 0; /* attributes desired by syntax */
3106 int has_syntax = FALSE; /* this buffer has syntax highl. */
3107 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003108 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003109 int draw_color_col = FALSE; /* highlight colorcolumn */
3110 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003111#endif
3112#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003113 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003114# define SPWORDLEN 150
3115 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003116 int nextlinecol = 0; /* column where nextline[] starts */
3117 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003118 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003119 int spell_attr = 0; /* attributes desired by spelling */
3120 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003121 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3122 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003123 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003124 static int cap_col = -1; /* column to check for Cap word */
3125 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003126 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#endif
3128 int extra_check; /* has syntax or linebreak */
3129#ifdef FEAT_MBYTE
3130 int multi_attr = 0; /* attributes desired by multibyte */
3131 int mb_l = 1; /* multi-byte byte length */
3132 int mb_c = 0; /* decoded multi-byte character */
3133 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003134 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135#endif
3136#ifdef FEAT_DIFF
3137 int filler_lines; /* nr of filler lines to be drawn */
3138 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003139 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 int change_start = MAXCOL; /* first col of changed area */
3141 int change_end = -1; /* last col of changed area */
3142#endif
3143 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3144#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003145 int need_showbreak = FALSE; /* overlong line, skipping first x
3146 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003148#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003149 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003151 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152#endif
3153#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003154 matchitem_T *cur; /* points to the match list */
3155 match_T *shl; /* points to search_hl or a match */
3156 int shl_flag; /* flag to indicate whether search_hl
3157 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003158 int pos_inprogress; /* marks that position match search is
3159 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003160 int prevcol_hl_flag; /* flag to indicate whether prevcol
3161 equals startcol of search_hl or one
3162 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#endif
3164#ifdef FEAT_ARABIC
3165 int prev_c = 0; /* previous Arabic character */
3166 int prev_c1 = 0; /* first composing char for prev_c */
3167#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003168#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003169 int did_line_attr = 0;
3170#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003171#ifdef FEAT_TERMINAL
3172 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003173 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 /* draw_state: items that are drawn in sequence: */
3177#define WL_START 0 /* nothing done yet */
3178#ifdef FEAT_CMDWIN
3179# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3180#else
3181# define WL_CMDLINE WL_START
3182#endif
3183#ifdef FEAT_FOLDING
3184# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3185#else
3186# define WL_FOLD WL_CMDLINE
3187#endif
3188#ifdef FEAT_SIGNS
3189# define WL_SIGN WL_FOLD + 1 /* column for signs */
3190#else
3191# define WL_SIGN WL_FOLD /* column for signs */
3192#endif
3193#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003194#ifdef FEAT_LINEBREAK
3195# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003197# define WL_BRI WL_NR
3198#endif
3199#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3200# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3201#else
3202# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203#endif
3204#define WL_LINE WL_SBR + 1 /* text in the line */
3205 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003206#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 int feedback_col = 0;
3208 int feedback_old_attr = -1;
3209#endif
3210
Bram Moolenaar860cae12010-06-05 23:22:07 +02003211#ifdef FEAT_CONCEAL
3212 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003213 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003214 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003215 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003216 int is_concealing = FALSE;
3217 int boguscols = 0; /* nonexistent columns added to force
3218 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003219 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003220 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003221 int match_conc = 0; /* cchar for match functions */
3222 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003223 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003224# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003225# define FIX_FOR_BOGUSCOLS \
3226 { \
3227 n_extra += vcol_off; \
3228 vcol -= vcol_off; \
3229 vcol_off = 0; \
3230 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003231 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003232 boguscols = 0; \
3233 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003234#else
3235# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
3238 if (startrow > endrow) /* past the end already! */
3239 return startrow;
3240
3241 row = startrow;
3242 screen_row = row + W_WINROW(wp);
3243
3244 /*
3245 * To speed up the loop below, set extra_check when there is linebreak,
3246 * trailing white space and/or syntax processing to be done.
3247 */
3248#ifdef FEAT_LINEBREAK
3249 extra_check = wp->w_p_lbr;
3250#else
3251 extra_check = 0;
3252#endif
3253#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003254 if (syntax_present(wp) && !wp->w_s->b_syn_error
3255# ifdef SYN_TIME_LIMIT
3256 && !wp->w_s->b_syn_slow
3257# endif
3258 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 {
3260 /* Prepare for syntax highlighting in this line. When there is an
3261 * error, stop syntax highlighting. */
3262 save_did_emsg = did_emsg;
3263 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003264 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003266 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 else
3268 {
3269 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003270#ifdef SYN_TIME_LIMIT
3271 if (!wp->w_s->b_syn_slow)
3272#endif
3273 {
3274 has_syntax = TRUE;
3275 extra_check = TRUE;
3276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 }
3278 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003279
3280 /* Check for columns to display for 'colorcolumn'. */
3281 color_cols = wp->w_p_cc_cols;
3282 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003283 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003284#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003285
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003286#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003287 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003288 {
3289 extra_check = TRUE;
3290 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003291 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003292 }
3293#endif
3294
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003295#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003296 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003297 && *wp->w_s->b_p_spl != NUL
3298 && wp->w_s->b_langp.ga_len > 0
3299 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003300 {
3301 /* Prepare for spell checking. */
3302 has_spell = TRUE;
3303 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003304
3305 /* Get the start of the next line, so that words that wrap to the next
3306 * line are found too: "et<line-break>al.".
3307 * Trick: skip a few chars for C/shell/Vim comments */
3308 nextline[SPWORDLEN] = NUL;
3309 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3310 {
3311 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3312 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3313 }
3314
3315 /* When a word wrapped from the previous line the start of the current
3316 * line is valid. */
3317 if (lnum == checked_lnum)
3318 cur_checked_col = checked_col;
3319 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003320
3321 /* When there was a sentence end in the previous line may require a
3322 * word starting with capital in this line. In line 1 always check
3323 * the first word. */
3324 if (lnum != capcol_lnum)
3325 cap_col = -1;
3326 if (lnum == 1)
3327 cap_col = 0;
3328 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#endif
3331
3332 /*
3333 * handle visual active in this window
3334 */
3335 fromcol = -10;
3336 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3338 {
3339 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003340 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 {
3342 top = &curwin->w_cursor;
3343 bot = &VIsual;
3344 }
3345 else /* Visual is before curwin->w_cursor */
3346 {
3347 top = &VIsual;
3348 bot = &curwin->w_cursor;
3349 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003350 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 if (VIsual_mode == Ctrl_V) /* block mode */
3352 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003353 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 {
3355 fromcol = wp->w_old_cursor_fcol;
3356 tocol = wp->w_old_cursor_lcol;
3357 }
3358 }
3359 else /* non-block mode */
3360 {
3361 if (lnum > top->lnum && lnum <= bot->lnum)
3362 fromcol = 0;
3363 else if (lnum == top->lnum)
3364 {
3365 if (VIsual_mode == 'V') /* linewise */
3366 fromcol = 0;
3367 else
3368 {
3369 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3370 if (gchar_pos(top) == NUL)
3371 tocol = fromcol + 1;
3372 }
3373 }
3374 if (VIsual_mode != 'V' && lnum == bot->lnum)
3375 {
3376 if (*p_sel == 'e' && bot->col == 0
3377#ifdef FEAT_VIRTUALEDIT
3378 && bot->coladd == 0
3379#endif
3380 )
3381 {
3382 fromcol = -10;
3383 tocol = MAXCOL;
3384 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003385 else if (bot->col == MAXCOL)
3386 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 else
3388 {
3389 pos = *bot;
3390 if (*p_sel == 'e')
3391 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3392 else
3393 {
3394 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3395 ++tocol;
3396 }
3397 }
3398 }
3399 }
3400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 /* Check if the character under the cursor should not be inverted */
3402 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003403#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 )
3407 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408
3409 /* if inverting in this line set area_highlighting */
3410 if (fromcol >= 0)
3411 {
3412 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003413 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003415 if ((clip_star.available && !clip_star.owned
3416 && clip_isautosel_star())
3417 || (clip_plus.available && !clip_plus.owned
3418 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003419 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420#endif
3421 }
3422 }
3423
3424 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003425 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003427 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 && wp == curwin
3429 && lnum >= curwin->w_cursor.lnum
3430 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3431 {
3432 if (lnum == curwin->w_cursor.lnum)
3433 getvcol(curwin, &(curwin->w_cursor),
3434 (colnr_T *)&fromcol, NULL, NULL);
3435 else
3436 fromcol = 0;
3437 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3438 {
3439 pos.lnum = lnum;
3440 pos.col = search_match_endcol;
3441 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3442 }
3443 else
3444 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003445 /* do at least one character; happens when past end of line */
3446 if (fromcol == tocol)
3447 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003449 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
3451
3452#ifdef FEAT_DIFF
3453 filler_lines = diff_check(wp, lnum);
3454 if (filler_lines < 0)
3455 {
3456 if (filler_lines == -1)
3457 {
3458 if (diff_find_change(wp, lnum, &change_start, &change_end))
3459 diff_hlf = HLF_ADD; /* added line */
3460 else if (change_start == 0)
3461 diff_hlf = HLF_TXD; /* changed text */
3462 else
3463 diff_hlf = HLF_CHD; /* changed line */
3464 }
3465 else
3466 diff_hlf = HLF_ADD; /* added line */
3467 filler_lines = 0;
3468 area_highlighting = TRUE;
3469 }
3470 if (lnum == wp->w_topline)
3471 filler_lines = wp->w_topfill;
3472 filler_todo = filler_lines;
3473#endif
3474
3475#ifdef LINE_ATTR
3476# ifdef FEAT_SIGNS
3477 /* If this line has a sign with line highlighting set line_attr. */
3478 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3479 if (v != 0)
3480 line_attr = sign_get_attr((int)v, TRUE);
3481# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003482# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003484 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003485 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486# endif
3487 if (line_attr != 0)
3488 area_highlighting = TRUE;
3489#endif
3490
3491 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3492 ptr = line;
3493
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003494#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003495 if (has_spell)
3496 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003497 /* For checking first word with a capital skip white space. */
3498 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003499 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003500
Bram Moolenaar30abd282005-06-22 22:35:10 +00003501 /* To be able to spell-check over line boundaries copy the end of the
3502 * current line into nextline[]. Above the start of the next line was
3503 * copied to nextline[SPWORDLEN]. */
3504 if (nextline[SPWORDLEN] == NUL)
3505 {
3506 /* No next line or it is empty. */
3507 nextlinecol = MAXCOL;
3508 nextline_idx = 0;
3509 }
3510 else
3511 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003512 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003513 if (v < SPWORDLEN)
3514 {
3515 /* Short line, use it completely and append the start of the
3516 * next line. */
3517 nextlinecol = 0;
3518 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003519 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003520 nextline_idx = v + 1;
3521 }
3522 else
3523 {
3524 /* Long line, use only the last SPWORDLEN bytes. */
3525 nextlinecol = v - SPWORDLEN;
3526 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3527 nextline_idx = SPWORDLEN + 1;
3528 }
3529 }
3530 }
3531#endif
3532
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003533 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003535 if (lcs_space || lcs_trail)
3536 extra_check = TRUE;
3537 /* find start of trailing whitespace */
3538 if (lcs_trail)
3539 {
3540 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003541 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003542 --trailcol;
3543 trailcol += (colnr_T) (ptr - line);
3544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546
3547 /*
3548 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3549 * first character to be displayed.
3550 */
3551 if (wp->w_p_wrap)
3552 v = wp->w_skipcol;
3553 else
3554 v = wp->w_leftcol;
3555 if (v > 0)
3556 {
3557#ifdef FEAT_MBYTE
3558 char_u *prev_ptr = ptr;
3559#endif
3560 while (vcol < v && *ptr != NUL)
3561 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003562 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 vcol += c;
3564#ifdef FEAT_MBYTE
3565 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003567 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
3569
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003570 /* When:
3571 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003572 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003573 * - 'virtualedit' is set, or
3574 * - the visual mode is active,
3575 * the end of the line may be before the start of the displayed part.
3576 */
3577 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003578#ifdef FEAT_SYN_HL
3579 wp->w_p_cuc || draw_color_col ||
3580#endif
3581#ifdef FEAT_VIRTUALEDIT
3582 virtual_active() ||
3583#endif
3584 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
3589 /* Handle a character that's not completely on the screen: Put ptr at
3590 * that character but skip the first few screen characters. */
3591 if (vcol > v)
3592 {
3593 vcol -= c;
3594#ifdef FEAT_MBYTE
3595 ptr = prev_ptr;
3596#else
3597 --ptr;
3598#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003599 /* If the character fits on the screen, don't need to skip it.
3600 * Except for a TAB. */
3601 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003602#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003603 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003604#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003605 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003606 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608
3609 /*
3610 * Adjust for when the inverted text is before the screen,
3611 * and when the start of the inverted text is before the screen.
3612 */
3613 if (tocol <= vcol)
3614 fromcol = 0;
3615 else if (fromcol >= 0 && fromcol < vcol)
3616 fromcol = vcol;
3617
3618#ifdef FEAT_LINEBREAK
3619 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3620 if (wp->w_p_wrap)
3621 need_showbreak = TRUE;
3622#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003623#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003624 /* When spell checking a word we need to figure out the start of the
3625 * word and if it's badly spelled or not. */
3626 if (has_spell)
3627 {
3628 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003629 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003630 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003631
3632 pos = wp->w_cursor;
3633 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003634 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003635 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003636
3637 /* spell_move_to() may call ml_get() and make "line" invalid */
3638 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3639 ptr = line + linecol;
3640
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003641 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003642 {
3643 /* no bad word found at line start, don't check until end of a
3644 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003645 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003646 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003647 }
3648 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003649 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003650 /* bad word found, use attributes until end of word */
3651 word_end = wp->w_cursor.col + len + 1;
3652
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003653 /* Turn index into actual attributes. */
3654 if (spell_hlf != HLF_COUNT)
3655 spell_attr = highlight_attr[spell_hlf];
3656 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003657 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003658
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003659# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003660 /* Need to restart syntax highlighting for this line. */
3661 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003662 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003663# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003664 }
3665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667
3668 /*
3669 * Correct highlighting for cursor that can't be disabled.
3670 * Avoids having to check this for each character.
3671 */
3672 if (fromcol >= 0)
3673 {
3674 if (noinvcur)
3675 {
3676 if ((colnr_T)fromcol == wp->w_virtcol)
3677 {
3678 /* highlighting starts at cursor, let it start just after the
3679 * cursor */
3680 fromcol_prev = fromcol;
3681 fromcol = -1;
3682 }
3683 else if ((colnr_T)fromcol < wp->w_virtcol)
3684 /* restart highlighting after the cursor */
3685 fromcol_prev = wp->w_virtcol;
3686 }
3687 if (fromcol >= tocol)
3688 fromcol = -1;
3689 }
3690
3691#ifdef FEAT_SEARCH_EXTRA
3692 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003693 * Handle highlighting the last used search pattern and matches.
3694 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003696 cur = wp->w_match_head;
3697 shl_flag = FALSE;
3698 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003700 if (shl_flag == FALSE)
3701 {
3702 shl = &search_hl;
3703 shl_flag = TRUE;
3704 }
3705 else
3706 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003707 shl->startcol = MAXCOL;
3708 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003710 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003711 v = (long)(ptr - line);
3712 if (cur != NULL)
3713 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003714 next_search_hl(wp, shl, lnum, (colnr_T)v,
3715 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003716
3717 /* Need to get the line again, a multi-line regexp may have made it
3718 * invalid. */
3719 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3720 ptr = line + v;
3721
3722 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003724 if (shl->lnum == lnum)
3725 shl->startcol = shl->rm.startpos[0].col;
3726 else
3727 shl->startcol = 0;
3728 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3729 - shl->rm.startpos[0].lnum)
3730 shl->endcol = shl->rm.endpos[0].col;
3731 else
3732 shl->endcol = MAXCOL;
3733 /* Highlight one character for an empty match. */
3734 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003737 if (has_mbyte && line[shl->endcol] != NUL)
3738 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3739 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003741 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003743 if ((long)shl->startcol < v) /* match at leftcol */
3744 {
3745 shl->attr_cur = shl->attr;
3746 search_attr = shl->attr;
3747 }
3748 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003750 if (shl != &search_hl && cur != NULL)
3751 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753#endif
3754
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003755#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003756 /* Cursor line highlighting for 'cursorline' in the current window. Not
3757 * when Visual mode is active, because it's not clear what is selected
3758 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003759 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003760 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003761 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003762 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003763 area_highlighting = TRUE;
3764 }
3765#endif
3766
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003767 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 col = 0;
3769#ifdef FEAT_RIGHTLEFT
3770 if (wp->w_p_rl)
3771 {
3772 /* Rightleft window: process the text in the normal direction, but put
3773 * it in current_ScreenLine[] from right to left. Start at the
3774 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003775 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 off += col;
3777 }
3778#endif
3779
3780 /*
3781 * Repeat for the whole displayed line.
3782 */
3783 for (;;)
3784 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003785#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003786 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 /* Skip this quickly when working on the text. */
3789 if (draw_state != WL_LINE)
3790 {
3791#ifdef FEAT_CMDWIN
3792 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3793 {
3794 draw_state = WL_CMDLINE;
3795 if (cmdwin_type != 0 && wp == curwin)
3796 {
3797 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003799 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003800 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802 }
3803#endif
3804
3805#ifdef FEAT_FOLDING
3806 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3807 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003808 int fdc = compute_foldcolumn(wp, 0);
3809
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003811 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003813 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003814 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003815 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003816 p_extra_free = alloc(12 + 1);
3817
3818 if (p_extra_free != NULL)
3819 {
3820 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3821 n_extra = fdc;
3822 p_extra_free[n_extra] = NUL;
3823 p_extra = p_extra_free;
3824 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003825 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828 }
3829#endif
3830
3831#ifdef FEAT_SIGNS
3832 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3833 {
3834 draw_state = WL_SIGN;
3835 /* Show the sign column when there are any signs in this
3836 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003837 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003839 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003841 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842# endif
3843
3844 /* Draw two cells with the sign value or blank. */
3845 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003846 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 n_extra = 2;
3848
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003849 if (row == startrow
3850#ifdef FEAT_DIFF
3851 + filler_lines && filler_todo <= 0
3852#endif
3853 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
3855 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3856 SIGN_TEXT);
3857# ifdef FEAT_SIGN_ICONS
3858 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3859 SIGN_ICON);
3860 if (gui.in_use && icon_sign != 0)
3861 {
3862 /* Use the image in this position. */
3863 c_extra = SIGN_BYTE;
3864# ifdef FEAT_NETBEANS_INTG
3865 if (buf_signcount(wp->w_buffer, lnum) > 1)
3866 c_extra = MULTISIGN_BYTE;
3867# endif
3868 char_attr = icon_sign;
3869 }
3870 else
3871# endif
3872 if (text_sign != 0)
3873 {
3874 p_extra = sign_get_text(text_sign);
3875 if (p_extra != NULL)
3876 {
3877 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003878 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 }
3880 char_attr = sign_get_attr(text_sign, FALSE);
3881 }
3882 }
3883 }
3884 }
3885#endif
3886
3887 if (draw_state == WL_NR - 1 && n_extra == 0)
3888 {
3889 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003890 /* Display the absolute or relative line number. After the
3891 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3892 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 && (row == startrow
3894#ifdef FEAT_DIFF
3895 + filler_lines
3896#endif
3897 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3898 {
3899 /* Draw the line number (empty space after wrapping). */
3900 if (row == startrow
3901#ifdef FEAT_DIFF
3902 + filler_lines
3903#endif
3904 )
3905 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003906 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003907 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003908
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003909 if (wp->w_p_nu && !wp->w_p_rnu)
3910 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003911 num = (long)lnum;
3912 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003913 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003914 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003915 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003916 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003917 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003918 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003919 num = lnum;
3920 fmt = "%-*ld ";
3921 }
3922 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003923
Bram Moolenaar700e7342013-01-30 12:31:36 +01003924 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003925 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (wp->w_skipcol > 0)
3927 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3928 *p_extra = '-';
3929#ifdef FEAT_RIGHTLEFT
3930 if (wp->w_p_rl) /* reverse line numbers */
3931 rl_mirror(extra);
3932#endif
3933 p_extra = extra;
3934 c_extra = NUL;
3935 }
3936 else
3937 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003938 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003939 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003940#ifdef FEAT_SYN_HL
3941 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003942 * the current line differently.
3943 * TODO: Can we use CursorLine instead of CursorLineNr
3944 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003945 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003946 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003947 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 }
3951
Bram Moolenaar597a4222014-06-25 14:39:50 +02003952#ifdef FEAT_LINEBREAK
3953 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3954 && n_extra == 0 && *p_sbr != NUL)
3955 /* draw indent after showbreak value */
3956 draw_state = WL_BRI;
3957 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3958 /* After the showbreak, draw the breakindent */
3959 draw_state = WL_BRI - 1;
3960
3961 /* draw 'breakindent': indent wrapped text accordingly */
3962 if (draw_state == WL_BRI - 1 && n_extra == 0)
3963 {
3964 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003965 /* if need_showbreak is set, breakindent also applies */
3966 if (wp->w_p_bri && n_extra == 0
3967 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003968# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003969 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003970# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003971 )
3972 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003973 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003974# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003975 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003976 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003977 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003978# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003979 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3980 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003981 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003982# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003983 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003984# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003985 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003986 c_extra = ' ';
3987 n_extra = get_breakindent_win(wp,
3988 ml_get_buf(wp->w_buffer, lnum, FALSE));
3989 /* Correct end of highlighted area for 'breakindent',
3990 * required when 'linebreak' is also set. */
3991 if (tocol == vcol)
3992 tocol += n_extra;
3993 }
3994 }
3995#endif
3996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3998 if (draw_state == WL_SBR - 1 && n_extra == 0)
3999 {
4000 draw_state = WL_SBR;
4001# ifdef FEAT_DIFF
4002 if (filler_todo > 0)
4003 {
4004 /* Draw "deleted" diff line(s). */
4005 if (char2cells(fill_diff) > 1)
4006 c_extra = '-';
4007 else
4008 c_extra = fill_diff;
4009# ifdef FEAT_RIGHTLEFT
4010 if (wp->w_p_rl)
4011 n_extra = col + 1;
4012 else
4013# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004014 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004015 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017# endif
4018# ifdef FEAT_LINEBREAK
4019 if (*p_sbr != NUL && need_showbreak)
4020 {
4021 /* Draw 'showbreak' at the start of each broken line. */
4022 p_extra = p_sbr;
4023 c_extra = NUL;
4024 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004025 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004027 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 /* Correct end of highlighted area for 'showbreak',
4029 * required when 'linebreak' is also set. */
4030 if (tocol == vcol)
4031 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004032#ifdef FEAT_SYN_HL
4033 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004034 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004035 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004036 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039# endif
4040 }
4041#endif
4042
4043 if (draw_state == WL_LINE - 1 && n_extra == 0)
4044 {
4045 draw_state = WL_LINE;
4046 if (saved_n_extra)
4047 {
4048 /* Continue item from end of wrapped line. */
4049 n_extra = saved_n_extra;
4050 c_extra = saved_c_extra;
4051 p_extra = saved_p_extra;
4052 char_attr = saved_char_attr;
4053 }
4054 else
4055 char_attr = 0;
4056 }
4057 }
4058
4059 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004060 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004061 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062#ifdef FEAT_DIFF
4063 && filler_todo <= 0
4064#endif
4065 )
4066 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004067 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004068 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069 /* Pretend we have finished updating the window. Except when
4070 * 'cursorcolumn' is set. */
4071#ifdef FEAT_SYN_HL
4072 if (wp->w_p_cuc)
4073 row = wp->w_cline_row + wp->w_cline_height;
4074 else
4075#endif
4076 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 break;
4078 }
4079
4080 if (draw_state == WL_LINE && area_highlighting)
4081 {
4082 /* handle Visual or match highlighting in this line */
4083 if (vcol == fromcol
4084#ifdef FEAT_MBYTE
4085 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4086 && (*mb_ptr2cells)(ptr) > 1)
4087#endif
4088 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004089 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 && vcol < tocol))
4091 area_attr = attr; /* start highlighting */
4092 else if (area_attr != 0
4093 && (vcol == tocol
4094 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
4097#ifdef FEAT_SEARCH_EXTRA
4098 if (!n_extra)
4099 {
4100 /*
4101 * Check for start/end of search pattern match.
4102 * After end, check for start/end of next match.
4103 * When another match, have to check for start again.
4104 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004105 * Do this for 'search_hl' and the match list (ordered by
4106 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004108 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004109 cur = wp->w_match_head;
4110 shl_flag = FALSE;
4111 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004113 if (shl_flag == FALSE
4114 && ((cur != NULL
4115 && cur->priority > SEARCH_HL_PRIORITY)
4116 || cur == NULL))
4117 {
4118 shl = &search_hl;
4119 shl_flag = TRUE;
4120 }
4121 else
4122 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004123 if (cur != NULL)
4124 cur->pos.cur = 0;
4125 pos_inprogress = TRUE;
4126 while (shl->rm.regprog != NULL
4127 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004129 if (shl->startcol != MAXCOL
4130 && v >= (long)shl->startcol
4131 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004133#ifdef FEAT_MBYTE
4134 int tmp_col = v + MB_PTR2LEN(ptr);
4135
4136 if (shl->endcol < tmp_col)
4137 shl->endcol = tmp_col;
4138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004140#ifdef FEAT_CONCEAL
4141 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4142 == cur->hlg_id)
4143 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004144 has_match_conc =
4145 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004146 match_conc = cur->conceal_char;
4147 }
4148 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004149 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004152 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 {
4154 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004155 next_search_hl(wp, shl, lnum, (colnr_T)v,
4156 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004157 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004158 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
4160 /* Need to get the line again, a multi-line regexp
4161 * may have made it invalid. */
4162 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4163 ptr = line + v;
4164
4165 if (shl->lnum == lnum)
4166 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004167 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004169 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004171 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004173 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
4175 /* highlight empty match, try again after
4176 * it */
4177#ifdef FEAT_MBYTE
4178 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004179 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004180 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 else
4182#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004183 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185
4186 /* Loop to check if the match starts at the
4187 * current position */
4188 continue;
4189 }
4190 }
4191 break;
4192 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004193 if (shl != &search_hl && cur != NULL)
4194 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004196
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004197 /* Use attributes from match with highest priority among
4198 * 'search_hl' and the match list. */
4199 search_attr = search_hl.attr_cur;
4200 cur = wp->w_match_head;
4201 shl_flag = FALSE;
4202 while (cur != NULL || shl_flag == FALSE)
4203 {
4204 if (shl_flag == FALSE
4205 && ((cur != NULL
4206 && cur->priority > SEARCH_HL_PRIORITY)
4207 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004208 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004209 shl = &search_hl;
4210 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004211 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004212 else
4213 shl = &cur->hl;
4214 if (shl->attr_cur != 0)
4215 search_attr = shl->attr_cur;
4216 if (shl != &search_hl && cur != NULL)
4217 cur = cur->next;
4218 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004219 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004220 if (*ptr == NUL && (did_line_attr >= 1
4221 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004222 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224#endif
4225
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004227 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004229 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4230 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004232 if (diff_hlf == HLF_TXD && ptr - line > change_end
4233 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004235 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004236 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004237 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
4239#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004240
4241 /* Decide which of the highlight attributes to use. */
4242 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004243#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004244 if (area_attr != 0)
4245 char_attr = hl_combine_attr(line_attr, area_attr);
4246 else if (search_attr != 0)
4247 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004248 /* Use line_attr when not in the Visual or 'incsearch' area
4249 * (area_attr may be 0 when "noinvcur" is set). */
4250 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004251 || vcol < fromcol || vcol_prev < fromcol_prev
4252 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004253 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004254#else
4255 if (area_attr != 0)
4256 char_attr = area_attr;
4257 else if (search_attr != 0)
4258 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004259#endif
4260 else
4261 {
4262 attr_pri = FALSE;
4263#ifdef FEAT_SYN_HL
4264 if (has_syntax)
4265 char_attr = syntax_attr;
4266 else
4267#endif
4268 char_attr = 0;
4269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271
4272 /*
4273 * Get the next character to put on the screen.
4274 */
4275 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004276 * The "p_extra" points to the extra stuff that is inserted to
4277 * represent special characters (non-printable stuff) and other
4278 * things. When all characters are the same, c_extra is used.
4279 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4280 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4282 */
4283 if (n_extra > 0)
4284 {
4285 if (c_extra != NUL)
4286 {
4287 c = c_extra;
4288#ifdef FEAT_MBYTE
4289 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004290 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 {
4292 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004293 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004294 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 else
4297 mb_utf8 = FALSE;
4298#endif
4299 }
4300 else
4301 {
4302 c = *p_extra;
4303#ifdef FEAT_MBYTE
4304 if (has_mbyte)
4305 {
4306 mb_c = c;
4307 if (enc_utf8)
4308 {
4309 /* If the UTF-8 character is more than one byte:
4310 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004311 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 mb_utf8 = FALSE;
4313 if (mb_l > n_extra)
4314 mb_l = 1;
4315 else if (mb_l > 1)
4316 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004317 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004319 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 }
4321 }
4322 else
4323 {
4324 /* if this is a DBCS character, put it in "mb_c" */
4325 mb_l = MB_BYTE2LEN(c);
4326 if (mb_l >= n_extra)
4327 mb_l = 1;
4328 else if (mb_l > 1)
4329 mb_c = (c << 8) + p_extra[1];
4330 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004331 if (mb_l == 0) /* at the NUL at end-of-line */
4332 mb_l = 1;
4333
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 /* If a double-width char doesn't fit display a '>' in the
4335 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004336 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337# ifdef FEAT_RIGHTLEFT
4338 wp->w_p_rl ? (col <= 0) :
4339# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004340 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 && (*mb_char2cells)(mb_c) == 2)
4342 {
4343 c = '>';
4344 mb_c = c;
4345 mb_l = 1;
4346 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004347 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 /* put the pointer back to output the double-width
4349 * character at the start of the next line. */
4350 ++n_extra;
4351 --p_extra;
4352 }
4353 else
4354 {
4355 n_extra -= mb_l - 1;
4356 p_extra += mb_l - 1;
4357 }
4358 }
4359#endif
4360 ++p_extra;
4361 }
4362 --n_extra;
4363 }
4364 else
4365 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004366#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004367 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004368#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004369
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004370 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004371 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 /*
4373 * Get a character from the line itself.
4374 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004375 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004376#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004377 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379#ifdef FEAT_MBYTE
4380 if (has_mbyte)
4381 {
4382 mb_c = c;
4383 if (enc_utf8)
4384 {
4385 /* If the UTF-8 character is more than one byte: Decode it
4386 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004387 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 mb_utf8 = FALSE;
4389 if (mb_l > 1)
4390 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004391 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 /* Overlong encoded ASCII or ASCII with composing char
4393 * is displayed normally, except a NUL. */
4394 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004395 {
4396 c = mb_c;
4397# ifdef FEAT_LINEBREAK
4398 c0 = mb_c;
4399# endif
4400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004402
4403 /* At start of the line we can have a composing char.
4404 * Draw it as a space with a composing char. */
4405 if (utf_iscomposing(mb_c))
4406 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004407 int i;
4408
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004409 for (i = Screen_mco - 1; i > 0; --i)
4410 u8cc[i] = u8cc[i - 1];
4411 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004412 mb_c = ' ';
4413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415
4416 if ((mb_l == 1 && c >= 0x80)
4417 || (mb_l >= 1 && mb_c == 0)
4418 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004419# ifdef UNICODE16
4420 || mb_c >= 0x10000
4421# endif
4422 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
4424 /*
4425 * Illegal UTF-8 byte: display as <xx>.
4426 * Non-BMP character : display as ? or fullwidth ?.
4427 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004428# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
4432 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004433# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 if (wp->w_p_rl) /* reverse */
4435 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004438# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 else if (utf_char2cells(mb_c) != 2)
4440 STRCPY(extra, "?");
4441 else
4442 /* 0xff1f in UTF-8: full-width '?' */
4443 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004444# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
4446 p_extra = extra;
4447 c = *p_extra;
4448 mb_c = mb_ptr2char_adv(&p_extra);
4449 mb_utf8 = (c >= 0x80);
4450 n_extra = (int)STRLEN(p_extra);
4451 c_extra = NUL;
4452 if (area_attr == 0 && search_attr == 0)
4453 {
4454 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004455 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 saved_attr2 = char_attr; /* save current attr */
4457 }
4458 }
4459 else if (mb_l == 0) /* at the NUL at end-of-line */
4460 mb_l = 1;
4461#ifdef FEAT_ARABIC
4462 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4463 {
4464 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004465 int pc, pc1, nc;
4466 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
4468 /* The idea of what is the previous and next
4469 * character depends on 'rightleft'. */
4470 if (wp->w_p_rl)
4471 {
4472 pc = prev_c;
4473 pc1 = prev_c1;
4474 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004475 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477 else
4478 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004479 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004481 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483 prev_c = mb_c;
4484
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004485 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 else
4488 prev_c = mb_c;
4489#endif
4490 }
4491 else /* enc_dbcs */
4492 {
4493 mb_l = MB_BYTE2LEN(c);
4494 if (mb_l == 0) /* at the NUL at end-of-line */
4495 mb_l = 1;
4496 else if (mb_l > 1)
4497 {
4498 /* We assume a second byte below 32 is illegal.
4499 * Hopefully this is OK for all double-byte encodings!
4500 */
4501 if (ptr[1] >= 32)
4502 mb_c = (c << 8) + ptr[1];
4503 else
4504 {
4505 if (ptr[1] == NUL)
4506 {
4507 /* head byte at end of line */
4508 mb_l = 1;
4509 transchar_nonprint(extra, c);
4510 }
4511 else
4512 {
4513 /* illegal tail byte */
4514 mb_l = 2;
4515 STRCPY(extra, "XX");
4516 }
4517 p_extra = extra;
4518 n_extra = (int)STRLEN(extra) - 1;
4519 c_extra = NUL;
4520 c = *p_extra++;
4521 if (area_attr == 0 && search_attr == 0)
4522 {
4523 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004524 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 saved_attr2 = char_attr; /* save current attr */
4526 }
4527 mb_c = c;
4528 }
4529 }
4530 }
4531 /* If a double-width char doesn't fit display a '>' in the
4532 * last column; the character is displayed at the start of the
4533 * next line. */
4534 if ((
4535# ifdef FEAT_RIGHTLEFT
4536 wp->w_p_rl ? (col <= 0) :
4537# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004538 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 && (*mb_char2cells)(mb_c) == 2)
4540 {
4541 c = '>';
4542 mb_c = c;
4543 mb_utf8 = FALSE;
4544 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004545 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 /* Put pointer back so that the character will be
4547 * displayed at the start of the next line. */
4548 --ptr;
4549 }
4550 else if (*ptr != NUL)
4551 ptr += mb_l - 1;
4552
4553 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004554 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004555 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004556 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004559 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 c = ' ';
4561 if (area_attr == 0 && search_attr == 0)
4562 {
4563 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004564 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 saved_attr2 = char_attr; /* save current attr */
4566 }
4567 mb_c = c;
4568 mb_utf8 = FALSE;
4569 mb_l = 1;
4570 }
4571
4572 }
4573#endif
4574 ++ptr;
4575
4576 if (extra_check)
4577 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004578#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004579 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004580#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004581
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004582#ifdef FEAT_TERMINAL
4583 if (get_term_attr)
4584 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004585 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004586
4587 if (!attr_pri)
4588 char_attr = syntax_attr;
4589 else
4590 char_attr = hl_combine_attr(syntax_attr, char_attr);
4591 }
4592#endif
4593
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004594#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 /* Get syntax attribute, unless still at the start of the line
4596 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004597 v = (long)(ptr - line);
4598 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 {
4600 /* Get the syntax attribute for the character. If there
4601 * is an error, disable syntax highlighting. */
4602 save_did_emsg = did_emsg;
4603 did_emsg = FALSE;
4604
Bram Moolenaar217ad922005-03-20 22:37:15 +00004605 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004606# ifdef FEAT_SPELL
4607 has_spell ? &can_spell :
4608# endif
4609 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
4611 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004612 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004613 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004614 has_syntax = FALSE;
4615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 else
4617 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004618#ifdef SYN_TIME_LIMIT
4619 if (wp->w_s->b_syn_slow)
4620 has_syntax = FALSE;
4621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
4623 /* Need to get the line again, a multi-line regexp may
4624 * have made it invalid. */
4625 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4626 ptr = line + v;
4627
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004628 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004630 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004631 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004632# ifdef FEAT_CONCEAL
4633 /* no concealing past the end of the line, it interferes
4634 * with line highlighting */
4635 if (c == NUL)
4636 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004637 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004638 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004641#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004642
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004643#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004644 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004645 * Only do this when there is no syntax highlighting, the
4646 * @Spell cluster is not used or the current syntax item
4647 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004648 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004649 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004650 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004651# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004652 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004653 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004654# endif
4655 if (c != 0 && (
4656# ifdef FEAT_SYN_HL
4657 !has_syntax ||
4658# endif
4659 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004660 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004661 char_u *prev_ptr, *p;
4662 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004663 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004664# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004665 if (has_mbyte)
4666 {
4667 prev_ptr = ptr - mb_l;
4668 v -= mb_l - 1;
4669 }
4670 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004671# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004672 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004673
4674 /* Use nextline[] if possible, it has the start of the
4675 * next line concatenated. */
4676 if ((prev_ptr - line) - nextlinecol >= 0)
4677 p = nextline + (prev_ptr - line) - nextlinecol;
4678 else
4679 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004680 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004681 len = spell_check(wp, p, &spell_hlf, &cap_col,
4682 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004683 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004684
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004685 /* In Insert mode only highlight a word that
4686 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004687 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004688 && (State & INSERT) != 0
4689 && wp->w_cursor.lnum == lnum
4690 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004691 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004692 && wp->w_cursor.col < (colnr_T)word_end)
4693 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004694 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004695 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004696 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004697
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004698 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004699 && (p - nextline) + len > nextline_idx)
4700 {
4701 /* Remember that the good word continues at the
4702 * start of the next line. */
4703 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004704 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004705 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004706
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004707 /* Turn index into actual attributes. */
4708 if (spell_hlf != HLF_COUNT)
4709 spell_attr = highlight_attr[spell_hlf];
4710
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004711 if (cap_col > 0)
4712 {
4713 if (p != prev_ptr
4714 && (p - nextline) + cap_col >= nextline_idx)
4715 {
4716 /* Remember that the word in the next line
4717 * must start with a capital. */
4718 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004719 cap_col = (int)((p - nextline) + cap_col
4720 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004721 }
4722 else
4723 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004724 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004725 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004726 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004727 }
4728 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004729 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004730 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004731 char_attr = hl_combine_attr(char_attr, spell_attr);
4732 else
4733 char_attr = hl_combine_attr(spell_attr, char_attr);
4734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735#endif
4736#ifdef FEAT_LINEBREAK
4737 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004738 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004740 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004741 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004743# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004744 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004745# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004746 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004748 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004750 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004751
Bram Moolenaar597a4222014-06-25 14:39:50 +02004752 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004753 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004754 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004755 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaara3650912014-11-19 13:21:57 +01004756 n_extra = (int)wp->w_buffer->b_p_ts
4757 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4758
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004759# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004760 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004761# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004763# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004764 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004765 {
4766#ifdef FEAT_CONCEAL
4767 if (c == TAB)
4768 /* See "Tab alignment" below. */
4769 FIX_FOR_BOGUSCOLS;
4770#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004771 if (!wp->w_p_list)
4772 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 }
4775#endif
4776
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004777 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4778 */
4779 if (wp->w_p_list
4780 && (((c == 160
4781#ifdef FEAT_MBYTE
4782 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4783#endif
4784 ) && lcs_nbsp)
4785 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4786 {
4787 c = (c == ' ') ? lcs_space : lcs_nbsp;
4788 if (area_attr == 0 && search_attr == 0)
4789 {
4790 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004791 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004792 saved_attr2 = char_attr; /* save current attr */
4793 }
4794#ifdef FEAT_MBYTE
4795 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004796 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004797 {
4798 mb_utf8 = TRUE;
4799 u8cc[0] = 0;
4800 c = 0xc0;
4801 }
4802 else
4803 mb_utf8 = FALSE;
4804#endif
4805 }
4806
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4808 {
4809 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004810 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 {
4812 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004813 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 saved_attr2 = char_attr; /* save current attr */
4815 }
4816#ifdef FEAT_MBYTE
4817 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004818 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 {
4820 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004821 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004822 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
4824 else
4825 mb_utf8 = FALSE;
4826#endif
4827 }
4828 }
4829
4830 /*
4831 * Handling of non-printable characters.
4832 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004833 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 {
4835 /*
4836 * when getting a character from the file, we may have to
4837 * turn it into something else on the way to putting it
4838 * into "ScreenLines".
4839 */
4840 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4841 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004842 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004843 long vcol_adjusted = vcol; /* removed showbreak length */
4844#ifdef FEAT_LINEBREAK
4845 /* only adjust the tab_len, when at the first column
4846 * after the showbreak value was drawn */
4847 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4848 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004851 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004852 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4853
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004854#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004855 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004856#endif
4857 /* tab amount depends on current column */
4858 n_extra = tab_len;
4859#ifdef FEAT_LINEBREAK
4860 else
4861 {
4862 char_u *p;
4863 int len = n_extra;
4864 int i;
4865 int saved_nextra = n_extra;
4866
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004867#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004868 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004869 /* there are characters to conceal */
4870 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004871 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4872 */
4873 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4874 && n_extra > tab_len)
4875 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004876#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004877
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004878 /* if n_extra > 0, it gives the number of chars, to
4879 * use for a tab, else we need to calculate the width
4880 * for a tab */
4881#ifdef FEAT_MBYTE
4882 len = (tab_len * mb_char2len(lcs_tab2));
4883 if (n_extra > 0)
4884 len += n_extra - tab_len;
4885#endif
4886 c = lcs_tab1;
4887 p = alloc((unsigned)(len + 1));
4888 vim_memset(p, ' ', len);
4889 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004890 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004891 p_extra_free = p;
4892 for (i = 0; i < tab_len; i++)
4893 {
4894#ifdef FEAT_MBYTE
4895 mb_char2bytes(lcs_tab2, p);
4896 p += mb_char2len(lcs_tab2);
4897 n_extra += mb_char2len(lcs_tab2)
4898 - (saved_nextra > 0 ? 1 : 0);
4899#else
4900 p[i] = lcs_tab2;
4901#endif
4902 }
4903 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004904#ifdef FEAT_CONCEAL
4905 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4906 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004907 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004908 n_extra -= vcol_off;
4909#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004910 }
4911#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004912#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004913 {
4914 int vc_saved = vcol_off;
4915
4916 /* Tab alignment should be identical regardless of
4917 * 'conceallevel' value. So tab compensates of all
4918 * previous concealed characters, and thus resets
4919 * vcol_off and boguscols accumulated so far in the
4920 * line. Note that the tab can be longer than
4921 * 'tabstop' when there are concealed characters. */
4922 FIX_FOR_BOGUSCOLS;
4923
4924 /* Make sure, the highlighting for the tab char will be
4925 * correctly set further below (effectively reverts the
4926 * FIX_FOR_BOGSUCOLS macro */
4927 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004928 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004929 tab_len += vc_saved;
4930 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932#ifdef FEAT_MBYTE
4933 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4934#endif
4935 if (wp->w_p_list)
4936 {
4937 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004938#ifdef FEAT_LINEBREAK
4939 if (wp->w_p_lbr)
4940 c_extra = NUL; /* using p_extra from above */
4941 else
4942#endif
4943 c_extra = lcs_tab2;
4944 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004945 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 saved_attr2 = char_attr; /* save current attr */
4947#ifdef FEAT_MBYTE
4948 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004949 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
4951 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004952 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004953 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955#endif
4956 }
4957 else
4958 {
4959 c_extra = ' ';
4960 c = ' ';
4961 }
4962 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004963 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004964 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004965 || ((fromcol >= 0 || fromcol_prev >= 0)
4966 && tocol > vcol
4967 && VIsual_mode != Ctrl_V
4968 && (
4969# ifdef FEAT_RIGHTLEFT
4970 wp->w_p_rl ? (col >= 0) :
4971# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004972 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004973 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004974 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004975 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004976 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004978 /* Display a '$' after the line or highlight an extra
4979 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4981 /* For a diff line the highlighting continues after the
4982 * "$". */
4983 if (
4984# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004985 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986# ifdef LINE_ATTR
4987 &&
4988# endif
4989# endif
4990# ifdef LINE_ATTR
4991 line_attr == 0
4992# endif
4993 )
4994#endif
4995 {
4996#ifdef FEAT_VIRTUALEDIT
4997 /* In virtualedit, visual selections may extend
4998 * beyond end of line. */
4999 if (area_highlighting && virtual_active()
5000 && tocol != MAXCOL && vcol < tocol)
5001 n_extra = 0;
5002 else
5003#endif
5004 {
5005 p_extra = at_end_str;
5006 n_extra = 1;
5007 c_extra = NUL;
5008 }
5009 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005010 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005011 c = lcs_eol;
5012 else
5013 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 lcs_eol_one = -1;
5015 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005016 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005018 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 n_attr = 1;
5020 }
5021#ifdef FEAT_MBYTE
5022 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005023 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
5025 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005026 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005027 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
5029 else
5030 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5031#endif
5032 }
5033 else if (c != NUL)
5034 {
5035 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005036 if (n_extra == 0)
5037 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038#ifdef FEAT_RIGHTLEFT
5039 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5040 rl_mirror(p_extra); /* reverse "<12>" */
5041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005043#ifdef FEAT_LINEBREAK
5044 if (wp->w_p_lbr)
5045 {
5046 char_u *p;
5047
5048 c = *p_extra;
5049 p = alloc((unsigned)n_extra + 1);
5050 vim_memset(p, ' ', n_extra);
5051 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5052 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005053 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005054 p_extra_free = p_extra = p;
5055 }
5056 else
5057#endif
5058 {
5059 n_extra = byte2cells(c) - 1;
5060 c = *p_extra++;
5061 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005062 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005065 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 saved_attr2 = char_attr; /* save current attr */
5067 }
5068#ifdef FEAT_MBYTE
5069 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5070#endif
5071 }
5072#ifdef FEAT_VIRTUALEDIT
5073 else if (VIsual_active
5074 && (VIsual_mode == Ctrl_V
5075 || VIsual_mode == 'v')
5076 && virtual_active()
5077 && tocol != MAXCOL
5078 && vcol < tocol
5079 && (
5080# ifdef FEAT_RIGHTLEFT
5081 wp->w_p_rl ? (col >= 0) :
5082# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005083 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
5085 c = ' ';
5086 --ptr; /* put it back at the NUL */
5087 }
5088#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005089#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 else if ((
5091# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005092 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005094# ifdef FEAT_TERMINAL
5095 term_attr != 0 ||
5096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 ) && (
5099# ifdef FEAT_RIGHTLEFT
5100 wp->w_p_rl ? (col >= 0) :
5101# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005102 (col
5103# ifdef FEAT_CONCEAL
5104 - boguscols
5105# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005106 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
5108 /* Highlight until the right side of the window */
5109 c = ' ';
5110 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005111
5112 /* Remember we do the char for line highlighting. */
5113 ++did_line_attr;
5114
5115 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005116 if (line_attr != 0 && char_attr == search_attr
5117 && (did_line_attr > 1
5118 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005119 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120# ifdef FEAT_DIFF
5121 if (diff_hlf == HLF_TXD)
5122 {
5123 diff_hlf = HLF_CHD;
5124 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005125 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005126 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005127 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5128 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005129 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 }
5132# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005133# ifdef FEAT_TERMINAL
5134 if (term_attr != 0)
5135 {
5136 char_attr = term_attr;
5137 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5138 char_attr = hl_combine_attr(char_attr,
5139 HL_ATTR(HLF_CUL));
5140 }
5141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
5143#endif
5144 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005145
5146#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005147 if ( wp->w_p_cole > 0
5148 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005149 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005150 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005151 && !(lnum_in_visual_area
5152 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005153 {
5154 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005155 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005156 && (syn_get_sub_char() != NUL || match_conc
5157 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005158 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005159 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005160 /* First time at this concealed item: display one
5161 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005162 if (match_conc)
5163 c = match_conc;
5164 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005165 c = syn_get_sub_char();
5166 else if (lcs_conceal != NUL)
5167 c = lcs_conceal;
5168 else
5169 c = ' ';
5170
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005171 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005172
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005173 if (n_extra > 0)
5174 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005175 vcol += n_extra;
5176 if (wp->w_p_wrap && n_extra > 0)
5177 {
5178# ifdef FEAT_RIGHTLEFT
5179 if (wp->w_p_rl)
5180 {
5181 col -= n_extra;
5182 boguscols -= n_extra;
5183 }
5184 else
5185# endif
5186 {
5187 boguscols += n_extra;
5188 col += n_extra;
5189 }
5190 }
5191 n_extra = 0;
5192 n_attr = 0;
5193 }
5194 else if (n_skip == 0)
5195 {
5196 is_concealing = TRUE;
5197 n_skip = 1;
5198 }
5199# ifdef FEAT_MBYTE
5200 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005201 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005202 {
5203 mb_utf8 = TRUE;
5204 u8cc[0] = 0;
5205 c = 0xc0;
5206 }
5207 else
5208 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5209# endif
5210 }
5211 else
5212 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005213 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005214 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005215 }
5216#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005219#ifdef FEAT_CONCEAL
5220 /* In the cursor line and we may be concealing characters: correct
5221 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005222 if (!did_wcol && draw_state == WL_LINE
5223 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005224 && conceal_cursor_line(wp)
5225 && (int)wp->w_virtcol <= vcol + n_skip)
5226 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005227# ifdef FEAT_RIGHTLEFT
5228 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005229 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005230 else
5231# endif
5232 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005233 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005234 did_wcol = TRUE;
5235 }
5236#endif
5237
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 /* Don't override visual selection highlighting. */
5239 if (n_attr > 0
5240 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005241 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 char_attr = extra_attr;
5243
Bram Moolenaar81695252004-12-29 20:58:21 +00005244#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 /* XIM don't send preedit_start and preedit_end, but they send
5246 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5247 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005248 if (p_imst == IM_ON_THE_SPOT
5249 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005250 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 && (State & INSERT)
5252 && !p_imdisable
5253 && im_is_preediting()
5254 && draw_state == WL_LINE)
5255 {
5256 colnr_T tcol;
5257
5258 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005259 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 else
5261 tcol = preedit_end_col;
5262 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5263 {
5264 if (feedback_old_attr < 0)
5265 {
5266 feedback_col = 0;
5267 feedback_old_attr = char_attr;
5268 }
5269 char_attr = im_get_feedback_attr(feedback_col);
5270 if (char_attr < 0)
5271 char_attr = feedback_old_attr;
5272 feedback_col++;
5273 }
5274 else if (feedback_old_attr >= 0)
5275 {
5276 char_attr = feedback_old_attr;
5277 feedback_old_attr = -1;
5278 feedback_col = 0;
5279 }
5280 }
5281#endif
5282 /*
5283 * Handle the case where we are in column 0 but not on the first
5284 * character of the line and the user wants us to show us a
5285 * special character (via 'listchars' option "precedes:<char>".
5286 */
5287 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005288 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5290#ifdef FEAT_DIFF
5291 && filler_todo <= 0
5292#endif
5293 && draw_state > WL_NR
5294 && c != NUL)
5295 {
5296 c = lcs_prec;
5297 lcs_prec_todo = NUL;
5298#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005299 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5300 {
5301 /* Double-width character being overwritten by the "precedes"
5302 * character, need to fill up half the character. */
5303 c_extra = MB_FILLER_CHAR;
5304 n_extra = 1;
5305 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005306 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005309 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 {
5311 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005312 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005313 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
5315 else
5316 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5317#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005318 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
5320 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005321 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 n_attr3 = 1;
5323 }
5324 }
5325
5326 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005327 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005329 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005330#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005331 || did_line_attr == 1
5332#endif
5333 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005335#ifdef FEAT_SEARCH_EXTRA
5336 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005337
5338 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005339 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005340 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005341#endif
5342
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005343 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 * highlight match at end of line. If it's beyond the last
5345 * char on the screen, just overwrite that one (tricky!) Not
5346 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005347#ifdef FEAT_SEARCH_EXTRA
5348 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005349 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005350 prevcol_hl_flag = TRUE;
5351 else
5352 {
5353 cur = wp->w_match_head;
5354 while (cur != NULL)
5355 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005356 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005357 {
5358 prevcol_hl_flag = TRUE;
5359 break;
5360 }
5361 cur = cur->next;
5362 }
5363 }
5364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005366 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005367 && (VIsual_mode != Ctrl_V
5368 || lnum == VIsual.lnum
5369 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005370 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371#ifdef FEAT_SEARCH_EXTRA
5372 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005373 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005374# ifdef FEAT_SYN_HL
5375 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5376 && !(wp == curwin && VIsual_active))
5377# endif
5378# ifdef FEAT_DIFF
5379 && diff_hlf == (hlf_T)0
5380# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005381# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005382 && did_line_attr <= 1
5383# endif
5384 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385#endif
5386 ))
5387 {
5388 int n = 0;
5389
5390#ifdef FEAT_RIGHTLEFT
5391 if (wp->w_p_rl)
5392 {
5393 if (col < 0)
5394 n = 1;
5395 }
5396 else
5397#endif
5398 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005399 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 n = -1;
5401 }
5402 if (n != 0)
5403 {
5404 /* At the window boundary, highlight the last character
5405 * instead (better than nothing). */
5406 off += n;
5407 col += n;
5408 }
5409 else
5410 {
5411 /* Add a blank character to highlight. */
5412 ScreenLines[off] = ' ';
5413#ifdef FEAT_MBYTE
5414 if (enc_utf8)
5415 ScreenLinesUC[off] = 0;
5416#endif
5417 }
5418#ifdef FEAT_SEARCH_EXTRA
5419 if (area_attr == 0)
5420 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005421 /* Use attributes from match with highest priority among
5422 * 'search_hl' and the match list. */
5423 char_attr = search_hl.attr;
5424 cur = wp->w_match_head;
5425 shl_flag = FALSE;
5426 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005427 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005428 if (shl_flag == FALSE
5429 && ((cur != NULL
5430 && cur->priority > SEARCH_HL_PRIORITY)
5431 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005432 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005433 shl = &search_hl;
5434 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005435 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005436 else
5437 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005438 if ((ptr - line) - 1 == (long)shl->startcol
5439 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005440 char_attr = shl->attr;
5441 if (shl != &search_hl && cur != NULL)
5442 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
5445#endif
5446 ScreenAttrs[off] = char_attr;
5447#ifdef FEAT_RIGHTLEFT
5448 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005451 --off;
5452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 else
5454#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005457 ++off;
5458 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005459 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005460#ifdef FEAT_SYN_HL
5461 eol_hl_off = 1;
5462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
Bram Moolenaar91170f82006-05-05 21:15:17 +00005466 /*
5467 * At end of the text line.
5468 */
5469 if (c == NUL)
5470 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005471#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005472 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5473 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005474 {
5475 /* highlight last char after line */
5476 --col;
5477 --off;
5478 --vcol;
5479 }
5480
Bram Moolenaar1a384422010-07-14 19:53:30 +02005481 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005482 if (wp->w_p_wrap)
5483 v = wp->w_skipcol;
5484 else
5485 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005486
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005487 /* check if line ends before left margin */
5488 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005489 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005490#ifdef FEAT_CONCEAL
5491 /* Get rid of the boguscols now, we want to draw until the right
5492 * edge for 'cursorcolumn'. */
5493 col -= boguscols;
5494 boguscols = 0;
5495#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005496
5497 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005498 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005499
5500 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005501 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5502 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005503 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005504 && lnum != wp->w_cursor.lnum)
5505 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005506# ifdef FEAT_RIGHTLEFT
5507 && !wp->w_p_rl
5508# endif
5509 )
5510 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005511 int rightmost_vcol = 0;
5512 int i;
5513
5514 if (wp->w_p_cuc)
5515 rightmost_vcol = wp->w_virtcol;
5516 if (draw_color_col)
5517 /* determine rightmost colorcolumn to possibly draw */
5518 for (i = 0; color_cols[i] >= 0; ++i)
5519 if (rightmost_vcol < color_cols[i])
5520 rightmost_vcol = color_cols[i];
5521
Bram Moolenaar02631462017-09-22 15:20:32 +02005522 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005523 {
5524 ScreenLines[off] = ' ';
5525#ifdef FEAT_MBYTE
5526 if (enc_utf8)
5527 ScreenLinesUC[off] = 0;
5528#endif
5529 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005530 if (draw_color_col)
5531 draw_color_col = advance_color_col(VCOL_HLC,
5532 &color_cols);
5533
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005534 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005535 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005536 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005537 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005538 else
5539 ScreenAttrs[off++] = 0;
5540
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005541 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005542 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005543
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005544 ++vcol;
5545 }
5546 }
5547#endif
5548
Bram Moolenaar53f81742017-09-22 14:35:51 +02005549 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005550 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 row++;
5552
5553 /*
5554 * Update w_cline_height and w_cline_folded if the cursor line was
5555 * updated (saves a call to plines() later).
5556 */
5557 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5558 {
5559 curwin->w_cline_row = startrow;
5560 curwin->w_cline_height = row - startrow;
5561#ifdef FEAT_FOLDING
5562 curwin->w_cline_folded = FALSE;
5563#endif
5564 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5565 }
5566
5567 break;
5568 }
5569
5570 /* line continues beyond line end */
5571 if (lcs_ext
5572 && !wp->w_p_wrap
5573#ifdef FEAT_DIFF
5574 && filler_todo <= 0
5575#endif
5576 && (
5577#ifdef FEAT_RIGHTLEFT
5578 wp->w_p_rl ? col == 0 :
5579#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005580 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005582 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5584 {
5585 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005586 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587#ifdef FEAT_MBYTE
5588 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005589 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
5591 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005592 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005593 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595 else
5596 mb_utf8 = FALSE;
5597#endif
5598 }
5599
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005600#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005601 /* advance to the next 'colorcolumn' */
5602 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005603 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005604
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005605 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005606 * highlight the cursor position itself.
5607 * Also highlight the 'colorcolumn' if it is different than
5608 * 'cursorcolumn' */
5609 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005610 if (draw_state == WL_LINE && !lnum_in_visual_area
5611 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005612 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005613 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005614 && lnum != wp->w_cursor.lnum)
5615 {
5616 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005617 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005618 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005619 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005620 {
5621 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005622 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005623 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005624 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005625#endif
5626
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 /*
5628 * Store character to be displayed.
5629 * Skip characters that are left of the screen for 'nowrap'.
5630 */
5631 vcol_prev = vcol;
5632 if (draw_state < WL_LINE || n_skip <= 0)
5633 {
5634 /*
5635 * Store the character.
5636 */
5637#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5638 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5639 {
5640 /* A double-wide character is: put first halve in left cell. */
5641 --off;
5642 --col;
5643 }
5644#endif
5645 ScreenLines[off] = c;
5646#ifdef FEAT_MBYTE
5647 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005648 {
5649 if ((mb_c & 0xff00) == 0x8e00)
5650 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 else if (enc_utf8)
5654 {
5655 if (mb_utf8)
5656 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005657 int i;
5658
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005660 if ((c & 0xff) == 0)
5661 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005662 for (i = 0; i < Screen_mco; ++i)
5663 {
5664 ScreenLinesC[i][off] = u8cc[i];
5665 if (u8cc[i] == 0)
5666 break;
5667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
5669 else
5670 ScreenLinesUC[off] = 0;
5671 }
5672 if (multi_attr)
5673 {
5674 ScreenAttrs[off] = multi_attr;
5675 multi_attr = 0;
5676 }
5677 else
5678#endif
5679 ScreenAttrs[off] = char_attr;
5680
5681#ifdef FEAT_MBYTE
5682 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5683 {
5684 /* Need to fill two screen columns. */
5685 ++off;
5686 ++col;
5687 if (enc_utf8)
5688 /* UTF-8: Put a 0 in the second screen char. */
5689 ScreenLines[off] = 0;
5690 else
5691 /* DBCS: Put second byte in the second screen char. */
5692 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005693 if (draw_state > WL_NR
5694#ifdef FEAT_DIFF
5695 && filler_todo <= 0
5696#endif
5697 )
5698 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 /* When "tocol" is halfway a character, set it to the end of
5700 * the character, otherwise highlighting won't stop. */
5701 if (tocol == vcol)
5702 ++tocol;
5703#ifdef FEAT_RIGHTLEFT
5704 if (wp->w_p_rl)
5705 {
5706 /* now it's time to backup one cell */
5707 --off;
5708 --col;
5709 }
5710#endif
5711 }
5712#endif
5713#ifdef FEAT_RIGHTLEFT
5714 if (wp->w_p_rl)
5715 {
5716 --off;
5717 --col;
5718 }
5719 else
5720#endif
5721 {
5722 ++off;
5723 ++col;
5724 }
5725 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005726#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005727 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005728 {
5729 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005730 ++vcol_off;
5731 if (n_extra > 0)
5732 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005733 if (wp->w_p_wrap)
5734 {
5735 /*
5736 * Special voodoo required if 'wrap' is on.
5737 *
5738 * Advance the column indicator to force the line
5739 * drawing to wrap early. This will make the line
5740 * take up the same screen space when parts are concealed,
5741 * so that cursor line computations aren't messed up.
5742 *
5743 * To avoid the fictitious advance of 'col' causing
5744 * trailing junk to be written out of the screen line
5745 * we are building, 'boguscols' keeps track of the number
5746 * of bad columns we have advanced.
5747 */
5748 if (n_extra > 0)
5749 {
5750 vcol += n_extra;
5751# ifdef FEAT_RIGHTLEFT
5752 if (wp->w_p_rl)
5753 {
5754 col -= n_extra;
5755 boguscols -= n_extra;
5756 }
5757 else
5758# endif
5759 {
5760 col += n_extra;
5761 boguscols += n_extra;
5762 }
5763 n_extra = 0;
5764 n_attr = 0;
5765 }
5766
5767
5768# ifdef FEAT_MBYTE
5769 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5770 {
5771 /* Need to fill two screen columns. */
5772# ifdef FEAT_RIGHTLEFT
5773 if (wp->w_p_rl)
5774 {
5775 --boguscols;
5776 --col;
5777 }
5778 else
5779# endif
5780 {
5781 ++boguscols;
5782 ++col;
5783 }
5784 }
5785# endif
5786
5787# ifdef FEAT_RIGHTLEFT
5788 if (wp->w_p_rl)
5789 {
5790 --boguscols;
5791 --col;
5792 }
5793 else
5794# endif
5795 {
5796 ++boguscols;
5797 ++col;
5798 }
5799 }
5800 else
5801 {
5802 if (n_extra > 0)
5803 {
5804 vcol += n_extra;
5805 n_extra = 0;
5806 n_attr = 0;
5807 }
5808 }
5809
5810 }
5811#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 else
5813 --n_skip;
5814
Bram Moolenaar64486672010-05-16 15:46:46 +02005815 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5816 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005817 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818#ifdef FEAT_DIFF
5819 && filler_todo <= 0
5820#endif
5821 )
5822 ++vcol;
5823
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005824#ifdef FEAT_SYN_HL
5825 if (vcol_save_attr >= 0)
5826 char_attr = vcol_save_attr;
5827#endif
5828
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 /* restore attributes after "predeces" in 'listchars' */
5830 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5831 char_attr = saved_attr3;
5832
5833 /* restore attributes after last 'listchars' or 'number' char */
5834 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5835 char_attr = saved_attr2;
5836
5837 /*
5838 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005839 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 */
5841 if ((
5842#ifdef FEAT_RIGHTLEFT
5843 wp->w_p_rl ? (col < 0) :
5844#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005845 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 && (*ptr != NUL
5847#ifdef FEAT_DIFF
5848 || filler_todo > 0
5849#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005850 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5852 )
5853 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005854#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005855 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005856 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005857 boguscols = 0;
5858#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005859 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005860 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 ++row;
5863 ++screen_row;
5864
5865 /* When not wrapping and finished diff lines, or when displayed
5866 * '$' and highlighting until last column, break here. */
5867 if ((!wp->w_p_wrap
5868#ifdef FEAT_DIFF
5869 && filler_todo <= 0
5870#endif
5871 ) || lcs_eol_one == -1)
5872 break;
5873
5874 /* When the window is too narrow draw all "@" lines. */
5875 if (draw_state != WL_LINE
5876#ifdef FEAT_DIFF
5877 && filler_todo <= 0
5878#endif
5879 )
5880 {
5881 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 row = endrow;
5884 }
5885
5886 /* When line got too long for screen break here. */
5887 if (row == endrow)
5888 {
5889 ++row;
5890 break;
5891 }
5892
5893 if (screen_cur_row == screen_row - 1
5894#ifdef FEAT_DIFF
5895 && filler_todo <= 0
5896#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005897 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {
5899 /* Remember that the line wraps, used for modeless copy. */
5900 LineWraps[screen_row - 1] = TRUE;
5901
5902 /*
5903 * Special trick to make copy/paste of wrapped lines work with
5904 * xterm/screen: write an extra character beyond the end of
5905 * the line. This will work with all terminal types
5906 * (regardless of the xn,am settings).
5907 * Only do this on a fast tty.
5908 * Only do this if the cursor is on the current line
5909 * (something has been written in it).
5910 * Don't do this for the GUI.
5911 * Don't do this for double-width characters.
5912 * Don't do this for a window not at the right screen border.
5913 */
5914 if (p_tf
5915#ifdef FEAT_GUI
5916 && !gui.in_use
5917#endif
5918#ifdef FEAT_MBYTE
5919 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005920 && ((*mb_off2cells)(LineOffset[screen_row],
5921 LineOffset[screen_row] + screen_Columns)
5922 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005924 + (int)Columns - 2,
5925 LineOffset[screen_row] + screen_Columns)
5926 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927#endif
5928 )
5929 {
5930 /* First make sure we are at the end of the screen line,
5931 * then output the same character again to let the
5932 * terminal know about the wrap. If the terminal doesn't
5933 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005934 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 screen_char(LineOffset[screen_row - 1]
5936 + (unsigned)Columns - 1,
5937 screen_row - 1, (int)(Columns - 1));
5938
5939#ifdef FEAT_MBYTE
5940 /* When there is a multi-byte character, just output a
5941 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005942 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5943 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 out_char(' ');
5945 else
5946#endif
5947 out_char(ScreenLines[LineOffset[screen_row - 1]
5948 + (Columns - 1)]);
5949 /* force a redraw of the first char on the next line */
5950 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5951 screen_start(); /* don't know where cursor is now */
5952 }
5953 }
5954
5955 col = 0;
5956 off = (unsigned)(current_ScreenLine - ScreenLines);
5957#ifdef FEAT_RIGHTLEFT
5958 if (wp->w_p_rl)
5959 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005960 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 off += col;
5962 }
5963#endif
5964
5965 /* reset the drawing state for the start of a wrapped line */
5966 draw_state = WL_START;
5967 saved_n_extra = n_extra;
5968 saved_p_extra = p_extra;
5969 saved_c_extra = c_extra;
5970 saved_char_attr = char_attr;
5971 n_extra = 0;
5972 lcs_prec_todo = lcs_prec;
5973#ifdef FEAT_LINEBREAK
5974# ifdef FEAT_DIFF
5975 if (filler_todo <= 0)
5976# endif
5977 need_showbreak = TRUE;
5978#endif
5979#ifdef FEAT_DIFF
5980 --filler_todo;
5981 /* When the filler lines are actually below the last line of the
5982 * file, don't draw the line itself, break here. */
5983 if (filler_todo == 0 && wp->w_botfill)
5984 break;
5985#endif
5986 }
5987
5988 } /* for every character in the line */
5989
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005990#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005991 /* After an empty line check first word for capital. */
5992 if (*skipwhite(line) == NUL)
5993 {
5994 capcol_lnum = lnum + 1;
5995 cap_col = 0;
5996 }
5997#endif
5998
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005999 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 return row;
6001}
6002
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006003#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006004static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006005
6006/*
6007 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006008 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006009 */
6010 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006011comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006012{
6013 int i;
6014
6015 for (i = 0; i < Screen_mco; ++i)
6016 {
6017 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6018 return TRUE;
6019 if (ScreenLinesC[i][off_from] == 0)
6020 break;
6021 }
6022 return FALSE;
6023}
6024#endif
6025
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026/*
6027 * Check whether the given character needs redrawing:
6028 * - the (first byte of the) character is different
6029 * - the attributes are different
6030 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006031 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 */
6033 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006034char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
6036 if (cols > 0
6037 && ((ScreenLines[off_from] != ScreenLines[off_to]
6038 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6039
6040#ifdef FEAT_MBYTE
6041 || (enc_dbcs != 0
6042 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6043 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6044 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6045 : (cols > 1 && ScreenLines[off_from + 1]
6046 != ScreenLines[off_to + 1])))
6047 || (enc_utf8
6048 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6049 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006050 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006051 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6052 && ScreenLines[off_from + 1]
6053 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054#endif
6055 ))
6056 return TRUE;
6057 return FALSE;
6058}
6059
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006060#if defined(FEAT_TERMINAL) || defined(PROTO)
6061/*
6062 * Return the index in ScreenLines[] for the current screen line.
6063 */
6064 int
6065screen_get_current_line_off()
6066{
6067 return (int)(current_ScreenLine - ScreenLines);
6068}
6069#endif
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071/*
6072 * Move one "cooked" screen line to the screen, but only the characters that
6073 * have actually changed. Handle insert/delete character.
6074 * "coloff" gives the first column on the screen for this line.
6075 * "endcol" gives the columns where valid characters are.
6076 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6077 * needs to be cleared, negative otherwise.
6078 * "rlflag" is TRUE in a rightleft window:
6079 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6080 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6081 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006082 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006083screen_line(
6084 int row,
6085 int coloff,
6086 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006087 int clear_width,
6088 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089{
6090 unsigned off_from;
6091 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006092#ifdef FEAT_MBYTE
6093 unsigned max_off_from;
6094 unsigned max_off_to;
6095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 int force = FALSE; /* force update rest of the line */
6099 int redraw_this /* bool: does character need redraw? */
6100#ifdef FEAT_GUI
6101 = TRUE /* For GUI when while-loop empty */
6102#endif
6103 ;
6104 int redraw_next; /* redraw_this for next character */
6105#ifdef FEAT_MBYTE
6106 int clear_next = FALSE;
6107 int char_cells; /* 1: normal char */
6108 /* 2: occupies two display cells */
6109# define CHAR_CELLS char_cells
6110#else
6111# define CHAR_CELLS 1
6112#endif
6113
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006114 /* Check for illegal row and col, just in case. */
6115 if (row >= Rows)
6116 row = Rows - 1;
6117 if (endcol > Columns)
6118 endcol = Columns;
6119
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120# ifdef FEAT_CLIPBOARD
6121 clip_may_clear_selection(row, row);
6122# endif
6123
6124 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6125 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006126#ifdef FEAT_MBYTE
6127 max_off_from = off_from + screen_Columns;
6128 max_off_to = LineOffset[row] + screen_Columns;
6129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130
6131#ifdef FEAT_RIGHTLEFT
6132 if (rlflag)
6133 {
6134 /* Clear rest first, because it's left of the text. */
6135 if (clear_width > 0)
6136 {
6137 while (col <= endcol && ScreenLines[off_to] == ' '
6138 && ScreenAttrs[off_to] == 0
6139# ifdef FEAT_MBYTE
6140 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6141# endif
6142 )
6143 {
6144 ++off_to;
6145 ++col;
6146 }
6147 if (col <= endcol)
6148 screen_fill(row, row + 1, col + coloff,
6149 endcol + coloff + 1, ' ', ' ', 0);
6150 }
6151 col = endcol + 1;
6152 off_to = LineOffset[row] + col + coloff;
6153 off_from += col;
6154 endcol = (clear_width > 0 ? clear_width : -clear_width);
6155 }
6156#endif /* FEAT_RIGHTLEFT */
6157
6158 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6159
6160 while (col < endcol)
6161 {
6162#ifdef FEAT_MBYTE
6163 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006164 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 else
6166 char_cells = 1;
6167#endif
6168
6169 redraw_this = redraw_next;
6170 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6171 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6172
6173#ifdef FEAT_GUI
6174 /* If the next character was bold, then redraw the current character to
6175 * remove any pixels that might have spilt over into us. This only
6176 * happens in the GUI.
6177 */
6178 if (redraw_next && gui.in_use)
6179 {
6180 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006181 if (hl > HL_ALL)
6182 hl = syn_attr2attr(hl);
6183 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 redraw_this = TRUE;
6185 }
6186#endif
6187
6188 if (redraw_this)
6189 {
6190 /*
6191 * Special handling when 'xs' termcap flag set (hpterm):
6192 * Attributes for characters are stored at the position where the
6193 * cursor is when writing the highlighting code. The
6194 * start-highlighting code must be written with the cursor on the
6195 * first highlighted character. The stop-highlighting code must
6196 * be written with the cursor just after the last highlighted
6197 * character.
6198 * Overwriting a character doesn't remove it's highlighting. Need
6199 * to clear the rest of the line, and force redrawing it
6200 * completely.
6201 */
6202 if ( p_wiv
6203 && !force
6204#ifdef FEAT_GUI
6205 && !gui.in_use
6206#endif
6207 && ScreenAttrs[off_to] != 0
6208 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6209 {
6210 /*
6211 * Need to remove highlighting attributes here.
6212 */
6213 windgoto(row, col + coloff);
6214 out_str(T_CE); /* clear rest of this screen line */
6215 screen_start(); /* don't know where cursor is now */
6216 force = TRUE; /* force redraw of rest of the line */
6217 redraw_next = TRUE; /* or else next char would miss out */
6218
6219 /*
6220 * If the previous character was highlighted, need to stop
6221 * highlighting at this character.
6222 */
6223 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6224 {
6225 screen_attr = ScreenAttrs[off_to - 1];
6226 term_windgoto(row, col + coloff);
6227 screen_stop_highlight();
6228 }
6229 else
6230 screen_attr = 0; /* highlighting has stopped */
6231 }
6232#ifdef FEAT_MBYTE
6233 if (enc_dbcs != 0)
6234 {
6235 /* Check if overwriting a double-byte with a single-byte or
6236 * the other way around requires another character to be
6237 * redrawn. For UTF-8 this isn't needed, because comparing
6238 * ScreenLinesUC[] is sufficient. */
6239 if (char_cells == 1
6240 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006241 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 {
6243 /* Writing a single-cell character over a double-cell
6244 * character: need to redraw the next cell. */
6245 ScreenLines[off_to + 1] = 0;
6246 redraw_next = TRUE;
6247 }
6248 else if (char_cells == 2
6249 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006250 && (*mb_off2cells)(off_to, max_off_to) == 1
6251 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {
6253 /* Writing the second half of a double-cell character over
6254 * a double-cell character: need to redraw the second
6255 * cell. */
6256 ScreenLines[off_to + 2] = 0;
6257 redraw_next = TRUE;
6258 }
6259
6260 if (enc_dbcs == DBCS_JPNU)
6261 ScreenLines2[off_to] = ScreenLines2[off_from];
6262 }
6263 /* When writing a single-width character over a double-width
6264 * character and at the end of the redrawn text, need to clear out
6265 * the right halve of the old character.
6266 * Also required when writing the right halve of a double-width
6267 * char over the left halve of an existing one. */
6268 if (has_mbyte && col + char_cells == endcol
6269 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006270 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006272 && (*mb_off2cells)(off_to, max_off_to) == 1
6273 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 clear_next = TRUE;
6275#endif
6276
6277 ScreenLines[off_to] = ScreenLines[off_from];
6278#ifdef FEAT_MBYTE
6279 if (enc_utf8)
6280 {
6281 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6282 if (ScreenLinesUC[off_from] != 0)
6283 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006284 int i;
6285
6286 for (i = 0; i < Screen_mco; ++i)
6287 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 }
6289 }
6290 if (char_cells == 2)
6291 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6292#endif
6293
6294#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006295 /* The bold trick makes a single column of pixels appear in the
6296 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 * character should be redrawn too. This happens for our own GUI
6298 * and for some xterms. */
6299 if (
6300# ifdef FEAT_GUI
6301 gui.in_use
6302# endif
6303# if defined(FEAT_GUI) && defined(UNIX)
6304 ||
6305# endif
6306# ifdef UNIX
6307 term_is_xterm
6308# endif
6309 )
6310 {
6311 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006312 if (hl > HL_ALL)
6313 hl = syn_attr2attr(hl);
6314 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 redraw_next = TRUE;
6316 }
6317#endif
6318 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6319#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006320 /* For simplicity set the attributes of second half of a
6321 * double-wide character equal to the first half. */
6322 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006324
6325 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 else
6328#endif
6329 screen_char(off_to, row, col + coloff);
6330 }
6331 else if ( p_wiv
6332#ifdef FEAT_GUI
6333 && !gui.in_use
6334#endif
6335 && col + coloff > 0)
6336 {
6337 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6338 {
6339 /*
6340 * Don't output stop-highlight when moving the cursor, it will
6341 * stop the highlighting when it should continue.
6342 */
6343 screen_attr = 0;
6344 }
6345 else if (screen_attr != 0)
6346 screen_stop_highlight();
6347 }
6348
6349 off_to += CHAR_CELLS;
6350 off_from += CHAR_CELLS;
6351 col += CHAR_CELLS;
6352 }
6353
6354#ifdef FEAT_MBYTE
6355 if (clear_next)
6356 {
6357 /* Clear the second half of a double-wide character of which the left
6358 * half was overwritten with a single-wide character. */
6359 ScreenLines[off_to] = ' ';
6360 if (enc_utf8)
6361 ScreenLinesUC[off_to] = 0;
6362 screen_char(off_to, row, col + coloff);
6363 }
6364#endif
6365
6366 if (clear_width > 0
6367#ifdef FEAT_RIGHTLEFT
6368 && !rlflag
6369#endif
6370 )
6371 {
6372#ifdef FEAT_GUI
6373 int startCol = col;
6374#endif
6375
6376 /* blank out the rest of the line */
6377 while (col < clear_width && ScreenLines[off_to] == ' '
6378 && ScreenAttrs[off_to] == 0
6379#ifdef FEAT_MBYTE
6380 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6381#endif
6382 )
6383 {
6384 ++off_to;
6385 ++col;
6386 }
6387 if (col < clear_width)
6388 {
6389#ifdef FEAT_GUI
6390 /*
6391 * In the GUI, clearing the rest of the line may leave pixels
6392 * behind if the first character cleared was bold. Some bold
6393 * fonts spill over the left. In this case we redraw the previous
6394 * character too. If we didn't skip any blanks above, then we
6395 * only redraw if the character wasn't already redrawn anyway.
6396 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006397 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 {
6399 hl = ScreenAttrs[off_to];
6400 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006401 {
6402 int prev_cells = 1;
6403# ifdef FEAT_MBYTE
6404 if (enc_utf8)
6405 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6406 * that its width is 2. */
6407 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6408 else if (enc_dbcs != 0)
6409 {
6410 /* find previous character by counting from first
6411 * column and get its width. */
6412 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006413 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006414
6415 while (off < off_to)
6416 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006417 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006418 off += prev_cells;
6419 }
6420 }
6421
6422 if (enc_dbcs != 0 && prev_cells > 1)
6423 screen_char_2(off_to - prev_cells, row,
6424 col + coloff - prev_cells);
6425 else
6426# endif
6427 screen_char(off_to - prev_cells, row,
6428 col + coloff - prev_cells);
6429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 }
6431#endif
6432 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6433 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 off_to += clear_width - col;
6435 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 }
6437 }
6438
6439 if (clear_width > 0)
6440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 /* For a window that's left of another, draw the separator char. */
6442 if (col + coloff < Columns)
6443 {
6444 int c;
6445
6446 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006447 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006448#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006449 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6450 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 || ScreenAttrs[off_to] != hl)
6453 {
6454 ScreenLines[off_to] = c;
6455 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006456#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 if (enc_utf8)
6458 {
6459 if (c >= 0x80)
6460 {
6461 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006462 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 }
6464 else
6465 ScreenLinesUC[off_to] = 0;
6466 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 screen_char(off_to, row, col + coloff);
6469 }
6470 }
6471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 LineWraps[row] = FALSE;
6473 }
6474}
6475
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006476#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006478 * Mirror text "str" for right-left displaying.
6479 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006481 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006482rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483{
6484 char_u *p1, *p2;
6485 int t;
6486
6487 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6488 {
6489 t = *p1;
6490 *p1 = *p2;
6491 *p2 = t;
6492 }
6493}
6494#endif
6495
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496/*
6497 * mark all status lines for redraw; used after first :cd
6498 */
6499 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006500status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501{
6502 win_T *wp;
6503
Bram Moolenaar29323592016-07-24 22:04:11 +02006504 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 if (wp->w_status_height)
6506 {
6507 wp->w_redr_status = TRUE;
6508 redraw_later(VALID);
6509 }
6510}
6511
6512/*
6513 * mark all status lines of the current buffer for redraw
6514 */
6515 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006516status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
6518 win_T *wp;
6519
Bram Moolenaar29323592016-07-24 22:04:11 +02006520 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6522 {
6523 wp->w_redr_status = TRUE;
6524 redraw_later(VALID);
6525 }
6526}
6527
6528/*
6529 * Redraw all status lines that need to be redrawn.
6530 */
6531 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006532redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533{
6534 win_T *wp;
6535
Bram Moolenaar29323592016-07-24 22:04:11 +02006536 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 if (wp->w_redr_status)
6538 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006539 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006540 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542
Bram Moolenaar4033c552017-09-16 20:54:51 +02006543#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544/*
6545 * Redraw all status lines at the bottom of frame "frp".
6546 */
6547 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006548win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
6550 if (frp->fr_layout == FR_LEAF)
6551 frp->fr_win->w_redr_status = TRUE;
6552 else if (frp->fr_layout == FR_ROW)
6553 {
6554 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6555 win_redraw_last_status(frp);
6556 }
6557 else /* frp->fr_layout == FR_COL */
6558 {
6559 frp = frp->fr_child;
6560 while (frp->fr_next != NULL)
6561 frp = frp->fr_next;
6562 win_redraw_last_status(frp);
6563 }
6564}
6565#endif
6566
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567/*
6568 * Draw the verticap separator right of window "wp" starting with line "row".
6569 */
6570 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006571draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
6573 int hl;
6574 int c;
6575
6576 if (wp->w_vsep_width)
6577 {
6578 /* draw the vertical separator right of this window */
6579 c = fillchar_vsep(&hl);
6580 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6581 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6582 c, ' ', hl);
6583 }
6584}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585
6586#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006587static int status_match_len(expand_T *xp, char_u *s);
6588static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589
6590/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006591 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 */
6593 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006594status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
6596 int len = 0;
6597
6598#ifdef FEAT_MENU
6599 int emenu = (xp->xp_context == EXPAND_MENUS
6600 || xp->xp_context == EXPAND_MENUNAMES);
6601
6602 /* Check for menu separators - replace with '|'. */
6603 if (emenu && menu_is_separator(s))
6604 return 1;
6605#endif
6606
6607 while (*s != NUL)
6608 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006609 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006610 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006611 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 }
6613
6614 return len;
6615}
6616
6617/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006618 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006619 * These are backslashes used for escaping. Do show backslashes in help tags.
6620 */
6621 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006622skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006623{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006624 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006625#ifdef FEAT_MENU
6626 || ((xp->xp_context == EXPAND_MENUS
6627 || xp->xp_context == EXPAND_MENUNAMES)
6628 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6629#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006630 )
6631 {
6632#ifndef BACKSLASH_IN_FILENAME
6633 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6634 return 2;
6635#endif
6636 return 1;
6637 }
6638 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006639}
6640
6641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 * Show wildchar matches in the status line.
6643 * Show at least the "match" item.
6644 * We start at item 'first_match' in the list and show all matches that fit.
6645 *
6646 * If inversion is possible we use it. Else '=' characters are used.
6647 */
6648 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006649win_redr_status_matches(
6650 expand_T *xp,
6651 int num_matches,
6652 char_u **matches, /* list of matches */
6653 int match,
6654 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
6656#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6657 int row;
6658 char_u *buf;
6659 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006660 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 int fillchar;
6662 int attr;
6663 int i;
6664 int highlight = TRUE;
6665 char_u *selstart = NULL;
6666 int selstart_col = 0;
6667 char_u *selend = NULL;
6668 static int first_match = 0;
6669 int add_left = FALSE;
6670 char_u *s;
6671#ifdef FEAT_MENU
6672 int emenu;
6673#endif
6674#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6675 int l;
6676#endif
6677
6678 if (matches == NULL) /* interrupted completion? */
6679 return;
6680
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006681#ifdef FEAT_MBYTE
6682 if (has_mbyte)
6683 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6684 else
6685#endif
6686 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 if (buf == NULL)
6688 return;
6689
6690 if (match == -1) /* don't show match but original text */
6691 {
6692 match = 0;
6693 highlight = FALSE;
6694 }
6695 /* count 1 for the ending ">" */
6696 clen = status_match_len(xp, L_MATCH(match)) + 3;
6697 if (match == 0)
6698 first_match = 0;
6699 else if (match < first_match)
6700 {
6701 /* jumping left, as far as we can go */
6702 first_match = match;
6703 add_left = TRUE;
6704 }
6705 else
6706 {
6707 /* check if match fits on the screen */
6708 for (i = first_match; i < match; ++i)
6709 clen += status_match_len(xp, L_MATCH(i)) + 2;
6710 if (first_match > 0)
6711 clen += 2;
6712 /* jumping right, put match at the left */
6713 if ((long)clen > Columns)
6714 {
6715 first_match = match;
6716 /* if showing the last match, we can add some on the left */
6717 clen = 2;
6718 for (i = match; i < num_matches; ++i)
6719 {
6720 clen += status_match_len(xp, L_MATCH(i)) + 2;
6721 if ((long)clen >= Columns)
6722 break;
6723 }
6724 if (i == num_matches)
6725 add_left = TRUE;
6726 }
6727 }
6728 if (add_left)
6729 while (first_match > 0)
6730 {
6731 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6732 if ((long)clen >= Columns)
6733 break;
6734 --first_match;
6735 }
6736
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006737 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738
6739 if (first_match == 0)
6740 {
6741 *buf = NUL;
6742 len = 0;
6743 }
6744 else
6745 {
6746 STRCPY(buf, "< ");
6747 len = 2;
6748 }
6749 clen = len;
6750
6751 i = first_match;
6752 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6753 {
6754 if (i == match)
6755 {
6756 selstart = buf + len;
6757 selstart_col = clen;
6758 }
6759
6760 s = L_MATCH(i);
6761 /* Check for menu separators - replace with '|' */
6762#ifdef FEAT_MENU
6763 emenu = (xp->xp_context == EXPAND_MENUS
6764 || xp->xp_context == EXPAND_MENUNAMES);
6765 if (emenu && menu_is_separator(s))
6766 {
6767 STRCPY(buf + len, transchar('|'));
6768 l = (int)STRLEN(buf + len);
6769 len += l;
6770 clen += l;
6771 }
6772 else
6773#endif
6774 for ( ; *s != NUL; ++s)
6775 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006776 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 clen += ptr2cells(s);
6778#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006779 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {
6781 STRNCPY(buf + len, s, l);
6782 s += l - 1;
6783 len += l;
6784 }
6785 else
6786#endif
6787 {
6788 STRCPY(buf + len, transchar_byte(*s));
6789 len += (int)STRLEN(buf + len);
6790 }
6791 }
6792 if (i == match)
6793 selend = buf + len;
6794
6795 *(buf + len++) = ' ';
6796 *(buf + len++) = ' ';
6797 clen += 2;
6798 if (++i == num_matches)
6799 break;
6800 }
6801
6802 if (i != num_matches)
6803 {
6804 *(buf + len++) = '>';
6805 ++clen;
6806 }
6807
6808 buf[len] = NUL;
6809
6810 row = cmdline_row - 1;
6811 if (row >= 0)
6812 {
6813 if (wild_menu_showing == 0)
6814 {
6815 if (msg_scrolled > 0)
6816 {
6817 /* Put the wildmenu just above the command line. If there is
6818 * no room, scroll the screen one line up. */
6819 if (cmdline_row == Rows - 1)
6820 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006821 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 ++msg_scrolled;
6823 }
6824 else
6825 {
6826 ++cmdline_row;
6827 ++row;
6828 }
6829 wild_menu_showing = WM_SCROLLED;
6830 }
6831 else
6832 {
6833 /* Create status line if needed by setting 'laststatus' to 2.
6834 * Set 'winminheight' to zero to avoid that the window is
6835 * resized. */
6836 if (lastwin->w_status_height == 0)
6837 {
6838 save_p_ls = p_ls;
6839 save_p_wmh = p_wmh;
6840 p_ls = 2;
6841 p_wmh = 0;
6842 last_status(FALSE);
6843 }
6844 wild_menu_showing = WM_SHOWN;
6845 }
6846 }
6847
6848 screen_puts(buf, row, 0, attr);
6849 if (selstart != NULL && highlight)
6850 {
6851 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006852 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 }
6854
6855 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6856 }
6857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 vim_free(buf);
6860}
6861#endif
6862
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863/*
6864 * Redraw the status line of window wp.
6865 *
6866 * If inversion is possible we use it. Else '=' characters are used.
6867 */
6868 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006869win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870{
6871 int row;
6872 char_u *p;
6873 int len;
6874 int fillchar;
6875 int attr;
6876 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006877 static int busy = FALSE;
6878
6879 /* It's possible to get here recursively when 'statusline' (indirectly)
6880 * invokes ":redrawstatus". Simply ignore the call then. */
6881 if (busy)
6882 return;
6883 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884
6885 wp->w_redr_status = FALSE;
6886 if (wp->w_status_height == 0)
6887 {
6888 /* no status line, can only be last window */
6889 redraw_cmdline = TRUE;
6890 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006891 else if (!redrawing()
6892#ifdef FEAT_INS_EXPAND
6893 /* don't update status line when popup menu is visible and may be
6894 * drawn over it */
6895 || pum_visible()
6896#endif
6897 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 {
6899 /* Don't redraw right now, do it later. */
6900 wp->w_redr_status = TRUE;
6901 }
6902#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006903 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {
6905 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006906 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 }
6908#endif
6909 else
6910 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006911 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006913 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 p = NameBuff;
6915 len = (int)STRLEN(p);
6916
Bram Moolenaard85f2712017-07-28 21:51:57 +02006917 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918#ifdef FEAT_QUICKFIX
6919 || wp->w_p_pvw
6920#endif
6921 || bufIsChanged(wp->w_buffer)
6922 || wp->w_buffer->b_p_ro)
6923 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006924 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006926 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 len += (int)STRLEN(p + len);
6928 }
6929#ifdef FEAT_QUICKFIX
6930 if (wp->w_p_pvw)
6931 {
6932 STRCPY(p + len, _("[Preview]"));
6933 len += (int)STRLEN(p + len);
6934 }
6935#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006936 if (bufIsChanged(wp->w_buffer)
6937#ifdef FEAT_TERMINAL
6938 && !bt_terminal(wp->w_buffer)
6939#endif
6940 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {
6942 STRCPY(p + len, "[+]");
6943 len += 3;
6944 }
6945 if (wp->w_buffer->b_p_ro)
6946 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006947 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006948 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 }
6950
Bram Moolenaar02631462017-09-22 15:20:32 +02006951 this_ru_col = ru_col - (Columns - wp->w_width);
6952 if (this_ru_col < (wp->w_width + 1) / 2)
6953 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 if (this_ru_col <= 1)
6955 {
6956 p = (char_u *)"<"; /* No room for file name! */
6957 len = 1;
6958 }
6959 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960#ifdef FEAT_MBYTE
6961 if (has_mbyte)
6962 {
6963 int clen = 0, i;
6964
6965 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006966 clen = mb_string2cells(p, -1);
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 /* Find first character that will fit.
6969 * Going from start to end is much faster for DBCS. */
6970 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006971 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 clen -= (*mb_ptr2cells)(p + i);
6973 len = clen;
6974 if (i > 0)
6975 {
6976 p = p + i - 1;
6977 *p = '<';
6978 ++len;
6979 }
6980
6981 }
6982 else
6983#endif
6984 if (len > this_ru_col - 1)
6985 {
6986 p += len - (this_ru_col - 1);
6987 *p = '<';
6988 len = this_ru_col - 1;
6989 }
6990
6991 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006992 screen_puts(p, row, wp->w_wincol, attr);
6993 screen_fill(row, row + 1, len + wp->w_wincol,
6994 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006996 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6998 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006999 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
7001#ifdef FEAT_CMDL_INFO
7002 win_redr_ruler(wp, TRUE);
7003#endif
7004 }
7005
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 /*
7007 * May need to draw the character below the vertical separator.
7008 */
7009 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7010 {
7011 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007012 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 else
7014 fillchar = fillchar_vsep(&attr);
7015 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7016 attr);
7017 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007018 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019}
7020
Bram Moolenaar238a5642006-02-21 22:12:05 +00007021#ifdef FEAT_STL_OPT
7022/*
7023 * Redraw the status line according to 'statusline' and take care of any
7024 * errors encountered.
7025 */
7026 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007027redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007028{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007029 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007030 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007031
7032 /* When called recursively return. This can happen when the statusline
7033 * contains an expression that triggers a redraw. */
7034 if (entered)
7035 return;
7036 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007037
Bram Moolenaara742e082016-04-05 21:10:38 +02007038 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007039 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007040 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007041 {
7042 /* When there is an error disable the statusline, otherwise the
7043 * display is messed up with errors and a redraw triggers the problem
7044 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007045 set_string_option_direct((char_u *)"statusline", -1,
7046 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007047 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007048 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007049 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007050 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007051}
7052#endif
7053
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054/*
7055 * Return TRUE if the status line of window "wp" is connected to the status
7056 * line of the window right of it. If not, then it's a vertical separator.
7057 * Only call if (wp->w_vsep_width != 0).
7058 */
7059 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007060stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061{
7062 frame_T *fr;
7063
7064 fr = wp->w_frame;
7065 while (fr->fr_parent != NULL)
7066 {
7067 if (fr->fr_parent->fr_layout == FR_COL)
7068 {
7069 if (fr->fr_next != NULL)
7070 break;
7071 }
7072 else
7073 {
7074 if (fr->fr_next != NULL)
7075 return TRUE;
7076 }
7077 fr = fr->fr_parent;
7078 }
7079 return FALSE;
7080}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083/*
7084 * Get the value to show for the language mappings, active 'keymap'.
7085 */
7086 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007087get_keymap_str(
7088 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007089 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007090 char_u *buf, /* buffer for the result */
7091 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092{
7093 char_u *p;
7094
7095 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7096 return FALSE;
7097
7098 {
7099#ifdef FEAT_EVAL
7100 buf_T *old_curbuf = curbuf;
7101 win_T *old_curwin = curwin;
7102 char_u *s;
7103
7104 curbuf = wp->w_buffer;
7105 curwin = wp;
7106 STRCPY(buf, "b:keymap_name"); /* must be writable */
7107 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007108 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 --emsg_skip;
7110 curbuf = old_curbuf;
7111 curwin = old_curwin;
7112 if (p == NULL || *p == NUL)
7113#endif
7114 {
7115#ifdef FEAT_KEYMAP
7116 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7117 p = wp->w_buffer->b_p_keymap;
7118 else
7119#endif
7120 p = (char_u *)"lang";
7121 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007122 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 buf[0] = NUL;
7124#ifdef FEAT_EVAL
7125 vim_free(s);
7126#endif
7127 }
7128 return buf[0] != NUL;
7129}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130
7131#if defined(FEAT_STL_OPT) || defined(PROTO)
7132/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007133 * Redraw the status line or ruler of window "wp".
7134 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 */
7136 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007137win_redr_custom(
7138 win_T *wp,
7139 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007141 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 int attr;
7143 int curattr;
7144 int row;
7145 int col = 0;
7146 int maxwidth;
7147 int width;
7148 int n;
7149 int len;
7150 int fillchar;
7151 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007152 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007154 struct stl_hlrec hltab[STL_MAX_ITEM];
7155 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007156 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007157 win_T *ewp;
7158 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
Bram Moolenaar1d633412013-12-11 15:52:01 +01007160 /* There is a tiny chance that this gets called recursively: When
7161 * redrawing a status line triggers redrawing the ruler or tabline.
7162 * Avoid trouble by not allowing recursion. */
7163 if (entered)
7164 return;
7165 entered = TRUE;
7166
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007168 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007170 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007171 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007172 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007173 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007174 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007175 maxwidth = Columns;
7176# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007177 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007180 else
7181 {
7182 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007183 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007184 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007185
7186 if (draw_ruler)
7187 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007188 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007189 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007190 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007191 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007192 if (*++stl == '-')
7193 stl++;
7194 if (atoi((char *)stl))
7195 while (VIM_ISDIGIT(*stl))
7196 stl++;
7197 if (*stl++ != '(')
7198 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007199 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007200 col = ru_col - (Columns - wp->w_width);
7201 if (col < (wp->w_width + 1) / 2)
7202 col = (wp->w_width + 1) / 2;
7203 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007204 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007205 {
7206 row = Rows - 1;
7207 --maxwidth; /* writing in last column may cause scrolling */
7208 fillchar = ' ';
7209 attr = 0;
7210 }
7211
7212# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007213 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007214# endif
7215 }
7216 else
7217 {
7218 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007219 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007220 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007221 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007222# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007223 use_sandbox = was_set_insecurely((char_u *)"statusline",
7224 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007225# endif
7226 }
7227
Bram Moolenaar53f81742017-09-22 14:35:51 +02007228 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007229 }
7230
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007232 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
Bram Moolenaar61452852011-02-01 18:01:11 +01007234 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7235 * the cursor away and back. */
7236 ewp = wp == NULL ? curwin : wp;
7237 p_crb_save = ewp->w_p_crb;
7238 ewp->w_p_crb = FALSE;
7239
Bram Moolenaar362f3562009-11-03 16:20:34 +00007240 /* Make a copy, because the statusline may include a function call that
7241 * might change the option value and free the memory. */
7242 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007243 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007244 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007245 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007246 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007247 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007249 /* Make all characters printable. */
7250 p = transstr(buf);
7251 if (p != NULL)
7252 {
7253 vim_strncpy(buf, p, sizeof(buf) - 1);
7254 vim_free(p);
7255 }
7256
7257 /* fill up with "fillchar" */
7258 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007259 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 {
7261#ifdef FEAT_MBYTE
7262 len += (*mb_char2bytes)(fillchar, buf + len);
7263#else
7264 buf[len++] = fillchar;
7265#endif
7266 ++width;
7267 }
7268 buf[len] = NUL;
7269
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007270 /*
7271 * Draw each snippet with the specified highlighting.
7272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 curattr = attr;
7274 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007275 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007277 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 screen_puts_len(p, len, row, col, curattr);
7279 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007280 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007282 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007284 else if (hltab[n].userhl < 0)
7285 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007286#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007287 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7288 && wp->w_status_height != 0)
7289 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007290 else if (wp != NULL && bt_terminal(wp->w_buffer)
7291 && wp->w_status_height != 0)
7292 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007293#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007294 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007295 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007297 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 }
7299 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007300
7301 if (wp == NULL)
7302 {
7303 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7304 col = 0;
7305 len = 0;
7306 p = buf;
7307 fillchar = 0;
7308 for (n = 0; tabtab[n].start != NULL; n++)
7309 {
7310 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7311 while (col < len)
7312 TabPageIdxs[col++] = fillchar;
7313 p = tabtab[n].start;
7314 fillchar = tabtab[n].userhl;
7315 }
7316 while (col < Columns)
7317 TabPageIdxs[col++] = fillchar;
7318 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007319
7320theend:
7321 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322}
7323
7324#endif /* FEAT_STL_OPT */
7325
7326/*
7327 * Output a single character directly to the screen and update ScreenLines.
7328 */
7329 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007330screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 char_u buf[MB_MAXBYTES + 1];
7333
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007334#ifdef FEAT_MBYTE
7335 if (has_mbyte)
7336 buf[(*mb_char2bytes)(c, buf)] = NUL;
7337 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007339 {
7340 buf[0] = c;
7341 buf[1] = NUL;
7342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 screen_puts(buf, row, col, attr);
7344}
7345
7346/*
7347 * Get a single character directly from ScreenLines into "bytes[]".
7348 * Also return its attribute in *attrp;
7349 */
7350 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007351screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352{
7353 unsigned off;
7354
7355 /* safety check */
7356 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7357 {
7358 off = LineOffset[row] + col;
7359 *attrp = ScreenAttrs[off];
7360 bytes[0] = ScreenLines[off];
7361 bytes[1] = NUL;
7362
7363#ifdef FEAT_MBYTE
7364 if (enc_utf8 && ScreenLinesUC[off] != 0)
7365 bytes[utfc_char2bytes(off, bytes)] = NUL;
7366 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7367 {
7368 bytes[0] = ScreenLines[off];
7369 bytes[1] = ScreenLines2[off];
7370 bytes[2] = NUL;
7371 }
7372 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7373 {
7374 bytes[1] = ScreenLines[off + 1];
7375 bytes[2] = NUL;
7376 }
7377#endif
7378 }
7379}
7380
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007381#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007382static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007383
7384/*
7385 * Return TRUE if composing characters for screen posn "off" differs from
7386 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007387 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007388 */
7389 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007390screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007391{
7392 int i;
7393
7394 for (i = 0; i < Screen_mco; ++i)
7395 {
7396 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7397 return TRUE;
7398 if (u8cc[i] == 0)
7399 break;
7400 }
7401 return FALSE;
7402}
7403#endif
7404
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405/*
7406 * Put string '*text' on the screen at position 'row' and 'col', with
7407 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7408 * Note: only outputs within one row, message is truncated at screen boundary!
7409 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7410 */
7411 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007412screen_puts(
7413 char_u *text,
7414 int row,
7415 int col,
7416 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417{
7418 screen_puts_len(text, -1, row, col, attr);
7419}
7420
7421/*
7422 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7423 * a NUL.
7424 */
7425 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007426screen_puts_len(
7427 char_u *text,
7428 int textlen,
7429 int row,
7430 int col,
7431 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432{
7433 unsigned off;
7434 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007435 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 int c;
7437#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007438 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 int mbyte_blen = 1;
7440 int mbyte_cells = 1;
7441 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007442 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 int clear_next_cell = FALSE;
7444# ifdef FEAT_ARABIC
7445 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007446 int pc, nc, nc1;
7447 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448# endif
7449#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007450#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7451 int force_redraw_this;
7452 int force_redraw_next = FALSE;
7453#endif
7454 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455
7456 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7457 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007458 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459
Bram Moolenaarc236c162008-07-13 17:41:49 +00007460#ifdef FEAT_MBYTE
7461 /* When drawing over the right halve of a double-wide char clear out the
7462 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007463 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007464# ifdef FEAT_GUI
7465 && !gui.in_use
7466# endif
7467 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007468 {
7469 ScreenLines[off - 1] = ' ';
7470 ScreenAttrs[off - 1] = 0;
7471 if (enc_utf8)
7472 {
7473 ScreenLinesUC[off - 1] = 0;
7474 ScreenLinesC[0][off - 1] = 0;
7475 }
7476 /* redraw the previous cell, make it empty */
7477 screen_char(off - 1, row, col - 1);
7478 /* force the cell at "col" to be redrawn */
7479 force_redraw_next = TRUE;
7480 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007481#endif
7482
Bram Moolenaar367329b2007-08-30 11:53:22 +00007483#ifdef FEAT_MBYTE
7484 max_off = LineOffset[row] + screen_Columns;
7485#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007486 while (col < screen_Columns
7487 && (len < 0 || (int)(ptr - text) < len)
7488 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 {
7490 c = *ptr;
7491#ifdef FEAT_MBYTE
7492 /* check if this is the first byte of a multibyte */
7493 if (has_mbyte)
7494 {
7495 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007496 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007498 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7500 mbyte_cells = 1;
7501 else if (enc_dbcs != 0)
7502 mbyte_cells = mbyte_blen;
7503 else /* enc_utf8 */
7504 {
7505 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007506 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 (int)((text + len) - ptr));
7508 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007509 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007511# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 /* Non-BMP character: display as ? or fullwidth ?. */
7513 if (u8c >= 0x10000)
7514 {
7515 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7516 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007517 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007519# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520# ifdef FEAT_ARABIC
7521 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7522 {
7523 /* Do Arabic shaping. */
7524 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7525 {
7526 /* Past end of string to be displayed. */
7527 nc = NUL;
7528 nc1 = NUL;
7529 }
7530 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007531 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007532 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7533 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007534 nc1 = pcc[0];
7535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 pc = prev_c;
7537 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007538 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 }
7540 else
7541 prev_c = u8c;
7542# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007543 if (col + mbyte_cells > screen_Columns)
7544 {
7545 /* Only 1 cell left, but character requires 2 cells:
7546 * display a '>' in the last column to avoid wrapping. */
7547 c = '>';
7548 mbyte_cells = 1;
7549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 }
7551 }
7552#endif
7553
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007554#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7555 force_redraw_this = force_redraw_next;
7556 force_redraw_next = FALSE;
7557#endif
7558
7559 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560#ifdef FEAT_MBYTE
7561 || (mbyte_cells == 2
7562 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7563 || (enc_dbcs == DBCS_JPNU
7564 && c == 0x8e
7565 && ScreenLines2[off] != ptr[1])
7566 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007567 && (ScreenLinesUC[off] !=
7568 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7569 || (ScreenLinesUC[off] != 0
7570 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571#endif
7572 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007573 || exmode_active;
7574
7575 if (need_redraw
7576#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7577 || force_redraw_this
7578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 )
7580 {
7581#if defined(FEAT_GUI) || defined(UNIX)
7582 /* The bold trick makes a single row of pixels appear in the next
7583 * character. When a bold character is removed, the next
7584 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007585 * and for some xterms. */
7586 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587# ifdef FEAT_GUI
7588 gui.in_use
7589# endif
7590# if defined(FEAT_GUI) && defined(UNIX)
7591 ||
7592# endif
7593# ifdef UNIX
7594 term_is_xterm
7595# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007596 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007598 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007600 if (n > HL_ALL)
7601 n = syn_attr2attr(n);
7602 if (n & HL_BOLD)
7603 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 }
7605#endif
7606#ifdef FEAT_MBYTE
7607 /* When at the end of the text and overwriting a two-cell
7608 * character with a one-cell character, need to clear the next
7609 * cell. Also when overwriting the left halve of a two-cell char
7610 * with the right halve of a two-cell char. Do this only once
7611 * (mb_off2cells() may return 2 on the right halve). */
7612 if (clear_next_cell)
7613 clear_next_cell = FALSE;
7614 else if (has_mbyte
7615 && (len < 0 ? ptr[mbyte_blen] == NUL
7616 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007617 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007619 && (*mb_off2cells)(off, max_off) == 1
7620 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 clear_next_cell = TRUE;
7622
7623 /* Make sure we never leave a second byte of a double-byte behind,
7624 * it confuses mb_off2cells(). */
7625 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007626 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007628 && (*mb_off2cells)(off, max_off) == 1
7629 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 ScreenLines[off + mbyte_blen] = 0;
7631#endif
7632 ScreenLines[off] = c;
7633 ScreenAttrs[off] = attr;
7634#ifdef FEAT_MBYTE
7635 if (enc_utf8)
7636 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007637 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 ScreenLinesUC[off] = 0;
7639 else
7640 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007641 int i;
7642
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007644 for (i = 0; i < Screen_mco; ++i)
7645 {
7646 ScreenLinesC[i][off] = u8cc[i];
7647 if (u8cc[i] == 0)
7648 break;
7649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 }
7651 if (mbyte_cells == 2)
7652 {
7653 ScreenLines[off + 1] = 0;
7654 ScreenAttrs[off + 1] = attr;
7655 }
7656 screen_char(off, row, col);
7657 }
7658 else if (mbyte_cells == 2)
7659 {
7660 ScreenLines[off + 1] = ptr[1];
7661 ScreenAttrs[off + 1] = attr;
7662 screen_char_2(off, row, col);
7663 }
7664 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7665 {
7666 ScreenLines2[off] = ptr[1];
7667 screen_char(off, row, col);
7668 }
7669 else
7670#endif
7671 screen_char(off, row, col);
7672 }
7673#ifdef FEAT_MBYTE
7674 if (has_mbyte)
7675 {
7676 off += mbyte_cells;
7677 col += mbyte_cells;
7678 ptr += mbyte_blen;
7679 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007680 {
7681 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007683 len = -1;
7684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 }
7686 else
7687#endif
7688 {
7689 ++off;
7690 ++col;
7691 ++ptr;
7692 }
7693 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007694
7695#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7696 /* If we detected the next character needs to be redrawn, but the text
7697 * doesn't extend up to there, update the character here. */
7698 if (force_redraw_next && col < screen_Columns)
7699 {
7700# ifdef FEAT_MBYTE
7701 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7702 screen_char_2(off, row, col);
7703 else
7704# endif
7705 screen_char(off, row, col);
7706 }
7707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708}
7709
7710#ifdef FEAT_SEARCH_EXTRA
7711/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007712 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 */
7714 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007715start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
7717 if (p_hls && !no_hlsearch)
7718 {
7719 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007720 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007721# ifdef FEAT_RELTIME
7722 /* Set the time limit to 'redrawtime'. */
7723 profile_setlimit(p_rdt, &search_hl.tm);
7724# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 }
7726}
7727
7728/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007729 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 */
7731 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007732end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
7734 if (search_hl.rm.regprog != NULL)
7735 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007736 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 search_hl.rm.regprog = NULL;
7738 }
7739}
7740
7741/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007742 * Init for calling prepare_search_hl().
7743 */
7744 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007745init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007746{
7747 matchitem_T *cur;
7748
7749 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7750 * match */
7751 cur = wp->w_match_head;
7752 while (cur != NULL)
7753 {
7754 cur->hl.rm = cur->match;
7755 if (cur->hlg_id == 0)
7756 cur->hl.attr = 0;
7757 else
7758 cur->hl.attr = syn_id2attr(cur->hlg_id);
7759 cur->hl.buf = wp->w_buffer;
7760 cur->hl.lnum = 0;
7761 cur->hl.first_lnum = 0;
7762# ifdef FEAT_RELTIME
7763 /* Set the time limit to 'redrawtime'. */
7764 profile_setlimit(p_rdt, &(cur->hl.tm));
7765# endif
7766 cur = cur->next;
7767 }
7768 search_hl.buf = wp->w_buffer;
7769 search_hl.lnum = 0;
7770 search_hl.first_lnum = 0;
7771 /* time limit is set at the toplevel, for all windows */
7772}
7773
7774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 * Advance to the match in window "wp" line "lnum" or past it.
7776 */
7777 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007778prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007780 matchitem_T *cur; /* points to the match list */
7781 match_T *shl; /* points to search_hl or a match */
7782 int shl_flag; /* flag to indicate whether search_hl
7783 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007784 int pos_inprogress; /* marks that position match search is
7785 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 int n;
7787
7788 /*
7789 * When using a multi-line pattern, start searching at the top
7790 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007791 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007793 cur = wp->w_match_head;
7794 shl_flag = FALSE;
7795 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007797 if (shl_flag == FALSE)
7798 {
7799 shl = &search_hl;
7800 shl_flag = TRUE;
7801 }
7802 else
7803 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 if (shl->rm.regprog != NULL
7805 && shl->lnum == 0
7806 && re_multiline(shl->rm.regprog))
7807 {
7808 if (shl->first_lnum == 0)
7809 {
7810# ifdef FEAT_FOLDING
7811 for (shl->first_lnum = lnum;
7812 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7813 if (hasFoldingWin(wp, shl->first_lnum - 1,
7814 NULL, NULL, TRUE, NULL))
7815 break;
7816# else
7817 shl->first_lnum = wp->w_topline;
7818# endif
7819 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007820 if (cur != NULL)
7821 cur->pos.cur = 0;
7822 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007824 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7825 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007827 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7828 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007829 pos_inprogress = cur == NULL || cur->pos.cur == 0
7830 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 if (shl->lnum != 0)
7832 {
7833 shl->first_lnum = shl->lnum
7834 + shl->rm.endpos[0].lnum
7835 - shl->rm.startpos[0].lnum;
7836 n = shl->rm.endpos[0].col;
7837 }
7838 else
7839 {
7840 ++shl->first_lnum;
7841 n = 0;
7842 }
7843 }
7844 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007845 if (shl != &search_hl && cur != NULL)
7846 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 }
7848}
7849
7850/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007851 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 * Uses shl->buf.
7853 * Sets shl->lnum and shl->rm contents.
7854 * Note: Assumes a previous match is always before "lnum", unless
7855 * shl->lnum is zero.
7856 * Careful: Any pointers for buffer lines will become invalid.
7857 */
7858 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007859next_search_hl(
7860 win_T *win,
7861 match_T *shl, /* points to search_hl or a match */
7862 linenr_T lnum,
7863 colnr_T mincol, /* minimal column for a match */
7864 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
7866 linenr_T l;
7867 colnr_T matchcol;
7868 long nmatched;
7869
7870 if (shl->lnum != 0)
7871 {
7872 /* Check for three situations:
7873 * 1. If the "lnum" is below a previous match, start a new search.
7874 * 2. If the previous match includes "mincol", use it.
7875 * 3. Continue after the previous match.
7876 */
7877 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7878 if (lnum > l)
7879 shl->lnum = 0;
7880 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7881 return;
7882 }
7883
7884 /*
7885 * Repeat searching for a match until one is found that includes "mincol"
7886 * or none is found in this line.
7887 */
7888 called_emsg = FALSE;
7889 for (;;)
7890 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007891#ifdef FEAT_RELTIME
7892 /* Stop searching after passing the time limit. */
7893 if (profile_passed_limit(&(shl->tm)))
7894 {
7895 shl->lnum = 0; /* no match found in time */
7896 break;
7897 }
7898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 /* Three situations:
7900 * 1. No useful previous match: search from start of line.
7901 * 2. Not Vi compatible or empty match: continue at next character.
7902 * Break the loop if this is beyond the end of the line.
7903 * 3. Vi compatible searching: continue at end of previous match.
7904 */
7905 if (shl->lnum == 0)
7906 matchcol = 0;
7907 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7908 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007909 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007911 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007912
7913 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007914 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007915 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007917 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 shl->lnum = 0;
7919 break;
7920 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007921#ifdef FEAT_MBYTE
7922 if (has_mbyte)
7923 matchcol += mb_ptr2len(ml);
7924 else
7925#endif
7926 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 }
7928 else
7929 matchcol = shl->rm.endpos[0].col;
7930
7931 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007932 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007934 /* Remember whether shl->rm is using a copy of the regprog in
7935 * cur->match. */
7936 int regprog_is_copy = (shl != &search_hl && cur != NULL
7937 && shl == &cur->hl
7938 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007939 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007940
Bram Moolenaarb3414592014-06-17 17:48:32 +02007941 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7942 matchcol,
7943#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007944 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007945#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007946 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007947#endif
7948 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007949 /* Copy the regprog, in case it got freed and recompiled. */
7950 if (regprog_is_copy)
7951 cur->match.regprog = cur->hl.rm.regprog;
7952
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007953 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007954 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007955 /* Error while handling regexp: stop using this regexp. */
7956 if (shl == &search_hl)
7957 {
7958 /* don't free regprog in the match list, it's a copy */
7959 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007960 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961 }
7962 shl->rm.regprog = NULL;
7963 shl->lnum = 0;
7964 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7965 message */
7966 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007967 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007968 }
7969 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007970 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007971 else
7972 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 if (nmatched == 0)
7974 {
7975 shl->lnum = 0; /* no match found */
7976 break;
7977 }
7978 if (shl->rm.startpos[0].lnum > 0
7979 || shl->rm.startpos[0].col >= mincol
7980 || nmatched > 1
7981 || shl->rm.endpos[0].col > mincol)
7982 {
7983 shl->lnum += shl->rm.startpos[0].lnum;
7984 break; /* useful match found */
7985 }
7986 }
7987}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988
Bram Moolenaar85077472016-10-16 14:35:48 +02007989/*
7990 * If there is a match fill "shl" and return one.
7991 * Return zero otherwise.
7992 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007993 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007994next_search_hl_pos(
7995 match_T *shl, /* points to a match */
7996 linenr_T lnum,
7997 posmatch_T *posmatch, /* match positions */
7998 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007999{
8000 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008001 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008002
Bram Moolenaarb3414592014-06-17 17:48:32 +02008003 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8004 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008005 llpos_T *pos = &posmatch->pos[i];
8006
8007 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008008 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008009 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008010 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008011 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008012 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008013 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008014 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008015 /* if this match comes before the one at "found" then swap
8016 * them */
8017 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008018 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008019 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008020
Bram Moolenaar85077472016-10-16 14:35:48 +02008021 *pos = posmatch->pos[found];
8022 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008023 }
8024 }
8025 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008026 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008027 }
8028 }
8029 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008030 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008031 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008032 colnr_T start = posmatch->pos[found].col == 0
8033 ? 0 : posmatch->pos[found].col - 1;
8034 colnr_T end = posmatch->pos[found].col == 0
8035 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008036
Bram Moolenaar85077472016-10-16 14:35:48 +02008037 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008038 shl->rm.startpos[0].lnum = 0;
8039 shl->rm.startpos[0].col = start;
8040 shl->rm.endpos[0].lnum = 0;
8041 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008042 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008043 posmatch->cur = found + 1;
8044 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008045 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008046 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008047}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008048#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008049
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008051screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052{
8053 attrentry_T *aep = NULL;
8054
8055 screen_attr = attr;
8056 if (full_screen
8057#ifdef WIN3264
8058 && termcap_active
8059#endif
8060 )
8061 {
8062#ifdef FEAT_GUI
8063 if (gui.in_use)
8064 {
8065 char buf[20];
8066
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008067 /* The GUI handles this internally. */
8068 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 OUT_STR(buf);
8070 }
8071 else
8072#endif
8073 {
8074 if (attr > HL_ALL) /* special HL attr. */
8075 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008076 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 aep = syn_cterm_attr2entry(attr);
8078 else
8079 aep = syn_term_attr2entry(attr);
8080 if (aep == NULL) /* did ":syntax clear" */
8081 attr = 0;
8082 else
8083 attr = aep->ae_attr;
8084 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008085 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008087 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008088#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008089 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8090 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8091 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008092#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008093 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008094 /* If the Normal FG color has BOLD attribute and the new HL
8095 * has a FG color defined, clear BOLD. */
8096 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008097 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008099 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008100 out_str(T_UCS);
8101 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008102 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8103 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008105 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008107 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008109 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008110 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111
8112 /*
8113 * Output the color or start string after bold etc., in case the
8114 * bold etc. override the color setting.
8115 */
8116 if (aep != NULL)
8117 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008118#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008119 /* When 'termguicolors' is set but fg or bg is unset,
8120 * fall back to the cterm colors. This helps for SpellBad,
8121 * where the GUI uses a red undercurl. */
8122 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008124 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008125 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008126 }
8127 else
8128#endif
8129 if (t_colors > 1)
8130 {
8131 if (aep->ae_u.cterm.fg_color)
8132 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8133 }
8134#ifdef FEAT_TERMGUICOLORS
8135 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8136 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008137 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008138 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 }
8140 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008141#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008142 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008144 if (aep->ae_u.cterm.bg_color)
8145 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8146 }
8147
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008148 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008149 {
8150 if (aep->ae_u.term.start != NULL)
8151 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 }
8153 }
8154 }
8155 }
8156}
8157
8158 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008159screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160{
8161 int do_ME = FALSE; /* output T_ME code */
8162
8163 if (screen_attr != 0
8164#ifdef WIN3264
8165 && termcap_active
8166#endif
8167 )
8168 {
8169#ifdef FEAT_GUI
8170 if (gui.in_use)
8171 {
8172 char buf[20];
8173
8174 /* use internal GUI code */
8175 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8176 OUT_STR(buf);
8177 }
8178 else
8179#endif
8180 {
8181 if (screen_attr > HL_ALL) /* special HL attr. */
8182 {
8183 attrentry_T *aep;
8184
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008185 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {
8187 /*
8188 * Assume that t_me restores the original colors!
8189 */
8190 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008191 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008192#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008193 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8194 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8195 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008196#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008197 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008198#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008199 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8200 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8201 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008202#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008203 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 do_ME = TRUE;
8205 }
8206 else
8207 {
8208 aep = syn_term_attr2entry(screen_attr);
8209 if (aep != NULL && aep->ae_u.term.stop != NULL)
8210 {
8211 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8212 do_ME = TRUE;
8213 else
8214 out_str(aep->ae_u.term.stop);
8215 }
8216 }
8217 if (aep == NULL) /* did ":syntax clear" */
8218 screen_attr = 0;
8219 else
8220 screen_attr = aep->ae_attr;
8221 }
8222
8223 /*
8224 * Often all ending-codes are equal to T_ME. Avoid outputting the
8225 * same sequence several times.
8226 */
8227 if (screen_attr & HL_STANDOUT)
8228 {
8229 if (STRCMP(T_SE, T_ME) == 0)
8230 do_ME = TRUE;
8231 else
8232 out_str(T_SE);
8233 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008234 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008235 {
8236 if (STRCMP(T_UCE, T_ME) == 0)
8237 do_ME = TRUE;
8238 else
8239 out_str(T_UCE);
8240 }
8241 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008242 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {
8244 if (STRCMP(T_UE, T_ME) == 0)
8245 do_ME = TRUE;
8246 else
8247 out_str(T_UE);
8248 }
8249 if (screen_attr & HL_ITALIC)
8250 {
8251 if (STRCMP(T_CZR, T_ME) == 0)
8252 do_ME = TRUE;
8253 else
8254 out_str(T_CZR);
8255 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008256 if (screen_attr & HL_STRIKETHROUGH)
8257 {
8258 if (STRCMP(T_STE, T_ME) == 0)
8259 do_ME = TRUE;
8260 else
8261 out_str(T_STE);
8262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8264 out_str(T_ME);
8265
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008266#ifdef FEAT_TERMGUICOLORS
8267 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008269 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008270 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008271 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008272 term_bg_rgb_color(cterm_normal_bg_gui_color);
8273 }
8274 else
8275#endif
8276 {
8277 if (t_colors > 1)
8278 {
8279 /* set Normal cterm colors */
8280 if (cterm_normal_fg_color != 0)
8281 term_fg_color(cterm_normal_fg_color - 1);
8282 if (cterm_normal_bg_color != 0)
8283 term_bg_color(cterm_normal_bg_color - 1);
8284 if (cterm_normal_fg_bold)
8285 out_str(T_MD);
8286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 }
8288 }
8289 }
8290 screen_attr = 0;
8291}
8292
8293/*
8294 * Reset the colors for a cterm. Used when leaving Vim.
8295 * The machine specific code may override this again.
8296 */
8297 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008298reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008300 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {
8302 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008303#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008304 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8305 || cterm_normal_bg_gui_color != INVALCOLOR)
8306 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008307#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {
8311 out_str(T_OP);
8312 screen_attr = -1;
8313 }
8314 if (cterm_normal_fg_bold)
8315 {
8316 out_str(T_ME);
8317 screen_attr = -1;
8318 }
8319 }
8320}
8321
8322/*
8323 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8324 * using the attributes from ScreenAttrs["off"].
8325 */
8326 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008327screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328{
8329 int attr;
8330
8331 /* Check for illegal values, just in case (could happen just after
8332 * resizing). */
8333 if (row >= screen_Rows || col >= screen_Columns)
8334 return;
8335
Bram Moolenaar494838a2015-02-10 19:20:37 +01008336 /* Outputting a character in the last cell on the screen may scroll the
8337 * screen up. Only do it when the "xn" termcap property is set, otherwise
8338 * mark the character invalid (update it when scrolled up). */
8339 if (*T_XN == NUL
8340 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341#ifdef FEAT_RIGHTLEFT
8342 /* account for first command-line character in rightleft mode */
8343 && !cmdmsg_rl
8344#endif
8345 )
8346 {
8347 ScreenAttrs[off] = (sattr_T)-1;
8348 return;
8349 }
8350
8351 /*
8352 * Stop highlighting first, so it's easier to move the cursor.
8353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 if (screen_char_attr != 0)
8355 attr = screen_char_attr;
8356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 attr = ScreenAttrs[off];
8358 if (screen_attr != attr)
8359 screen_stop_highlight();
8360
8361 windgoto(row, col);
8362
8363 if (screen_attr != attr)
8364 screen_start_highlight(attr);
8365
8366#ifdef FEAT_MBYTE
8367 if (enc_utf8 && ScreenLinesUC[off] != 0)
8368 {
8369 char_u buf[MB_MAXBYTES + 1];
8370
Bram Moolenaarcb070082016-04-02 22:14:51 +02008371 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008372 {
8373 if (*p_ambw == 'd'
8374# ifdef FEAT_GUI
8375 && !gui.in_use
8376# endif
8377 )
8378 {
8379 /* Clear the two screen cells. If the character is actually
8380 * single width it won't change the second cell. */
8381 out_str((char_u *)" ");
8382 term_windgoto(row, col);
8383 }
8384 /* not sure where the cursor is after drawing the ambiguous width
8385 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008386 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008387 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008388 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008390
8391 /* Convert the UTF-8 character to bytes and write it. */
8392 buf[utfc_char2bytes(off, buf)] = NUL;
8393 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 }
8395 else
8396#endif
8397 {
8398#ifdef FEAT_MBYTE
8399 out_flush_check();
8400#endif
8401 out_char(ScreenLines[off]);
8402#ifdef FEAT_MBYTE
8403 /* double-byte character in single-width cell */
8404 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8405 out_char(ScreenLines2[off]);
8406#endif
8407 }
8408
8409 screen_cur_col++;
8410}
8411
8412#ifdef FEAT_MBYTE
8413
8414/*
8415 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8416 * on the screen at position 'row' and 'col'.
8417 * The attributes of the first byte is used for all. This is required to
8418 * output the two bytes of a double-byte character with nothing in between.
8419 */
8420 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008421screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422{
8423 /* Check for illegal values (could be wrong when screen was resized). */
8424 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8425 return;
8426
8427 /* Outputting the last character on the screen may scrollup the screen.
8428 * Don't to it! Mark the character invalid (update it when scrolled up) */
8429 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8430 {
8431 ScreenAttrs[off] = (sattr_T)-1;
8432 return;
8433 }
8434
8435 /* Output the first byte normally (positions the cursor), then write the
8436 * second byte directly. */
8437 screen_char(off, row, col);
8438 out_char(ScreenLines[off + 1]);
8439 ++screen_cur_col;
8440}
8441#endif
8442
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443/*
8444 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8445 * This uses the contents of ScreenLines[] and doesn't change it.
8446 */
8447 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008448screen_draw_rectangle(
8449 int row,
8450 int col,
8451 int height,
8452 int width,
8453 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 int r, c;
8456 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008457#ifdef FEAT_MBYTE
8458 int max_off;
8459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008461 /* Can't use ScreenLines unless initialized */
8462 if (ScreenLines == NULL)
8463 return;
8464
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 if (invert)
8466 screen_char_attr = HL_INVERSE;
8467 for (r = row; r < row + height; ++r)
8468 {
8469 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008470#ifdef FEAT_MBYTE
8471 max_off = off + screen_Columns;
8472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 for (c = col; c < col + width; ++c)
8474 {
8475#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008476 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {
8478 screen_char_2(off + c, r, c);
8479 ++c;
8480 }
8481 else
8482#endif
8483 {
8484 screen_char(off + c, r, c);
8485#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008486 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 ++c;
8488#endif
8489 }
8490 }
8491 }
8492 screen_char_attr = 0;
8493}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495/*
8496 * Redraw the characters for a vertically split window.
8497 */
8498 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008499redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500{
8501 int col;
8502 int width;
8503
8504# ifdef FEAT_CLIPBOARD
8505 clip_may_clear_selection(row, end - 1);
8506# endif
8507
8508 if (wp == NULL)
8509 {
8510 col = 0;
8511 width = Columns;
8512 }
8513 else
8514 {
8515 col = wp->w_wincol;
8516 width = wp->w_width;
8517 }
8518 screen_draw_rectangle(row, col, end - row, width, FALSE);
8519}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008521 static void
8522space_to_screenline(int off, int attr)
8523{
8524 ScreenLines[off] = ' ';
8525 ScreenAttrs[off] = attr;
8526# ifdef FEAT_MBYTE
8527 if (enc_utf8)
8528 ScreenLinesUC[off] = 0;
8529# endif
8530}
8531
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532/*
8533 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8534 * with character 'c1' in first column followed by 'c2' in the other columns.
8535 * Use attributes 'attr'.
8536 */
8537 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008538screen_fill(
8539 int start_row,
8540 int end_row,
8541 int start_col,
8542 int end_col,
8543 int c1,
8544 int c2,
8545 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 int row;
8548 int col;
8549 int off;
8550 int end_off;
8551 int did_delete;
8552 int c;
8553 int norm_term;
8554#if defined(FEAT_GUI) || defined(UNIX)
8555 int force_next = FALSE;
8556#endif
8557
8558 if (end_row > screen_Rows) /* safety check */
8559 end_row = screen_Rows;
8560 if (end_col > screen_Columns) /* safety check */
8561 end_col = screen_Columns;
8562 if (ScreenLines == NULL
8563 || start_row >= end_row
8564 || start_col >= end_col) /* nothing to do */
8565 return;
8566
8567 /* it's a "normal" terminal when not in a GUI or cterm */
8568 norm_term = (
8569#ifdef FEAT_GUI
8570 !gui.in_use &&
8571#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008572 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 for (row = start_row; row < end_row; ++row)
8574 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008575#ifdef FEAT_MBYTE
8576 if (has_mbyte
8577# ifdef FEAT_GUI
8578 && !gui.in_use
8579# endif
8580 )
8581 {
8582 /* When drawing over the right halve of a double-wide char clear
8583 * out the left halve. When drawing over the left halve of a
8584 * double wide-char clear out the right halve. Only needed in a
8585 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008586 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008587 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008588 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008589 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008590 }
8591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 /*
8593 * Try to use delete-line termcap code, when no attributes or in a
8594 * "normal" terminal, where a bold/italic space is just a
8595 * space.
8596 */
8597 did_delete = FALSE;
8598 if (c2 == ' '
8599 && end_col == Columns
8600 && can_clear(T_CE)
8601 && (attr == 0
8602 || (norm_term
8603 && attr <= HL_ALL
8604 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8605 {
8606 /*
8607 * check if we really need to clear something
8608 */
8609 col = start_col;
8610 if (c1 != ' ') /* don't clear first char */
8611 ++col;
8612
8613 off = LineOffset[row] + col;
8614 end_off = LineOffset[row] + end_col;
8615
8616 /* skip blanks (used often, keep it fast!) */
8617#ifdef FEAT_MBYTE
8618 if (enc_utf8)
8619 while (off < end_off && ScreenLines[off] == ' '
8620 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8621 ++off;
8622 else
8623#endif
8624 while (off < end_off && ScreenLines[off] == ' '
8625 && ScreenAttrs[off] == 0)
8626 ++off;
8627 if (off < end_off) /* something to be cleared */
8628 {
8629 col = off - LineOffset[row];
8630 screen_stop_highlight();
8631 term_windgoto(row, col);/* clear rest of this screen line */
8632 out_str(T_CE);
8633 screen_start(); /* don't know where cursor is now */
8634 col = end_col - col;
8635 while (col--) /* clear chars in ScreenLines */
8636 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008637 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 ++off;
8639 }
8640 }
8641 did_delete = TRUE; /* the chars are cleared now */
8642 }
8643
8644 off = LineOffset[row] + start_col;
8645 c = c1;
8646 for (col = start_col; col < end_col; ++col)
8647 {
8648 if (ScreenLines[off] != c
8649#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008650 || (enc_utf8 && (int)ScreenLinesUC[off]
8651 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652#endif
8653 || ScreenAttrs[off] != attr
8654#if defined(FEAT_GUI) || defined(UNIX)
8655 || force_next
8656#endif
8657 )
8658 {
8659#if defined(FEAT_GUI) || defined(UNIX)
8660 /* The bold trick may make a single row of pixels appear in
8661 * the next character. When a bold character is removed, the
8662 * next character should be redrawn too. This happens for our
8663 * own GUI and for some xterms. */
8664 if (
8665# ifdef FEAT_GUI
8666 gui.in_use
8667# endif
8668# if defined(FEAT_GUI) && defined(UNIX)
8669 ||
8670# endif
8671# ifdef UNIX
8672 term_is_xterm
8673# endif
8674 )
8675 {
8676 if (ScreenLines[off] != ' '
8677 && (ScreenAttrs[off] > HL_ALL
8678 || ScreenAttrs[off] & HL_BOLD))
8679 force_next = TRUE;
8680 else
8681 force_next = FALSE;
8682 }
8683#endif
8684 ScreenLines[off] = c;
8685#ifdef FEAT_MBYTE
8686 if (enc_utf8)
8687 {
8688 if (c >= 0x80)
8689 {
8690 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008691 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 }
8693 else
8694 ScreenLinesUC[off] = 0;
8695 }
8696#endif
8697 ScreenAttrs[off] = attr;
8698 if (!did_delete || c != ' ')
8699 screen_char(off, row, col);
8700 }
8701 ++off;
8702 if (col == start_col)
8703 {
8704 if (did_delete)
8705 break;
8706 c = c2;
8707 }
8708 }
8709 if (end_col == Columns)
8710 LineWraps[row] = FALSE;
8711 if (row == Rows - 1) /* overwritten the command line */
8712 {
8713 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008714 if (start_col == 0 && end_col == Columns
8715 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008717 if (start_col == 0)
8718 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 }
8720 }
8721}
8722
8723/*
8724 * Check if there should be a delay. Used before clearing or redrawing the
8725 * screen or the command line.
8726 */
8727 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008728check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729{
8730 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8731 && !did_wait_return
8732 && emsg_silent == 0)
8733 {
8734 out_flush();
8735 ui_delay(1000L, TRUE);
8736 emsg_on_display = FALSE;
8737 if (check_msg_scroll)
8738 msg_scroll = FALSE;
8739 }
8740}
8741
8742/*
8743 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008744 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 * Returns TRUE if there is a valid screen to write to.
8746 * Returns FALSE when starting up and screen not initialized yet.
8747 */
8748 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008749screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008751 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 return (ScreenLines != NULL);
8753}
8754
8755/*
8756 * Resize the shell to Rows and Columns.
8757 * Allocate ScreenLines[] and associated items.
8758 *
8759 * There may be some time between setting Rows and Columns and (re)allocating
8760 * ScreenLines[]. This happens when starting up and when (manually) changing
8761 * the shell size. Always use screen_Rows and screen_Columns to access items
8762 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8763 * final size of the shell is needed.
8764 */
8765 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008766screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
8768 int new_row, old_row;
8769#ifdef FEAT_GUI
8770 int old_Rows;
8771#endif
8772 win_T *wp;
8773 int outofmem = FALSE;
8774 int len;
8775 schar_T *new_ScreenLines;
8776#ifdef FEAT_MBYTE
8777 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008778 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008780 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781#endif
8782 sattr_T *new_ScreenAttrs;
8783 unsigned *new_LineOffset;
8784 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008785 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008786 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008788 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008789 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008791retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 /*
8793 * Allocation of the screen buffers is done only when the size changes and
8794 * when Rows and Columns have been set and we have started doing full
8795 * screen stuff.
8796 */
8797 if ((ScreenLines != NULL
8798 && Rows == screen_Rows
8799 && Columns == screen_Columns
8800#ifdef FEAT_MBYTE
8801 && enc_utf8 == (ScreenLinesUC != NULL)
8802 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008803 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804#endif
8805 )
8806 || Rows == 0
8807 || Columns == 0
8808 || (!full_screen && ScreenLines == NULL))
8809 return;
8810
8811 /*
8812 * It's possible that we produce an out-of-memory message below, which
8813 * will cause this function to be called again. To break the loop, just
8814 * return here.
8815 */
8816 if (entered)
8817 return;
8818 entered = TRUE;
8819
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008820 /*
8821 * Note that the window sizes are updated before reallocating the arrays,
8822 * thus we must not redraw here!
8823 */
8824 ++RedrawingDisabled;
8825
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 win_new_shellsize(); /* fit the windows in the new sized shell */
8827
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 comp_col(); /* recompute columns for shown command and ruler */
8829
8830 /*
8831 * We're changing the size of the screen.
8832 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8833 * - Move lines from the old arrays into the new arrays, clear extra
8834 * lines (unless the screen is going to be cleared).
8835 * - Free the old arrays.
8836 *
8837 * If anything fails, make ScreenLines NULL, so we don't do anything!
8838 * Continuing with the old ScreenLines may result in a crash, because the
8839 * size is wrong.
8840 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008841 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008843 if (aucmd_win != NULL)
8844 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
8846 new_ScreenLines = (schar_T *)lalloc((long_u)(
8847 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8848#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008849 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 if (enc_utf8)
8851 {
8852 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8853 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008854 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008855 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8857 }
8858 if (enc_dbcs == DBCS_JPNU)
8859 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8860 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8861#endif
8862 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8863 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8864 new_LineOffset = (unsigned *)lalloc((long_u)(
8865 Rows * sizeof(unsigned)), FALSE);
8866 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008867 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008869 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 {
8871 if (win_alloc_lines(wp) == FAIL)
8872 {
8873 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008874 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 }
8876 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008877 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8878 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008879 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008880give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008882#ifdef FEAT_MBYTE
8883 for (i = 0; i < p_mco; ++i)
8884 if (new_ScreenLinesC[i] == NULL)
8885 break;
8886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 if (new_ScreenLines == NULL
8888#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008889 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8891#endif
8892 || new_ScreenAttrs == NULL
8893 || new_LineOffset == NULL
8894 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008895 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 || outofmem)
8897 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008898 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008899 {
8900 /* guess the size */
8901 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8902
8903 /* Remember we did this to avoid getting outofmem messages over
8904 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008905 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008906 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008907 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008909 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008910 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008911 VIM_CLEAR(new_ScreenLinesC[i]);
8912 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008914 VIM_CLEAR(new_ScreenAttrs);
8915 VIM_CLEAR(new_LineOffset);
8916 VIM_CLEAR(new_LineWraps);
8917 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 }
8919 else
8920 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008921 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008922
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 for (new_row = 0; new_row < Rows; ++new_row)
8924 {
8925 new_LineOffset[new_row] = new_row * Columns;
8926 new_LineWraps[new_row] = FALSE;
8927
8928 /*
8929 * If the screen is not going to be cleared, copy as much as
8930 * possible from the old screen to the new one and clear the rest
8931 * (used when resizing the window at the "--more--" prompt or when
8932 * executing an external command, for the GUI).
8933 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008934 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 {
8936 (void)vim_memset(new_ScreenLines + new_row * Columns,
8937 ' ', (size_t)Columns * sizeof(schar_T));
8938#ifdef FEAT_MBYTE
8939 if (enc_utf8)
8940 {
8941 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8942 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008943 for (i = 0; i < p_mco; ++i)
8944 (void)vim_memset(new_ScreenLinesC[i]
8945 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 0, (size_t)Columns * sizeof(u8char_T));
8947 }
8948 if (enc_dbcs == DBCS_JPNU)
8949 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8950 0, (size_t)Columns * sizeof(schar_T));
8951#endif
8952 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8953 0, (size_t)Columns * sizeof(sattr_T));
8954 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008955 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 {
8957 if (screen_Columns < Columns)
8958 len = screen_Columns;
8959 else
8960 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008961#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008962 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008963 * may be invalid now. Also when p_mco changes. */
8964 if (!(enc_utf8 && ScreenLinesUC == NULL)
8965 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008966#endif
8967 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8968 ScreenLines + LineOffset[old_row],
8969 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008971 if (enc_utf8 && ScreenLinesUC != NULL
8972 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 {
8974 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8975 ScreenLinesUC + LineOffset[old_row],
8976 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008977 for (i = 0; i < p_mco; ++i)
8978 mch_memmove(new_ScreenLinesC[i]
8979 + new_LineOffset[new_row],
8980 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 (size_t)len * sizeof(u8char_T));
8982 }
8983 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8984 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8985 ScreenLines2 + LineOffset[old_row],
8986 (size_t)len * sizeof(schar_T));
8987#endif
8988 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8989 ScreenAttrs + LineOffset[old_row],
8990 (size_t)len * sizeof(sattr_T));
8991 }
8992 }
8993 }
8994 /* Use the last line of the screen for the current line. */
8995 current_ScreenLine = new_ScreenLines + Rows * Columns;
8996 }
8997
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008998 free_screenlines();
8999
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 ScreenLines = new_ScreenLines;
9001#ifdef FEAT_MBYTE
9002 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009003 for (i = 0; i < p_mco; ++i)
9004 ScreenLinesC[i] = new_ScreenLinesC[i];
9005 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 ScreenLines2 = new_ScreenLines2;
9007#endif
9008 ScreenAttrs = new_ScreenAttrs;
9009 LineOffset = new_LineOffset;
9010 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009011 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012
9013 /* It's important that screen_Rows and screen_Columns reflect the actual
9014 * size of ScreenLines[]. Set them before calling anything. */
9015#ifdef FEAT_GUI
9016 old_Rows = screen_Rows;
9017#endif
9018 screen_Rows = Rows;
9019 screen_Columns = Columns;
9020
9021 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009022 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 screenclear2();
9024
9025#ifdef FEAT_GUI
9026 else if (gui.in_use
9027 && !gui.starting
9028 && ScreenLines != NULL
9029 && old_Rows != Rows)
9030 {
9031 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9032 /*
9033 * Adjust the position of the cursor, for when executing an external
9034 * command.
9035 */
9036 if (msg_row >= Rows) /* Rows got smaller */
9037 msg_row = Rows - 1; /* put cursor at last row */
9038 else if (Rows > old_Rows) /* Rows got bigger */
9039 msg_row += Rows - old_Rows; /* put cursor in same place */
9040 if (msg_col >= Columns) /* Columns got smaller */
9041 msg_col = Columns - 1; /* put cursor at last column */
9042 }
9043#endif
9044
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009046 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009047
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009048 /*
9049 * Do not apply autocommands more than 3 times to avoid an endless loop
9050 * in case applying autocommands always changes Rows or Columns.
9051 */
9052 if (starting == 0 && ++retry_count <= 3)
9053 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009054 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009055 /* In rare cases, autocommands may have altered Rows or Columns,
9056 * jump back to check if we need to allocate the screen again. */
9057 goto retry;
9058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059}
9060
9061 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009062free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009063{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009064#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009065 int i;
9066
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009067 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009068 for (i = 0; i < Screen_mco; ++i)
9069 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009070 vim_free(ScreenLines2);
9071#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009072 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009073 vim_free(ScreenAttrs);
9074 vim_free(LineOffset);
9075 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009076 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009077}
9078
9079 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009080screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082 check_for_delay(FALSE);
9083 screenalloc(FALSE); /* allocate screen buffers if size changed */
9084 screenclear2(); /* clear the screen */
9085}
9086
9087 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009088screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089{
9090 int i;
9091
9092 if (starting == NO_SCREEN || ScreenLines == NULL
9093#ifdef FEAT_GUI
9094 || (gui.in_use && gui.starting)
9095#endif
9096 )
9097 return;
9098
9099#ifdef FEAT_GUI
9100 if (!gui.in_use)
9101#endif
9102 screen_attr = -1; /* force setting the Normal colors */
9103 screen_stop_highlight(); /* don't want highlighting here */
9104
9105#ifdef FEAT_CLIPBOARD
9106 /* disable selection without redrawing it */
9107 clip_scroll_selection(9999);
9108#endif
9109
9110 /* blank out ScreenLines */
9111 for (i = 0; i < Rows; ++i)
9112 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009113 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 LineWraps[i] = FALSE;
9115 }
9116
9117 if (can_clear(T_CL))
9118 {
9119 out_str(T_CL); /* clear the display */
9120 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009121 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 }
9123 else
9124 {
9125 /* can't clear the screen, mark all chars with invalid attributes */
9126 for (i = 0; i < Rows; ++i)
9127 lineinvalid(LineOffset[i], (int)Columns);
9128 clear_cmdline = TRUE;
9129 }
9130
9131 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9132
9133 win_rest_invalid(firstwin);
9134 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009135 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 if (must_redraw == CLEAR) /* no need to clear again */
9137 must_redraw = NOT_VALID;
9138 compute_cmdrow();
9139 msg_row = cmdline_row; /* put cursor on last line for messages */
9140 msg_col = 0;
9141 screen_start(); /* don't know where cursor is now */
9142 msg_scrolled = 0; /* can't scroll back */
9143 msg_didany = FALSE;
9144 msg_didout = FALSE;
9145}
9146
9147/*
9148 * Clear one line in ScreenLines.
9149 */
9150 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009151lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152{
9153 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9154#ifdef FEAT_MBYTE
9155 if (enc_utf8)
9156 (void)vim_memset(ScreenLinesUC + off, 0,
9157 (size_t)width * sizeof(u8char_T));
9158#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009159 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160}
9161
9162/*
9163 * Mark one line in ScreenLines invalid by setting the attributes to an
9164 * invalid value.
9165 */
9166 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009167lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168{
9169 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9170}
9171
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172/*
9173 * Copy part of a Screenline for vertically split window "wp".
9174 */
9175 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009176linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177{
9178 unsigned off_to = LineOffset[to] + wp->w_wincol;
9179 unsigned off_from = LineOffset[from] + wp->w_wincol;
9180
9181 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9182 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009183#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 if (enc_utf8)
9185 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009186 int i;
9187
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9189 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009190 for (i = 0; i < p_mco; ++i)
9191 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9192 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 }
9194 if (enc_dbcs == DBCS_JPNU)
9195 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9196 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9199 wp->w_width * sizeof(sattr_T));
9200}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
9202/*
9203 * Return TRUE if clearing with term string "p" would work.
9204 * It can't work when the string is empty or it won't set the right background.
9205 */
9206 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009207can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208{
9209 return (*p != NUL && (t_colors <= 1
9210#ifdef FEAT_GUI
9211 || gui.in_use
9212#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009213#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009214 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009215 || (!p_tgc && cterm_normal_bg_color == 0)
9216#else
9217 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009218#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009219 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220}
9221
9222/*
9223 * Reset cursor position. Use whenever cursor was moved because of outputting
9224 * something directly to the screen (shell commands) or a terminal control
9225 * code.
9226 */
9227 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009228screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229{
9230 screen_cur_row = screen_cur_col = 9999;
9231}
9232
9233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 * Move the cursor to position "row","col" in the screen.
9235 * This tries to find the most efficient way to move, minimizing the number of
9236 * characters sent to the terminal.
9237 */
9238 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009239windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009241 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 int i;
9243 int plan;
9244 int cost;
9245 int wouldbe_col;
9246 int noinvcurs;
9247 char_u *bs;
9248 int goto_cost;
9249 int attr;
9250
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009251#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9253
9254#define PLAN_LE 1
9255#define PLAN_CR 2
9256#define PLAN_NL 3
9257#define PLAN_WRITE 4
9258 /* Can't use ScreenLines unless initialized */
9259 if (ScreenLines == NULL)
9260 return;
9261
9262 if (col != screen_cur_col || row != screen_cur_row)
9263 {
9264 /* Check for valid position. */
9265 if (row < 0) /* window without text lines? */
9266 row = 0;
9267 if (row >= screen_Rows)
9268 row = screen_Rows - 1;
9269 if (col >= screen_Columns)
9270 col = screen_Columns - 1;
9271
9272 /* check if no cursor movement is allowed in highlight mode */
9273 if (screen_attr && *T_MS == NUL)
9274 noinvcurs = HIGHL_COST;
9275 else
9276 noinvcurs = 0;
9277 goto_cost = GOTO_COST + noinvcurs;
9278
9279 /*
9280 * Plan how to do the positioning:
9281 * 1. Use CR to move it to column 0, same row.
9282 * 2. Use T_LE to move it a few columns to the left.
9283 * 3. Use NL to move a few lines down, column 0.
9284 * 4. Move a few columns to the right with T_ND or by writing chars.
9285 *
9286 * Don't do this if the cursor went beyond the last column, the cursor
9287 * position is unknown then (some terminals wrap, some don't )
9288 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009289 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 * characters to move the cursor to the right.
9291 */
9292 if (row >= screen_cur_row && screen_cur_col < Columns)
9293 {
9294 /*
9295 * If the cursor is in the same row, bigger col, we can use CR
9296 * or T_LE.
9297 */
9298 bs = NULL; /* init for GCC */
9299 attr = screen_attr;
9300 if (row == screen_cur_row && col < screen_cur_col)
9301 {
9302 /* "le" is preferred over "bc", because "bc" is obsolete */
9303 if (*T_LE)
9304 bs = T_LE; /* "cursor left" */
9305 else
9306 bs = T_BC; /* "backspace character (old) */
9307 if (*bs)
9308 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9309 else
9310 cost = 999;
9311 if (col + 1 < cost) /* using CR is less characters */
9312 {
9313 plan = PLAN_CR;
9314 wouldbe_col = 0;
9315 cost = 1; /* CR is just one character */
9316 }
9317 else
9318 {
9319 plan = PLAN_LE;
9320 wouldbe_col = col;
9321 }
9322 if (noinvcurs) /* will stop highlighting */
9323 {
9324 cost += noinvcurs;
9325 attr = 0;
9326 }
9327 }
9328
9329 /*
9330 * If the cursor is above where we want to be, we can use CR LF.
9331 */
9332 else if (row > screen_cur_row)
9333 {
9334 plan = PLAN_NL;
9335 wouldbe_col = 0;
9336 cost = (row - screen_cur_row) * 2; /* CR LF */
9337 if (noinvcurs) /* will stop highlighting */
9338 {
9339 cost += noinvcurs;
9340 attr = 0;
9341 }
9342 }
9343
9344 /*
9345 * If the cursor is in the same row, smaller col, just use write.
9346 */
9347 else
9348 {
9349 plan = PLAN_WRITE;
9350 wouldbe_col = screen_cur_col;
9351 cost = 0;
9352 }
9353
9354 /*
9355 * Check if any characters that need to be written have the
9356 * correct attributes. Also avoid UTF-8 characters.
9357 */
9358 i = col - wouldbe_col;
9359 if (i > 0)
9360 cost += i;
9361 if (cost < goto_cost && i > 0)
9362 {
9363 /*
9364 * Check if the attributes are correct without additionally
9365 * stopping highlighting.
9366 */
9367 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9368 while (i && *p++ == attr)
9369 --i;
9370 if (i != 0)
9371 {
9372 /*
9373 * Try if it works when highlighting is stopped here.
9374 */
9375 if (*--p == 0)
9376 {
9377 cost += noinvcurs;
9378 while (i && *p++ == 0)
9379 --i;
9380 }
9381 if (i != 0)
9382 cost = 999; /* different attributes, don't do it */
9383 }
9384#ifdef FEAT_MBYTE
9385 if (enc_utf8)
9386 {
9387 /* Don't use an UTF-8 char for positioning, it's slow. */
9388 for (i = wouldbe_col; i < col; ++i)
9389 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9390 {
9391 cost = 999;
9392 break;
9393 }
9394 }
9395#endif
9396 }
9397
9398 /*
9399 * We can do it without term_windgoto()!
9400 */
9401 if (cost < goto_cost)
9402 {
9403 if (plan == PLAN_LE)
9404 {
9405 if (noinvcurs)
9406 screen_stop_highlight();
9407 while (screen_cur_col > col)
9408 {
9409 out_str(bs);
9410 --screen_cur_col;
9411 }
9412 }
9413 else if (plan == PLAN_CR)
9414 {
9415 if (noinvcurs)
9416 screen_stop_highlight();
9417 out_char('\r');
9418 screen_cur_col = 0;
9419 }
9420 else if (plan == PLAN_NL)
9421 {
9422 if (noinvcurs)
9423 screen_stop_highlight();
9424 while (screen_cur_row < row)
9425 {
9426 out_char('\n');
9427 ++screen_cur_row;
9428 }
9429 screen_cur_col = 0;
9430 }
9431
9432 i = col - screen_cur_col;
9433 if (i > 0)
9434 {
9435 /*
9436 * Use cursor-right if it's one character only. Avoids
9437 * removing a line of pixels from the last bold char, when
9438 * using the bold trick in the GUI.
9439 */
9440 if (T_ND[0] != NUL && T_ND[1] == NUL)
9441 {
9442 while (i-- > 0)
9443 out_char(*T_ND);
9444 }
9445 else
9446 {
9447 int off;
9448
9449 off = LineOffset[row] + screen_cur_col;
9450 while (i-- > 0)
9451 {
9452 if (ScreenAttrs[off] != screen_attr)
9453 screen_stop_highlight();
9454#ifdef FEAT_MBYTE
9455 out_flush_check();
9456#endif
9457 out_char(ScreenLines[off]);
9458#ifdef FEAT_MBYTE
9459 if (enc_dbcs == DBCS_JPNU
9460 && ScreenLines[off] == 0x8e)
9461 out_char(ScreenLines2[off]);
9462#endif
9463 ++off;
9464 }
9465 }
9466 }
9467 }
9468 }
9469 else
9470 cost = 999;
9471
9472 if (cost >= goto_cost)
9473 {
9474 if (noinvcurs)
9475 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009476 if (row == screen_cur_row && (col > screen_cur_col)
9477 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 term_cursor_right(col - screen_cur_col);
9479 else
9480 term_windgoto(row, col);
9481 }
9482 screen_cur_row = row;
9483 screen_cur_col = col;
9484 }
9485}
9486
9487/*
9488 * Set cursor to its position in the current window.
9489 */
9490 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009491setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009493 setcursor_mayforce(FALSE);
9494}
9495
9496/*
9497 * Set cursor to its position in the current window.
9498 * When "force" is TRUE also when not redrawing.
9499 */
9500 void
9501setcursor_mayforce(int force)
9502{
9503 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 {
9505 validate_cursor();
9506 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009507 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009509 /* With 'rightleft' set and the cursor on a double-wide
9510 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009511 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009513 (has_mbyte
9514 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9515 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516# endif
9517 1)) :
9518#endif
9519 curwin->w_wcol));
9520 }
9521}
9522
9523
9524/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009525 * Insert 'line_count' lines at 'row' in window 'wp'.
9526 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9527 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 * scrolling.
9529 * Returns FAIL if the lines are not inserted, OK for success.
9530 */
9531 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009532win_ins_lines(
9533 win_T *wp,
9534 int row,
9535 int line_count,
9536 int invalid,
9537 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
9539 int did_delete;
9540 int nextrow;
9541 int lastrow;
9542 int retval;
9543
9544 if (invalid)
9545 wp->w_lines_valid = 0;
9546
9547 if (wp->w_height < 5)
9548 return FAIL;
9549
9550 if (line_count > wp->w_height - row)
9551 line_count = wp->w_height - row;
9552
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009553 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 if (retval != MAYBE)
9555 return retval;
9556
9557 /*
9558 * If there is a next window or a status line, we first try to delete the
9559 * lines at the bottom to avoid messing what is after the window.
9560 * If this fails and there are following windows, don't do anything to avoid
9561 * messing up those windows, better just redraw.
9562 */
9563 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 if (wp->w_next != NULL || wp->w_status_height)
9565 {
9566 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009567 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 did_delete = TRUE;
9569 else if (wp->w_next)
9570 return FAIL;
9571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 /*
9573 * if no lines deleted, blank the lines that will end up below the window
9574 */
9575 if (!did_delete)
9576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009579 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 lastrow = nextrow + line_count;
9581 if (lastrow > Rows)
9582 lastrow = Rows;
9583 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009584 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 ' ', ' ', 0);
9586 }
9587
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009588 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 == FAIL)
9590 {
9591 /* deletion will have messed up other windows */
9592 if (did_delete)
9593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 win_rest_invalid(W_NEXT(wp));
9596 }
9597 return FAIL;
9598 }
9599
9600 return OK;
9601}
9602
9603/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009604 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9606 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9607 * scrolling
9608 * Return OK for success, FAIL if the lines are not deleted.
9609 */
9610 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009611win_del_lines(
9612 win_T *wp,
9613 int row,
9614 int line_count,
9615 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009616 int mayclear,
9617 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
9619 int retval;
9620
9621 if (invalid)
9622 wp->w_lines_valid = 0;
9623
9624 if (line_count > wp->w_height - row)
9625 line_count = wp->w_height - row;
9626
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009627 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 if (retval != MAYBE)
9629 return retval;
9630
9631 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009632 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 return FAIL;
9634
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 /*
9636 * If there are windows or status lines below, try to put them at the
9637 * correct place. If we can't do that, they have to be redrawn.
9638 */
9639 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9640 {
9641 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009642 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 {
9644 wp->w_redr_status = TRUE;
9645 win_rest_invalid(wp->w_next);
9646 }
9647 }
9648 /*
9649 * If this is the last window and there is no status line, redraw the
9650 * command line later.
9651 */
9652 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 redraw_cmdline = TRUE;
9654 return OK;
9655}
9656
9657/*
9658 * Common code for win_ins_lines() and win_del_lines().
9659 * Returns OK or FAIL when the work has been done.
9660 * Returns MAYBE when not finished yet.
9661 */
9662 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009663win_do_lines(
9664 win_T *wp,
9665 int row,
9666 int line_count,
9667 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009668 int del,
9669 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670{
9671 int retval;
9672
9673 if (!redrawing() || line_count <= 0)
9674 return FAIL;
9675
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009676 /* When inserting lines would result in loss of command output, just redraw
9677 * the lines. */
9678 if (no_win_do_lines_ins && !del)
9679 return FAIL;
9680
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009682 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009684 if (!no_win_do_lines_ins)
9685 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 return FAIL;
9687 }
9688
9689 /*
9690 * Delete all remaining lines
9691 */
9692 if (row + line_count >= wp->w_height)
9693 {
9694 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009695 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 ' ', ' ', 0);
9697 return OK;
9698 }
9699
9700 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009701 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009703 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009705 if (!no_win_do_lines_ins)
9706 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707
9708 /*
9709 * If the terminal can set a scroll region, use that.
9710 * Always do this in a vertically split window. This will redraw from
9711 * ScreenLines[] when t_CV isn't defined. That's faster than using
9712 * win_line().
9713 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009714 * a character in the lower right corner of the scroll region may cause a
9715 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009717 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 {
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_set(wp, row);
9721 if (del)
9722 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009723 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 else
9725 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009726 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 scroll_region_reset();
9729 return retval;
9730 }
9731
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9733 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734
9735 return MAYBE;
9736}
9737
9738/*
9739 * window 'wp' and everything after it is messed up, mark it for redraw
9740 */
9741 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009742win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 {
9746 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 wp->w_redr_status = TRUE;
9748 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 }
9750 redraw_cmdline = TRUE;
9751}
9752
9753/*
9754 * The rest of the routines in this file perform screen manipulations. The
9755 * given operation is performed physically on the screen. The corresponding
9756 * change is also made to the internal screen image. In this way, the editor
9757 * anticipates the effect of editing changes on the appearance of the screen.
9758 * That way, when we call screenupdate a complete redraw isn't usually
9759 * necessary. Another advantage is that we can keep adding code to anticipate
9760 * screen changes, and in the meantime, everything still works.
9761 */
9762
9763/*
9764 * types for inserting or deleting lines
9765 */
9766#define USE_T_CAL 1
9767#define USE_T_CDL 2
9768#define USE_T_AL 3
9769#define USE_T_CE 4
9770#define USE_T_DL 5
9771#define USE_T_SR 6
9772#define USE_NL 7
9773#define USE_T_CD 8
9774#define USE_REDRAW 9
9775
9776/*
9777 * insert lines on the screen and update ScreenLines[]
9778 * 'end' is the line after the scrolled part. Normally it is Rows.
9779 * When scrolling region used 'off' is the offset from the top for the region.
9780 * 'row' and 'end' are relative to the start of the region.
9781 *
9782 * return FAIL for failure, OK for success.
9783 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009784 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009785screen_ins_lines(
9786 int off,
9787 int row,
9788 int line_count,
9789 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009790 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009791 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792{
9793 int i;
9794 int j;
9795 unsigned temp;
9796 int cursor_row;
9797 int type;
9798 int result_empty;
9799 int can_ce = can_clear(T_CE);
9800
9801 /*
9802 * FAIL if
9803 * - there is no valid screen
9804 * - the screen has to be redrawn completely
9805 * - the line count is less than one
9806 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009807 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009809 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9810#ifdef FEAT_CLIPBOARD
9811 || (clip_star.state != SELECT_CLEARED
9812 && redrawing_for_callback > 0)
9813#endif
9814 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 return FAIL;
9816
9817 /*
9818 * There are seven ways to insert lines:
9819 * 0. When in a vertically split window and t_CV isn't set, redraw the
9820 * characters from ScreenLines[].
9821 * 1. Use T_CD (clear to end of display) if it exists and the result of
9822 * the insert is just empty lines
9823 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9824 * present or line_count > 1. It looks better if we do all the inserts
9825 * at once.
9826 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9827 * insert is just empty lines and T_CE is not present or line_count >
9828 * 1.
9829 * 4. Use T_AL (insert line) if it exists.
9830 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9831 * just empty lines.
9832 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9833 * just empty lines.
9834 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9835 * the 'da' flag is not set or we have clear line capability.
9836 * 8. redraw the characters from ScreenLines[].
9837 *
9838 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9839 * the scrollbar for the window. It does have insert line, use that if it
9840 * exists.
9841 */
9842 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9844 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009845 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 type = USE_T_CD;
9847 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9848 type = USE_T_CAL;
9849 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9850 type = USE_T_CDL;
9851 else if (*T_AL != NUL)
9852 type = USE_T_AL;
9853 else if (can_ce && result_empty)
9854 type = USE_T_CE;
9855 else if (*T_DL != NUL && result_empty)
9856 type = USE_T_DL;
9857 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9858 type = USE_T_SR;
9859 else
9860 return FAIL;
9861
9862 /*
9863 * For clearing the lines screen_del_lines() is used. This will also take
9864 * care of t_db if necessary.
9865 */
9866 if (type == USE_T_CD || type == USE_T_CDL ||
9867 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009868 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
9870 /*
9871 * If text is retained below the screen, first clear or delete as many
9872 * lines at the bottom of the window as are about to be inserted so that
9873 * the deleted lines won't later surface during a screen_del_lines.
9874 */
9875 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009876 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877
9878#ifdef FEAT_CLIPBOARD
9879 /* Remove a modeless selection when inserting lines halfway the screen
9880 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009881 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009882 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 else
9884 clip_scroll_selection(-line_count);
9885#endif
9886
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887#ifdef FEAT_GUI
9888 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9889 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009890 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891#endif
9892
9893 if (*T_CCS != NUL) /* cursor relative to region */
9894 cursor_row = row;
9895 else
9896 cursor_row = row + off;
9897
9898 /*
9899 * Shift LineOffset[] line_count down to reflect the inserted lines.
9900 * Clear the inserted lines in ScreenLines[].
9901 */
9902 row += off;
9903 end += off;
9904 for (i = 0; i < line_count; ++i)
9905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 if (wp != NULL && wp->w_width != Columns)
9907 {
9908 /* need to copy part of a line */
9909 j = end - 1 - i;
9910 while ((j -= line_count) >= row)
9911 linecopy(j + line_count, j, wp);
9912 j += line_count;
9913 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009914 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9915 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 else
9917 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9918 LineWraps[j] = FALSE;
9919 }
9920 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 {
9922 j = end - 1 - i;
9923 temp = LineOffset[j];
9924 while ((j -= line_count) >= row)
9925 {
9926 LineOffset[j + line_count] = LineOffset[j];
9927 LineWraps[j + line_count] = LineWraps[j];
9928 }
9929 LineOffset[j + line_count] = temp;
9930 LineWraps[j + line_count] = FALSE;
9931 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009932 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 else
9934 lineinvalid(temp, (int)Columns);
9935 }
9936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937
9938 screen_stop_highlight();
9939 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009940 if (clear_attr != 0)
9941 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 /* redraw the characters */
9944 if (type == USE_REDRAW)
9945 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009946 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 {
9948 term_append_lines(line_count);
9949 screen_start(); /* don't know where cursor is now */
9950 }
9951 else
9952 {
9953 for (i = 0; i < line_count; i++)
9954 {
9955 if (type == USE_T_AL)
9956 {
9957 if (i && cursor_row != 0)
9958 windgoto(cursor_row, 0);
9959 out_str(T_AL);
9960 }
9961 else /* type == USE_T_SR */
9962 out_str(T_SR);
9963 screen_start(); /* don't know where cursor is now */
9964 }
9965 }
9966
9967 /*
9968 * With scroll-reverse and 'da' flag set we need to clear the lines that
9969 * have been scrolled down into the region.
9970 */
9971 if (type == USE_T_SR && *T_DA)
9972 {
9973 for (i = 0; i < line_count; ++i)
9974 {
9975 windgoto(off + i, 0);
9976 out_str(T_CE);
9977 screen_start(); /* don't know where cursor is now */
9978 }
9979 }
9980
9981#ifdef FEAT_GUI
9982 gui_can_update_cursor();
9983 if (gui.in_use)
9984 out_flush(); /* always flush after a scroll */
9985#endif
9986 return OK;
9987}
9988
9989/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009990 * Delete lines on the screen and update ScreenLines[].
9991 * "end" is the line after the scrolled part. Normally it is Rows.
9992 * When scrolling region used "off" is the offset from the top for the region.
9993 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 *
9995 * Return OK for success, FAIL if the lines are not deleted.
9996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009998screen_del_lines(
9999 int off,
10000 int row,
10001 int line_count,
10002 int end,
10003 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010004 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010005 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006{
10007 int j;
10008 int i;
10009 unsigned temp;
10010 int cursor_row;
10011 int cursor_end;
10012 int result_empty; /* result is empty until end of region */
10013 int can_delete; /* deleting line codes can be used */
10014 int type;
10015
10016 /*
10017 * FAIL if
10018 * - there is no valid screen
10019 * - the screen has to be redrawn completely
10020 * - the line count is less than one
10021 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010022 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010024 if (!screen_valid(TRUE) || line_count <= 0
10025 || (!force && line_count > p_ttyscroll)
10026#ifdef FEAT_CLIPBOARD
10027 || (clip_star.state != SELECT_CLEARED
10028 && redrawing_for_callback > 0)
10029#endif
10030 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 return FAIL;
10032
10033 /*
10034 * Check if the rest of the current region will become empty.
10035 */
10036 result_empty = row + line_count >= end;
10037
10038 /*
10039 * We can delete lines only when 'db' flag not set or when 'ce' option
10040 * available.
10041 */
10042 can_delete = (*T_DB == NUL || can_clear(T_CE));
10043
10044 /*
10045 * There are six ways to delete lines:
10046 * 0. When in a vertically split window and t_CV isn't set, redraw the
10047 * characters from ScreenLines[].
10048 * 1. Use T_CD if it exists and the result is empty.
10049 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10050 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10051 * none of the other ways work.
10052 * 4. Use T_CE (erase line) if the result is empty.
10053 * 5. Use T_DL (delete line) if it exists.
10054 * 6. redraw the characters from ScreenLines[].
10055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10057 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010058 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 type = USE_T_CD;
10060#if defined(__BEOS__) && defined(BEOS_DR8)
10061 /*
10062 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10063 * its internal termcap... this works okay for tests which test *T_DB !=
10064 * NUL. It has the disadvantage that the user cannot use any :set t_*
10065 * command to get T_DB (back) to empty_option, only :set term=... will do
10066 * the trick...
10067 * Anyway, this hack will hopefully go away with the next OS release.
10068 * (Olaf Seibert)
10069 */
10070 else if (row == 0 && T_DB == empty_option
10071 && (line_count == 1 || *T_CDL == NUL))
10072#else
10073 else if (row == 0 && (
10074#ifndef AMIGA
10075 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10076 * up, so use delete-line command */
10077 line_count == 1 ||
10078#endif
10079 *T_CDL == NUL))
10080#endif
10081 type = USE_NL;
10082 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10083 type = USE_T_CDL;
10084 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010085 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 type = USE_T_CE;
10087 else if (*T_DL != NUL && can_delete)
10088 type = USE_T_DL;
10089 else if (*T_CDL != NUL && can_delete)
10090 type = USE_T_CDL;
10091 else
10092 return FAIL;
10093
10094#ifdef FEAT_CLIPBOARD
10095 /* Remove a modeless selection when deleting lines halfway the screen or
10096 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010097 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010098 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 else
10100 clip_scroll_selection(line_count);
10101#endif
10102
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103#ifdef FEAT_GUI
10104 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10105 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010106 gui_dont_update_cursor(gui.cursor_row >= row + off
10107 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108#endif
10109
10110 if (*T_CCS != NUL) /* cursor relative to region */
10111 {
10112 cursor_row = row;
10113 cursor_end = end;
10114 }
10115 else
10116 {
10117 cursor_row = row + off;
10118 cursor_end = end + off;
10119 }
10120
10121 /*
10122 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10123 * Clear the inserted lines in ScreenLines[].
10124 */
10125 row += off;
10126 end += off;
10127 for (i = 0; i < line_count; ++i)
10128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 if (wp != NULL && wp->w_width != Columns)
10130 {
10131 /* need to copy part of a line */
10132 j = row + i;
10133 while ((j += line_count) <= end - 1)
10134 linecopy(j - line_count, j, wp);
10135 j -= line_count;
10136 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010137 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10138 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 else
10140 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10141 LineWraps[j] = FALSE;
10142 }
10143 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 {
10145 /* whole width, moving the line pointers is faster */
10146 j = row + i;
10147 temp = LineOffset[j];
10148 while ((j += line_count) <= end - 1)
10149 {
10150 LineOffset[j - line_count] = LineOffset[j];
10151 LineWraps[j - line_count] = LineWraps[j];
10152 }
10153 LineOffset[j - line_count] = temp;
10154 LineWraps[j - line_count] = FALSE;
10155 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010156 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 else
10158 lineinvalid(temp, (int)Columns);
10159 }
10160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010162 if (screen_attr != clear_attr)
10163 screen_stop_highlight();
10164 if (clear_attr != 0)
10165 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 /* redraw the characters */
10168 if (type == USE_REDRAW)
10169 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010170 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 {
10172 windgoto(cursor_row, 0);
10173 out_str(T_CD);
10174 screen_start(); /* don't know where cursor is now */
10175 }
10176 else if (type == USE_T_CDL)
10177 {
10178 windgoto(cursor_row, 0);
10179 term_delete_lines(line_count);
10180 screen_start(); /* don't know where cursor is now */
10181 }
10182 /*
10183 * Deleting lines at top of the screen or scroll region: Just scroll
10184 * the whole screen (scroll region) up by outputting newlines on the
10185 * last line.
10186 */
10187 else if (type == USE_NL)
10188 {
10189 windgoto(cursor_end - 1, 0);
10190 for (i = line_count; --i >= 0; )
10191 out_char('\n'); /* cursor will remain on same line */
10192 }
10193 else
10194 {
10195 for (i = line_count; --i >= 0; )
10196 {
10197 if (type == USE_T_DL)
10198 {
10199 windgoto(cursor_row, 0);
10200 out_str(T_DL); /* delete a line */
10201 }
10202 else /* type == USE_T_CE */
10203 {
10204 windgoto(cursor_row + i, 0);
10205 out_str(T_CE); /* erase a line */
10206 }
10207 screen_start(); /* don't know where cursor is now */
10208 }
10209 }
10210
10211 /*
10212 * If the 'db' flag is set, we need to clear the lines that have been
10213 * scrolled up at the bottom of the region.
10214 */
10215 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10216 {
10217 for (i = line_count; i > 0; --i)
10218 {
10219 windgoto(cursor_end - i, 0);
10220 out_str(T_CE); /* erase a line */
10221 screen_start(); /* don't know where cursor is now */
10222 }
10223 }
10224
10225#ifdef FEAT_GUI
10226 gui_can_update_cursor();
10227 if (gui.in_use)
10228 out_flush(); /* always flush after a scroll */
10229#endif
10230
10231 return OK;
10232}
10233
10234/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010235 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 *
10237 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10238 * If clear_cmdline is FALSE there may be a message there that needs to be
10239 * cleared only if a mode is shown.
10240 * Return the length of the message (0 if no message).
10241 */
10242 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010243showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244{
10245 int need_clear;
10246 int length = 0;
10247 int do_mode;
10248 int attr;
10249 int nwr_save;
10250#ifdef FEAT_INS_EXPAND
10251 int sub_attr;
10252#endif
10253
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010254 do_mode = ((p_smd && msg_silent == 0)
10255 && ((State & INSERT)
10256 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010257 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 if (do_mode || Recording)
10259 {
10260 /*
10261 * Don't show mode right now, when not redrawing or inside a mapping.
10262 * Call char_avail() only when we are going to show something, because
10263 * it takes a bit of time.
10264 */
10265 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10266 {
10267 redraw_cmdline = TRUE; /* show mode later */
10268 return 0;
10269 }
10270
10271 nwr_save = need_wait_return;
10272
10273 /* wait a bit before overwriting an important message */
10274 check_for_delay(FALSE);
10275
10276 /* if the cmdline is more than one line high, erase top lines */
10277 need_clear = clear_cmdline;
10278 if (clear_cmdline && cmdline_row < Rows - 1)
10279 msg_clr_cmdline(); /* will reset clear_cmdline */
10280
10281 /* Position on the last line in the window, column 0 */
10282 msg_pos_mode();
10283 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010284 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 if (do_mode)
10286 {
10287 MSG_PUTS_ATTR("--", attr);
10288#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010289 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010290# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010291 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010292# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010293 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010294# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010295 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010296# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 MSG_PUTS_ATTR(" IM", attr);
10298# else
10299 MSG_PUTS_ATTR(" XIM", attr);
10300# endif
10301#endif
10302#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10303 if (gui.in_use)
10304 {
10305 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010306 {
10307 /* HANGUL */
10308 if (enc_utf8)
10309 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10310 else
10311 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 }
10314#endif
10315#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010316 /* CTRL-X in Insert mode */
10317 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 {
10319 /* These messages can get long, avoid a wrap in a narrow
10320 * window. Prefer showing edit_submode_extra. */
10321 length = (Rows - msg_row) * Columns - 3;
10322 if (edit_submode_extra != NULL)
10323 length -= vim_strsize(edit_submode_extra);
10324 if (length > 0)
10325 {
10326 if (edit_submode_pre != NULL)
10327 length -= vim_strsize(edit_submode_pre);
10328 if (length - vim_strsize(edit_submode) > 0)
10329 {
10330 if (edit_submode_pre != NULL)
10331 msg_puts_attr(edit_submode_pre, attr);
10332 msg_puts_attr(edit_submode, attr);
10333 }
10334 if (edit_submode_extra != NULL)
10335 {
10336 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10337 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010338 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 else
10340 sub_attr = attr;
10341 msg_puts_attr(edit_submode_extra, sub_attr);
10342 }
10343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 }
10345 else
10346#endif
10347 {
10348#ifdef FEAT_VREPLACE
10349 if (State & VREPLACE_FLAG)
10350 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10351 else
10352#endif
10353 if (State & REPLACE_FLAG)
10354 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10355 else if (State & INSERT)
10356 {
10357#ifdef FEAT_RIGHTLEFT
10358 if (p_ri)
10359 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10360#endif
10361 MSG_PUTS_ATTR(_(" INSERT"), attr);
10362 }
10363 else if (restart_edit == 'I')
10364 MSG_PUTS_ATTR(_(" (insert)"), attr);
10365 else if (restart_edit == 'R')
10366 MSG_PUTS_ATTR(_(" (replace)"), attr);
10367 else if (restart_edit == 'V')
10368 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10369#ifdef FEAT_RIGHTLEFT
10370 if (p_hkmap)
10371 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10372# ifdef FEAT_FKMAP
10373 if (p_fkmap)
10374 MSG_PUTS_ATTR(farsi_text_5, attr);
10375# endif
10376#endif
10377#ifdef FEAT_KEYMAP
10378 if (State & LANGMAP)
10379 {
10380# ifdef FEAT_ARABIC
10381 if (curwin->w_p_arab)
10382 MSG_PUTS_ATTR(_(" Arabic"), attr);
10383 else
10384# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010385 if (get_keymap_str(curwin, (char_u *)" (%s)",
10386 NameBuff, MAXPATHL))
10387 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 }
10389#endif
10390 if ((State & INSERT) && p_paste)
10391 MSG_PUTS_ATTR(_(" (paste)"), attr);
10392
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 if (VIsual_active)
10394 {
10395 char *p;
10396
10397 /* Don't concatenate separate words to avoid translation
10398 * problems. */
10399 switch ((VIsual_select ? 4 : 0)
10400 + (VIsual_mode == Ctrl_V) * 2
10401 + (VIsual_mode == 'V'))
10402 {
10403 case 0: p = N_(" VISUAL"); break;
10404 case 1: p = N_(" VISUAL LINE"); break;
10405 case 2: p = N_(" VISUAL BLOCK"); break;
10406 case 4: p = N_(" SELECT"); break;
10407 case 5: p = N_(" SELECT LINE"); break;
10408 default: p = N_(" SELECT BLOCK"); break;
10409 }
10410 MSG_PUTS_ATTR(_(p), attr);
10411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 MSG_PUTS_ATTR(" --", attr);
10413 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010414
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 need_clear = TRUE;
10416 }
10417 if (Recording
10418#ifdef FEAT_INS_EXPAND
10419 && edit_submode == NULL /* otherwise it gets too long */
10420#endif
10421 )
10422 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010423 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 need_clear = TRUE;
10425 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010426
10427 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 if (need_clear || clear_cmdline)
10429 msg_clr_eos();
10430 msg_didout = FALSE; /* overwrite this message */
10431 length = msg_col;
10432 msg_col = 0;
10433 need_wait_return = nwr_save; /* never ask for hit-return for this */
10434 }
10435 else if (clear_cmdline && msg_silent == 0)
10436 /* Clear the whole command line. Will reset "clear_cmdline". */
10437 msg_clr_cmdline();
10438
10439#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 /* In Visual mode the size of the selected area must be redrawn. */
10441 if (VIsual_active)
10442 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443
10444 /* If the last window has no status line, the ruler is after the mode
10445 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010446 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 win_redr_ruler(lastwin, TRUE);
10448#endif
10449 redraw_cmdline = FALSE;
10450 clear_cmdline = FALSE;
10451
10452 return length;
10453}
10454
10455/*
10456 * Position for a mode message.
10457 */
10458 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010459msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460{
10461 msg_col = 0;
10462 msg_row = Rows - 1;
10463}
10464
10465/*
10466 * Delete mode message. Used when ESC is typed which is expected to end
10467 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010468 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 */
10470 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010471unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472{
10473 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010474 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 */
10476 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10477 redraw_cmdline = TRUE; /* delete mode later */
10478 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010479 clearmode();
10480}
10481
10482/*
10483 * Clear the mode message.
10484 */
10485 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010486clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010487{
10488 msg_pos_mode();
10489 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010490 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010491 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492}
10493
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010494 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010495recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010496{
10497 MSG_PUTS_ATTR(_("recording"), attr);
10498 if (!shortmess(SHM_RECORDING))
10499 {
10500 char_u s[4];
10501 sprintf((char *)s, " @%c", Recording);
10502 MSG_PUTS_ATTR(s, attr);
10503 }
10504}
10505
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010506/*
10507 * Draw the tab pages line at the top of the Vim window.
10508 */
10509 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010510draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010511{
10512 int tabcount = 0;
10513 tabpage_T *tp;
10514 int tabwidth;
10515 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010516 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010517 int attr;
10518 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010519 win_T *cwp;
10520 int wincount;
10521 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010522 int c;
10523 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010524 int attr_sel = HL_ATTR(HLF_TPS);
10525 int attr_nosel = HL_ATTR(HLF_TP);
10526 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010527 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010528 int room;
10529 int use_sep_chars = (t_colors < 8
10530#ifdef FEAT_GUI
10531 && !gui.in_use
10532#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010533#ifdef FEAT_TERMGUICOLORS
10534 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010535#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010536 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010537
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010538 if (ScreenLines == NULL)
10539 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010540 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010541
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010542#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010543 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010544 if (gui_use_tabline())
10545 {
10546 gui_update_tabline();
10547 return;
10548 }
10549#endif
10550
10551 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010552 return;
10553
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010554#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010555
10556 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10557 for (scol = 0; scol < Columns; ++scol)
10558 TabPageIdxs[scol] = 0;
10559
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010560 /* Use the 'tabline' option if it's set. */
10561 if (*p_tal != NUL)
10562 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010563 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010564
10565 /* Check for an error. If there is one we would loop in redrawing the
10566 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010567 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010568 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010569 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010570 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010571 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010572 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010573 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010574 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010575#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010576 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010577 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010578 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010579
Bram Moolenaar238a5642006-02-21 22:12:05 +000010580 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10581 if (tabwidth < 6)
10582 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010583
Bram Moolenaar238a5642006-02-21 22:12:05 +000010584 attr = attr_nosel;
10585 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010586 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010587 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10588 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010589 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010590 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010591
Bram Moolenaar238a5642006-02-21 22:12:05 +000010592 if (tp->tp_topframe == topframe)
10593 attr = attr_sel;
10594 if (use_sep_chars && col > 0)
10595 screen_putchar('|', 0, col++, attr);
10596
10597 if (tp->tp_topframe != topframe)
10598 attr = attr_nosel;
10599
10600 screen_putchar(' ', 0, col++, attr);
10601
10602 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010603 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010604 cwp = curwin;
10605 wp = firstwin;
10606 }
10607 else
10608 {
10609 cwp = tp->tp_curwin;
10610 wp = tp->tp_firstwin;
10611 }
10612
10613 modified = FALSE;
10614 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10615 if (bufIsChanged(wp->w_buffer))
10616 modified = TRUE;
10617 if (modified || wincount > 1)
10618 {
10619 if (wincount > 1)
10620 {
10621 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010622 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010623 if (col + len >= Columns - 3)
10624 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010625 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010626#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010627 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010628#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010629 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010630#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010631 );
10632 col += len;
10633 }
10634 if (modified)
10635 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10636 screen_putchar(' ', 0, col++, attr);
10637 }
10638
10639 room = scol - col + tabwidth - 1;
10640 if (room > 0)
10641 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010642 /* Get buffer name in NameBuff[] */
10643 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010644 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010645 len = vim_strsize(NameBuff);
10646 p = NameBuff;
10647#ifdef FEAT_MBYTE
10648 if (has_mbyte)
10649 while (len > room)
10650 {
10651 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010652 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010653 }
10654 else
10655#endif
10656 if (len > room)
10657 {
10658 p += len - room;
10659 len = room;
10660 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010661 if (len > Columns - col - 1)
10662 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010663
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010664 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010665 col += len;
10666 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010667 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010668
10669 /* Store the tab page number in TabPageIdxs[], so that
10670 * jump_to_mouse() knows where each one is. */
10671 ++tabcount;
10672 while (scol < col)
10673 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010674 }
10675
Bram Moolenaar238a5642006-02-21 22:12:05 +000010676 if (use_sep_chars)
10677 c = '_';
10678 else
10679 c = ' ';
10680 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010681
10682 /* Put an "X" for closing the current tab if there are several. */
10683 if (first_tabpage->tp_next != NULL)
10684 {
10685 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10686 TabPageIdxs[Columns - 1] = -999;
10687 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010688 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010689
10690 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10691 * set. */
10692 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010693}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010694
10695/*
10696 * Get buffer name for "buf" into NameBuff[].
10697 * Takes care of special buffer names and translates special characters.
10698 */
10699 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010700get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010701{
10702 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010703 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010704 else
10705 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10706 trans_characters(NameBuff, MAXPATHL);
10707}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010708
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709/*
10710 * Get the character to use in a status line. Get its attributes in "*attr".
10711 */
10712 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010713fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714{
10715 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010716
10717#ifdef FEAT_TERMINAL
10718 if (bt_terminal(wp->w_buffer))
10719 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010720 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010721 {
10722 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010723 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010724 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010725 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010726 {
10727 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010728 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010729 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010730 }
10731 else
10732#endif
10733 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010735 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 fill = fill_stl;
10737 }
10738 else
10739 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010740 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 fill = fill_stlnc;
10742 }
10743 /* Use fill when there is highlighting, and highlighting of current
10744 * window differs, or the fillchars differ, or this is not the
10745 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010746 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010747 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 || (fill_stl != fill_stlnc)))
10749 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010750 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 return '^';
10752 return '=';
10753}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755/*
10756 * Get the character to use in a separator between vertically split windows.
10757 * Get its attributes in "*attr".
10758 */
10759 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010760fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010762 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 if (*attr == 0 && fill_vert == ' ')
10764 return '|';
10765 else
10766 return fill_vert;
10767}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768
10769/*
10770 * Return TRUE if redrawing should currently be done.
10771 */
10772 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010773redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010775#ifdef FEAT_EVAL
10776 if (disable_redraw_for_testing)
10777 return 0;
10778 else
10779#endif
10780 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10782}
10783
10784/*
10785 * Return TRUE if printing messages should currently be done.
10786 */
10787 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010788messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789{
10790 return (!(p_lz && char_avail() && !KeyTyped));
10791}
10792
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010793#ifdef FEAT_MENU
10794/*
10795 * Draw the window toolbar.
10796 */
10797 static void
10798redraw_win_toolbar(win_T *wp)
10799{
10800 vimmenu_T *menu;
10801 int item_idx = 0;
10802 int item_count = 0;
10803 int col = 0;
10804 int next_col;
10805 int off = (int)(current_ScreenLine - ScreenLines);
10806 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10807 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10808
10809 vim_free(wp->w_winbar_items);
10810 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10811 ++item_count;
10812 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10813 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10814
10815 /* TODO: use fewer spaces if there is not enough room */
10816 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010817 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010818 {
10819 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010820 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010821 break;
10822 if (col > 1)
10823 {
10824 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010825 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010826 break;
10827 }
10828
10829 wp->w_winbar_items[item_idx].wb_startcol = col;
10830 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010831 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010832 break;
10833
10834 next_col = text_to_screenline(wp, menu->name, col);
10835 while (col < next_col)
10836 {
10837 ScreenAttrs[off + col] = button_attr;
10838 ++col;
10839 }
10840 wp->w_winbar_items[item_idx].wb_endcol = col;
10841 wp->w_winbar_items[item_idx].wb_menu = menu;
10842 ++item_idx;
10843
Bram Moolenaar02631462017-09-22 15:20:32 +020010844 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010845 break;
10846 space_to_screenline(off + col, button_attr);
10847 ++col;
10848 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010849 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010850 {
10851 space_to_screenline(off + col, fill_attr);
10852 ++col;
10853 }
10854 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10855
Bram Moolenaar02631462017-09-22 15:20:32 +020010856 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10857 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010858}
10859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860/*
10861 * Show current status info in ruler and various other places
10862 * If always is FALSE, only show ruler if position has changed.
10863 */
10864 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010865showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866{
10867 if (!always && !redrawing())
10868 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010869#ifdef FEAT_INS_EXPAND
10870 if (pum_visible())
10871 {
10872 /* Don't redraw right now, do it later. */
10873 curwin->w_redr_status = TRUE;
10874 return;
10875 }
10876#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010877#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010878 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010879 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010880 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 else
10883#endif
10884#ifdef FEAT_CMDL_INFO
10885 win_redr_ruler(curwin, always);
10886#endif
10887
10888#ifdef FEAT_TITLE
10889 if (need_maketitle
10890# ifdef FEAT_STL_OPT
10891 || (p_icon && (stl_syntax & STL_IN_ICON))
10892 || (p_title && (stl_syntax & STL_IN_TITLE))
10893# endif
10894 )
10895 maketitle();
10896#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010897 /* Redraw the tab pages line if needed. */
10898 if (redraw_tabline)
10899 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900}
10901
10902#ifdef FEAT_CMDL_INFO
10903 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010904win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010906#define RULER_BUF_LEN 70
10907 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 int row;
10909 int fillchar;
10910 int attr;
10911 int empty_line = FALSE;
10912 colnr_T virtcol;
10913 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010914 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 int this_ru_col;
10917 int off = 0;
10918 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919
10920 /* If 'ruler' off or redrawing disabled, don't do anything */
10921 if (!p_ru)
10922 return;
10923
10924 /*
10925 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10926 * after deleting lines, before cursor.lnum is corrected.
10927 */
10928 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10929 return;
10930
10931#ifdef FEAT_INS_EXPAND
10932 /* Don't draw the ruler while doing insert-completion, it might overwrite
10933 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 if (edit_submode != NULL)
10936 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010937 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10938 if (pum_visible())
10939 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940#endif
10941
10942#ifdef FEAT_STL_OPT
10943 if (*p_ruf)
10944 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010945 int save_called_emsg = called_emsg;
10946
10947 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010949 if (called_emsg)
10950 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010951 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010952 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 return;
10954 }
10955#endif
10956
10957 /*
10958 * Check if not in Insert mode and the line is empty (will show "0-1").
10959 */
10960 if (!(State & INSERT)
10961 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10962 empty_line = TRUE;
10963
10964 /*
10965 * Only draw the ruler when something changed.
10966 */
10967 validate_virtcol_win(wp);
10968 if ( redraw_cmdline
10969 || always
10970 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10971 || wp->w_cursor.col != wp->w_ru_cursor.col
10972 || wp->w_virtcol != wp->w_ru_virtcol
10973#ifdef FEAT_VIRTUALEDIT
10974 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10975#endif
10976 || wp->w_topline != wp->w_ru_topline
10977 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10978#ifdef FEAT_DIFF
10979 || wp->w_topfill != wp->w_ru_topfill
10980#endif
10981 || empty_line != wp->w_ru_empty)
10982 {
10983 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 if (wp->w_status_height)
10985 {
10986 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010987 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010988 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010989 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 }
10991 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 {
10993 row = Rows - 1;
10994 fillchar = ' ';
10995 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 width = Columns;
10997 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 }
10999
11000 /* In list mode virtcol needs to be recomputed */
11001 virtcol = wp->w_virtcol;
11002 if (wp->w_p_list && lcs_tab1 == NUL)
11003 {
11004 wp->w_p_list = FALSE;
11005 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11006 wp->w_p_list = TRUE;
11007 }
11008
11009 /*
11010 * Some sprintfs return the length, some return a pointer.
11011 * To avoid portability problems we use strlen() here.
11012 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011013 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11015 ? 0L
11016 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011017 len = STRLEN(buffer);
11018 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11020 (int)virtcol + 1);
11021
11022 /*
11023 * Add a "50%" if there is room for it.
11024 * On the last line, don't print in the last column (scrolls the
11025 * screen up on some terminals).
11026 */
11027 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011028 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 this_ru_col = ru_col - (Columns - width);
11033 if (this_ru_col < 0)
11034 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 /* Never use more than half the window/screen width, leave the other
11036 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011037 if (this_ru_col < (width + 1) / 2)
11038 this_ru_col = (width + 1) / 2;
11039 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011041 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011042 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 {
11044#ifdef FEAT_MBYTE
11045 if (has_mbyte)
11046 i += (*mb_char2bytes)(fillchar, buffer + i);
11047 else
11048#endif
11049 buffer[i++] = fillchar;
11050 ++o;
11051 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011052 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 }
11054 /* Truncate at window boundary. */
11055#ifdef FEAT_MBYTE
11056 if (has_mbyte)
11057 {
11058 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011059 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 {
11061 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011062 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 {
11064 buffer[i] = NUL;
11065 break;
11066 }
11067 }
11068 }
11069 else
11070#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011071 if (this_ru_col + (int)STRLEN(buffer) > width)
11072 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073
Bram Moolenaar4033c552017-09-16 20:54:51 +020011074 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 i = redraw_cmdline;
11076 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011077 this_ru_col + off + (int)STRLEN(buffer),
11078 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 fillchar, fillchar, attr);
11080 /* don't redraw the cmdline because of showing the ruler */
11081 redraw_cmdline = i;
11082 wp->w_ru_cursor = wp->w_cursor;
11083 wp->w_ru_virtcol = wp->w_virtcol;
11084 wp->w_ru_empty = empty_line;
11085 wp->w_ru_topline = wp->w_topline;
11086 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11087#ifdef FEAT_DIFF
11088 wp->w_ru_topfill = wp->w_topfill;
11089#endif
11090 }
11091}
11092#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011093
11094#if defined(FEAT_LINEBREAK) || defined(PROTO)
11095/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011096 * Return the width of the 'number' and 'relativenumber' column.
11097 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011098 * Otherwise it depends on 'numberwidth' and the line count.
11099 */
11100 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011101number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011102{
11103 int n;
11104 linenr_T lnum;
11105
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011106 if (wp->w_p_rnu && !wp->w_p_nu)
11107 /* cursor line shows "0" */
11108 lnum = wp->w_height;
11109 else
11110 /* cursor line shows absolute line number */
11111 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011112
Bram Moolenaar6b314672015-03-20 15:42:10 +010011113 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011114 return wp->w_nrwidth_width;
11115 wp->w_nrwidth_line_count = lnum;
11116
11117 n = 0;
11118 do
11119 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011120 lnum /= 10;
11121 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011122 } while (lnum > 0);
11123
11124 /* 'numberwidth' gives the minimal width plus one */
11125 if (n < wp->w_p_nuw - 1)
11126 n = wp->w_p_nuw - 1;
11127
11128 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011129 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011130 return n;
11131}
11132#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011133
11134/*
11135 * Return the current cursor column. This is the actual position on the
11136 * screen. First column is 0.
11137 */
11138 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011139screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011140{
11141 return screen_cur_col;
11142}
11143
11144/*
11145 * Return the current cursor row. This is the actual position on the screen.
11146 * First row is 0.
11147 */
11148 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011149screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011150{
11151 return screen_cur_row;
11152}